Skip to content

Commit c16424f

Browse files
authored
refactor the conversion functions (#56)
* use pipe to chain the functions used to construct the quantified object * don't allow passing a string and a registry to *attach_units * refactor the array_attach_units test * move treating Variable objects to a separate function * refactor extract_units and extract_unit_attributes * refactor the array_convert_units tests * use Quantity instead of multiplying with the unit * refactor the attach_units tests * refactor the attach_unit_attributes tests * remove the extra attach_unit_attributes step for dimension coordinates * make sure dequantify works for dimension coordinates * refactor strip_units * refactor the convert_units tests * refactor the extract_units tests * also extract the units from dimension coordinates * refactor convert_units * also dequantify dimension coordinates on Dataset objects * rewrite the array_convert_units tests to also get expected as parameter * also test the conversion of non-quantities * more missing test data * remove the capability to attach attributes to Variable objects * don't accept units other than in a dict * wrap the value in a Quantity if it is not already one * actually expect errors in the convert_units tests * add a entry to whats-new.rst * move the support for Variable objects to separate functions * rewrite some more tests * remove the duplicated conversion functions * style fixes * correctly use either_dict_or_kwargs
1 parent a0bdb67 commit c16424f

File tree

5 files changed

+462
-442
lines changed

5 files changed

+462
-442
lines changed

docs/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ What's new
55
------------------
66
- rewrite :py:meth:`Dataset.pint.quantify` and :py:meth:`DataArray.pint.quantify`,
77
to use pint's `parse_units` instead of `parse_expression` (:pull:`40`)
8+
- refactor the internal conversion functions (:pull:``)
89
910
v0.1 (October 26 2020)
1011
----------------------

pint_xarray/accessors.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,21 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs):
225225
unit_kwargs[self.da.name] = units
226226
units = None
227227

228-
units = either_dict_or_kwargs(units, unit_kwargs, ".quantify")
228+
units = either_dict_or_kwargs(units, unit_kwargs, "quantify")
229229

230230
registry = get_registry(unit_registry, units, conversion.extract_units(self.da))
231231

232232
unit_attrs = conversion.extract_unit_attributes(self.da)
233-
new_obj = conversion.strip_unit_attributes(self.da)
234233

235234
units = {
236235
name: _decide_units(unit, registry, unit_attribute)
237236
for name, (unit, unit_attribute) in zip_mappings(units, unit_attrs).items()
238237
if unit is not None or unit_attribute is not None
239238
}
240239

241-
# TODO: remove once indexes support units
242-
dim_units = {name: unit for name, unit in units.items() if name in self.da.dims}
243-
for name in dim_units.keys():
244-
units.pop(name)
245-
new_obj = conversion.attach_unit_attributes(new_obj, dim_units)
246-
247-
return conversion.attach_units(new_obj, units)
240+
return self.da.pipe(conversion.strip_unit_attributes).pipe(
241+
conversion.attach_units, units
242+
)
248243

249244
def dequantify(self):
250245
"""
@@ -260,12 +255,14 @@ def dequantify(self):
260255
that was previously wrapped by `pint.Quantity`.
261256
"""
262257

263-
units = units_to_str_or_none(conversion.extract_units(self.da))
264-
new_obj = conversion.attach_unit_attributes(
265-
conversion.strip_units(self.da), units
266-
)
258+
units = conversion.extract_unit_attributes(self.da)
259+
units.update(conversion.extract_units(self.da))
267260

268-
return new_obj
261+
return (
262+
self.da.pipe(conversion.strip_units)
263+
.pipe(conversion.strip_unit_attributes)
264+
.pipe(conversion.attach_unit_attributes, units_to_str_or_none(units))
265+
)
269266

270267
@property
271268
def magnitude(self):
@@ -492,25 +489,20 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs):
492489
a (x) int64 <Quantity([0 3 2], 'meter')>
493490
b (x) int64 <Quantity([ 5 -2 1], 'decimeter')>
494491
"""
495-
units = either_dict_or_kwargs(units, unit_kwargs, ".quantify")
492+
units = either_dict_or_kwargs(units, unit_kwargs, "quantify")
496493
registry = get_registry(unit_registry, units, conversion.extract_units(self.ds))
497494

498495
unit_attrs = conversion.extract_unit_attributes(self.ds)
499-
new_obj = conversion.strip_unit_attributes(self.ds)
500496

501497
units = {
502498
name: _decide_units(unit, registry, attr)
503499
for name, (unit, attr) in zip_mappings(units, unit_attrs).items()
504500
if unit is not None or attr is not None
505501
}
506502

507-
# TODO: remove once indexes support units
508-
dim_units = {name: unit for name, unit in units.items() if name in new_obj.dims}
509-
for name in dim_units.keys():
510-
units.pop(name)
511-
new_obj = conversion.attach_unit_attributes(new_obj, dim_units)
512-
513-
return conversion.attach_units(new_obj, units)
503+
return self.ds.pipe(conversion.strip_unit_attributes).pipe(
504+
conversion.attach_units, units
505+
)
514506

515507
def dequantify(self):
516508
"""
@@ -525,11 +517,14 @@ def dequantify(self):
525517
Dataset whose data variables are unitless, and of the type
526518
that was previously wrapped by ``pint.Quantity``.
527519
"""
528-
units = units_to_str_or_none(conversion.extract_units(self.ds))
529-
new_obj = conversion.attach_unit_attributes(
530-
conversion.strip_units(self.ds), units
520+
units = conversion.extract_unit_attributes(self.ds)
521+
units.update(conversion.extract_units(self.ds))
522+
523+
return (
524+
self.ds.pipe(conversion.strip_units)
525+
.pipe(conversion.strip_unit_attributes)
526+
.pipe(conversion.attach_unit_attributes, units_to_str_or_none(units))
531527
)
532-
return new_obj
533528

534529
def to(self, units=None, **unit_kwargs):
535530
"""convert the quantities in a DataArray

0 commit comments

Comments
 (0)