~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to chaco/array_plot_data.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
""" Defines ArrayPlotData.
2
2
"""
3
3
 
4
 
from numpy import array
 
4
from numpy import array, ndarray
5
5
 
6
6
# Enthought library imports
7
7
from traits.api import Dict
8
8
 
9
9
# Local, relative imports
10
 
from abstract_plot_data import AbstractPlotData
 
10
from .abstract_plot_data import AbstractPlotData
 
11
from .abstract_data_source import AbstractDataSource
11
12
 
12
13
 
13
14
class ArrayPlotData(AbstractPlotData):
60
61
        of this class is convenience.
61
62
        """
62
63
        super(AbstractPlotData, self).__init__()
63
 
        self.arrays.update(kw)
64
 
        for i in range(1, len(data)+1):
65
 
            self.arrays['series'+str(i)] = data[i-1]
66
 
        return
67
 
 
68
 
    #------------------------------------------------------------------------
69
 
    # Dictionary Interface
70
 
    #------------------------------------------------------------------------
71
 
 
72
 
    def __getitem__(self, name):
73
 
        return self.arrays.get(name, None)
74
 
 
75
 
    def __setitem__(self, name, value):
76
 
        return self.set_data(name, value)
77
 
 
78
 
    def __delitem__(self, name):
79
 
        return self.del_data(name)
80
 
 
 
64
        self._update_data(kw)
 
65
        data = dict(zip(self._generate_names(len(data)), data))
 
66
        self._update_data(data)
 
67
 
 
68
 
 
69
    #------------------------------------------------------------------------
 
70
    # AbstractPlotData Interface
 
71
    #------------------------------------------------------------------------
81
72
 
82
73
    def list_data(self):
83
74
        """ Returns a list of the names of the arrays managed by this instance.
92
83
        """
93
84
        return self.arrays.get(name, None)
94
85
 
 
86
 
95
87
    def del_data(self, name):
96
88
        """ Deletes the array specified by *name*, or raises a KeyError if
97
89
        the named array does not exist.
98
90
        """
 
91
        if not self.writable:
 
92
            return None
 
93
 
99
94
        if name in self.arrays:
100
95
            del self.arrays[name]
 
96
            self.data_changed = {'removed': [name]}
101
97
        else:
102
98
            raise KeyError("Data series '%s' does not exist." % name)
103
99
 
106
102
        """ Sets the specified array as the value for either the specified
107
103
        name or a generated name.
108
104
 
109
 
        Implements AbstractPlotData.
 
105
        If the instance's `writable` attribute is True, then this method sets
 
106
        the data associated with the given name to the new value, otherwise it
 
107
        does nothing.
110
108
 
111
109
        Parameters
112
110
        ----------
122
120
        Returns
123
121
        -------
124
122
        The name under which the array was set.
 
123
 
125
124
        """
126
125
        if not self.writable:
127
126
            return None
128
127
 
129
128
        if generate_name:
130
 
            # Find all 'series*' and increment
131
 
            candidates = [n[6:] for n in self.arrays.keys() if n.startswith('series')]
132
 
            max_int = 0
133
 
            for c in candidates:
134
 
                try:
135
 
                    if int(c) > max_int:
136
 
                        max_int = int(c)
137
 
                except ValueError:
138
 
                    pass
139
 
            name = "series%d" % (max_int + 1)
140
 
 
 
129
            names = self._generate_names(1)
 
130
            name = names[0]
 
131
            
 
132
        self.update_data({name: new_data})
 
133
        return name
 
134
 
 
135
 
 
136
    def update_data(self, *args, **kwargs):
 
137
        """ Sets the specified array as the value for either the specified
 
138
        name or a generated name.
 
139
 
 
140
        Implements AbstractPlotData's update_data() method.  This method has
 
141
        the same signature as the dictionary update() method.
 
142
 
 
143
        """
 
144
        if not self.writable:
 
145
            return None
 
146
        
 
147
        data = dict(*args, **kwargs)
141
148
        event = {}
142
 
        if name in self.arrays:
143
 
            event['changed'] = [name]
144
 
        else:
145
 
            event['added'] = [name]
146
 
 
147
 
        if isinstance(new_data, list) or isinstance(new_data, tuple):
148
 
            new_data = array(new_data)
149
 
 
150
 
        self.arrays[name] = new_data
 
149
        for name in data:
 
150
            if name in self.arrays:
 
151
                event.setdefault('changed', []).append(name)
 
152
            else:
 
153
                event.setdefault('added', []).append(name)
 
154
 
 
155
        self._update_data(data)
151
156
        self.data_changed = event
152
 
        return name
153
157
 
154
158
 
155
159
    def set_selection(self, name, selection):
157
161
        """
158
162
        pass
159
163
 
 
164
    #------------------------------------------------------------------------
 
165
    # Private methods
 
166
    #------------------------------------------------------------------------    
 
167
 
 
168
    def _generate_names(self, n):
 
169
        """ Generate n new names
 
170
        """
 
171
        max_index = max(self._generate_indices())
 
172
        names = ["series{0:d}".format(n) for n in range(max_index+1, max_index+n+1)]
 
173
        return names
 
174
 
 
175
    def _generate_indices(self):
 
176
        """ Generator that yields all integers that match "series%d" in keys
 
177
        """
 
178
        yield 0 # default minimum
 
179
        for name in self.list_data():
 
180
            if name.startswith('series'):
 
181
                try:
 
182
                    v = int(name[6:])
 
183
                except ValueError:
 
184
                    continue
 
185
                yield v
 
186
 
 
187
    def _update_data(self, data):
 
188
        """ Update the array, ensuring that data is an array
 
189
        """
 
190
        # note that this call modifies data, but that's OK since the callers
 
191
        # all create the dictionary that they pass in
 
192
        for name, value in data.items():
 
193
            if not isinstance(value, (ndarray, AbstractDataSource)):
 
194
                data[name] = array(value)
 
195
            else:
 
196
                data[name] = value
 
197
 
 
198
        self.arrays.update(data)
160
199