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

add parameters to stat.tsv #302

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 18 additions & 2 deletions alphadia/outputtransform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# native imports
import logging
import os
import pickle
from collections.abc import Iterator

import directlfq.config as lfqconfig
Expand Down Expand Up @@ -653,9 +654,13 @@ def build_stat_df(
stat_df_list = []
for folder in folder_list:
raw_name = os.path.basename(folder)
optimization_manager_path = os.path.join(folder, "optimization_manager.pkl")
stat_df_list.append(
_build_run_stat_df(
raw_name, psm_df[psm_df["run"] == raw_name], all_channels
raw_name,
psm_df[psm_df["run"] == raw_name],
optimization_manager_path,
all_channels,
)
)

Expand Down Expand Up @@ -820,7 +825,10 @@ def build_library(


def _build_run_stat_df(
raw_name: str, run_df: pd.DataFrame, channels: list[int] | None = None
raw_name: str,
run_df: pd.DataFrame,
optimization_manager_path: str,
channels: list[int] | None = None,
):
"""Build stat dataframe for a single run.

Expand Down Expand Up @@ -866,6 +874,14 @@ def _build_run_stat_df(
if "mobility_fwhm" in channel_df.columns:
base_dict["fwhm_mobility"] = np.mean(channel_df["mobility_fwhm"])

with open(optimization_manager_path, "rb") as f:
odespard marked this conversation as resolved.
Show resolved Hide resolved
optimization_manager = pickle.load(f)

base_dict["ms2_error"] = optimization_manager.ms2_error
base_dict["ms1_error"] = optimization_manager.ms1_error
base_dict["rt_error"] = optimization_manager.rt_error
base_dict["mobility_error"] = optimization_manager.mobility_error

out_df.append(base_dict)
odespard marked this conversation as resolved.
Show resolved Hide resolved

return pd.DataFrame(out_df)
Expand Down
17 changes: 2 additions & 15 deletions alphadia/workflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,9 @@ def load(
self._calibration_manager.disable_mobility_calibration()

# initialize the optimization manager
optimization_manager_config = {
"ms1_error": self.config["search_initial"]["initial_ms1_tolerance"],
"ms2_error": self.config["search_initial"]["initial_ms2_tolerance"],
"rt_error": self.config["search_initial"]["initial_rt_tolerance"],
"mobility_error": self.config["search_initial"][
"initial_mobility_tolerance"
],
"column_type": "library",
"num_candidates": self.config["search_initial"]["initial_num_candidates"],
"classifier_version": -1,
"fwhm_rt": self.config["optimization_manager"]["fwhm_rt"],
"fwhm_mobility": self.config["optimization_manager"]["fwhm_mobility"],
"score_cutoff": self.config["optimization_manager"]["score_cutoff"],
}

self._optimization_manager = manager.OptimizationManager(
optimization_manager_config,
self.config,
path=os.path.join(self.path, self.OPTIMIZATION_MANAGER_PATH),
load_from_file=self.config["general"]["reuse_calibration"],
figure_path=os.path.join(self.path, self.FIGURE_PATH),
Expand Down
14 changes: 13 additions & 1 deletion alphadia/workflow/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def fit_predict(
class OptimizationManager(BaseManager):
def __init__(
self,
initial_parameters: dict,
config: dict,
path: None | str = None,
load_from_file: bool = True,
**kwargs,
Expand All @@ -463,6 +463,18 @@ def __init__(
self.reporter.log_string(f"Initializing {self.__class__.__name__}")
self.reporter.log_event("initializing", {"name": f"{self.__class__.__name__}"})

initial_parameters = {
odespard marked this conversation as resolved.
Show resolved Hide resolved
"ms1_error": config["search_initial"]["initial_ms1_tolerance"],
"ms2_error": config["search_initial"]["initial_ms2_tolerance"],
"rt_error": config["search_initial"]["initial_rt_tolerance"],
"mobility_error": config["search_initial"]["initial_mobility_tolerance"],
"column_type": "library",
"num_candidates": config["search_initial"]["initial_num_candidates"],
"classifier_version": -1,
"fwhm_rt": config["optimization_manager"]["fwhm_rt"],
"fwhm_mobility": config["optimization_manager"]["fwhm_mobility"],
"score_cutoff": config["optimization_manager"]["score_cutoff"],
}
if not self.is_loaded_from_file:
self.__dict__.update(initial_parameters)

Expand Down
18 changes: 18 additions & 0 deletions tests/unit_tests/test_outputtransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from conftest import mock_fragment_df, mock_precursor_df

from alphadia import outputtransform
from alphadia.workflow import manager


def test_output_transform():
Expand Down Expand Up @@ -36,6 +37,18 @@ def test_output_transform():
"multiplexing": {
"enabled": False,
},
"search_initial": {
"initial_ms1_tolerance": 4,
"initial_ms2_tolerance": 7,
"initial_rt_tolerance": 200,
"initial_mobility_tolerance": 0.04,
"initial_num_candidates": 1,
},
"optimization_manager": {
"fwhm_rt": 2.75,
"fwhm_mobility": 2,
"score_cutoff": 50,
},
}

temp_folder = os.path.join(tempfile.gettempdir(), "alphadia")
Expand All @@ -62,6 +75,11 @@ def test_output_transform():
frag_df.to_parquet(os.path.join(raw_folder, "frag.parquet"), index=False)
psm_df.to_parquet(os.path.join(raw_folder, "psm.parquet"), index=False)

optimization_manager = manager.OptimizationManager(
config, os.path.join(raw_folder, "optimization_manager.pkl")
odespard marked this conversation as resolved.
Show resolved Hide resolved
)
optimization_manager.fit({"ms2_error": 7})

output = outputtransform.SearchPlanOutput(config, temp_folder)
_ = output.build_precursor_table(raw_folders, save=True)
_ = output.build_stat_df(raw_folders, save=True)
Expand Down
43 changes: 19 additions & 24 deletions tests/unit_tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,26 @@ def test_calibration_manager_save_load():
os.remove(temp_path)


OPTIMIZATION_TEST_DATA = {
"fwhm_cycles": 5,
"fwhm_mobility": 0.01,
OPTIMIZATION_CONFIG = {
odespard marked this conversation as resolved.
Show resolved Hide resolved
"search_initial": {
"initial_ms1_tolerance": 4,
"initial_ms2_tolerance": 7,
"initial_rt_tolerance": 200,
"initial_mobility_tolerance": 0.04,
"initial_num_candidates": 1,
},
"optimization_manager": {
"fwhm_rt": 5,
"fwhm_mobility": 0.01,
"score_cutoff": 50,
},
}


def test_optimization_manager():
optimization_manager = manager.OptimizationManager(OPTIMIZATION_TEST_DATA)
optimization_manager = manager.OptimizationManager(OPTIMIZATION_CONFIG)

assert optimization_manager.fwhm_cycles == 5
assert optimization_manager.fwhm_rt == 5
assert optimization_manager.fwhm_mobility == 0.01

assert optimization_manager.is_loaded_from_file is False
Expand All @@ -226,7 +236,7 @@ def test_optimization_manager_save_load():
temp_path = os.path.join(tempfile.tempdir, "optimization_manager.pkl")

optimization_manager = manager.OptimizationManager(
OPTIMIZATION_TEST_DATA, path=temp_path, load_from_file=False
OPTIMIZATION_CONFIG, path=temp_path, load_from_file=False
)

assert optimization_manager.is_loaded_from_file is False
Expand All @@ -235,7 +245,7 @@ def test_optimization_manager_save_load():
optimization_manager.save()

optimization_manager_loaded = manager.OptimizationManager(
OPTIMIZATION_TEST_DATA, path=temp_path, load_from_file=True
OPTIMIZATION_CONFIG, path=temp_path, load_from_file=True
)

assert optimization_manager_loaded.is_loaded_from_file is True
Expand All @@ -247,7 +257,7 @@ def test_optimization_manager_save_load():
def test_optimization_manager_fit():
temp_path = os.path.join(tempfile.tempdir, "optimization_manager.pkl")
optimization_manager = manager.OptimizationManager(
OPTIMIZATION_TEST_DATA, path=temp_path, load_from_file=False
OPTIMIZATION_CONFIG, path=temp_path, load_from_file=False
)

assert optimization_manager.is_loaded_from_file is False
Expand Down Expand Up @@ -410,23 +420,8 @@ def create_workflow_instance():
reporter=workflow.reporter,
)

optimization_manager_config = {
"ms1_error": workflow.config["search_initial"]["initial_ms1_tolerance"],
"ms2_error": workflow.config["search_initial"]["initial_ms2_tolerance"],
"rt_error": workflow.config["search_initial"]["initial_rt_tolerance"],
"mobility_error": workflow.config["search_initial"][
"initial_mobility_tolerance"
],
"column_type": "library",
"num_candidates": workflow.config["search_initial"]["initial_num_candidates"],
"classifier_version": -1,
"fwhm_rt": workflow.config["optimization_manager"]["fwhm_rt"],
"fwhm_mobility": workflow.config["optimization_manager"]["fwhm_mobility"],
"score_cutoff": workflow.config["optimization_manager"]["score_cutoff"],
}

workflow._optimization_manager = manager.OptimizationManager(
optimization_manager_config,
OPTIMIZATION_CONFIG,
path=os.path.join(workflow.path, workflow.OPTIMIZATION_MANAGER_PATH),
load_from_file=workflow.config["general"]["reuse_calibration"],
figure_path=os.path.join(workflow.path, workflow.FIGURE_PATH),
Expand Down
Loading