Skip to content

Commit

Permalink
refactor: readiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
Beforerr committed Oct 16, 2024
1 parent 32da860 commit 915a2e6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 54 deletions.
4 changes: 2 additions & 2 deletions discontinuitypy/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
'discontinuitypy/core/propeties.py'),
'discontinuitypy.core.propeties.calc_normal_direction': ( 'ids_properties.html#calc_normal_direction',
'discontinuitypy/core/propeties.py'),
'discontinuitypy.core.propeties.calc_rotation_angle': ( 'ids_properties.html#calc_rotation_angle',
'discontinuitypy/core/propeties.py'),
'discontinuitypy.core.propeties.df_rotation_angle': ( 'ids_properties.html#df_rotation_angle',
'discontinuitypy/core/propeties.py'),
'discontinuitypy.core.propeties.get_candidate_data': ( 'ids_properties.html#get_candidate_data',
Expand All @@ -80,6 +78,8 @@
'discontinuitypy.core.propeties.pdp.ApplyToRows._transform': ( 'ids_properties.html#pdp.applytorows._transform',
'discontinuitypy/core/propeties.py'),
'discontinuitypy.core.propeties.process_events': ( 'ids_properties.html#process_events',
'discontinuitypy/core/propeties.py'),
'discontinuitypy.core.propeties.rotation_angle': ( 'ids_properties.html#rotation_angle',
'discontinuitypy/core/propeties.py')},
'discontinuitypy.datasets': { 'discontinuitypy.datasets.IDsDataset': ( 'datasets.html#idsdataset',
'discontinuitypy/datasets.py'),
Expand Down
40 changes: 20 additions & 20 deletions discontinuitypy/core/propeties.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../notebooks/02_ids_properties.ipynb.

# %% auto 0
__all__ = ['get_candidate_data', 'get_candidates', 'calc_candidate_duration', 'get_data_at_times', 'calc_rotation_angle',
__all__ = ['get_data_at_times', 'get_candidate_data', 'get_candidates', 'calc_candidate_duration', 'rotation_angle',
'df_rotation_angle', 'calc_events_rotation_angle', 'calc_normal_direction', 'calc_events_normal_direction',
'calc_events_vec_change', 'IDsPdPipeline', 'process_events']

Expand Down Expand Up @@ -38,6 +38,14 @@
from pdpipe.util import out_of_place_col_insert

# %% ../../notebooks/02_ids_properties.ipynb 2
def get_data_at_times(data: xr.DataArray, times) -> np.ndarray:
"""
Select data at specified times.
"""
# Use xarray's selection capability if data supports it
return data.sel(time=times, method="nearest").to_numpy()


def get_candidate_data(
candidate: dict, data: xr.DataArray, neighbor: int = 0
) -> xr.DataArray:
Expand Down Expand Up @@ -77,15 +85,7 @@ def calc_candidate_duration(candidate, data, **kwargs):
raise e

# %% ../../notebooks/02_ids_properties.ipynb 6
def get_data_at_times(data: xr.DataArray, times) -> np.ndarray:
"""
Select data at specified times.
"""
# Use xarray's selection capability if data supports it
return data.sel(time=times, method="nearest").to_numpy()

# %% ../../notebooks/02_ids_properties.ipynb 7
def calc_rotation_angle(v1, v2):
def rotation_angle(v1, v2):
"""
Computes the rotation angle between two vectors.
Expand All @@ -112,16 +112,16 @@ def calc_rotation_angle(v1, v2):
# Convert the angles from radians to degrees
return np.degrees(angle)

# %% ../../notebooks/02_ids_properties.ipynb 8
# %% ../../notebooks/02_ids_properties.ipynb 7
def df_rotation_angle(df: pl.DataFrame, v1_cols, v2_cols, name):
v1 = df.select(v1_cols).to_numpy()
v2 = df.select(v2_cols).to_numpy()

result = calc_rotation_angle(v1, v2)
result = rotation_angle(v1, v2)

return df.with_columns(pl.Series(result).alias(name))

# %% ../../notebooks/02_ids_properties.ipynb 9
# %% ../../notebooks/02_ids_properties.ipynb 8
def calc_events_rotation_angle(events, data: xr.DataArray):
"""
Computes the rotation angle(s) at two different time steps.
Expand All @@ -132,10 +132,10 @@ def calc_events_rotation_angle(events, data: xr.DataArray):
vecs_before = get_data_at_times(data, tstart)
vecs_after = get_data_at_times(data, tstop)

rotation_angles = calc_rotation_angle(vecs_before, vecs_after)
rotation_angles = rotation_angle(vecs_before, vecs_after)
return rotation_angles

# %% ../../notebooks/02_ids_properties.ipynb 11
# %% ../../notebooks/02_ids_properties.ipynb 10
def calc_normal_direction(v1, v2, normalize=True) -> np.ndarray:
"""
Computes the normal direction of two vectors.
Expand All @@ -150,7 +150,7 @@ def calc_normal_direction(v1, v2, normalize=True) -> np.ndarray:
c = np.cross(v1, v2)
return c / np.linalg.norm(c, axis=-1, keepdims=True)

# %% ../../notebooks/02_ids_properties.ipynb 12
# %% ../../notebooks/02_ids_properties.ipynb 11
def calc_events_normal_direction(events, data: xr.DataArray):
"""
Computes the normal directions(s) at two different time steps.
Expand All @@ -165,7 +165,7 @@ def calc_events_normal_direction(events, data: xr.DataArray):
# need to convert to list first, as only 1D array is supported
return normal_directions.tolist()

# %% ../../notebooks/02_ids_properties.ipynb 13
# %% ../../notebooks/02_ids_properties.ipynb 12
def calc_events_vec_change(events, data: xr.DataArray):
"""
Utils function to calculate features related to the change of the magnetic field
Expand All @@ -177,7 +177,7 @@ def calc_events_vec_change(events, data: xr.DataArray):
vecs_after = get_data_at_times(data, tstop)
return (vecs_after - vecs_before).tolist()

# %% ../../notebooks/02_ids_properties.ipynb 16
# %% ../../notebooks/02_ids_properties.ipynb 15
@patch
def _transform(self: pdp.ApplyToRows, X, verbose):
new_cols = X.apply(self._func, axis=1)
Expand Down Expand Up @@ -210,7 +210,7 @@ def _transform(self: pdp.ApplyToRows, X, verbose):
" Only Series and DataFrame are allowed."
)

# %% ../../notebooks/02_ids_properties.ipynb 18
# %% ../../notebooks/02_ids_properties.ipynb 17
class IDsPdPipeline:
@staticmethod
def calc_duration(data: xr.DataArray, **kwargs):
Expand Down Expand Up @@ -250,7 +250,7 @@ def calc_normal_direction(data, name="normal_direction", **kwargs):
func_desc="calculating normal direction",
)

# %% ../../notebooks/02_ids_properties.ipynb 19
# %% ../../notebooks/02_ids_properties.ipynb 18
def process_events(
candidates_pl: pl.DataFrame, # potential candidates DataFrame
sat_fgm: xr.DataArray, # satellite FGM data
Expand Down
8 changes: 1 addition & 7 deletions discontinuitypy/propeties/mva.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,7 @@ def fit_maxiumum_variance_direction(
# Ensure there are enough data points to fit the model
if len(y) < 4:
# Not enough data points, return None or an empty result instead of raising an error
amplitude = np.nan
sigma = np.nan
center = np.nan
rsquared = np.nan
chisqr = np.nan
c = np.nan
best_fit = np.nan
amplitude = sigma = center = rsquared = chisqr = c = best_fit = np.nan
else:
out = mod.fit(y, params, x=x)
amplitude = out.params["amplitude"].value
Expand Down
29 changes: 11 additions & 18 deletions notebooks/02_ids_properties.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
"outputs": [],
"source": [
"# | export\n",
"def get_data_at_times(data: xr.DataArray, times) -> np.ndarray:\n",
" \"\"\"\n",
" Select data at specified times.\n",
" \"\"\"\n",
" # Use xarray's selection capability if data supports it\n",
" return data.sel(time=times, method=\"nearest\").to_numpy()\n",
"\n",
"\n",
"def get_candidate_data(\n",
" candidate: dict, data: xr.DataArray, neighbor: int = 0\n",
") -> xr.DataArray:\n",
Expand Down Expand Up @@ -125,22 +133,7 @@
"outputs": [],
"source": [
"# | export\n",
"def get_data_at_times(data: xr.DataArray, times) -> np.ndarray:\n",
" \"\"\"\n",
" Select data at specified times.\n",
" \"\"\"\n",
" # Use xarray's selection capability if data supports it\n",
" return data.sel(time=times, method=\"nearest\").to_numpy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# | export\n",
"def calc_rotation_angle(v1, v2):\n",
"def rotation_angle(v1, v2):\n",
" \"\"\"\n",
" Computes the rotation angle between two vectors.\n",
"\n",
Expand Down Expand Up @@ -179,7 +172,7 @@
" v1 = df.select(v1_cols).to_numpy()\n",
" v2 = df.select(v2_cols).to_numpy()\n",
"\n",
" result = calc_rotation_angle(v1, v2)\n",
" result = rotation_angle(v1, v2)\n",
"\n",
" return df.with_columns(pl.Series(result).alias(name))"
]
Expand All @@ -201,7 +194,7 @@
" vecs_before = get_data_at_times(data, tstart)\n",
" vecs_after = get_data_at_times(data, tstop)\n",
"\n",
" rotation_angles = calc_rotation_angle(vecs_before, vecs_after)\n",
" rotation_angles = rotation_angle(vecs_before, vecs_after)\n",
" return rotation_angles"
]
},
Expand Down
8 changes: 1 addition & 7 deletions notebooks/properties/00_mva.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,7 @@
" # Ensure there are enough data points to fit the model\n",
" if len(y) < 4:\n",
" # Not enough data points, return None or an empty result instead of raising an error\n",
" amplitude = np.nan\n",
" sigma = np.nan\n",
" center = np.nan\n",
" rsquared = np.nan\n",
" chisqr = np.nan\n",
" c = np.nan\n",
" best_fit = np.nan\n",
" amplitude = sigma = center = rsquared = chisqr = c = best_fit = np.nan\n",
" else:\n",
" out = mod.fit(y, params, x=x)\n",
" amplitude = out.params[\"amplitude\"].value\n",
Expand Down

0 comments on commit 915a2e6

Please sign in to comment.