| 167 | |
| 168 | |
| 169 | class UnitData: |
| 170 | def __init__(self, data=None): |
| 171 | """ |
| 172 | Create mapping between unique categorical values and integer ids. |
| 173 | |
| 174 | Parameters |
| 175 | ---------- |
| 176 | data : iterable |
| 177 | sequence of string values |
| 178 | """ |
| 179 | self._mapping = OrderedDict() |
| 180 | self._counter = itertools.count() |
| 181 | if data is not None: |
| 182 | self.update(data) |
| 183 | |
| 184 | @staticmethod |
| 185 | def _str_is_convertible(val): |
| 186 | """ |
| 187 | Helper method to check whether a string can be parsed as float or date. |
| 188 | """ |
| 189 | try: |
| 190 | float(val) |
| 191 | except ValueError: |
| 192 | try: |
| 193 | dateutil.parser.parse(val) |
| 194 | except (ValueError, TypeError): |
| 195 | # TypeError if dateutil >= 2.8.1 else ValueError |
| 196 | return False |
| 197 | return True |
| 198 | |
| 199 | def update(self, data): |
| 200 | """ |
| 201 | Map new values to integer identifiers. |
| 202 | |
| 203 | Parameters |
| 204 | ---------- |
| 205 | data : iterable of str or bytes |
| 206 | |
| 207 | Raises |
| 208 | ------ |
| 209 | TypeError |
| 210 | If elements in *data* are neither str nor bytes. |
| 211 | """ |
| 212 | data = np.atleast_1d(np.array(data, dtype=object)) |
| 213 | # check if convertible to number: |
| 214 | convertible = True |
| 215 | for val in OrderedDict.fromkeys(data): |
| 216 | # OrderedDict just iterates over unique values in data. |
| 217 | _api.check_isinstance((str, bytes), value=val) |
| 218 | if convertible: |
| 219 | # this will only be called so long as convertible is True. |
| 220 | convertible = self._str_is_convertible(val) |
| 221 | if val not in self._mapping: |
| 222 | self._mapping[val] = next(self._counter) |
| 223 | if data.size and convertible: |
| 224 | _log.info('Using categorical units to plot a list of strings ' |
| 225 | 'that are all parsable as floats or dates. If these ' |
| 226 | 'strings should be plotted as numbers, cast to the ' |
no outgoing calls
searching dependent graphs…