def calculate(row): f, z = pp.cropFrequencies(FREQ, row.complex_z, 200) f, z = pp.ignoreBelowX(f, z) circuit = 'p(R1,C1)-p(R2,C2)' guess = [5e+4, 1e-10, 5e+6, 1e-10] model = modelling.model_impedance(circuit, guess, f, z) return modelling.get_resistance(model)
def test_circuit_fit(): # Test example circuit from "Getting Started" page circuit = 'R0-p(R1,C1)-p(R2-Wo1,C2)' initial_guess = [.01, .01, 100, .01, .05, 100, 1] results_local = np.array( [1.65e-2, 8.68e-3, 3.32e0, 5.39e-3, 6.31e-2, 2.33e2, 2.20e-1]) results_global = np.array( [1.65e-2, 5.34e-3, 0.22, 9.15e-3, 1.31e-1, 1.10e3, 2.78]) # Filter example_frequencies_filtered, \ Z_correct_filtered = ignoreBelowX(example_frequencies, Z_correct) # Test local fitting assert np.isclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={})[0], results_local, rtol=1e-2).all() # Test global fitting on multiple seeds # All seeds should converge to the same value # seed = 0 assert np.isclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={}, global_opt=True, seed=0)[0], results_global, rtol=1e-1).all() # seed = 1 assert np.isclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={}, global_opt=True, seed=1)[0], results_global, rtol=1e-1).all() # seed = 42 assert np.isclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={}, global_opt=True, seed=42)[0], results_global, rtol=1e-1).all()
def model_conductivity(freq, complex_z, cutoff, circuit, guess): """Takes a row of the dataframe and return the model object, the resistance and the rmse. Args: row ([type]): [description] Returns: model: the model object resistance: calculated resistance rmse: the root mean square error on the resistance """ f, z = pp.cropFrequencies(np.array(freq), complex_z, cutoff) f, z = pp.ignoreBelowX(f, z) model = model_impedance(circuit, guess, f, z) rmse = fitting.rmse(z, model.predict(f)) resistance = get_resistance(model) return model, resistance, rmse
def cole(data, freq, temp, freq_min=200, freq_max=None, fit=False, ax=None, **kwargs): """Creates a Cole-Cole plot (imaginary versus real impedance) at a given temperature. Finds the available data closest to the temperature specified by 'temp'. A linear least squares circle fit can be added by setting fit=True. :param temp: temperature in degrees C :type temp: float/int """ data = conductivity_only(data) # for temp in temp_list: [index, Tval] = index_temp(data.temp, temp) f, z = pp.cropFrequencies(frequencies=np.array(freq), Z=data.complex_z.iloc[index], freqmin=freq_min, freqmax=freq_max) f, z = pp.ignoreBelowX(f, z) p = ax.scatter(np.real(z) / 1000, np.abs(np.imag(z)) / 1000, **kwargs) if fit: model = data.model.iloc[index] predicted = model.predict(np.geomspace(0.001, 2000000, 100)) ax.plot(np.real(predicted) / 1000, np.abs(np.imag(predicted)) / 1000) ax.add_artist(AnchoredText(model.circuit, loc=2)) if not ax.xaxis.get_label().get_text(): ax.axis('equal') # ax.axis('square') ax.set_xlim(left=0) ax.set_ylim(bottom=0) ax.set_ylabel(r'$-Im(Z) [{}]$'.format(K_OHM)) ax.set_xlabel(r'$Re(Z) [{}]$'.format(K_OHM)) ax.ticklabel_format(style='sci', scilimits=(-3, 4), axis='both') ax.set_title('Cole-Cole') return ax
import matplotlib.pyplot as plt from impedance import preprocessing from impedance.models.circuits import CustomCircuit from impedance.visualization import plot_nyquist if __name__ == '__main__': frequencies1, Z1 = preprocessing.readZPlot( 'C:\\Users\\luowe\\Desktop\\IRP_code\\30th\\1.z') frequencies1, Z1 = preprocessing.ignoreBelowX(frequencies1, Z1) frequencies2, Z2 = preprocessing.readZPlot( 'C:\\Users\\luowe\\Desktop\\IRP_code\\50th\\1.z') frequencies2, Z2 = preprocessing.ignoreBelowX(frequencies2, Z2) ''' frequencies3, Z3 = preprocessing.readZPlot( 'C:\\Users\\luowe\\Desktop\\IRP_code\\1st\\3.z') frequencies3, Z3 = preprocessing.ignoreBelowX(frequencies3, Z3) frequencies4, Z4 = preprocessing.readZPlot( 'C:\\Users\\luowe\\Desktop\\IRP_code\\1st\\4.z') frequencies4, Z4 = preprocessing.ignoreBelowX(frequencies4, Z4) frequencies5, Z5 = preprocessing.readZPlot( 'C:\\Users\\luowe\\Desktop\\IRP_code\\1st\\5.z') frequencies5, Z5 = preprocessing.ignoreBelowX(frequencies5, Z5) frequencies6, Z6 = preprocessing.readZPlot( 'C:\\Users\\luowe\\Desktop\\IRP_code\\1st\\6.z') frequencies6, Z6 = preprocessing.ignoreBelowX(frequencies6, Z6) frequencies7, Z7 = preprocessing.readZPlot(
def test_ignoreBelowX(): filtered_freq, filtered_Z = ignoreBelowX(frequencies, Z_correct) assert (np.imag(filtered_Z) == Zi_np).all()
def test_circuit_fit(): # Test trivial model (10 Ohm resistor) circuit = 'R0' initial_guess = [10] results_simple = [10] frequencies = np.array([10, 100, 1000]) Z_data = np.array([10, 10, 10]) # impedance is constant assert np.allclose(circuit_fit(frequencies, Z_data, circuit, initial_guess, constants={}, global_opt=True)[0], results_simple, rtol=1e-1) # Test example circuit from "Getting Started" page circuit = 'R0-p(R1,C1)-p(R2-Wo1,C2)' initial_guess = [.01, .01, 100, .01, .05, 100, 1] bounds = [(0, 0, 0, 0, 0, 0, 0), (10, 1, 1e3, 1, 1, 1e4, 100)] # results change slightly using predefined bounds results_local = np.array( [1.65e-2, 8.68e-3, 3.32, 5.39e-3, 6.31e-2, 2.33e2, 2.20e-1]) results_local_bounds = results_local.copy() results_local_bounds[5] = 2.38e2 results_local_weighted = np.array( [1.64e-2, 9.06e-3, 3.06, 5.29e-3, 1.45e-1, 1.32e3, 2.02e-1]) results_global = np.array( [1.65e-2, 5.34e-3, 0.22, 9.15e-3, 1.31e-1, 1.10e3, 2.78]) # Filter example_frequencies_filtered, \ Z_correct_filtered = ignoreBelowX(example_frequencies, Z_correct) # Test local fitting assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={})[0], results_local, rtol=1e-2) # Test local fitting with predefined bounds assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, bounds=bounds, constants={})[0], results_local_bounds, rtol=1e-2) # Test local fitting with predefined weights # Use abs(Z), stacked in order of (Re, Im) components sigma = np.hstack((np.abs(Z_correct_filtered), np.abs(Z_correct_filtered))) assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, sigma=sigma, constants={})[0], results_local_weighted, rtol=1e-2) # Test if using weight_by_modulus=True produces the same results assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, weight_by_modulus=True, constants={})[0], results_local_weighted, rtol=1e-2) # Test global fitting on multiple seeds # All seeds should converge to the same parameter values # seed = 0 (default) assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={}, global_opt=True)[0], results_global, rtol=1e-1) # seed = 0, with predefined bounds assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={}, global_opt=True, bounds=bounds, seed=0)[0], results_global, rtol=1e-1) # seed = 1 assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={}, global_opt=True, seed=1)[0], results_global, rtol=1e-1) # seed = 42 assert np.allclose(circuit_fit(example_frequencies_filtered, Z_correct_filtered, circuit, initial_guess, constants={}, global_opt=True, seed=42)[0], results_global, rtol=1e-1)
import matplotlib.pyplot as plt from impedance import preprocessing from impedance.models.circuits import CustomCircuit from impedance.visualization import plot_nyquist circuit = 'R0-p(R1,C1)-p(R2-Wo1,C2)' initial_guess = [.1, .01, .1, .05, .001, 20, 1] circuit = CustomCircuit(circuit, initial_guess=initial_guess) if __name__ == '__main__': # Read and fit EIS data frequencies, Z = preprocessing.readZPlot( 'C:\\Users\\luowe\\Desktop\\IRP_code\\Battery_Data\\50th\\10.z') frequencies, Z = preprocessing.ignoreBelowX(frequencies, Z) circuit.fit(frequencies, Z) para = circuit.parameters_ # Generate training data f = 'training data.txt' with open(f, "a") as file: file.write('50' + ',' + str(para[0]) + ',' + str(para[1]) + ',' + str(para[3]) + '\n') file.close() # Plot data curve and fit curve Z_fit = circuit.predict(frequencies) fig, ax = plt.subplots() plot_nyquist(ax, Z, fmt='o') plot_nyquist(ax, Z_fit, fmt='-') plt.legend(['Data', 'Fit']) plt.show()