Quickstart
This is a quick example that shows how to fit a Foster network model to thermal impedance data from a spreadsheet.
First, we need to import the necessary packages and load the data. The thermal-network
package does not include openpyxl
as a dependency. To run this example, you will need to install it: pip install openpyxl
.
-
Load data from the spreadsheet
The excel file should be in the same folder as the script
import numpy as npfrom openpyxl import load_workbook, workbookfrom thermal_network.fitting import fit_optimal_foster_network, trim_steady_statefrom thermal_network.conversions import foster_to_cauer# Load data from the spreadsheetworkbook = load_workbook('excel_example.xlsx', read_only=True, data_only=True)worksheet = workbook["Thermal Data"]# Load time datacells = worksheet["C6:C103"]time_data = np.array([row[0].value for row in cells])# Load temperature datacells = worksheet["E6:E103"]impedance_data = np.array([row[0].value for row in cells]) -
Trim steady-state data points
The raw data may contain many data points at the end of the curve that are already in steady-state. These points can be removed to avoid overweighting steady-state in the loss function of the optimization algorithm (steady-state is enforced exactly by reparametrization).
time_data, impedance_data = trim_steady_state(time_data=time_data,impedance_data=impedance_data) -
Fit a model to the data
We specify that the optimal model can have at most 10 layers
model = fit_optimal_foster_network(time_data=time_data,impedance_data=impedance_data,max_layers=10,tau_floor= 1e-3) -
Convert the resulting Foster network to the Cauer topology
best_model = model.best_modelfoster_network = best_model.networkcauer_network = foster_to_cauer(foster_network)print("Fit completed!")print(f"Convergence was successful: {best_model.convergence_info['converged']}")print(f"Final Loss (MSE): {best_model.final_loss:.6f}")print("\nFitted Foster Network Parameters:")print(foster_network)print("\nEquivalent Cauer Network Parameters:")print(cauer_network)
The fit_optimal_foster_network
function returns the best Foster network model found. We can then convert it to a Cauer network using the foster_to_cauer
function.