XarrayAccessor
- class PlasmaCalcs.tools.xarray_tools.xarray_accessors.XarrayAccessor(xarray_obj)
Bases:
objectaccess attributes of DataArrays or Datasets.
e.g. xr.DataArray.accessor_name.accessor_method(…).Not intended for direct use. See pcAccessor for example usage.Check cls.registry for details about available methods.[TODO] cls.help() which lists available methods…accessor_name: None or strname for accessor associated with this class.E.g., ‘pc’ for pcAccessor.None –> use accessor_name from parent class.access_type: UNSET, None, ‘array’, or ‘dataset’which type of xarray objects methods registered here apply to (by default).UNSET –> use access_type from parent class.None –> methods apply to both DataArrays and Datasets.‘array’ –> methods apply to DataArrays only‘dataset’ –> methods apply to Datasets only.Implementation assumes when creating a new accessor name, you will follow the pattern:class customAccessor(XarrayAccessor, accessor_name=’custom’, access_type=None): …class customArrayAccessor(customAccessor, access_type=’array’): …class customDatasetAccessor(customAccessor, access_type=’dataset’): …Methods
register(f_or_name, *[, aliases, totype, _name])attaches method which applies f(self.obj, *args, **kw) to xr.DataArray.pc.{name}, then returns f.
register_attr(name, value, *[, totype])register cls.{name} = value, using a similar interface as cls.register.
Attributes
registered_aliasesregistered_attrsregistered_methods- classmethod register(f_or_name, *, aliases=[], totype=UNSET, _name=None)
attaches method which applies f(self.obj, *args, **kw) to xr.DataArray.pc.{name}, then returns f.
(in general, use cls.accessor_name, not necessarily ‘pc’; ‘pc’ is for pcAccessor.)This ensures f can be accessed via xr.DataArray.pc.{name} or xr.Dataset.pc.{name}.pcAccessor.register –> available on both DataArrays and Datasets.pcArrayAccessor.register –> available on DataArrays only.pcDatasetAccessor.register –> available on Datasets only.f_or_name: str or callablestr –> returns a function: f -> register(f, name=f_or_name)callable –> register this function then return it.will be registered at _name if provided, else at f.__name__.This enables this method to be used directly as a decorator, or as a decorator factory.aliases: list of straliases for f. Create alias property for each of these.totype: UNSET, None, ‘array’, or ‘dataset’which type of xarray objects the registered method applies to.UNSET –> use cls.access_type.None –> methods apply to both DataArrays and Datasets.‘array’ –> methods apply to DataArrays only‘dataset’ –> methods apply to Datasets only._name: str, optionalname to register f at. If not provided, use f.__name__.Not intended to be provided directly.Examples (using pcAccessor subclass for concreteness):@pcAccessor.registerdef my_method1(xarray_object, arg1):print(arg1)print(xarray_obj)xr.DataArray(data).pc.my_method1(7) # prints 7 then prints DataArray(data).xr.Dataset(data).pc.my_method1(5) # prints 5 then prints Dataset(data).@pcAccessor.register(name=’my_method2’, totype=’array’)def xarray_my_method2(xarray_object):print(xarray_object * 10)xr.DataArray(data).pc.my_method2() # prints 10*DataArray(data).xr.Dataset(data).pc.my_method2() # crashes; my_method2 not registered to datasets.
- classmethod register_attr(name, value, *, totype=UNSET)
register cls.{name} = value, using a similar interface as cls.register.
when totype is UNSET, this is equivalent to:cls.{name} = value; cls.attrs_registry[name] = (cls.access_type, value),when totype is provided, use cls=cls.access_type_to_cls[totype], instead.returns value, after doing setattr(cls, name, value)totype: UNSET, None, ‘array’, or ‘dataset’which type of xarray objects the registered attr applies to.UNSET –> use cls.access_type.None –> methods apply to both DataArrays and Datasets.‘array’ –> methods apply to DataArrays only‘dataset’ –> methods apply to Datasets only.Examples (using pcAccessor subclass for concreteness):pcAccessor.register_attr(‘MY_CONSTANT1’, 7)pcAccessor.register_attr(‘MY_CONSTANT2’, 5, totype=’array’)pcAccessor.register_attr(‘nMbytes’, property(lambda self: self.obj.nbytes/1024**2))arr = xr.DataArray(some_data)ds = xr.Dataset(other_data)arr.pc.MY_CONSTANT1 # == 7ds.pc.MY_CONSTANT1 # == 7arr.pc.MY_CONSTANT2 # == 5ds.pc.MY_CONSTANT2 # crashes; MY_CONSTANT2 not registered to datasets.arr.pc.nMbytes # == DataArray(data).nbytes/1024**2ds.pc.nMbytes # == Dataset(data).nbytes/1024**2