From ff9b8bb838ecdfbfc1dc81038fcf3b2a87636982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Zrounba?= <6691770+clement-z@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:06:01 +0200 Subject: Initial release --- pyspecs/__init__.py | 0 pyspecs/draw_graph_from_json.py | 145 +++++++++ pyspecs/pyspecs_example.ipynb | 682 ++++++++++++++++++++++++++++++++++++++++ pyspecs/specs.py | 236 ++++++++++++++ pyspecs/specsparse.py | 301 ++++++++++++++++++ 5 files changed, 1364 insertions(+) create mode 100644 pyspecs/__init__.py create mode 100644 pyspecs/draw_graph_from_json.py create mode 100644 pyspecs/pyspecs_example.ipynb create mode 100644 pyspecs/specs.py create mode 100644 pyspecs/specsparse.py (limited to 'pyspecs') diff --git a/pyspecs/__init__.py b/pyspecs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyspecs/draw_graph_from_json.py b/pyspecs/draw_graph_from_json.py new file mode 100644 index 0000000..78a3c64 --- /dev/null +++ b/pyspecs/draw_graph_from_json.py @@ -0,0 +1,145 @@ +import sys +import json +import html +import graphviz + +def shorten(s, length=8): + if type(s) == list: + return "[...]" + s = str(s) + if len(s) > length: + return s[:length-3] + else: + return s + +def edge_label(name, net): + bgcolors = { + "default": "white", + "OANALOG": "turquoise", + "EANALOG": "orange", + } + try: + bgcolor = bgcolors[net["type"]] + except: + bgcolor = bgcolors["default"] + + short_name = name + if short_name.startswith('ROOT/'): + short_name = short_name[5:] + + attributes = [f'
| {html.escape(short_name)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"
+ ]
+ },
+ "metadata": {
+ "needs_background": "light"
+ },
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "y = df_probes['PROBE{ROOT/out}/power']\n",
+ "if specs.verify_wl_sweep(df_probes):\n",
+ " x = df_probes['wavelength']\n",
+ "else:\n",
+ " x = df_probes['time']\n",
+ "\n",
+ "if not specs.verify_wl_sweep(df_probes):\n",
+ " plt.step(x,y,where='post')\n",
+ "else:\n",
+ " plt.plot(x,y)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "1cc73fee",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ " \n",
+ "\n",
+ " "
+ ],
+ "text/plain": [
+ " time PROBE{ROOT/N00001}/power PROBE{ROOT/N00001}/abs \\\n",
+ "0 0.000000e+00 0.000000 0.000000 \n",
+ "1 5.000000e-10 0.000000 0.000000 \n",
+ "2 5.020000e-10 0.000000 0.000000 \n",
+ "3 5.040000e-10 0.021994 0.148303 \n",
+ "4 5.060000e-10 0.021994 0.148303 \n",
+ "... ... ... ... \n",
+ "1246 2.990000e-09 0.000569 0.023863 \n",
+ "1247 2.992000e-09 0.000544 0.023326 \n",
+ "1248 2.994000e-09 0.000544 0.023326 \n",
+ "1249 2.996000e-09 0.000520 0.022801 \n",
+ "1250 2.998000e-09 0.000520 0.022801 \n",
+ "\n",
+ " PROBE{ROOT/N00001}/phase PROBE{ROOT/N00002}/power \\\n",
+ "0 0.000000 0.000000 \n",
+ "1 0.000000 0.022500 \n",
+ "2 0.000000 0.022500 \n",
+ "3 1.570796 0.087986 \n",
+ "4 1.570796 0.087986 \n",
+ "... ... ... \n",
+ "1246 1.570796 0.000557 \n",
+ "1247 1.570796 0.000532 \n",
+ "1248 1.570796 0.000532 \n",
+ "1249 1.570796 0.000508 \n",
+ "1250 1.570796 0.000508 \n",
+ "\n",
+ " PROBE{ROOT/N00002}/abs PROBE{ROOT/N00002}/phase \\\n",
+ "0 0.000000 0.000000 \n",
+ "1 0.150000 1.570796 \n",
+ "2 0.150000 1.570796 \n",
+ "3 0.296625 1.570796 \n",
+ "4 0.296625 1.570796 \n",
+ "... ... ... \n",
+ "1246 0.023593 1.570796 \n",
+ "1247 0.023062 1.570796 \n",
+ "1248 0.023062 1.570796 \n",
+ "1249 0.022543 1.570796 \n",
+ "1250 0.022543 1.570796 \n",
+ "\n",
+ " PROBE{ROOT/N00003}/power PROBE{ROOT/N00003}/abs \\\n",
+ "0 0.000000 0.000000 \n",
+ "1 0.000000 0.000000 \n",
+ "2 0.022500 0.150000 \n",
+ "3 0.022500 0.150000 \n",
+ "4 0.087986 0.296625 \n",
+ "... ... ... \n",
+ "1246 0.000557 0.023593 \n",
+ "1247 0.000557 0.023593 \n",
+ "1248 0.000532 0.023062 \n",
+ "1249 0.000532 0.023062 \n",
+ "1250 0.000508 0.022543 \n",
+ "\n",
+ " PROBE{ROOT/N00003}/phase ... PROBE{ROOT/out}/phase ROOT/PROBE1/power \\\n",
+ "0 0.000000 ... 0.000000e+00 0.000000 \n",
+ "1 0.000000 ... 0.000000e+00 0.977500 \n",
+ "2 1.570796 ... 0.000000e+00 0.977500 \n",
+ "3 1.570796 ... -6.316716e-15 0.934007 \n",
+ "4 1.570796 ... -6.316716e-15 0.934007 \n",
+ "... ... ... ... ... \n",
+ "1246 1.570796 ... -3.141593e+00 0.000013 \n",
+ "1247 1.570796 ... -3.141593e+00 0.000012 \n",
+ "1248 1.570796 ... -3.141593e+00 0.000012 \n",
+ "1249 1.570796 ... -3.141593e+00 0.000012 \n",
+ "1250 1.570796 ... -3.141593e+00 0.000012 \n",
+ "\n",
+ " ROOT/PROBE1/abs ROOT/PROBE1/phase ROOT/PROBE2/power ROOT/PROBE2/abs \\\n",
+ "0 0.000000 0.000000e+00 0.000000 0.000000 \n",
+ "1 0.988686 0.000000e+00 0.000000 0.000000 \n",
+ "2 0.988686 0.000000e+00 0.000506 0.022500 \n",
+ "3 0.966441 -6.316716e-15 0.000506 0.022500 \n",
+ "4 0.966441 -6.316716e-15 0.001980 0.044494 \n",
+ "... ... ... ... ... \n",
+ "1246 0.003579 -3.141593e+00 0.000013 0.003539 \n",
+ "1247 0.003499 -3.141593e+00 0.000013 0.003539 \n",
+ "1248 0.003499 -3.141593e+00 0.000012 0.003459 \n",
+ "1249 0.003420 -3.141593e+00 0.000012 0.003459 \n",
+ "1250 0.003420 -3.141593e+00 0.000011 0.003381 \n",
+ "\n",
+ " ROOT/PROBE2/phase ROOT/PROBE3/power ROOT/PROBE3/abs ROOT/PROBE3/phase \n",
+ "0 0.000000 0.0 0.0 0.0 \n",
+ "1 0.000000 1.0 1.0 0.0 \n",
+ "2 -3.141593 1.0 1.0 0.0 \n",
+ "3 -3.141593 1.0 1.0 0.0 \n",
+ "4 -3.141593 1.0 1.0 0.0 \n",
+ "... ... ... ... ... \n",
+ "1246 -3.141593 0.0 0.0 0.0 \n",
+ "1247 -3.141593 0.0 0.0 0.0 \n",
+ "1248 -3.141593 0.0 0.0 0.0 \n",
+ "1249 -3.141593 0.0 0.0 0.0 \n",
+ "1250 -3.141593 0.0 0.0 0.0 \n",
+ "\n",
+ "[1251 rows x 34 columns]"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df_probes"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "64c2170f-f879-43d2-ae1f-850ebf667601",
+ "metadata": {},
+ "source": [
+ "### Helper to recover data\n",
+ "\n",
+ "As SPECS is not a sampled simulator, but rather event-driven, unchanged values will not appear in the dataframe.\n",
+ "\n",
+ "To make it a bit easier to recover a value by time, we created the 'find_by_time' function that automates\n",
+ "some of the recovery for you. \n",
+ "\n",
+ "NOTE: if you want to do it for multiple values, it is better to implement it using the Pandas framework in your own way. Check the Pandas dataframe.ffill() function. Using find_by_time in loops will result in very slow python code."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "d042f4a5-de7d-425d-b772-f245e6346871",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.07090737655723536"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "target_time = 2.5668e-9\n",
+ "specs.find_by_time(df_probes,'PROBE{ROOT/N00001}/power',target_time)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "75ede147-1232-46f5-b3b1-4a9dc75a28b7",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.8.10"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/pyspecs/specs.py b/pyspecs/specs.py
new file mode 100644
index 0000000..ca0d6f9
--- /dev/null
+++ b/pyspecs/specs.py
@@ -0,0 +1,236 @@
+import numpy as np
+import matplotlib.pyplot as plt
+import pandas as pd
+import subprocess
+import time
+from .specsparse import *
+
+"""
+Ideas:
+ - generate value files
+ - generate netlists based on graph representations
+ - simulate
+ - paralel run
+ - others?
+
+"""
+
+default_settings = {
+ "o": "traces/delete_me.vcd",
+ "abstol": 1e-8,
+ "reltol": 1e-4
+}
+
+
+
+def create_arguments_netlist(netlist_file, output_file, abstol, reltol):
+ """ Function called by specs.simulate(), generates
+ the argument string for the command-line tool.
+
+ Returns:
+ arguments (list of str):
+ Formatted list of arguments required by SPECS
+ """
+
+ arguments_dictionary = { "f":netlist_file,
+ "o":output_file,
+ "abstol":abstol,
+ "reltol":reltol
+ }
+
+ arguments = create_arguments_dictionary(arguments_dictionary)
+
+ return arguments
+
+
+def create_arguments_dictionary(arguments_dictionary:dict):
+ """ Function called by specs.simulate(), generates
+ the argument string for the command-line tool.
+
+ Args:
+ arguments_dictionary (dict):
+ The keys of this dictionary are keywords expected
+ by SPECS, while their contents hold their value.
+
+ Example: {"f":"circuit.txt", "o":"data.vcd","
+ "abstol":1e-8, "reltol":1e-4
+ }
+
+ Returns:
+ arguments (list of str):
+ Formatted list of arguments required by SPECS
+ """
+
+ arguments = []
+
+ for keyword_string in arguments_dictionary:
+ value = arguments_dictionary[keyword_string]
+
+ # Long keywords are prepended with double dash
+ if len(keyword_string) > 1:
+ keyword_string = "--" + keyword_string
+ else:
+ keyword_string = "-" + keyword_string
+
+ arguments.append(keyword_string)
+ if value is not None:
+ arguments.append(str(value))
+
+ return arguments
+
+def run_and_time(simulator_command, arguments, verbose):
+ """Function that runs SPECS and times it.
+
+ This is BLOCKING execution, so not intended for paralel
+ usage. For this case, use par_run().
+
+ Args:
+ simulator_command (str):
+ The command to call SPECS comprised of
+ its directory and simulator name. Ex: ./specs
+ arguments (str):
+ Command-line arguments passed to SPECS
+ verbose (bool):
+ If true, will print SPECS output to the Python shell
+ Returns:
+ time_ms:
+ Execution time of SPECS for that run, in miliseconds (ms)
+ """
+
+ print("Executing SPECS from python. . .")
+
+ #spec_env = {"SC_COPYRIGHT_MESSAGE":"DISABLE"}
+
+ process_call = simulator_command + arguments
+
+ tic_ns = time.perf_counter_ns()
+ print("Command: ", ' '.join(process_call))
+ #p = subprocess.Popen(process_call, env=spec_env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ p = subprocess.Popen(process_call, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ specs_stdout, specs_stderr = p.communicate()
+ p.wait()
+ toc_ns = time.perf_counter_ns()
+
+ time_ms = (toc_ns - tic_ns)*1e-6
+
+ specs_return_code = p.returncode
+ print("Execution finished with code: ", specs_return_code)
+
+ if (not specs_return_code == 0) or (verbose):
+ print("SPECS had an error. Here is the full output: ")
+ for line in specs_stdout.splitlines():
+ print(line.decode(encoding='utf-8', errors='ignore'))
+
+ return time_ms, (specs_return_code, specs_stdout)
+
+def simulate(netlist_file=None,
+ custom_arguments=None,
+ simulator_directory="",
+ verbose=False,
+ **kwargs):
+ """Function that call SPECS from the Python shell
+
+ Args:
+ netlist_file (str):
+ File containing the netlist description
+ of the circuit to simulate. Should contain
+ a simulation directive within it (.tran, .dc, .op).
+
+ The netlist is a text file that can be:
+ - Created using the KiCAD library of components in
+ its schematic editor (eeschema)
+ - Created and edited by hand by knowing the syntax
+ - Generated proceduraly using scripts. It's a basic .txt
+
+ It can be achieved with custom arguments by defining
+ {"f": "path/to/file"} in the dict custom_arguments.
+
+ Defaults to "".
+
+ output_file (str, optional):
+ VCD file containing all the recorded events.
+ Can be read with GTKWAVE or parsed with parse_vcd()
+ Defaults to "traces/delete_me.vcd".
+
+ abstol (double, optional):
+ Field absolute tolerance for propagating events in the simulator.
+ Is relevant when running circuits with feedback.
+
+ It can be achieved with custom arguments by defining
+ {"abstol": 1e-8} in the dict custom_arguments.
+
+ Defaults to 1e-8.
+
+ reltol (double, optional):
+ Field relative tolerance for propagating events in the simulator.
+ Is relevant when running circuits with feedback.
+
+ It can be achieved with custom arguments by defining
+ {"reltol": 1e-4} in the dict custom_arguments.
+
+ Defaults to 1e-4.
+
+ custom_arguments (dict, optional):
+ <<< ADVANCED USE >>>
+ It is possible to call SPECS with many other arguments.
+ You can consult them by running specs without arguments
+ in the command line interface.
+
+ If custom_arguments is defined, you need to specify
+ the netlist as a part of the dictionary.
+
+ There is no specific order that the arguments must follow.
+
+ simulator_directory (str, optional):
+ SPECS can either be included in the PATH variable
+ or exist as a standalone executable. In the latter case,
+ it is necessary to define its location.
+
+ Examples:
+ simulator_directory = "./"
+ Means that the executable is in the same
+ directory as this script."
+ simulator_directory = "/home/user/Documents/"
+ Means that the SPECS executable is in that
+ specific path.
+
+ Defaults to "".
+
+ verbose (bool, optional):
+ If true, will print all the outputs generated by SPECS
+ to the Python shell.
+ """
+
+ assert((netlist_file is not None) or (custom_arguments is not None))
+
+
+ if 'abstol' in kwargs:
+ abstol = kwargs['abstol']
+ else:
+ abstol = default_settings['abstol']
+
+ if 'reltol' in kwargs:
+ reltol = kwargs['reltol']
+ else:
+ reltol = default_settings['reltol']
+
+ if 'output_file' in kwargs:
+ output_file = kwargs['output_file']
+ else:
+ output_file = default_settings['o']
+
+ arguments = []
+ if (netlist_file is not None):
+ arguments = create_arguments_netlist(netlist_file, output_file, abstol, reltol)
+
+ if (custom_arguments is not None):
+ current_settings = default_settings.copy()
+ current_settings.update(custom_arguments) # overwrites defaults with the user input where needed
+ arguments += create_arguments_dictionary(current_settings)
+
+ simulator_name = "specs"
+ simulator_command = [simulator_directory + simulator_name]
+
+ outputs = run_and_time(simulator_command, arguments, verbose)
+
+ return outputs
diff --git a/pyspecs/specsparse.py b/pyspecs/specsparse.py
new file mode 100644
index 0000000..91f4f4f
--- /dev/null
+++ b/pyspecs/specsparse.py
@@ -0,0 +1,301 @@
+from pyDigitalWaveTools.vcd.parser import VcdParser
+import pandas as pd
+import numpy as np
+import matplotlib.pyplot as plt
+import json
+
+def parse_vcd_dataframe(filename):
+ """Parses a SPECS VCD file and returns simulation data as a DataFrame.
+
+ Args:
+ filename (str):
+ The path to the VCD file.
+
+ Returns:
+ simulation_data_df:
+ The simulation data with signals as columns and time/wavelength as rows.
+ """
+ with open(filename) as vcd_file:
+ vcd = VcdParser()
+ vcd.parse(vcd_file)
+ data = vcd.scope.toJson()
+
+ # print(json.dumps(data, indent=4, sort_keys=True))
+ simulation_data_df = pd.DataFrame() # initialize empty dataframe
+
+ top_level_name = 'SystemC'
+ top_scope = None
+ for child in data['children']:
+ if is_scope(child) and child['name'] == top_level_name:
+ top_scope = child
+ break
+ if top_scope is None:
+ raise RuntimeError(f'Top level scope not found in {filename}')
+
+ assert('name' in top_scope.keys())
+ assert('type' in top_scope.keys())
+ assert('children' in top_scope.keys())
+ assert(top_scope['type']['name'] == 'struct')
+
+ scopes = []
+ probes = []
+ pdets = []
+ values = []
+ for child in top_scope['children']:
+ if is_scope(child):
+ scopes.append(child)
+ if is_probe(child):
+ probes.append(child)
+ if is_pdet(child):
+ pdets.append(child)
+ else:
+ values.append(child)
+
+ # print(f'top level values: {[x["name"] for x in values]}')
+ # print(f'top level scopes: {[x["name"] for x in scopes]}')
+ # print(f'top level probes: {[x["name"] for x in probes]}')
+
+ is_wl_sweep = check_wl_sweep(probes)
+ has_wl = False
+ save_wl = False
+
+ df_probes = pd.DataFrame()
+ if len(probes) > 0:
+ for i,probe in enumerate(probes):
+ probe_name = probe['name']
+ for j,signal in enumerate(probe['children']):
+ signal_name = signal['name']
+ full_name = probe_name + '/' + signal_name
+
+ save_wl = is_wl_sweep and not has_wl
+ if (not signal_name=='wavelength' or (signal_name=='wavelength' and save_wl)):
+ current_signal_df = pd.DataFrame()
+ current_signal_df['tick'] = vcd_get_ticks_vector(signal)
+
+ if signal_name=='wavelength':
+ has_wl = False # this variable set to true will save WL vector only once
+ full_name = signal_name # wavelength should be saved independent of probe
+
+ current_signal_df[full_name] = vcd_get_data_vector(signal)
+
+ if i==0 and j==0:
+ df_probes = current_signal_df
+ else:
+ df_probes = df_probes.merge(current_signal_df, how='outer')
+
+ df_probes = df_probes.sort_values('tick')
+ df_probes = df_probes.reset_index(drop=True)
+
+ if not is_wl_sweep: # making sure that time goes first
+ df_probes.insert(0, 'time', df_probes['tick'] * vcd_get_multiplier(vcd))
+ else: # making sure that wl goes first
+ first_column = df_probes.pop('wavelength')
+ df_probes.insert(0, 'wavelength', first_column)
+
+ # we don't need ticks anymore
+ df_probes = df_probes.drop(columns=['tick'])
+
+ # getting rid of the NaNs: for time domain we repeat the last valid value,
+ # for freq domain we interpolate linearly
+ if is_wl_sweep:
+ df_probes = df_probes.drop(df_probes.loc[df_probes['wavelength']== 0].index.values)
+ df_probes = df_probes.interpolate()
+ else:
+ df_probes = df_probes.fillna(method='ffill')
+
+ # Same with pdetectors
+ df_pdet = pd.DataFrame()
+ if not is_wl_sweep and len(pdets) > 0:
+ for i,pdet in enumerate(pdets):
+ pdet_name = pdet['name']
+ for j,signal in enumerate(pdet['children']):
+ if signal["type"]["name"] == "struct":
+ continue
+ signal_name = signal['name']
+ full_name = pdet_name + '/' + signal_name
+
+ # Add data to a temporary DF
+ current_signal_df = pd.DataFrame()
+ current_signal_df['tick'] = vcd_get_ticks_vector(signal)
+ current_signal_df[full_name] = vcd_get_data_vector(signal)
+
+ if df_pdet.empty:
+ df_pdet = current_signal_df
+ else:
+ df_pdet = df_pdet.merge(current_signal_df, how='outer')
+
+ df_pdet = df_pdet.sort_values('tick')
+ df_pdet = df_pdet.reset_index(drop=True)
+ if False:
+ # Add a point next to first value from photodetector
+ # to ensure 'step' appearance (as its more correct)
+ print(df_pdet.head())
+ df_pdet.loc[-1] = df_pdet.loc[0]
+ df_pdet.loc[0, 'tick'] = df_pdet.loc[1]['tick']
+ df_pdet = df_pdet.sort_values('tick')
+ df_pdet = df_pdet.reset_index(drop=True)
+ print(df_pdet.head())
+
+ # making sure that time goes first
+ df_pdet.insert(0, 'time', df_pdet['tick'] * vcd_get_multiplier(vcd))
+
+ # we don't need ticks anymore
+ df_pdet = df_pdet.drop(columns=['tick'])
+
+ # getting rid of the NaNs: for time domain we repeat the last valid value,
+ df_pdet = df_pdet.fillna(method='ffill')
+
+
+ print('--- Contents of simulation ---')
+ print ('In df_pdet')
+ for col in list(df_pdet.columns):
+ print('\t',col)
+ print ('In df_probes')
+ for col in list(df_probes.columns):
+ print('\t',col)
+ if is_wl_sweep:
+ print('Type of simulation: FD')
+ else:
+ print('Type of simulation: TD')
+
+ print('------------------------------')
+
+ return df_probes, df_pdet
+
+def verify_wl_sweep(simulation_data):
+ """Checks the type of simulation based on the processed data.
+
+ Args:
+ simulation_data (DataFrame):
+ Data recovered using parse_vcd_xxxx methods.
+
+ Returns:
+ is_wl_sweep(bool):
+ True if it is a wavelength sweep, False otherwise.
+ """
+ is_wl_sweep = False
+
+ if isinstance(simulation_data,pd.DataFrame):
+ is_wl_sweep = list(simulation_data.columns)[0] == 'wavelength'
+ else:
+ print('Error: Invalid simulation data')
+
+ return is_wl_sweep
+
+def find_by_time(simulation_data,colname,time):
+ """Finds specific time values from the processed data
+ Useful when you want to a datapoint of a specific moment
+ but there was no transition there so it is not necessarily
+ contained in the data frame.
+
+ As it is an event-based simulator, it means that if
+ the exact time that you want is not there, the closest
+ previous value is the correct one, and this function gets it
+ automatically for you.
+
+ Args:
+ simulation_data (DataFrame):
+ Data recovered using parse_vcd_xxxx methods.
+ colname (string):
+ Name of the column that you want to extract data from.
+ time (float):
+ Time that you want to get your sample.
+
+ Returns:
+ value(float):
+ Sample value at the specified time..
+ """
+ # first check if exact match is present
+ # else, gets most recent value smaller than
+ # the specified time
+
+ value = None
+
+ exact_match_df = simulation_data.loc[simulation_data['time'] == time]
+ if exact_match_df.empty:
+ previous_match_df = simulation_data.loc[simulation_data['time'] <= time]
+ previous_match_df = previous_match_df.tail(1)
+ value = previous_match_df[colname].values[0]
+ else:
+ value = exact_match_df[colname].values[0]
+
+ return value
+
+######### The next functions are intended for internal use #########
+
+def is_scope(data):
+ return data['type']['name'] == 'struct'
+
+def is_pdet(data):
+ if not is_scope(data):
+ return False
+
+ allowed_attrs = ["readout", "readout_no_interference"]
+ for child in data['children']:
+ if child['name'] not in allowed_attrs:
+ return False
+ return True
+
+def is_probe(data):
+ if not is_scope(data):
+ return False
+
+ allowed_attrs = ["wavelength", "power", "abs", "phase", "real", "imag"]
+ for child in data['children']:
+ if is_scope(child):
+ # Probe contains only values
+ return False
+ base_name = child['name']
+ # For MLprobe ignore what's after the "@"
+ # If there is no "@", it remains the full name
+ i_last_at = base_name.rfind('@')
+ if i_last_at > 0:
+ base_name = base_name[0:i_last_at]
+ if base_name not in allowed_attrs:
+ return False
+ return True
+
+def check_wl_sweep(probes):
+ for probe in probes:
+ # see if wavelength is part of probe attributes
+ attrs = ["wavelength", "power", "abs", "phase"]
+ lengths = {a:-1 for a in attrs}
+ for signal in probe['children']:
+ for a in attrs:
+ if signal['name'] == a:
+ lengths[a] = len( signal['data'] )
+
+ match = False
+ if lengths[attrs[1]] != -1:
+ match = lengths[attrs[0]] == lengths[attrs[1]]
+ if lengths[attrs[2]] != -1:
+ match = lengths[attrs[0]] == lengths[attrs[2]]
+
+ if match and lengths[attrs[0]] != 1:
+ return True
+ return False
+
+def vcd_get_data_vector(signal):
+ data = np.array([float(tup[1][1::]) for tup in signal['data']])
+ return data
+
+def vcd_get_ticks_vector(signal):
+ ticks = np.array([tup[0] for tup in signal['data']])
+ return ticks
+
+def vcd_get_multiplier(vcd):
+ timescale_list = vcd.timescale.split()
+ base_scale = float(timescale_list[0])
+ unit = timescale_list[1][0]
+
+ multipliers = {
+ 's': 1.0,
+ 'm': 1e-3,
+ 'u': 1e-6,
+ 'n': 1e-9,
+ 'p': 1e-12,
+ 'f': 1e-15,
+ }
+
+ # returns value in seconds
+ return base_scale * multipliers[unit]
--
cgit v1.2.3
1251 rows × 34 columns \n", + " |