Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gorkowski/issue352 (Sourcery refactored) #355

Merged
merged 2 commits into from
Nov 12, 2023
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
38 changes: 12 additions & 26 deletions particula/util/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def coerce_type(data, dtype):
"""
if not isinstance(data, dtype):
try:
if dtype == np.ndarray:
return np.array(data)
return dtype(data)
return np.array(data) if dtype == np.ndarray else dtype(data)
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved
except (ValueError, TypeError) as exc:
raise ValueError(f'Could not coerce {data} to {dtype}') from exc
return data
Expand Down Expand Up @@ -90,9 +88,7 @@ def radius_diameter(value: float, to_diameter: bool = True) -> float:
-----------
The converted value.
"""
if to_diameter:
return value * 2
return value / 2
return value * 2 if to_diameter else value / 2
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved


def volume_to_length(volume: float, length_type: str = 'radius') -> float:
Expand All @@ -115,9 +111,7 @@ def volume_to_length(volume: float, length_type: str = 'radius') -> float:

radius = (volume * 3 / (4 * np.pi)) ** (1 / 3)

if length_type == 'radius':
return radius
return radius * 2
return radius if length_type == 'radius' else radius * 2
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved


def length_to_volume(length: float, length_type: str = 'radius') -> float:
Expand All @@ -135,10 +129,8 @@ def length_to_volume(length: float, length_type: str = 'radius') -> float:
The volume.
"""
if length_type == 'diameter':
length = length / 2
elif length_type == 'radius':
pass
else:
length /= 2
elif length_type != 'radius':
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError('length_type must be radius or diameter')
return (4/3)*np.pi*(length**3)

Expand Down Expand Up @@ -431,9 +423,7 @@ def convert_sizer_dn(
# Convert from dn to dn/dlogdp
return dn_dlogdp / np.log10(upper/lower)

# Convert from dn/dlogdp to dn
d_num = dn_dlogdp * np.log10(upper/lower)
return d_num
return dn_dlogdp * np.log10(upper/lower)
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved


def datetime64_from_epoch_array(
Expand Down Expand Up @@ -478,7 +468,7 @@ def list_to_dict(list_of_str: list) -> dict:
dict: A dictionary where the keys are the strings and the values are
the index of the string in the list.
"""
assert len(list_of_str) > 0, "Input list_of_str must not be empty."
assert list_of_str, "Input list_of_str must not be empty."
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved
assert all(isinstance(item, str) for item in list_of_str), \
"Input list_of_str must contain only strings."

Expand Down Expand Up @@ -563,11 +553,8 @@ def data_shape_check(
concatenate_axis_new = 0 # Default to the first axis
# Check if the last axis of data matches the length of time
if data.shape[-1] != len(time):
if data.shape[0] == len(time):
warnings.warn("Square data with time shape assumes time \
warnings.warn("Square data with time shape assumes time \
axis is the first axis in data.")
else:
warnings.warn("Inconsistent shapes between data and time.")
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved
else:
# Find the axis that doesn't match the length of time
concatenate_axis_new = np.argwhere(
Expand All @@ -582,12 +569,11 @@ def data_shape_check(
print(header)
raise ValueError("Header list length must match the first \
dimension of data_new.")
else:
# check if header is a single entry
if len(header) != 1:
raise ValueError("Header list must be a single entry if data_new \
is 1D.")
elif len(header) == 1:
# Reshape new data so the concatenate axis is the first axis
data = np.expand_dims(data, 0)

else:
raise ValueError("Header list must be a single entry if data_new \
is 1D.")
return data
14 changes: 6 additions & 8 deletions particula/util/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ def merge_formatting(
header_new.shape,
' are not the same shape, check the data formatting'
)

# check if all the headers are numbers by checking the first element
list_of_numbers = np.all([x[0].isnumeric() for x in header_current])

if list_of_numbers: # make it a sorted list of numbers
# check if the headers are numeric
if list_of_numbers := np.all([x[0].isnumeric() for x in header_current]):
# convert the headers to floats
header_list_numeric = np.array(header_current).astype(float)
header_new_numeric = np.array(header_new).astype(float)
Expand All @@ -135,14 +132,13 @@ def merge_formatting(

# sort the data
data_current = data_current[header_stored_indices, :]
data_new = data_new[header_new_indices, :]
else:
# match header_new to header_current and sort the data
header_new_indices = [
header_new.index(x) for x in header_current
]
header_new = [header_new[i] for i in header_new_indices]
data_new = data_new[header_new_indices, :]
data_new = data_new[header_new_indices, :]
return data_current, header_current, data_new, header_new


Expand Down Expand Up @@ -199,10 +195,12 @@ def average_to_interval(
# given average interval.
if len(time_stream) > 100:
time_lookup_span = round(
(
average_base_sec
* interval_look_buffer_multiple
/ np.nanmean(np.diff(time_stream[0:100]))
/ np.nanmean(np.diff(time_stream[:100]))
)
)
Gorkowski marked this conversation as resolved.
Show resolved Hide resolved
else:
time_lookup_span = 100

Expand Down