Spaces:
Sleeping
Sleeping
Update app/api_utils.py
#33
by axelrvite - opened
- app/api_utils.py +10 -65
app/api_utils.py
CHANGED
|
@@ -23,7 +23,7 @@ def iv_curve(voltage, rsh, rs, nNsVth, J0, Jph):
|
|
| 23 |
|
| 24 |
def comp_iv_curve(filename):
|
| 25 |
path_data = tempfile.gettempdir()
|
| 26 |
-
path_model = os.path.join(os.getcwd(), "models", "
|
| 27 |
path_input = os.path.join(os.getcwd(), "models")
|
| 28 |
visualizer = SolarCellVisualizer()
|
| 29 |
|
|
@@ -35,83 +35,28 @@ def comp_iv_curve(filename):
|
|
| 35 |
X_scaled, I_list, V_list = processor.preprocess_input([filename], path_data)
|
| 36 |
params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot = False)
|
| 37 |
|
| 38 |
-
param_fit, I_after_fit, ssd = fit_after_model(I_list, V_list, params)
|
| 39 |
|
| 40 |
return param_fit, I_after_fit
|
| 41 |
|
| 42 |
def comp_iv_curve_cnn1d(filename):
|
| 43 |
path_data = tempfile.gettempdir()
|
| 44 |
-
path_model = os.path.join(os.getcwd(), "models", "
|
| 45 |
path_input = os.path.join(os.getcwd(), "models")
|
| 46 |
visualizer = SolarCellVisualizer()
|
| 47 |
|
| 48 |
-
# Chargement modèle
|
| 49 |
-
model = tf.keras.models.load_model(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
processor = SolarDataProcessor(path_input, path_data)
|
| 55 |
X_scaled, I_list, V_list = processor.preprocess_input([filename], path_data)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot=False)
|
| 61 |
-
|
| 62 |
-
param_fit, I_after_fit, ssd = fit_after_model(I_list, V_list, params)
|
| 63 |
|
| 64 |
return param_fit, I_after_fit
|
| 65 |
|
| 66 |
-
|
| 67 |
-
def fit_after_model(I_list, V_list, params):
|
| 68 |
-
#permet de rééfectuer une descente de gradient avec comme préfit les paramètres du modèle
|
| 69 |
-
k = 1.3806503e-23 # Boltzmann constant (J/K)
|
| 70 |
-
q = 1.60217646e-19 # Elementary charge (C)
|
| 71 |
-
|
| 72 |
-
#self.Tmes = int(chemin.split("\\")[-1].split("_")[1]) + 273.15
|
| 73 |
-
Tmes = 23 + 273.15
|
| 74 |
-
|
| 75 |
-
Vth = k * Tmes / q
|
| 76 |
-
|
| 77 |
-
params_to_fit = { "Rsh": params['Rsh'],
|
| 78 |
-
"Rs": params['Rs'],
|
| 79 |
-
"nVth": params['n']*Vth,
|
| 80 |
-
"J0": params['J0'],
|
| 81 |
-
"Jph": params['Jph']}
|
| 82 |
-
bounds = {
|
| 83 |
-
"Rsh": [1, 1e9],
|
| 84 |
-
"Rs": [0.1, 25],
|
| 85 |
-
"nVth": [0.8*Vth, 3*Vth],
|
| 86 |
-
"J0": [1e-20, 1e-5],
|
| 87 |
-
}
|
| 88 |
-
try:
|
| 89 |
-
if params_to_fit["Jph"] > 0:
|
| 90 |
-
low_bounds = [bounds["Rsh"][0],bounds["Rs"][0], bounds["nVth"][0], bounds["J0"][0], params_to_fit["Jph"] * 0.8]
|
| 91 |
-
high_bounds = [bounds["Rsh"][1],bounds["Rs"][1], bounds["nVth"][1], bounds["J0"][1],params_to_fit["Jph"] * 1.2]
|
| 92 |
-
else:
|
| 93 |
-
low_bounds = [bounds["Rsh"][0],bounds["Rs"][0], bounds["nVth"][0], bounds["J0"][0], params_to_fit["Jph"] * 1.2]
|
| 94 |
-
high_bounds = [bounds["Rsh"][1],bounds["Rs"][1], bounds["nVth"][1], bounds["J0"][1],params_to_fit["Jph"] * 0.8]
|
| 95 |
-
except:
|
| 96 |
-
low_bounds = [bounds["Rsh"][0],bounds["Rs"][0], bounds["nVth"][0], bounds["J0"][0], -1]
|
| 97 |
-
high_bounds = [bounds["Rsh"][1],bounds["Rs"][1], bounds["nVth"][1], bounds["J0"][1],1]
|
| 98 |
-
|
| 99 |
-
# Ajustement automatique des paramètres s'ils dépassent les bornes
|
| 100 |
-
params_to_fit["Rsh"] = min(max(params_to_fit["Rsh"], bounds["Rsh"][0]), bounds["Rsh"][1])
|
| 101 |
-
params_to_fit["Rs"] = min(max(params_to_fit["Rs"], bounds["Rs"][0]), bounds["Rs"][1])
|
| 102 |
-
params_to_fit["nVth"] = min(max(params_to_fit["nVth"], bounds["nVth"][0]), bounds["nVth"][1])
|
| 103 |
-
params_to_fit["J0"] = min(max(params_to_fit["J0"], bounds["J0"][0]), bounds["J0"][1])
|
| 104 |
-
popt, pcov = curve_fit(iv_curve, V_list[0], -I_list[0],
|
| 105 |
-
p0=list(params_to_fit.values()), bounds=(low_bounds, high_bounds), maxfev=10000)
|
| 106 |
-
param_fit = { "Rsh": popt[0],
|
| 107 |
-
"Rs": popt[1],
|
| 108 |
-
"nVth": popt[2],
|
| 109 |
-
"J0": popt[3],
|
| 110 |
-
"Jph":popt[4]}
|
| 111 |
-
param_fit["n"] = param_fit["nVth"] / Vth
|
| 112 |
-
I_after_fit = -iv_curve(V_list[0], param_fit['Rsh'], param_fit['Rs'], param_fit['n']*Vth, param_fit['J0'], param_fit['Jph'])
|
| 113 |
-
ssd = np.mean((I_list[0] - I_after_fit) ** 2)
|
| 114 |
-
return param_fit, I_after_fit, ssd
|
| 115 |
|
| 116 |
def read_iv_csv(filepath):
|
| 117 |
points = []
|
|
|
|
| 23 |
|
| 24 |
def comp_iv_curve(filename):
|
| 25 |
path_data = tempfile.gettempdir()
|
| 26 |
+
path_model = os.path.join(os.getcwd(), "models", "final_model.h5")
|
| 27 |
path_input = os.path.join(os.getcwd(), "models")
|
| 28 |
visualizer = SolarCellVisualizer()
|
| 29 |
|
|
|
|
| 35 |
X_scaled, I_list, V_list = processor.preprocess_input([filename], path_data)
|
| 36 |
params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot = False)
|
| 37 |
|
| 38 |
+
param_fit, I_after_fit, ssd = visualizer.fit_after_model(I_list[0], V_list[0], params)
|
| 39 |
|
| 40 |
return param_fit, I_after_fit
|
| 41 |
|
| 42 |
def comp_iv_curve_cnn1d(filename):
|
| 43 |
path_data = tempfile.gettempdir()
|
| 44 |
+
path_model = os.path.join(os.getcwd(), "models", "final_model.h5")
|
| 45 |
path_input = os.path.join(os.getcwd(), "models")
|
| 46 |
visualizer = SolarCellVisualizer()
|
| 47 |
|
| 48 |
+
# Chargement modèle
|
| 49 |
+
model = tf.keras.models.load_model(path_model,
|
| 50 |
+
custom_objects={'mse': losses.MeanSquaredError(), 'mae': metrics.MeanAbsoluteError()})
|
| 51 |
+
|
|
|
|
|
|
|
| 52 |
processor = SolarDataProcessor(path_input, path_data)
|
| 53 |
X_scaled, I_list, V_list = processor.preprocess_input([filename], path_data)
|
| 54 |
+
params, I_model = visualizer.plot_comparaison_courbes(processor, model, X_scaled, I_list, V_list, Plot = False)
|
| 55 |
+
|
| 56 |
+
param_fit, I_after_fit, ssd = visualizer.fit_after_model(I_list[0], V_list[0], params)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
return param_fit, I_after_fit
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def read_iv_csv(filepath):
|
| 62 |
points = []
|