Skip to content

Commit

Permalink
chore: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
MyPyDavid committed Feb 18, 2024
1 parent 43d9e18 commit b836bd9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 276 deletions.
123 changes: 0 additions & 123 deletions src/raman_fitting/exports/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

from raman_fitting.exports.plotting_fit_results import fit_spectrum_plot
from raman_fitting.exports.plotting_raw_data import raw_data_spectra_plot
from raman_fitting.exports.file_table import raw_data_spectra_export


import pandas as pd
from loguru import logger


Expand Down Expand Up @@ -58,124 +56,3 @@ def export_files(self):
}
)
return exports


def raw_data_export(fitting_specs): # pragma: no cover
current_sample = fitting_specs[0].windowname, fitting_specs[0].sIDmean_col
try:
raw_data_spectra_plot(fitting_specs)
except Exception as e:
print("no extra Raw Data plots for {1} \n {0}".format(e, current_sample))
try:
raw_data_spectra_export(fitting_specs)
except Exception as e:
print("no extra Raw Data plots for {1} \n {0}".format(e, current_sample))


class _Exporter:
"""
The Exporter class handles all the exporting of spectra and models
into figures and xlsx files.
"""

def __init__(
self, arg, raw_out=True, plot=True, model_names_prefix=["first", "second"]
):
self.raw_out = raw_out
self.plot = plot
try:
self.delegator(arg)
except ExporterError:
logger.warning(
f"{self.__class__.__qualname__} failed export from {type(arg)}"
)
except Exception as e:
logger.error(
f"{self.__class__.__qualname__} failed export with unexpected error {e}"
)

# Exporting and Plotting
def delegator(self, arg):
self.fitter = arg
if "Fitter" in type(arg).__name__:
self.fitter = arg
self.split_results()

if self.raw_out:
self.raw_export()

if self.plot:
self.export_fitting_plotting_models()
elif isinstance(arg, list):
# "list" in type([]).__name__:
# FIXME
try:
self.export_from_list(arg)
except Exception as e:
logger.error(
"f{self.__class__.__qualname__} failed export from list", e
)
else:
logger.warning(
"f{self.__class__.__qualname__} failed export from unknown arg type {type(arg)}"
)
raise ExporterError

def export_from_list(self, arg):
fitter_args = [i for i in arg if hasattr(arg, "fitter")]
if fitter_args:
FitRes = pd.concat(
[
val.FitParameters
for exp in fitter_args
for k, val in exp.fitter.FitResults.items()
]
)
_info = fitter_args[0].fitter.info
self.export_fitparams_grp_per_model(FitRes, _info)

def export_fitparams_grp_per_model(self, FitRes, _info):
DestGrpDir = _info.get("DestGrpDir")
grpnm = _info["SampleGroup"]
for pknm, pkgrp in FitRes.groupby(level=0):
peak_destpath = DestGrpDir.joinpath(f"{grpnm}_FitParameters_{pknm}")
pkgrp.dropna(axis=1).to_excel(
peak_destpath.with_suffix(".xlsx"), index=False
)

def raw_export(self):
raw_data_export(self.fitter.spectra_arg.fitting_spectra)

def export_fitting_plotting_models(self):
pars1, pars2 = [], []

_first = {
k: val for k, val in self.fitter.FitResults.items() if k.startswith("first")
}
_second = {
k: val
for k, val in self.fitter.FitResults.items()
if k.startswith("second")
}

for modname_2, fitres_2 in _second.items():
self.export_xls_from_spec(fitres_2)
pars2.append(fitres_2.FitParameters)
for modname_1, fitres_1 in _first.items():
self.export_xls_from_spec(fitres_1)
try:
fit_spectrum_plot(
modname_1,
modname_2,
fitres_1,
fitres_2,
plot_Annotation=True,
plot_Residuals=True,
)
except Exception as e:
print(
f"Error fit_spectrum_plot:{modname_1}, {fitres_1.raw_data_col}.\n {e}"
)
pars1.append(fitres_1.FitParameters)
return pd.concat(pars1, sort=False), pd.concat(pars2, sort=False)
23 changes: 12 additions & 11 deletions src/raman_fitting/exports/plotting_fit_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def fit_spectrum_plot(
aggregated_spectra: Dict[WindowNames, AggregatedSampleSpectrumFitResult],
export_paths: ExportPathSettings,
export_paths: ExportPathSettings | None = None,
plot_Annotation=True,
plot_Residuals=True,
): # pragma: no cover
Expand Down Expand Up @@ -221,14 +221,15 @@ def fit_spectrum_plot(
ax2nd.set_xlabel("Raman shift (cm$^{-1}$)"),
ax2nd.set_ylabel("normalized I / a.u."),
)
savepath = export_paths.plots.joinpath(f"Model_{first_model_name}").with_suffix(
".png"
)
plt.savefig(
savepath,
dpi=100,
bbox_extra_artists=_bbox_artists,
bbox_inches="tight",
)
logger.debug(f"plot saved to {savepath}")
if export_paths is not None:
savepath = export_paths.plots.joinpath(f"Model_{first_model_name}").with_suffix(
".png"
)
plt.savefig(
savepath,
dpi=100,
bbox_extra_artists=_bbox_artists,
bbox_inches="tight",
)
logger.debug(f"Plot saved to {savepath}")
plt.close()
79 changes: 42 additions & 37 deletions src/raman_fitting/imports/files/file_indexer.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
""" Indexer for raman data files """
from itertools import groupby, filterfalse
from typing import List, Sequence
from typing import TypeAlias

from itertools import filterfalse, groupby
from pathlib import Path
from typing import List, Sequence, TypeAlias

from loguru import logger
from pydantic import (
BaseModel,
ConfigDict,
Field,
FilePath,
NewPath,
Field,
model_validator,
ConfigDict,
)

from raman_fitting.config import settings
from raman_fitting.imports.collector import collect_raman_file_infos
from raman_fitting.imports.models import RamanFileInfo

from raman_fitting.imports.files.utils import load_dataset_from_file


from loguru import logger
from raman_fitting.imports.models import RamanFileInfo
from tablib import Dataset


RamanFileInfoSet: TypeAlias = Sequence[RamanFileInfo]


Expand All @@ -41,23 +34,13 @@ def read_or_load_data(self) -> "RamanFileIndex":
if not any([self.index_file, self.raman_files, self.dataset]):
raise ValueError("Not all fields should be empty.")

if self.index_file is not None:
if self.index_file.exists() and not self.force_reload:
self.dataset = load_dataset_from_file(self.index_file)
self.raman_files = parse_dataset_to_index(self.dataset)
return self
elif self.index_file.exists() and self.force_reload:
logger.warning(
f"Index index_file file {self.index_file} exists and will be overwritten."
)
elif not self.index_file.exists() and self.force_reload:
logger.info(
"Index index_file file does not exists but was asked to reload from it."
)
elif not self.index_file.exists() and not self.force_reload:
pass
else:
logger.debug("Index file not provided, index will not be persisted.")
reload_from_file = validate_reload_from_index_file(
self.index_file, self.force_reload
)
if reload_from_file:
self.dataset = load_dataset_from_file(self.index_file)
self.raman_files = parse_dataset_to_index(self.dataset)
return self

if self.raman_files is not None:
dataset_rf = cast_raman_files_to_dataset(self.raman_files)
Expand All @@ -66,16 +49,38 @@ def read_or_load_data(self) -> "RamanFileIndex":
dataset_rf == self.dataset
), "Both dataset and raman_files provided and they are different."
self.dataset = dataset_rf
else:
if self.dataset is not None:
self.raman_files = parse_dataset_to_index(self.dataset)
else:
raise ValueError(
"Index error, both raman_files and dataset are not provided."
)

if self.dataset is not None:
self.raman_files = parse_dataset_to_index(self.dataset)

if self.raman_files is None and self.dataset is None:
raise ValueError(
"Index error, both raman_files and dataset are not provided."
)
return self


def validate_reload_from_index_file(
index_file: Path | None, force_reload: bool
) -> bool:
if index_file is None:
logger.debug(
"Index file not provided, index will not be reloaded or persisted."
)
return False
if index_file.exists() and not force_reload:
return True
elif force_reload:
logger.warning(
f"Index index_file file {index_file} exists and will be overwritten."
)
else:
logger.info(
"Index index_file file does not exists but was asked to reload from it."
)
return False


def cast_raman_files_to_dataset(raman_files: List[RamanFileInfo]) -> Dataset:
headers = list(RamanFileInfo.model_fields.keys())
data = Dataset(headers=headers)
Expand Down
Loading

0 comments on commit b836bd9

Please sign in to comment.