source: orange/Orange/misc/__init__.py @ 9891:66ee6522a40f

Revision 9891:66ee6522a40f, 22.8 KB checked in by gregorr, 2 years ago (diff)

SymMatrix documentation (data -> misc).

Line 
1"""
2.. index:: misc
3
4Module Orange.misc contains common functions and classes which are used in other modules.
5
6.. index: SymMatrix
7
8-----------------------
9SymMatrix
10-----------------------
11
12:obj:`SymMatrix` implements symmetric matrices of size fixed at
13construction time (and stored in :obj:`SymMatrix.dim`).
14
15.. class:: SymMatrix
16
17    .. attribute:: dim
18   
19        Matrix dimension.
20           
21    .. attribute:: matrix_type
22
23        Can be ``SymMatrix.Lower`` (0), ``SymMatrix.Upper`` (1),
24        ``SymMatrix.Symmetric`` (2, default), ``SymMatrix.Lower_Filled`` (3) or
25        ``SymMatrix.Upper_Filled`` (4).
26
27        If the matrix type is ``Lower`` or ``Upper``, indexing
28        above or below the diagonal, respectively, will fail.
29        With ``Lower_Filled`` and ``Upper_Filled``,
30        the elements upper or lower, respectively, still
31        exist and are set to zero, but they cannot be modified. The
32        default matrix type is ``Symmetric``, but can be changed
33        at any time.
34
35        If matrix type is ``Upper``, it is printed as:
36
37        >>> m.matrix_type = m.Upper
38        >>> print m
39        (( 1.000,  2.000,  3.000,  4.000),
40         (         4.000,  6.000,  8.000),
41         (                 9.000, 12.000),
42         (                        16.000))
43
44        Changing the type to ``Lower_Filled`` changes the printout to
45
46        >>> m.matrix_type = m.Lower_Filled
47        >>> print m
48        (( 1.000,  0.000,  0.000,  0.000),
49         ( 2.000,  4.000,  0.000,  0.000),
50         ( 3.000,  6.000,  9.000,  0.000),
51         ( 4.000,  8.000, 12.000, 16.000))
52   
53    .. method:: __init__(dim[, default_value])
54
55        Construct a symmetric matrix of the given dimension.
56
57        :param dim: matrix dimension
58        :type dim: int
59
60        :param default_value: default value (0 by default)
61        :type default_value: double
62       
63       
64    .. method:: __init__(instances)
65
66        Construct a new symmetric matrix containing the given data instances.
67        These can be given as Python list containing lists or tuples.
68
69        :param instances: data instances
70        :type instances: list of lists
71       
72        The following example fills a matrix created above with
73        data in a list::
74
75            import Orange
76            m = [[],
77                 [ 3],
78                 [ 2, 4],
79                 [17, 5, 4],
80                 [ 2, 8, 3, 8],
81                 [ 7, 5, 10, 11, 2],
82                 [ 8, 4, 1, 5, 11, 13],
83                 [ 4, 7, 12, 8, 10, 1, 5],
84                 [13, 9, 14, 15, 7, 8, 4, 6],
85                 [12, 10, 11, 15, 2, 5, 7, 3, 1]]
86                   
87            matrix = Orange.data.SymMatrix(m)
88
89        SymMatrix also stores diagonal elements. They are set
90        to zero, if they are not specified. The missing elements
91        (shorter lists) are set to zero as well. If a list
92        spreads over the diagonal, the constructor checks
93        for asymmetries. For instance, the matrix
94
95        ::
96
97            m = [[],
98                 [ 3,  0, f],
99                 [ 2,  4]]
100   
101        is only OK if f equals 2. Finally, no row can be longer
102        than matrix size. 
103
104    .. method:: get_values()
105   
106        Return all matrix values in a Python list.
107
108    .. method:: get_KNN(i, k)
109   
110        Return k columns with the lowest value in the i-th row.
111       
112        :param i: i-th row
113        :type i: int
114       
115        :param k: number of neighbors
116        :type k: int
117       
118    .. method:: avg_linkage(clusters)
119   
120        Return a symmetric matrix with average distances between given clusters. 
121     
122        :param clusters: list of clusters
123        :type clusters: list of lists
124       
125    .. method:: invert(type)
126   
127        Invert values in the symmetric matrix.
128       
129        :param type: 0 (-X), 1 (1 - X), 2 (max - X), 3 (1 / X)
130        :type type: int
131
132    .. method:: normalize(type)
133   
134        Normalize values in the symmetric matrix.
135       
136        :param type: 0 (normalize to [0, 1] interval), 1 (Sigmoid)
137        :type type: int
138       
139       
140-------------------
141Indexing
142-------------------
143
144For symmetric matrices the order of indices is not important:
145if ``m`` is a SymMatrix, then ``m[2, 4]`` addresses the same element as ``m[4, 2]``.
146
147.. literalinclude:: code/symmatrix.py
148    :lines: 1-6
149
150Although only the lower left half of the matrix was set explicitely,
151the whole matrix is constructed.
152
153>>> print m
154(( 1.000,  2.000,  3.000,  4.000),
155 ( 2.000,  4.000,  6.000,  8.000),
156 ( 3.000,  6.000,  9.000, 12.000),
157 ( 4.000,  8.000, 12.000, 16.000))
158 
159Entire rows are indexed with a single index. They can be iterated
160over in a for loop or sliced (with, for example, ``m[:3]``):
161
162>>> print m[1]
163(3.0, 6.0, 9.0, 0.0)
164>>> m.matrix_type = m.Lower
165>>> for row in m:
166...     print row
167(1.0,)
168(2.0, 4.0)
169(3.0, 6.0, 9.0)
170(4.0, 8.0, 12.0, 16.0)
171
172.. index: Random number generator
173
174-----------------------
175Random number generator
176-----------------------
177
178:obj:`Random` uses the
179`Mersenne twister <http://en.wikipedia.org/wiki/Mersenne_twister>`_ algorithm
180to generate random numbers.
181
182::
183
184    >>> import Orange
185    >>> rg = Orange.misc.Random(42)
186    >>> rg(10)
187    4
188    >>> rg(10)
189    7
190    >>> rg.uses  # We called rg two times.
191    2
192    >>> rg.reset()
193    >>> rg(10)
194    4
195    >>> rg(10)
196    7
197    >>> rg.uses
198    2
199
200
201.. class:: Random(initseed)
202
203    :param initseed: Seed used for initializing the random generator.
204    :type initseed: int
205
206    .. method:: __call__(n)
207
208        Return a random integer R such that 0 <= R < n.
209
210        :type n: int
211
212    .. method:: reset([initseed])
213
214        Reinitialize the random generator with `initseed`. If `initseed`
215        is not given use the existing value of attribute `initseed`.
216
217    .. attribute:: uses
218       
219        The number of times the generator was called after
220        initialization/reset.
221   
222    .. attribute:: initseed
223
224        Random seed.
225
226Two examples or random number generator uses found in the documentation
227are :obj:`Orange.evaluation.testing` and :obj:`Orange.data.Table`.
228
229.. automodule:: Orange.misc.counters
230  :members:
231
232.. automodule:: Orange.misc.render
233  :members:
234
235.. automodule:: Orange.misc.selection
236
237.. automodule:: Orange.misc.addons
238
239.. automodule:: Orange.misc.serverfiles
240
241.. automodule:: Orange.misc.environ
242
243.. automodule:: Orange.misc.r
244
245
246"""
247import environ
248import counters
249import render
250
251from Orange.core import RandomGenerator as Random
252from orange import SymMatrix
253
254# addons is intentionally not imported; if it were, add-ons' directories would
255# be added to the python path. If that sounds OK, this can be changed ...
256
257__all__ = ["counters", "selection", "render", "serverfiles",
258           "deprecated_members", "deprecated_keywords",
259           "deprecated_attribute", "deprecation_warning"]
260
261import random, types, sys
262import time
263
264def getobjectname(x, default=""):
265    if type(x)==types.StringType:
266        return x
267     
268    for i in ["name", "shortDescription", "description", "func_doc", "func_name"]:
269        if getattr(x, i, ""):
270            return getattr(x, i)
271
272    if hasattr(x, "__class__"):
273        r = repr(x.__class__)
274        if r[1:5]=="type":
275            return str(x.__class__)[7:-2]
276        elif r[1:6]=="class":
277            return str(x.__class__)[8:-2]
278    return default
279
280
281def demangle_examples(x):
282    if type(x)==types.TupleType:
283        return x
284    else:
285        return x, 0
286
287
288def frange(*argw):
289    """ Like builtin `range` but works with floats
290    """
291    start, stop, step = 0.0, 1.0, 0.1
292    if len(argw)==1:
293        start=step=argw[0]
294    elif len(argw)==2:
295        stop, step = argw
296    elif len(argw)==3:
297        start, stop, step = argw
298    elif len(argw)>3:
299        raise AttributeError, "1-3 arguments expected"
300
301    stop+=1e-10
302    i=0
303    res=[]
304    while 1:
305        f=start+i*step
306        if f>stop:
307            break
308        res.append(f)
309        i+=1
310    return res
311
312verbose = 0
313
314def print_verbose(text, *verb):
315    if len(verb) and verb[0] or verbose:
316        print text
317
318__doc__ += """\
319------------------
320Reporting progress
321------------------
322
323.. autoclass:: Orange.misc.ConsoleProgressBar
324    :members:
325
326"""
327
328class ConsoleProgressBar(object):
329    """ A class to for printing progress bar reports in the console.
330   
331    Example ::
332   
333        >>> import sys, time
334        >>> progress = ConsoleProgressBar("Example", output=sys.stdout)
335        >>> for i in range(100):
336        ...    progress.advance()
337        ...    # Or
338        ...    progress.set_state(i)
339        ...    time.sleep(0.01)
340        ...
341        ...
342        Example ===================================>100%
343       
344    """
345    def __init__(self, title="", charwidth=40, step=1, output=None):
346        """ Initialize the progress bar.
347       
348        :param title: The title for the progress bar.
349        :type title: str
350        :param charwidth: The maximum progress bar width in characters.
351       
352            .. todo:: Get the console width from the ``output`` if the
353                information can be retrieved.
354               
355        :type charwidth: int
356        :param step: A default step used if ``advance`` is called without
357            any  arguments
358       
359        :type step: int
360        :param output: The output file. If None (default) then ``sys.stderr``
361            is used.
362           
363        :type output: An file like object to print the progress report to.
364         
365        """
366        self.title = title + " "
367        self.charwidth = charwidth
368        self.step = step
369        self.currstring = ""
370        self.state = 0
371        if output is None:
372            output = sys.stderr
373        self.output = output
374
375    def clear(self, i=-1):
376        """ Clear the current progress line indicator string.
377        """
378        try:
379            if hasattr(self.output, "isatty") and self.output.isatty():
380                self.output.write("\b" * (i if i != -1 else len(self.currstring)))
381            else:
382                self.output.seek(-i if i != -1 else -len(self.currstring), 2)
383        except Exception: ## If for some reason we failed
384            self.output.write("\n")
385
386    def getstring(self):
387        """ Return the progress indicator string.
388        """
389        progchar = int(round(float(self.state) * (self.charwidth - 5) / 100.0))
390        return self.title + "=" * (progchar) + ">" + " " * (self.charwidth\
391            - 5 - progchar) + "%3i" % int(round(self.state)) + "%"
392
393    def printline(self, string):
394        """ Print the ``string`` to the output file.
395        """
396        try:
397            self.clear()
398            self.output.write(string)
399            self.output.flush()
400        except Exception:
401            pass
402        self.currstring = string
403
404    def __call__(self, newstate=None):
405        """ Set the ``newstate`` as the current state of the progress bar.
406        ``newstate`` must be in the interval [0, 100].
407       
408        .. note:: ``set_state`` is the prefered way to set a new steate.
409       
410        :param newstate: The new state of the progress bar.
411        :type newstate: float
412         
413        """
414        if newstate is None:
415            self.advance()
416        else:
417            self.set_state(newstate)
418           
419    def set_state(self, newstate):
420        """ Set the ``newstate`` as the current state of the progress bar.
421        ``newstate`` must be in the interval [0, 100].
422       
423        :param newstate: The new state of the progress bar.
424        :type newstate: float
425       
426        """
427        if int(newstate) != int(self.state):
428            self.state = newstate
429            self.printline(self.getstring())
430        else:
431            self.state = newstate
432           
433    def advance(self, step=None):
434        """ Advance the current state by ``step``. If ``step`` is None use
435        the default step as set at class initialization.
436         
437        """
438        if step is None:
439            step = self.step
440           
441        newstate = self.state + step
442        self.set_state(newstate)
443
444    def finish(self):
445        """ Finish the progress bar (i.e. set the state to 100 and
446        print the final newline to the ``output`` file).
447        """
448        self.__call__(100)
449        self.output.write("\n")
450
451def progress_bar_milestones(count, iterations=100):
452    return set([int(i*count/float(iterations)) for i in range(iterations)])
453
454def lru_cache(maxsize=100):
455    """ A least recently used cache function decorator.
456    (Similar to the functools.lru_cache in python 3.2)
457    """
458   
459    def decorating_function(func):
460        import functools
461        cache = {}
462       
463        @functools.wraps(func)
464        def wrapped(*args, **kwargs):
465            key = args + tuple(sorted(kwargs.items()))
466            if key not in cache:
467                res = func(*args, **kwargs)
468                cache[key] = (time.time(), res)
469                if len(cache) > maxsize:
470                    key, (_, _) = min(cache.iteritems(), key=lambda item: item[1][0])
471                    del cache[key]
472            else:
473                _, res = cache[key]
474                cache[key] = (time.time(), res) # update the time
475               
476            return res
477       
478        def clear():
479            cache.clear()
480       
481        wrapped.clear = clear
482        wrapped._cache = cache
483       
484        return wrapped
485    return decorating_function
486
487#from Orange.misc.render import contextmanager
488from contextlib import contextmanager
489
490
491@contextmanager
492def member_set(obj, name, val):
493    """ A context manager that sets member ``name`` on ``obj`` to ``val``
494    and restores the previous value on exit.
495    """
496    old_val = getattr(obj, name, val)
497    setattr(obj, name, val)
498    yield
499    setattr(obj, name, old_val)
500   
501   
502class recursion_limit(object):
503    """ A context manager that sets a new recursion limit.
504   
505    """
506    def __init__(self, limit=1000):
507        self.limit = limit
508       
509    def __enter__(self):
510        self.old_limit = sys.getrecursionlimit()
511        sys.setrecursionlimit(self.limit)
512   
513    def __exit__(self, exc_type, exc_val, exc_tb):
514        sys.setrecursionlimit(self.old_limit)
515
516
517__doc__ += """\
518-----------------------------
519Deprecation utility functions
520-----------------------------
521
522.. autofunction:: Orange.misc.deprecation_warning
523
524.. autofunction:: Orange.misc.deprecated_members
525
526.. autofunction:: Orange.misc.deprecated_keywords
527
528.. autofunction:: Orange.misc.deprecated_attribute
529
530.. autofunction:: Orange.misc.deprecated_function_name
531
532"""
533
534import warnings
535def deprecation_warning(old, new, stacklevel=-2):
536    """ Raise a deprecation warning of an obsolete attribute access.
537   
538    :param old: Old attribute name (used in warning message).
539    :param new: New attribute name (used in warning message).
540   
541    """
542    warnings.warn("'%s' is deprecated. Use '%s' instead!" % (old, new), DeprecationWarning, stacklevel=stacklevel)
543   
544# We need to get the instancemethod type
545class _Foo():
546    def bar(self):
547        pass
548instancemethod = type(_Foo.bar)
549del _Foo
550
551function = type(lambda: None)
552
553class universal_set(set):
554    """ A universal set, pretends it contains everything.
555    """
556    def __contains__(self, value):
557        return True
558   
559from functools import wraps
560
561def deprecated_members(name_map, wrap_methods="all", in_place=True):
562    """ Decorate a class with properties for accessing attributes, and methods
563    with deprecated names. In addition methods from the `wrap_methods` list
564    will be wrapped to receive mapped keyword arguments.
565   
566    :param name_map: A dictionary mapping old into new names.
567    :type name_map: dict
568   
569    :param wrap_methods: A list of method names to wrap. Wrapped methods will
570        be called with mapped keyword arguments (by default all methods will
571        be wrapped).
572    :type wrap_methods: list
573   
574    :param in_place: If True the class will be modified in place, otherwise
575        it will be subclassed (default True).
576    :type in_place: bool
577   
578    Example ::
579           
580        >>> class A(object):
581        ...     def __init__(self, foo_bar="bar"):
582        ...         self.set_foo_bar(foo_bar)
583        ...     
584        ...     def set_foo_bar(self, foo_bar="bar"):
585        ...         self.foo_bar = foo_bar
586        ...
587        ... A = deprecated_members(
588        ... {"fooBar": "foo_bar",
589        ...  "setFooBar":"set_foo_bar"},
590        ... wrap_methods=["set_foo_bar", "__init__"])(A)
591        ...
592        ...
593        >>> a = A(fooBar="foo")
594        __main__:1: DeprecationWarning: 'fooBar' is deprecated. Use 'foo_bar' instead!
595        >>> print a.fooBar, a.foo_bar
596        foo foo
597        >>> a.setFooBar("FooBar!")
598        __main__:1: DeprecationWarning: 'setFooBar' is deprecated. Use 'set_foo_bar' instead!
599       
600    .. note:: This decorator does nothing if \
601        :obj:`Orange.misc.environ.orange_no_deprecated_members` environment \
602        variable is set to `True`.
603       
604    """
605    if environ.orange_no_deprecated_members:
606        return lambda cls: cls
607   
608    def is_wrapped(method):
609        """ Is member method already wrapped.
610        """
611        if getattr(method, "_deprecate_members_wrapped", False):
612            return True
613        elif hasattr(method, "im_func"):
614            im_func = method.im_func
615            return getattr(im_func, "_deprecate_members_wrapped", False)
616        else:
617            return False
618       
619    if wrap_methods == "all":
620        wrap_methods = universal_set()
621    elif not wrap_methods:
622        wrap_methods = set()
623       
624    def wrapper(cls):
625        cls_names = {}
626        # Create properties for accessing deprecated members
627        for old_name, new_name in name_map.items():
628            cls_names[old_name] = deprecated_attribute(old_name, new_name)
629           
630        # wrap member methods to map keyword arguments
631        for key, value in cls.__dict__.items():
632            if isinstance(value, (instancemethod, function)) \
633                and not is_wrapped(value) and key in wrap_methods:
634               
635                wrapped = deprecated_keywords(name_map)(value)
636                wrapped._deprecate_members_wrapped = True # A flag indicating this function already maps keywords
637                cls_names[key] = wrapped
638        if in_place:
639            for key, val in cls_names.items():
640                setattr(cls, key, val)
641            return cls
642        else:
643            return type(cls.__name__, (cls,), cls_names)
644       
645    return wrapper
646
647def deprecated_keywords(name_map):
648    """ Deprecates the keyword arguments of the function.
649   
650    Example ::
651   
652        >>> @deprecated_keywords({"myArg": "my_arg"})
653        ... def my_func(my_arg=None):
654        ...     print my_arg
655        ...
656        ...
657        >>> my_func(myArg="Arg")
658        __main__:1: DeprecationWarning: 'myArg' is deprecated. Use 'my_arg' instead!
659        Arg
660       
661    .. note:: This decorator does nothing if \
662        :obj:`Orange.misc.environ.orange_no_deprecated_members` environment \
663        variable is set to `True`.
664       
665    """
666    if environ.orange_no_deprecated_members:
667        return lambda func: func
668   
669    def decorator(func):
670        @wraps(func)
671        def wrap_call(*args, **kwargs):
672            kwargs = dict(kwargs)
673            for name in name_map:
674                if name in kwargs:
675                    deprecation_warning(name, name_map[name], stacklevel=3)
676                    kwargs[name_map[name]] = kwargs[name]
677                    del kwargs[name]
678            return func(*args, **kwargs)
679        return wrap_call
680    return decorator
681
682def deprecated_attribute(old_name, new_name):
683    """ Return a property object that accesses an attribute named `new_name`
684    and raises a deprecation warning when doing so.
685   
686    Example ::
687   
688        >>> class A(object):
689        ...     def __init__(self):
690        ...         self.my_attr = "123"
691        ...     myAttr = deprecated_attribute("myAttr", "my_attr")
692        ...
693        ...
694        >>> a = A()
695        >>> print a.myAttr
696        __main__:1: DeprecationWarning: 'myAttr' is deprecated. Use 'my_attr' instead!
697        123
698       
699    .. note:: This decorator does nothing and returns None if \
700        :obj:`Orange.misc.environ.orange_no_deprecated_members` environment \
701        variable is set to `True`.
702       
703    """
704    if environ.orange_no_deprecated_members:
705        return None
706   
707    def fget(self):
708        deprecation_warning(old_name, new_name, stacklevel=3)
709        return getattr(self, new_name)
710   
711    def fset(self, value):
712        deprecation_warning(old_name, new_name, stacklevel=3)
713        setattr(self, new_name, value)
714   
715    def fdel(self):
716        deprecation_warning(old_name, new_name, stacklevel=3)
717        delattr(self, new_name)
718   
719    prop = property(fget, fset, fdel,
720                    doc="A deprecated member '%s'. Use '%s' instead." % (old_name, new_name))
721    return prop
722
723
724def deprecated_function_name(func):
725    """ Return a wrapped function that raises an deprecation warning when
726    called. This should be used for deprecation of module level function names.
727   
728    Example ::
729   
730        >>> def func_a(arg):
731        ...    print "This is func_a  (used to be named funcA) called with", arg
732        ...
733        ...
734        >>> funcA = deprecated_function_name(func_a)
735        >>> funcA(None)
736         
737   
738    .. note:: This decorator does nothing and if \
739        :obj:`Orange.misc.environ.orange_no_deprecated_members` environment \
740        variable is set to `True`.
741       
742    """
743    if environ.orange_no_deprecated_members:
744        return func
745   
746    @wraps(func)
747    def wrapped(*args, **kwargs):
748        warnings.warn("Deprecated function name. Use %r instead!" % func.__name__,
749                      DeprecationWarning, stacklevel=2)
750        return func(*args, **kwargs)
751    return wrapped
752   
753
754"""
755Some utility functions common to Orange classes.
756 
757"""
758
759def _orange__new__(base=None):
760    """ Return an orange 'schizofrenic' __new__ class method.
761   
762    :param base: base orange class (default orange.Learner)
763    :type base: type
764         
765    Example::
766        class NewOrangeLearner(orange.Learner):
767            __new__ = _orange__new(orange.Learner)
768       
769    """
770    if base is None:
771        import Orange
772        base = Orange.core.Learner
773       
774    @wraps(base.__new__)
775    def _orange__new_wrapped(cls, data=None, **kwargs):
776        self = base.__new__(cls, **kwargs)
777        if data:
778            self.__init__(**kwargs)
779            return self.__call__(data)
780        else:
781            return self
782    return _orange__new_wrapped
783
784
785def _orange__reduce__(self):
786    """ A default __reduce__ method for orange types. Assumes the object
787    can be reconstructed with the call `constructor(__dict__)` where __dict__
788    if the stored (pickled) __dict__ attribute.
789   
790    Example::
791        class NewOrangeType(orange.Learner):
792            __reduce__ = _orange__reduce()
793    """ 
794    return type(self), (), dict(self.__dict__)
795
796
797demangleExamples = deprecated_function_name(demangle_examples)
798progressBarMilestones = deprecated_function_name(progress_bar_milestones)
799printVerbose = deprecated_function_name(print_verbose)
800
801# Must be imported after deprecation function definitions
802import selection
Note: See TracBrowser for help on using the repository browser.