Skip to content

Commit

Permalink
save_board_to_ufiv
Browse files Browse the repository at this point in the history
  • Loading branch information
annndruha committed Dec 29, 2023
1 parent 49f4413 commit ee9f5c6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 47 deletions.
29 changes: 17 additions & 12 deletions generate_dataset/dataset_generator.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import json
import os
import glob

from generate_dataset.parameters_changer import ParametersChanger
from generate_dataset.simulate_ivc import SimulatorIVC

GENERATE_SETTINGS_PATH = 'generate_dataset\\parameters_variations.json'
MEASUREMENTS_SETTINGS_PATH = 'generate_dataset\\measurement_settings.json'


def generate_dataset():
def generate_dataset(save_png=False):
# changer = ParametersChanger('circuit_classes\\DR_R\\DR_R.cir', GENERATE_SETTINGS_PATH)
# changer.generate_circuits()
# path = os.path.join('dataset', 'measurement_default', 'DR')
# changer.dump_circuits_on_disk(path)

with open(MEASUREMENTS_SETTINGS_PATH, 'r') as f:
measurements_settings = json.load(f)

folders = glob.glob("circuit_classes/*")
for measurements_settings in ['measurement_none']:
for folder in folders:
top, cls = os.path.split(folder)
cir_path = os.path.join(folder, cls + '.cir')
png_path = os.path.join(folder, cls + '.png')
for measurement in measurements_settings['measurement_variants']:
for circuit_class_path in folders:
_, cls = os.path.split(circuit_class_path)
path = os.path.join('dataset', measurement['name'], cls)
cir_path = os.path.join(circuit_class_path, cls + '.cir')
png_path = os.path.join(circuit_class_path, cls + '.png')

changer = ParametersChanger(cir_path, GENERATE_SETTINGS_PATH)
changer.generate_circuits()
path = os.path.join('dataset', measurements_settings, cls)
changer.dump_circuits_on_disk(path)

simulator = SimulatorIVC(1000, 0.3, 0, 0, 0)
simulator = SimulatorIVC(measurement['measurement_settings'])
for i, circuit in enumerate(changer.circuits):
print(path, i)
analysis = simulator.get_ivc(circuit)
fname = os.path.join(path, f'{i}.csv')
simulator.save_ivc(circuit, analysis, fname)
cname = os.path.join(path, f'{i}.uzf')
simulator.save_ivc(circuit, analysis, cname)

pname = os.path.join(path, f'{i}.png')
simulator.save_plot(circuit, analysis, pname, png_path)



generate_dataset()
generate_dataset(save_png=True)
28 changes: 12 additions & 16 deletions generate_dataset/measurement_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@
"measurement_variants": [
{
"variant": 1,
"name": "measurement_variant_1",
"name": "measurement_1000_03",
"measurement_settings": {
"properties": {
"probe_signal_frequency": 1000,
"max_voltage": 0.3,
"precharge_delay": 1,
"sampling_rate": 1,
"internal_resistance": 1
}
"probe_signal_frequency": 1000,
"max_voltage": 0.3,
"precharge_delay": 1,
"sampling_rate": 1,
"internal_resistance": 1
}
},
{
"variant": 2,
"name": "measurement_variant_2",
"name": "measurement_500_05",
"measurement_settings": {
"properties": {
"probe_signal_frequency": 500,
"max_voltage": 0.5,
"precharge_delay": 2,
"sampling_rate": 2,
"internal_resistance": 2
}
"probe_signal_frequency": 500,
"max_voltage": 0.5,
"precharge_delay": 1,
"sampling_rate": 1,
"internal_resistance": 1
}
}
]
Expand Down
42 changes: 23 additions & 19 deletions generate_dataset/simulate_ivc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


from epcore.elements import Board
from epcore.filemanager import save_board_to_ufiv, load_board_from_ufiv
import csv
import numpy as np
import matplotlib.pyplot as plt
Expand All @@ -8,17 +8,13 @@


class SimulatorIVC:
def __init__(self,
probe_signal_frequency,
max_voltage,
precharge_delay,
sampling_rate,
internal_resistance):
self.probe_signal_frequency = probe_signal_frequency
self.max_voltage = max_voltage
self.precharge_delay = precharge_delay
self.sampling_rate = sampling_rate
self.internal_resistance = internal_resistance
def __init__(self, measurement_settings_json):
self.measurement_settings_json = measurement_settings_json
self.probe_signal_frequency = measurement_settings_json['probe_signal_frequency']
self.max_voltage = measurement_settings_json['max_voltage']
self.precharge_delay = measurement_settings_json['precharge_delay']
self.sampling_rate = measurement_settings_json['sampling_rate']
self.internal_resistance = measurement_settings_json['internal_resistance']
self.num_cycles = 1
self.SNR = 40

Expand All @@ -38,12 +34,20 @@ def get_ivc(self, circuit: Circuit):
analysis.VCurrent = analysis.VCurrent[len(analysis.VCurrent) - lendata:len(analysis.VCurrent)]
return analysis

@staticmethod
def save_ivc(circuit, analysis, path):
with open(path, 'w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=';')
csv_writer.writerow(analysis.input_dummy)
csv_writer.writerow(analysis.VCurrent)
def save_ivc(self, circuit, analysis, path):
currents = list(analysis.VCurrent.as_ndarray())
voltages = list(analysis.input_dummy.as_ndarray())
measurement = {'measurement_settings': self.measurement_settings_json,
'comment': circuit.plot_title.replace('\n', ' '),
'currents': currents,
'voltages': voltages}

# TODO: Fix epcore, actually PCB not saved into ufiv
board = {'version': "1.1.2",
"PCB": {"pcb_name": "myclass", "comment": "super_comment"},
'elements': [{'pins': [{'iv_curves': [measurement], 'x': 0, 'y': 0}]}]}
epcore_board = Board.create_from_json(board)
save_board_to_ufiv(path, epcore_board)

def save_plot(self, circuit, analysis, path, png_path, plot_measurements_settings=True):
fig, ax = plt.subplots(1, figsize=(8, 4))
Expand Down

0 comments on commit ee9f5c6

Please sign in to comment.