Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions src/psPlotKit/data_manager/ps_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,40 @@ def _define_custom_units(self, custom_units=None):
custom_units = CustomUnits()
self.custom_units = custom_units.get_units_dict()

def _raise_unit_error(self, error, operation, requested_units=None, other=None):
"""Build and raise an enhanced unit error preserving the original type.

The raised exception keeps the same Python type as *error* so existing
``except ValueError`` / ``except LookupError`` blocks continue to work,
but the message now includes the data_key and data_type so users can
identify which data triggered the incompatible unit problem.
"""
msg = "Unit error for data_key={!r}, data_type={!r}. Operation: {}. ".format(
self.data_key, self.data_type, operation
)
if requested_units is not None:
msg += "Requested units: {!r}. ".format(requested_units)
if other is not None:
if isinstance(other, PsData):
msg += "Other operand: data_key={!r}, data_type={!r}. ".format(
other.data_key, other.data_type
)
else:
msg += "Other operand: {!r}. ".format(other)
msg += "Original error: {}: {}".format(type(error).__name__, error)
_logger.error(msg)
raise type(error)(msg) from error

def _assign_units(self, manual_conversion=1):
if self.data_is_numbers:
self.data = self.data * manual_conversion
qsunits = self._get_qs_unit()
self.data_with_units = qs.Quantity(self.data.copy(), qsunits)
self.raw_data_with_units = qs.Quantity(self.raw_data.copy(), qsunits)
self.data = self.data_with_units.magnitude
try:
self.data_with_units = qs.Quantity(self.data.copy(), qsunits)
self.raw_data_with_units = qs.Quantity(self.raw_data.copy(), qsunits)
self.data = self.data_with_units.magnitude
except (ValueError, LookupError) as e:
self._raise_unit_error(e, "assign units", requested_units=self.sunits)
self.set_label()

@property
Expand Down Expand Up @@ -280,15 +307,21 @@ def set_data(self, data):
self.raw_data = data

def to_units(self, new_units):
self.sunits = self._convert_string_unit(new_units)
converted_units = self._convert_string_unit(new_units)
old_sunits = self.sunits
self.sunits = converted_units
qsunits = self._get_qs_unit()
if new_units == "degC" and str(self.data_with_units.units) == "1.0 K":
self.data_with_units = qs.Quantity(
self.data_with_units.magnitude[:] - 273.15, new_units
)
else:
self.data_with_units = self.data_with_units.rescale(qsunits)
self.raw_data_with_units = self.raw_data_with_units.rescale(qsunits)
try:
if new_units == "degC" and str(self.data_with_units.units) == "1.0 K":
self.data_with_units = qs.Quantity(
self.data_with_units.magnitude[:] - 273.15, new_units
)
else:
self.data_with_units = self.data_with_units.rescale(qsunits)
self.raw_data_with_units = self.raw_data_with_units.rescale(qsunits)
except (ValueError, LookupError) as e:
self.sunits = old_sunits
self._raise_unit_error(e, "convert units", requested_units=new_units)
self.data = self.data_with_units.magnitude[:]
self.raw_data = self.raw_data_with_units.magnitude
self.set_label()
Expand Down Expand Up @@ -349,7 +382,10 @@ def _arithmetic_op(self, other, op, symbol):
"Arithmetic operations require a PsData object or numeric "
"scalar, got {}".format(type(other))
)
result_quantity = op(self.data_with_units, other_val)
try:
result_quantity = op(self.data_with_units, other_val)
except (ValueError, LookupError) as e:
self._raise_unit_error(e, "arithmetic '{}'".format(symbol), other=other)
result_key = "({} {} {})".format(self.data_key, symbol, other_label)
return PsData(
data_key=result_key,
Expand All @@ -371,7 +407,12 @@ def _r_arithmetic_op(self, other, op, symbol):
"Arithmetic operations require a PsData object or numeric "
"scalar, got {}".format(type(other))
)
result_quantity = op(other_val, self.data_with_units)
try:
result_quantity = op(other_val, self.data_with_units)
except (ValueError, LookupError) as e:
self._raise_unit_error(
e, "reflected arithmetic '{}'".format(symbol), other=other
)
result_key = "({} {} {})".format(other_label, symbol, self.data_key)
return PsData(
data_key=result_key,
Expand Down Expand Up @@ -426,7 +467,10 @@ def __rpow__(self, other):
)

def __neg__(self):
result_quantity = -1 * self.data_with_units
try:
result_quantity = -1 * self.data_with_units
except (ValueError, LookupError) as e:
self._raise_unit_error(e, "negation")
result_key = "(-{})".format(self.data_key)
return PsData(
data_key=result_key,
Expand Down
Loading
Loading