File: distribution.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (75 lines) | stat: -rw-r--r-- 2,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#------------------------------------------------------------------------------
# Copyright (c) 2007, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought statistical distribution package component>
#------------------------------------------------------------------------------

""" Base class representing ditribution input variables used for stocastic modeling """

from enthought.traits.api import HasTraits, Enum, Property, Float, Int
from enthought.traits.ui.api import View, Item, Handler, InstanceEditor

import numpy

class Distribution(HasTraits):
    """ Base Class for input variables representing a variable which
        produces a range of values

    """

    # the values representing the distribution
    values = Property(Int)
    _values = None

    # how many values should be generated?
    samples = Int(10)

    _state = None

    def _get_values(self):
        """ getter for the values property """
        if self._state is None:
            self._state = numpy.random.RandomState()
        if self._values is None:
            self._values = self._get_value(self.samples)
        return self._values

    def _get_value(self, n):
        """ returns 'n' values for the distribution """
        raise NotImplemented

    def get_state(self):
        """ returns the random state variable """
        if self._state is None:
            self.set_state(None)

        return self._state.get_state()

    def set_state(self, state):
        """ sets the random state. If the argument is None the state
            will be initialized to a new random state. The method
            returns the state that was set

        """
        if state is None:
            self._state = numpy.random.RandomState()
        else:
            self._state.set_state(state)


        #invalidate the cached values
        self._values = None

        return self._state.get_state()

    def _anytrait_changed(self):
        #invalidate the _values so they have to be regenerated
        self._values = None