quite frankly, no idea why it started fitting. No idea why theres an error on line 77, and line 108

This commit is contained in:
ralf 2024-09-09 12:48:50 +03:00
parent e11cc744d9
commit 4fa1acbb90

View File

@ -1,5 +1,6 @@
import h5py import h5py
import pandas as pd import pandas as pd
import numpy as np
import re import re
import matplotlib as plt import matplotlib as plt
@ -10,6 +11,8 @@ from Data import Data
file = "ltcc_current.h5" file = "ltcc_current.h5"
dfs_by_sex_tag_spid = {}
def print_attrs(name, obj): def print_attrs(name, obj):
# print(f"\nAttributes for {name}:") # print(f"\nAttributes for {name}:")
@ -18,12 +21,6 @@ def print_attrs(name, obj):
pass 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: with h5py.File(file, "r") as h5_file:
for eid in h5_file.keys(): for eid in h5_file.keys():
attributes = h5_file[eid].attrs 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(): for key, df in dfs_by_sex_tag_spid.items():
print(f"Fitting and plotting data for {key}...") print(f"Fitting and plotting data for {key}...")
combined_data = [] combined_current = []
combined_time = []
for eid in df["experiment_id"].tolist(): for eid in df["experiment_id"].tolist():
data = Data(file, group_key=eid) 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() fit.optimize()
res, fig = fit.optimize() res, fig = fit.optimize()
plt.figure() plt.figure()
plt.title(f"Fit results for {key}") plt.title(f"Fit results for {key}")
for single_data in combined_data: for i, eid in enumerate(df["experiment_id"].tolist()):
plt.plot(single_data.x, single_data.y, plt.plot(combined_time, combined_current,
label=f"Experiment {single_data.eid}") 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.legend()
plt.xlabel("X") plt.xlabel("Time")
plt.ylabel("Y") plt.ylabel("Current")
key_cleaned = re.sub(r"[^\w.-]", "", key) key_cleaned = re.sub(r"[^\w.-]", "", key)
plt.savefig(f"combined_plot_{key_cleaned}.png") 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(":", "-") res_filename = res_filename.replace(" ", "_").replace(":", "-")
fit_hist.to_csv(res_filename, index=True) 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) fit_and_plot_dataframes(dfs_by_sex_tag_spid)