Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
edited docstrings, pre-commit
Browse files Browse the repository at this point in the history
1. checked docstrings of `IsotopicAveragineDistribution`
    + Done
2. Tests of `IsotopicAveragineDistribution` failed, due to
   wrong scope of fixtures inside class.
    + fixed
    + Done
3. run pre-commit
  • Loading branch information
TimOliverMaier committed Dec 22, 2022
1 parent 1fe740e commit 09ffb07
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 232 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ docs_src/
.pytest_cache/

# output of dvc plots
dvc_plots/
dvc_plots/
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[build-system]
# Minimum requirements for the build system to execute.
requires = ["setuptools", "wheel"] # PEP 508 specifications.
# from https://peps.python.org/pep-0518/
# from https://peps.python.org/pep-0518/
59 changes: 38 additions & 21 deletions src/pystoms/models_3d/abstract_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
from numpy.typing import ArrayLike
from time import time

# typing
NDArrayFloat = npt.NDArray[np.float64]
NDArrayInt = npt.NDArray[np.int64]
Expand Down Expand Up @@ -452,7 +453,15 @@ def arviz_plots(
else:
plt.show()

az.plot_trace(idata_sliced, var_names, legend=True, chain_prop={"linestyle": ("solid", "dotted", "dashed", "dashdot"),"color":("black","blue","green","red")})
az.plot_trace(
idata_sliced,
var_names,
legend=True,
chain_prop={
"linestyle": ("solid", "dotted", "dashed", "dashdot"),
"color": ("black", "blue", "green", "red"),
},
)
if save_fig:
plt.tight_layout()
plt.savefig(feature_path + "trace.png")
Expand Down Expand Up @@ -484,41 +493,43 @@ def arviz_plots(

# posterior predictive lm
idata_feature_sliced = self.idata.isel(feature=idx)
az.plot_lm(y=idata_feature_sliced.observed_data.obs,
y_hat=idata_feature_sliced.posterior_predictive.obs,
num_samples=500)
az.plot_lm(
y=idata_feature_sliced.observed_data.obs,
y_hat=idata_feature_sliced.posterior_predictive.obs,
num_samples=500,
)
if save_fig:
plt.savefig(feature_path + "posterior_predictive_lm.png")
plt.close()
else:
plt.show()

# prior predictive lm
fig,ax = plt.subplots(1,1)
az.plot_lm(y=idata_feature_sliced.observed_data.obs,
y_hat=idata_feature_sliced.prior_predictive.obs,
num_samples=500,
legend=False,
axes=ax)
h,l = ax.get_legend_handles_labels()
for idx,lab in enumerate(l):
fig, ax = plt.subplots(1, 1)
az.plot_lm(
y=idata_feature_sliced.observed_data.obs,
y_hat=idata_feature_sliced.prior_predictive.obs,
num_samples=500,
legend=False,
axes=ax,
)
h, l = ax.get_legend_handles_labels()
for idx, lab in enumerate(l):
if lab == "Posterior predictive samples":
l[idx] = "Prior predictive samples"
ax.legend(h,l)
ax.legend(h, l)
if save_fig:
fig.savefig(feature_path + "prior_predictive_lm.png")
plt.close()
else:
plt.show()

az.plot_parallel(idata_sliced,norm_method="minmax")
az.plot_parallel(idata_sliced, norm_method="minmax")
if save_fig:
plt.savefig(feature_path + "plot_parallel.png")
plt.close()
else:
plt.show()



def evaluation(
self,
Expand All @@ -531,7 +542,7 @@ def evaluation(
progressbar: bool = True,
pred_name_list: Optional[List[str]] = None,
**plot_kwargs,
) -> Tuple[az.InferenceData,float]:
) -> Tuple[az.InferenceData, float]:
"""Evaluate precursor feature model.
This function is wrapping several steps
Expand Down Expand Up @@ -571,7 +582,7 @@ def evaluation(
end_sampling = time()

if pred_name_list is None:
pred_name_list = ["obs","mu"]
pred_name_list = ["obs", "mu"]

if plots is None:
# make 'in' operator available for plots
Expand All @@ -588,7 +599,9 @@ def evaluation(

if prior_pred_out:
# prior predictive analysis out-of-sample
self._sample_predictive(is_prior=True, is_grid_predictive=True, var_names=pred_name_list)
self._sample_predictive(
is_prior=True, is_grid_predictive=True, var_names=pred_name_list
)
if "prior_pred_out" in plots:
for pred_name in pred_name_list:
plot_kwargs["pred_name"] = pred_name
Expand All @@ -606,10 +619,14 @@ def evaluation(

if posterior_pred_out:
# posterior predictive analysis out-of-sample
self._sample_predictive(is_grid_predictive=True, progressbar=progressbar, var_names=pred_name_list)
self._sample_predictive(
is_grid_predictive=True,
progressbar=progressbar,
var_names=pred_name_list,
)
if "posterior_pred_out" in plots:
for pred_name in pred_name_list:
plot_kwargs["pred_name"] = pred_name
self.visualize_predictions_scatter(in_sample=False, **plot_kwargs)

return (self.idata.copy(),end_sampling-start_sampling)
return (self.idata.copy(), end_sampling - start_sampling)
25 changes: 18 additions & 7 deletions src/pystoms/models_3d/model_3d_m1.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ModelM1(AbstractModel):
Args:
features (AlignedFeatureData): Feature's data wrapped in
pystoms.AlignedFeatureData container.
model_parameters (Dictionary, optional): parameters for
model_parameters (Dictionary, optional): parameters for
priors and hyperpriors. Defaults to None.
likelihood (str, optional): Likelihood distribution. Currently
supported: 'Normal', 'StudentT'. Defaults to 'Normal'.
Expand Down Expand Up @@ -62,7 +62,9 @@ def __init__(
if model_parameters is None:
model_parameters = {}
self.model_parameters = model_parameters.copy()
super().__init__(feature_ids, batch_size, random_number_generator, name, coords=coords)
super().__init__(
feature_ids, batch_size, random_number_generator, name, coords=coords
)
self.setup_mutable_data(features)
# TODO is there a nicer way to share this between init and setup_mutable_data()
dims_2d = ["data_point", "feature"]
Expand Down Expand Up @@ -158,12 +160,21 @@ def setup_mutable_data(
axis=0,
weights=intensity,
).reshape((1, num_features, 1))
self.model_parameters.setdefault("mz_sigma",10)
mz_sigma = np.ones((1, num_features, 1), dtype="float") * self.model_parameters["mz_sigma"]
self.model_parameters.setdefault("mz_sigma", 10)
mz_sigma = (
np.ones((1, num_features, 1), dtype="float")
* self.model_parameters["mz_sigma"]
)
self.model_parameters.setdefault("alpha_lam", 1 / intensity.max(axis=0))
alpha_lam = np.ones((1, num_features), dtype="float") * self.model_parameters["alpha_lam"]
self.model_parameters.setdefault("me_sigma",10)
me_sigma = np.ones((1, num_features), dtype="float") * self.model_parameters["me_sigma"]
alpha_lam = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["alpha_lam"]
)
self.model_parameters.setdefault("me_sigma", 10)
me_sigma = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["me_sigma"]
)
z = np.array(charge).reshape((1, num_features, 1))
mz_tile = np.tile(mz, (1, 1, num_isotopic_peaks))
peaks = np.arange(num_isotopic_peaks)
Expand Down
50 changes: 35 additions & 15 deletions src/pystoms/models_3d/model_3d_m2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
model M2
"""

from typing import Optional,Dict
from typing import Optional, Dict
import pymc as pm
import pymc.math as pmath
import numpy as np
Expand Down Expand Up @@ -33,7 +33,7 @@ class ModelM2(AbstractModel):
Args:
features (AlignedFeatureData): Feature's data wrapped in
pystoms.AlignedFeatureData container.
model_parameters (Dictionary, optional): parameters for
model_parameters (Dictionary, optional): parameters for
priors and hyperpriors. Defaults to None.
likelihood (str, optional): Likelihood distribution. Currently
supported: 'Normal', 'StudentT'. Defaults to 'Normal'.
Expand Down Expand Up @@ -62,7 +62,9 @@ def __init__(
if model_parameters is None:
model_parameters = {}
self.model_parameters = model_parameters.copy()
super().__init__(feature_ids, batch_size, random_number_generator, name, coords=coords)
super().__init__(
feature_ids, batch_size, random_number_generator, name, coords=coords
)
self.setup_mutable_data(features)
# TODO is there a nicer way to share this between init and setup_mutable_data()
dims_2d = ["data_point", "feature"]
Expand Down Expand Up @@ -164,18 +166,36 @@ def setup_mutable_data(
axis=0,
weights=intensity,
).reshape((1, num_features, 1))
self.model_parameters.setdefault("mz_sigma",10)
mz_sigma = np.ones((1, num_features, 1), dtype="float") * self.model_parameters["mz_sigma"]
self.model_parameters.setdefault("mz_s_scale",0.05)
ms_s_scale = np.ones((1, num_features, 1), dtype="float") * self.model_parameters["mz_s_scale"]
self.model_parameters.setdefault("ms_s_alpha",2)
ms_s_alpha = np.ones((1, num_features, 1), dtype="float") * self.model_parameters["ms_s_alpha"]
self.model_parameters.setdefault("alpha_scale",intensity.max(axis=0))
alpha_scale = np.ones((1, num_features), dtype="float") * self.model_parameters["alpha_scale"]
self.model_parameters.setdefault("alpha_alpha",2)
alpha_alpha = np.ones((1, num_features), dtype="float") * self.model_parameters["alpha_alpha"]
self.model_parameters.setdefault("me_sigma",10)
me_sigma = np.ones((1, num_features), dtype="float") * self.model_parameters["me_sigma"]
self.model_parameters.setdefault("mz_sigma", 10)
mz_sigma = (
np.ones((1, num_features, 1), dtype="float")
* self.model_parameters["mz_sigma"]
)
self.model_parameters.setdefault("mz_s_scale", 0.05)
ms_s_scale = (
np.ones((1, num_features, 1), dtype="float")
* self.model_parameters["mz_s_scale"]
)
self.model_parameters.setdefault("ms_s_alpha", 2)
ms_s_alpha = (
np.ones((1, num_features, 1), dtype="float")
* self.model_parameters["ms_s_alpha"]
)
self.model_parameters.setdefault("alpha_scale", intensity.max(axis=0))
alpha_scale = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["alpha_scale"]
)
self.model_parameters.setdefault("alpha_alpha", 2)
alpha_alpha = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["alpha_alpha"]
)
self.model_parameters.setdefault("me_sigma", 10)
me_sigma = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["me_sigma"]
)
z = np.array(charge).reshape((1, num_features, 1))
mz_tile = np.tile(mz, (1, 1, num_isotopic_peaks))
peaks = np.arange(num_isotopic_peaks)
Expand Down
50 changes: 35 additions & 15 deletions src/pystoms/models_3d/model_3d_m3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
model M3
"""

from typing import Optional,Dict
from typing import Optional, Dict
import pymc as pm
import pymc.math as pmath
import numpy as np
Expand Down Expand Up @@ -33,7 +33,7 @@ class ModelM3(AbstractModel):
Args:
features (AlignedFeatureData): Feature's data wrapped in
pystoms.AlignedFeatureData container.
model_parameters (Dictionary, optional): parameters for
model_parameters (Dictionary, optional): parameters for
priors and hyperpriors. Defaults to None.
likelihood (str, optional): Likelihood distribution. Currently
supported: 'Normal', 'StudentT'. Defaults to 'Normal'.
Expand Down Expand Up @@ -62,7 +62,9 @@ def __init__(
if model_parameters is None:
model_parameters = {}
self.model_parameters = model_parameters.copy()
super().__init__(feature_ids, batch_size, random_number_generator, name, coords=coords)
super().__init__(
feature_ids, batch_size, random_number_generator, name, coords=coords
)
self.setup_mutable_data(features)
# TODO is there a nicer way to share this between init and setup_mutable_data()
dims_2d = ["data_point", "feature"]
Expand Down Expand Up @@ -164,18 +166,36 @@ def setup_mutable_data(
axis=0,
weights=intensity,
).reshape((1, num_features, 1))
self.model_parameters.setdefault("mz_sigma",1)
mz_sigma = np.ones((1, num_features, 1), dtype="float") * self.model_parameters["mz_sigma"]
self.model_parameters.setdefault("mz_s_scale",0.05)
ms_s_scale = np.ones((1, num_features, 1), dtype="float") * self.model_parameters["mz_s_scale"]
self.model_parameters.setdefault("ms_s_alpha",2)
ms_s_alpha = np.ones((1, num_features, 1), dtype="float") * self.model_parameters["ms_s_alpha"]
self.model_parameters.setdefault("alpha_scale",intensity.max(axis=0))
alpha_scale = np.ones((1, num_features), dtype="float") * self.model_parameters["alpha_scale"]
self.model_parameters.setdefault("alpha_alpha",2)
alpha_alpha = np.ones((1, num_features), dtype="float") * self.model_parameters["alpha_alpha"]
self.model_parameters.setdefault("me_lambda",1)
me_lambda = np.ones((1, num_features), dtype="float") * self.model_parameters["me_lambda"]
self.model_parameters.setdefault("mz_sigma", 1)
mz_sigma = (
np.ones((1, num_features, 1), dtype="float")
* self.model_parameters["mz_sigma"]
)
self.model_parameters.setdefault("mz_s_scale", 0.05)
ms_s_scale = (
np.ones((1, num_features, 1), dtype="float")
* self.model_parameters["mz_s_scale"]
)
self.model_parameters.setdefault("ms_s_alpha", 2)
ms_s_alpha = (
np.ones((1, num_features, 1), dtype="float")
* self.model_parameters["ms_s_alpha"]
)
self.model_parameters.setdefault("alpha_scale", intensity.max(axis=0))
alpha_scale = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["alpha_scale"]
)
self.model_parameters.setdefault("alpha_alpha", 2)
alpha_alpha = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["alpha_alpha"]
)
self.model_parameters.setdefault("me_lambda", 1)
me_lambda = (
np.ones((1, num_features), dtype="float")
* self.model_parameters["me_lambda"]
)
z = np.array(charge).reshape((1, num_features, 1))
mz_tile = np.tile(mz, (1, 1, num_isotopic_peaks))
peaks = np.arange(num_isotopic_peaks)
Expand Down
26 changes: 18 additions & 8 deletions src/pystoms/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from pystoms.aligned_feature_data import AlignedFeatureData


def plot_marginals(feature: AlignedFeatureData, plot_path:Optional[str] = None) -> None:
def plot_marginals(
feature: AlignedFeatureData, plot_path: Optional[str] = None
) -> None:
"""plots marginal mz, scan and intensities
Args:
Expand All @@ -18,19 +20,27 @@ def plot_marginals(feature: AlignedFeatureData, plot_path:Optional[str] = None)
feature_dataset = feature.feature_data
feature_ids = feature.accepted_feature_ids
for feature_id in feature_ids:
feature_data = feature_dataset.sel({"feature":feature_id}).to_dataframe()
feature_data = feature_dataset.sel({"feature": feature_id}).to_dataframe()
charge = feature_data["Charge"].values[0]
Fig,axs = plt.subplot_mosaic("AB;CC",figsize=(5,5),sharey=True)
Fig, axs = plt.subplot_mosaic("AB;CC", figsize=(5, 5), sharey=True)
Fig.set_dpi(300)
axs["A"].hist(x=feature_data.Intensity.values)
axs["A"].set_xlabel("Intensity")
axs["A"].set_ylabel("Count")
axs["A"].annotate(f"Max: {np.max(feature_data.Intensity.values)}", xy=(0.5, 0.9), xycoords='axes fraction')
axs["A"].annotate(
f"Max: {np.max(feature_data.Intensity.values)}",
xy=(0.5, 0.9),
xycoords="axes fraction",
)
axs["B"].hist(x=feature_data.Scan.values)
axs["B"].set_xlabel("Scan")
axs["B"].annotate(f"n: {len(feature_data.Scan.values)}", xy=(0.7, 0.9), xycoords='axes fraction')
axs["C"].hist(x=feature_data.Mz.values,bins=100)
axs["C"].annotate(f"Charge: {charge}", xy=(0.8, 0.9), xycoords='axes fraction')
axs["B"].annotate(
f"n: {len(feature_data.Scan.values)}",
xy=(0.7, 0.9),
xycoords="axes fraction",
)
axs["C"].hist(x=feature_data.Mz.values, bins=100)
axs["C"].annotate(f"Charge: {charge}", xy=(0.8, 0.9), xycoords="axes fraction")
axs["C"].set_xlabel("Mz")
axs["C"].set_ylabel("Count")
plt.tight_layout()
Expand All @@ -39,4 +49,4 @@ def plot_marginals(feature: AlignedFeatureData, plot_path:Optional[str] = None)
else:
if not os.path.exists(f"{plot_path}/feature{feature_id}/"):
os.mkdir(f"{plot_path}/feature{feature_id}/")
plt.savefig(f"{plot_path}/feature{feature_id}/marginals.png")
plt.savefig(f"{plot_path}/feature{feature_id}/marginals.png")
Loading

0 comments on commit 09ffb07

Please sign in to comment.