From 4fa1acbb90897912bcb6252d85b6d18e50c41d0f Mon Sep 17 00:00:00 2001 From: ralf Date: Mon, 9 Sep 2024 12:48:50 +0300 Subject: [PATCH] quite frankly, no idea why it started fitting. No idea why theres an error on line 77, and line 108 --- multiple_experiment_fitter.py | 48 ++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/multiple_experiment_fitter.py b/multiple_experiment_fitter.py index 16810b9..85f725b 100644 --- a/multiple_experiment_fitter.py +++ b/multiple_experiment_fitter.py @@ -1,5 +1,6 @@ import h5py import pandas as pd +import numpy as np import re import matplotlib as plt @@ -10,6 +11,8 @@ from Data import Data file = "ltcc_current.h5" +dfs_by_sex_tag_spid = {} + def print_attrs(name, obj): # print(f"\nAttributes for {name}:") @@ -18,12 +21,6 @@ def print_attrs(name, obj): 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 @@ -53,30 +50,45 @@ 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 = [] + combined_current = [] + combined_time = [] for eid in df["experiment_id"].tolist(): data = Data(file, group_key=eid) - combined_data.append(data) + combined_current.append(data.current) + combined_time.append(data.current_t) - collective_data = Data.combine(combined_data) + combined_current = np.concatenate(combined_current) + combined_time = np.concatenate(combined_time) - fit = Fitter(Model, collective_data) + # Sort by time for consistency - inspired by data + sorted_indices = np.argsort(combined_time) + combined_time = combined_time[sorted_indices] + combined_current = combined_current[sorted_indices] + + combined_data = Data(file, group_key=eid) + combined_data.current = combined_current + combined_data.current_t = combined_time + + fit = Fitter(Model, combined_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}") + for i, eid in enumerate(df["experiment_id"].tolist()): + plt.plot(combined_time, combined_current, + label=f"Experiment {eid}") + + plt.plot( + combined_data.current_t, combined_data.current, "k-", + label="Combined Fit" + ) - plt.plot(collective_data.x, collective_data.y, 'k-', - label="Combined Fit") plt.legend() - plt.xlabel("X") - plt.ylabel("Y") + plt.xlabel("Time") + plt.ylabel("Current") key_cleaned = re.sub(r"[^\w.-]", "", key) plt.savefig(f"combined_plot_{key_cleaned}.png") @@ -90,7 +102,7 @@ def fit_and_plot_dataframes(dfs_by_sex_tag_spid): res_filename = res_filename.replace(" ", "_").replace(":", "-") fit_hist.to_csv(res_filename, index=True) - print(f"Finished fitting for {key}. Results saved.") + print(f"Finished fitting for {key}.") fit_and_plot_dataframes(dfs_by_sex_tag_spid)