diff --git a/.gitignore b/.gitignore index 8e77355..d393d71 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ fit_results_1c5ca4b12ae2ddffc3960c1fe39a3cce35967ce23dbac57c010f450e796d01fd_201 plot_1c5ca4b12ae2ddffc3960c1fe39a3cce35967ce23dbac57c010f450e796d01fd_2017.11.27140704.pdf plot_1c5ca4b12ae2ddffc3960c1fe39a3cce35967ce23dbac57c010f450e796d01fd_2017.11.27140704.png naidis_fit.pdf +__pycache__/Data.cpython-312.pyc +__pycache__/Model.cpython-312.pyc +__pycache__/fitter.cpython-312.pyc diff --git a/experiment_fitter.py b/experiment_fitter.py index 1f2a5fd..d67a0a4 100644 --- a/experiment_fitter.py +++ b/experiment_fitter.py @@ -6,6 +6,45 @@ from Model import Model from fitter import Fitter from Data import Data +file = "ltcc_current.h5" + + +def print_attrs(name, obj): + print(f"\nAttributes for {name}:") + for key, val in obj.attrs.items(): + print(f" {key}: {val}") + + +with h5py.File(file, "r") as h5_file: + h5_file.visititems(print_attrs) + +# Dict to hold DFs 'sex', 'tag' & 'spid' +dfs_by_sex_tag_spid = {} + +with h5py.File(file, "r") as h5_file: + for eid in h5_file.keys(): + attributes = h5_file[eid].attrs + sex = attributes.get("sex") + tag = attributes.get("tag") + spid = attributes.get("spid") + + key = f"{sex}_{tag}_{spid}" + + if key not in dfs_by_sex_tag_spid: + dfs_by_sex_tag_spid[key] = pd.DataFrame() + + row_data = {"experiment_id": eid, "sex": sex, "tag": tag, "spid": spid} + temp_df = pd.DataFrame([row_data]) + + dfs_by_sex_tag_spid[key] = pd.concat( + [dfs_by_sex_tag_spid[key], temp_df], ignore_index=True + ) + +for key, df in dfs_by_sex_tag_spid.items(): + print(f"DataFrame for {key}:") + print(df) + print() + def fit_data(): filename = "ltcc_current.h5" diff --git a/multiple_experiment_fitter.py b/multiple_experiment_fitter.py new file mode 100644 index 0000000..16810b9 --- /dev/null +++ b/multiple_experiment_fitter.py @@ -0,0 +1,124 @@ +import h5py +import pandas as pd +import re + +import matplotlib as plt + +from Model import Model +from fitter import Fitter +from Data import Data + +file = "ltcc_current.h5" + + +def print_attrs(name, obj): + # print(f"\nAttributes for {name}:") + # for key, val in obj.attrs.items(): + # print(f" {key}: {val}") + pass + + +with h5py.File(file, "r") as h5_file: + h5_file.visititems(print_attrs) + + +dfs_by_sex_tag_spid = {} + +with h5py.File(file, "r") as h5_file: + for eid in h5_file.keys(): + attributes = h5_file[eid].attrs + sex = attributes.get("sex") + tag = attributes.get("tag") + spid = attributes.get("spid") + + key = f"{sex}_{tag}_{spid}" + + if key not in dfs_by_sex_tag_spid: + dfs_by_sex_tag_spid[key] = pd.DataFrame() + + row_data = {"experiment_id": eid, "sex": sex, "tag": tag, "spid": spid} + temp_df = pd.DataFrame([row_data]) + + dfs_by_sex_tag_spid[key] = pd.concat( + [dfs_by_sex_tag_spid[key], temp_df], ignore_index=True + ) + +# for key, df in dfs_by_sex_tag_spid.items(): +# print(f"DataFrame for {key}:") +# print(df) +# print() + + +def fit_and_plot_dataframes(dfs_by_sex_tag_spid): + for key, df in dfs_by_sex_tag_spid.items(): + print(f"Fitting and plotting data for {key}...") + + combined_data = [] + + for eid in df["experiment_id"].tolist(): + data = Data(file, group_key=eid) + combined_data.append(data) + + collective_data = Data.combine(combined_data) + + fit = Fitter(Model, collective_data) + fit.optimize() + res, fig = fit.optimize() + + plt.figure() + plt.title(f"Fit results for {key}") + + for single_data in combined_data: + plt.plot(single_data.x, single_data.y, + label=f"Experiment {single_data.eid}") + + plt.plot(collective_data.x, collective_data.y, 'k-', + label="Combined Fit") + plt.legend() + plt.xlabel("X") + plt.ylabel("Y") + + key_cleaned = re.sub(r"[^\w.-]", "", key) + plt.savefig(f"combined_plot_{key_cleaned}.png") + plt.savefig(f"combined_plot_{key_cleaned}.pdf") + plt.close() + + fit_hist = pd.DataFrame.from_dict(fit.fit_results, orient="index").T + fit_hist.index.name = "Iterations" + + res_filename = f"combined_fit_results_{key}.csv" + res_filename = res_filename.replace(" ", "_").replace(":", "-") + fit_hist.to_csv(res_filename, index=True) + + print(f"Finished fitting for {key}. Results saved.") + + +fit_and_plot_dataframes(dfs_by_sex_tag_spid) + +""" +def fit_data(): + filename = "ltcc_current.h5" + with h5py.File(filename, "r") as h5: + eids = list(h5.keys()) + + for eid in eids: + data = Data(filename, group_key=eid) + fit = Fitter(Model, data) + fit.optimize() + res, fig = fit.optimize() + + fit_hist = pd.DataFrame.from_dict(fit.fit_results, orient="index").T + fit_hist.index.name = "Iterations" + + res_filename = f"fit_results_{eid}.csv" + res_filename = res_filename.replace(" ", "_").replace(":", "-") + fit_hist.to_csv(res_filename, index=True) + + eid_cleaned = re.sub(r"[^w.-]", "", eid) # Eemaldab kõik eritähed + fig.savefig(f"plot_{eid_cleaned}.png") + fig.savefig(f"plot_{eid_cleaned}.pdf") + + +fit_data() + +"""