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

Pydantic model #487

Merged
merged 33 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
64848b9
adding this flag due to https://github.com/astral-sh/ruff/issues/5434
zain-sohail Jul 31, 2024
eb5ee3f
first pydantic model for config
zain-sohail Jul 31, 2024
600ef55
without typeddict
zain-sohail Sep 14, 2024
24998df
remove defaults
zain-sohail Sep 14, 2024
bea6079
update lock file with pydantic
zain-sohail Sep 14, 2024
c7aac14
nest the models
zain-sohail Sep 15, 2024
c179d9a
add copytool in configmodel and other attrs
zain-sohail Sep 15, 2024
d7cab5f
update config files to conform to model
zain-sohail Sep 15, 2024
75f1200
use configmodel in processor class
zain-sohail Sep 16, 2024
8ef5807
update modules to new config
zain-sohail Sep 16, 2024
02aea0a
fix some config problems
zain-sohail Sep 16, 2024
d84f6e4
update lockfile
rettigl Oct 7, 2024
5c0f75e
Merge remote-tracking branch 'origin/v1_feature_branch' into pydantic…
rettigl Oct 12, 2024
1146b4f
fix tests for calibrators
rettigl Oct 12, 2024
a1a9b27
make model fail on extra parameters
rettigl Oct 12, 2024
e3577bb
fix flash loader
rettigl Oct 12, 2024
8054bfa
fix calibrator tests again
rettigl Oct 12, 2024
35dcd11
fix processor tests
rettigl Oct 12, 2024
738cd85
fix sxp loader
rettigl Oct 12, 2024
bc6f457
update notebooks
rettigl Oct 12, 2024
bc40fea
fix remaining tests
rettigl Oct 12, 2024
b6db85f
add config model for copy tool
rettigl Oct 13, 2024
bdc5bac
Add further type refinements to config model
rettigl Oct 13, 2024
022dc69
add tests for config model
zain-sohail Oct 14, 2024
52a11dc
fix remaining tutorials
rettigl Oct 14, 2024
9edbea2
fix config model tests
rettigl Oct 15, 2024
ad8705d
add review suggestions
rettigl Oct 15, 2024
7aa7231
fix reporting of energy/delay offsets
rettigl Oct 15, 2024
ec20e03
fix handling of nexus input files and tests
rettigl Oct 15, 2024
4e535cf
fix error reporting
rettigl Oct 15, 2024
7a7441c
fix sxp notebook
rettigl Oct 17, 2024
fb04ce6
changes from review
rettigl Oct 21, 2024
fea015a
Move static (#511)
zain-sohail Oct 22, 2024
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
2 changes: 2 additions & 0 deletions .cspell/custom-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ ftype
fwhm
genindex
getgid
getgrgid
getmtime
gpfs
griddata
Expand Down Expand Up @@ -290,6 +291,7 @@ ptargs
pullrequest
pval
pyarrow
pydantic
pyenv
pygments
pynxtools
Expand Down
134 changes: 133 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ tqdm = ">=4.62.3"
xarray = ">=0.20.2"
joblib = ">=1.2.0"
pyarrow = ">=14.0.1, <17.0"
pydantic = ">=2.8.2"
jupyter = {version = ">=1.0.0", optional = true}
ipykernel = {version = ">=6.9.1", optional = true}
jupyterlab = {version = "^3.4.0", optional = true}
Expand Down
60 changes: 29 additions & 31 deletions sed/calibrator/delay.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def __init__(
self._verbose = verbose
set_verbosity(logger, self._verbose)

self.adc_column: str = self._config["dataframe"].get("adc_column", None)
self.delay_column: str = self._config["dataframe"]["delay_column"]
self.corrected_delay_column = self._config["dataframe"].get(
"corrected_delay_column",
self.adc_column: str = config["dataframe"]["columns"]["adc"]
self.delay_column: str = config["dataframe"]["columns"]["delay"]
self.corrected_delay_column = self._config["dataframe"]["columns"].get(
"corrected_delay",
self.delay_column,
)
self.calibration: dict[str, Any] = self._config["delay"].get("calibration", {})
Expand Down Expand Up @@ -102,9 +102,9 @@ def append_delay_axis(
df (pd.DataFrame | dask.dataframe.DataFrame): The dataframe where
to apply the delay calibration to.
adc_column (str, optional): Source column for delay calibration.
Defaults to config["dataframe"]["adc_column"].
Defaults to config["dataframe"]["columns"]["adc"].
delay_column (str, optional): Destination column for delay calibration.
Defaults to config["dataframe"]["delay_column"].
Defaults to config["dataframe"]["columns"]["delay"].
calibration (dict, optional): Calibration dictionary with parameters for
delay calibration.
adc_range (tuple | list | np.ndarray, optional): The range of used
Expand Down Expand Up @@ -146,7 +146,7 @@ def append_delay_axis(
or datafile is not None
):
calibration = {}
calibration["creation_date"] = datetime.now().timestamp()
calibration["creation_date"] = datetime.now()
if adc_range is not None:
calibration["adc_range"] = adc_range
if delay_range is not None:
Expand All @@ -158,9 +158,7 @@ def append_delay_axis(
else:
# report usage of loaded parameters
if "creation_date" in calibration and not suppress_output:
datestring = datetime.fromtimestamp(calibration["creation_date"]).strftime(
"%m/%d/%Y, %H:%M:%S",
)
datestring = calibration["creation_date"].strftime("%m/%d/%Y, %H:%M:%S")
logger.info(f"Using delay calibration parameters generated on {datestring}")

if adc_column is None:
Expand Down Expand Up @@ -212,7 +210,7 @@ def append_delay_axis(
)
if not suppress_output:
logger.info(f"Converted delay_range (ps) = {calibration['delay_range']}")
calibration["creation_date"] = datetime.now().timestamp()
calibration["creation_date"] = datetime.now()

if "delay_range" in calibration.keys():
df[delay_column] = calibration["delay_range"][0] + (
Expand Down Expand Up @@ -285,9 +283,10 @@ def add_offsets(
# pylint:disable=duplicate-code
# use passed parameters, overwrite config
offsets = {}
offsets["creation_date"] = datetime.now().timestamp()
offsets["creation_date"] = datetime.now()
# column-based offsets
if columns is not None:
offsets["columns"] = {}
if weights is None:
weights = 1
if isinstance(weights, (int, float, np.integer, np.floating)):
Expand All @@ -314,7 +313,7 @@ def add_offsets(

# store in offsets dictionary
for col, weight, pmean, red in zip(columns, weights, preserve_mean, reductions):
offsets[col] = {
offsets["columns"][col] = {
"weight": weight,
"preserve_mean": pmean,
"reduction": red,
Expand All @@ -330,9 +329,7 @@ def add_offsets(
offsets["flip_delay_axis"] = flip_delay_axis

elif "creation_date" in offsets and not suppress_output:
datestring = datetime.fromtimestamp(offsets["creation_date"]).strftime(
"%m/%d/%Y, %H:%M:%S",
)
datestring = offsets["creation_date"].strftime("%m/%d/%Y, %H:%M:%S")
logger.info(f"Using delay offset parameters generated on {datestring}")

if len(offsets) > 0:
Expand All @@ -359,21 +356,22 @@ def add_offsets(
f"Invalid value for flip_delay_axis in config: {flip_delay_axis}.",
)
log_str += f"\n Flip delay axis: {flip_delay_axis}"
else:
columns.append(k)
try:
weight = v["weight"]
except KeyError:
weight = 1
weights.append(weight)
pm = v.get("preserve_mean", False)
preserve_mean.append(pm)
red = v.get("reduction", None)
reductions.append(red)
log_str += (
f"\n Column[{k}]: Weight={weight}, Preserve Mean: {pm}, "
f"Reductions: {red}."
)
elif k == "columns":
for k2, v2 in offsets["columns"].items():
columns.append(k2)
try:
weight = v2["weight"]
except KeyError:
weight = 1
weights.append(weight)
pm = v2.get("preserve_mean", False)
preserve_mean.append(pm)
red = v2.get("reduction", None)
reductions.append(red)
log_str += (
f"\n Column[{k}]: Weight={weight}, Preserve Mean: {pm}, "
f"Reductions: {red}."
)

if not suppress_output:
logger.info(log_str)
Expand Down
Loading