Skip to content

Commit

Permalink
* fix mismatch problem with array
Browse files Browse the repository at this point in the history
- add insert nan_values function
- get the positive imaginary indeces
  • Loading branch information
fuzhanrahmanian committed Aug 7, 2023
1 parent 7a4ec02 commit 4cbaee4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion madap/echem/e_impedance/e_impedance.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ def __init__(self, impedance, voltage: float = None, suggested_circuit: str = No
self.chi_val = None
self.custom_circuit = None
self.z_fit = None
self.z_fit_clean = None
self.impedance.phase_shift = self._calculate_phase_shift() if self.impedance.phase_shift is None else self.impedance.phase_shift
self.figure = None
self.pos_img_index = None


# Schönleber, M. et al. A Method for Improving the Robustness of
Expand All @@ -120,6 +122,8 @@ def analyze(self):
log.info(f"Chi value from lin_KK method is {self.chi_val}")

if any(x < 0 for x in self.impedance.imaginary_impedance):
# Find the indexes of the positive values
self.pos_img_index = np.where(self.impedance.imaginary_impedance > 0)
f_circuit, z_circuit = preprocessing.ignoreBelowX(f_circuit, z_circuit)

# if the user did not choose any circuit, some default suggestions will be applied.
Expand Down Expand Up @@ -260,10 +264,17 @@ def save_data(self, save_dir:str, optional_name:str = None):
added_data = {'rc_linKK': self.num_rc_linkk, "eval_fit_linKK": self.eval_fit_linkk, "RMSE_fit_error": self.rmse_calc,
"conductivity [S/cm]": self.conductivity, "chi_square": self.chi_val}
utils.append_to_save_data(directory=save_dir, added_data=added_data, name=name)
# check if the positive index is available
if self.pos_img_index is not None:
self._insert_nan_values()
else:
self.z_fit_clean = self.z_fit


# Save the dataset
data = utils.assemble_data_frame(**{"frequency [Hz]": self.impedance.frequency,
"impedance [\u03a9]": self.impedance.real_impedance + 1j*self.impedance.imaginary_impedance,
"fit_impedance [\u03a9]": self.z_fit, "residual_real":self.res_real, "residual_imag":self.res_imag,
"fit_impedance [\u03a9]": self.z_fit_clean, "residual_real":self.res_real, "residual_imag":self.res_imag,
"Z_linKK [\u03a9]": self.z_linkk})
data_name = utils.assemble_file_name(optional_name, self.__class__.__name__, "data.csv") if \
optional_name else utils.assemble_file_name(self.__class__.__name__, "data.csv")
Expand Down Expand Up @@ -348,6 +359,18 @@ def _initialize_random_guess(self, guess_value, guess_initial_resistance):
guess_value = [guess_initial_resistance if element == 't' else element for element in guess_value]
return guess_value

def _insert_nan_values(self):
"""Insert nan values in the fit for the positive imaginary impedance values.
"""
self.z_fit_clean = np.empty(len(self.z_fit) + len(self.pos_img_index[0]), dtype=np.complex128)
self.z_fit_clean[:] = np.nan
j = 0
for i in range(len(self.z_fit_clean)):
if i in self.pos_img_index[0].tolist():
continue
self.z_fit_clean[i] = self.z_fit[j]
j += 1

class Mottschotcky(EIS, EChemProcedure):
""" Class for performing the Mottschotcky procedure.
Expand Down

0 comments on commit 4cbaee4

Please sign in to comment.