Skip to content

base

Base class for things that have units.

Source code in navis/core/base.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
class UnitObject:
    """Base class for things that have units."""

    @property
    def units(self) -> Union[numbers.Number, np.ndarray]:
        """Units for coordinate space."""
        # Note that we are regenerating the pint.Quantity from the string
        # That is to avoid problems with pickling e.g. when using multiprocessing
        unit_str = getattr(self, "_unit_str", None)

        if utils.is_iterable(unit_str):
            values = [config.ureg(u) for u in unit_str]
            conv = [v.to(values[0]).magnitude for v in values]
            return config.ureg.Quantity(np.array(conv), values[0].units)
        else:
            return config.ureg(unit_str)

    @property
    def units_xyz(self) -> np.ndarray:
        """Units for coordinate space. Always returns x/y/z array."""
        units = self.units

        if not utils.is_iterable(units):
            units = config.ureg.Quantity([units.magnitude] * 3, units.units)

        return units

    @units.setter
    def units(self, units: Union[pint.Unit, pint.Quantity, str, None]):
        # Note that we are storing the string, not the actual pint.Quantity
        # That is to avoid problems with pickling e.g. when using multiprocessing

        # Do NOT remove the is_iterable condition - otherwise we might
        # accidentally strip the units from a pint Quantity vector
        if not utils.is_iterable(units):
            units = utils.make_iterable(units)

        if len(units) not in [1, 3]:
            raise ValueError(
                "Must provide either a single unit or one for "
                "for x, y and z dimension."
            )

        # Make sure we actually have valid unit(s)
        unit_str = []
        for v in units:
            if isinstance(v, str):
                # This makes sure we have meters (i.e. nm, um, etc) because
                # "microns", for example, produces odd behaviour like
                # "millimicrons" on division
                v = v.replace("microns", "um").replace("micron", "um")
                unit_str.append(str(v))
            elif isinstance(v, (pint.Unit, pint.Quantity)):
                unit_str.append(str(v))
            elif isinstance(v, type(None)):
                unit_str.append(None)
            elif isinstance(v, numbers.Number):
                unit_str.append(str(config.ureg(f"{v} dimensionless")))
            else:
                raise TypeError(f'Expect str or pint Unit/Quantity, got "{type(v)}"')

        # Some clean-up
        if len(set(unit_str)) == 1:
            unit_str = unit_str[0]
        else:
            # Check if all base units (e.g. "microns") are the same
            unique_units = set([str(config.ureg(u).units) for u in unit_str])
            if len(unique_units) != 1:
                raise ValueError(
                    'Non-isometric units must share the same base,'
                    f' got: {", ".join(unique_units)}'
                )
            unit_str = tuple(unit_str)

        self._unit_str = unit_str

    @property
    def is_isometric(self):
        """Test if neuron is isometric."""
        u = self.units
        if utils.is_iterable(u) and len(set(u)) > 1:
            return False
        return True

Test if neuron is isometric.

Units for coordinate space.

Units for coordinate space. Always returns x/y/z array.