Stream: python-questions
Topic: DataArrary accessor
Matt Long (Jun 19 2020 at 03:07):
I am working on an EOF analysis. I find it convenient to write the following "accessor"
@xr.register_dataarray_accessor('eofs') class eof_analysis(object): """perform EOF analysis""" def __init__(self, eof_da): self._da = eof_da self.constituent = eof_da.name def compute(self): coslat = np.cos(np.deg2rad(self._da.lat.values)) wgts = np.sqrt(coslat)[..., np.newaxis] self.solver = Eof(self._da, center=True, weights=wgts) self._ds = xr.merge(( self.solver.eofs(), self.solver.pcs(pcscaling=pcscaling['unit_variance']), self.solver.varianceFraction().rename('vf'), )) def add_reconstruction(self, eof_list): recon_name = f'{self.constituent}_'+'_'.join([str(e) for e in eof_list]) self.ds[recon_name] = self.solver.reconstructedField(eof_list) @property def ds(self): return self._ds
However, in a notebook, I get the following error if this definition is executed more than once:
/glade/work/mclong/miniconda3/envs/co2-hole/lib/python3.7/site-packages/ipykernel_launcher.py:8: AccessorRegistrationWarning: registration of accessor <class '__main__.eof_analysis'> under name 'eofs' for type <class 'xarray.core.dataarray.DataArray'> is overriding a preexisting attribute with the same name.
and the object definition is not updated with edits I might have made. There could be an interaction with "autoload" magic.
Is there a way to "unregister" an accessor?
Anderson Banihirwe (Jun 19 2020 at 12:45):
Is there a way to "unregister" an accessor?
You can use delattr
on thexr.DataArray
class:
try: delattr(xr.DataArray, 'eofs') except AttributeError: pass
Precious Mongwe (Jun 22 2020 at 17:14):
Sure Deepak, but your link does not work, can you send me the content?, perhaps by email
Last updated: Jan 30 2022 at 12:01 UTC