Skip to content
Snippets Groups Projects
Commit 56896c86 authored by Sebastian Müller's avatar Sebastian Müller 🐈
Browse files

data: check for quantified data in get routines; minor refactoring

parent 9d5cd731
No related branches found
No related tags found
1 merge request!260Masked array support
Pipeline #188152 passed with stages
in 5 minutes and 10 seconds
...@@ -61,7 +61,7 @@ def prepare(data, info, time_entries=1, force_copy=False, report_conversion=Fals ...@@ -61,7 +61,7 @@ def prepare(data, info, time_entries=1, force_copy=False, report_conversion=Fals
""" """
units_converted = None units_converted = None
units = info.units units = info.units
if isinstance(data, pint.Quantity): if is_quantified(data):
if not compatible_units(data.units, units): if not compatible_units(data.units, units):
raise FinamDataError( raise FinamDataError(
f"Given data has incompatible units. " f"Given data has incompatible units. "
...@@ -73,6 +73,7 @@ def prepare(data, info, time_entries=1, force_copy=False, report_conversion=Fals ...@@ -73,6 +73,7 @@ def prepare(data, info, time_entries=1, force_copy=False, report_conversion=Fals
elif force_copy: elif force_copy:
data = data.copy() data = data.copy()
else: else:
# this covers masked arrays as well
if isinstance(data, np.ndarray): if isinstance(data, np.ndarray):
if force_copy: if force_copy:
data = np.copy(data) data = np.copy(data)
...@@ -199,24 +200,6 @@ def to_datetime(date): ...@@ -199,24 +200,6 @@ def to_datetime(date):
return datetime.datetime.utcfromtimestamp(timestamp) return datetime.datetime.utcfromtimestamp(timestamp)
def get_magnitude(xdata):
"""
Get magnitude of given data.
Parameters
----------
xdata : pint.Quantity
The given data array.
Returns
-------
numpy.ndarray
Magnitude of given data.
"""
check_quantified(xdata, "get_magnitude")
return xdata.magnitude
def strip_time(xdata, grid): def strip_time(xdata, grid):
"""Returns a view of the data with the time dimension squeezed if there is only a single entry """Returns a view of the data with the time dimension squeezed if there is only a single entry
...@@ -247,6 +230,24 @@ def strip_time(xdata, grid): ...@@ -247,6 +230,24 @@ def strip_time(xdata, grid):
return xdata return xdata
def get_magnitude(xdata):
"""
Get magnitude of given data.
Parameters
----------
xdata : pint.Quantity
The given data array.
Returns
-------
numpy.ndarray
Magnitude of given data.
"""
check_quantified(xdata, "get_magnitude")
return xdata.magnitude
def get_units(xdata): def get_units(xdata):
""" """
Get units of the data. Get units of the data.
...@@ -261,6 +262,7 @@ def get_units(xdata): ...@@ -261,6 +262,7 @@ def get_units(xdata):
pint.Unit pint.Unit
Units of the data. Units of the data.
""" """
check_quantified(xdata, "get_units")
return xdata.units return xdata.units
...@@ -278,6 +280,7 @@ def get_dimensionality(xdata): ...@@ -278,6 +280,7 @@ def get_dimensionality(xdata):
pint.UnitsContainer pint.UnitsContainer
Dimensionality of the data. Dimensionality of the data.
""" """
check_quantified(xdata, "get_dimensionality")
return xdata.dimensionality return xdata.dimensionality
...@@ -341,11 +344,10 @@ def full_like(xdata, value): ...@@ -341,11 +344,10 @@ def full_like(xdata, value):
with the data filled with fill_value. with the data filled with fill_value.
Units will be taken from the input if present. Units will be taken from the input if present.
""" """
d = np.full_like(xdata, value) data = np.full_like(xdata, value)
if is_quantified(xdata): if is_quantified(xdata):
return UNITS.Quantity(d, xdata.units) return UNITS.Quantity(data, xdata.units)
return data
return d
def full(value, info): def full(value, info):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment