-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
037b569
commit 15690a5
Showing
5 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/depiction_targeted_preproc/workflow/exp/plot_map_comparison.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from pathlib import Path | ||
from typing import Annotated | ||
import xarray | ||
import typer | ||
from matplotlib import pyplot as plt | ||
|
||
|
||
def exp_plot_map_comparison( | ||
input_mass_shift_paths: Annotated[list[Path], typer.Argument()], | ||
output_pdf_path: Annotated[Path, typer.Option()], | ||
) -> None: | ||
# load all the inputs | ||
mass_shifts = [xarray.open_dataarray(path) for path in input_mass_shift_paths] | ||
|
||
fig, axs = plt.subplots(1, len(mass_shifts), figsize=(10 * len(mass_shifts), 10)) | ||
for i, shift_map in enumerate(mass_shifts): | ||
shift_map.isel(c=0).plot(x="x", y="y", ax=axs[i], cmap="coolwarm", vmin=-1, vmax=+1) | ||
axs[i].set_aspect("equal") | ||
variant = input_mass_shift_paths[i].parent.name | ||
test_mass = shift_map.coords["c"][0] | ||
axs[i].set_title(f"{variant} computed shift for test mass {test_mass:.2f}") | ||
|
||
plt.savefig(output_pdf_path, bbox_inches="tight") | ||
|
||
if __name__ == "__main__": | ||
typer.run(exp_plot_map_comparison) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/depiction_targeted_preproc/workflow/vis/test_mass_shifts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from pathlib import Path | ||
from typing import Annotated | ||
|
||
import numpy as np | ||
import polars as pl | ||
import typer | ||
import xarray as xr | ||
from typer import Option | ||
|
||
from depiction_targeted_preproc.pipeline_config.model import PipelineParameters | ||
from depiction_targeted_preproc.workflow.proc.calibrate import get_calibration_from_config | ||
|
||
|
||
def vis_test_mass_shifts( | ||
calib_hdf5_path: Annotated[Path, Option()], | ||
mass_list_path: Annotated[Path, Option()], | ||
config_path: Annotated[Path, Option()], | ||
output_hdf5_path: Annotated[Path, Option()], | ||
) -> None: | ||
# load inputs | ||
model_coefs = xr.open_dataarray(calib_hdf5_path, group="model_coefs") | ||
config = PipelineParameters.parse_yaml(config_path) | ||
mass_list = pl.read_csv(mass_list_path) | ||
calibration = get_calibration_from_config(mass_list=mass_list, calib_config=config.calibration) | ||
|
||
# define test masses | ||
# to keep it simple for now only 1 | ||
test_masses = np.array([(mass_list["mass"].max() + mass_list["mass"].min()) / 2]) | ||
test_masses_int = np.ones_like(test_masses) | ||
|
||
# compute the shifts | ||
def compute_shifts(coef): | ||
result = calibration.apply_spectrum_model(spectrum_mz_arr=test_masses, spectrum_int_arr=test_masses_int, | ||
model_coef=xr.DataArray(coef, dims=["c"])) | ||
return xr.DataArray(result[0] - test_masses, dims=["m"]) | ||
|
||
shifts = xr.apply_ufunc( | ||
compute_shifts, | ||
model_coefs, | ||
input_core_dims=[["c"]], | ||
output_core_dims=[["m"]], | ||
vectorize=True, | ||
).rename({"m": "c"}) | ||
shifts = shifts.assign_coords(c=test_masses) | ||
|
||
shifts_2d = shifts.set_xindex(["x", "y"]).unstack("i") | ||
shifts_2d.attrs["bg_value"] = np.nan | ||
|
||
# save the result | ||
shifts_2d.to_netcdf(output_hdf5_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(vis_test_mass_shifts) |