Skip to content

Commit 139bd73

Browse files
committed
Some style clean-ups
1 parent dee144b commit 139bd73

7 files changed

Lines changed: 35 additions & 66 deletions

File tree

simphony/time_domain/ideal.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def run(self, inputs: dict) -> dict:
108108
"o0": o0_response,
109109
"o1": o1_response,
110110
}
111+
return response
111112

112113
return response
113114

@@ -173,6 +174,7 @@ def run(self, inputs: dict, **kwargs) -> dict:
173174
"o0": o0_response,
174175
"o1": o1_response,
175176
}
177+
return response
176178

177179

178180
class PhaseModulator(SampleModeComponent, BlockModeComponent):
@@ -303,14 +305,14 @@ def __init__(
303305
for i in range(self.r): # inputs 0 … r-1
304306
for j in range(self.s): # outputs r … r+s-1
305307
phi = phases[i, j + self.r]
306-
s_dict_time[(f"o{i}", f"o{j+self.r}")] = (
308+
s_dict_time[(f"o{i}", f"o{j + self.r}")] = (
307309
amplitude / jnp.sqrt(self.s) * jnp.exp(1j * phi)
308310
)
309311

310312
for i in range(self.s): # inputs r … r+s-1
311313
for j in range(self.r): # outputs 0 … r-1
312314
phi = phases[i + self.s, j]
313-
s_dict_time[(f"o{i+self.r}", f"o{j}")] = (
315+
s_dict_time[(f"o{i + self.r}", f"o{j}")] = (
314316
amplitude / jnp.sqrt(self.r) * jnp.exp(1j * phi)
315317
)
316318

@@ -330,7 +332,7 @@ def response(self, inputs: dict) -> dict:
330332
N = len(self.ports)
331333
for j, port in enumerate(self.ports):
332334
# sum over all inputs
333-
resp = sum(inputs[f"o{l}"] * self.s_dict_time[j, l] for l in range(N))
335+
resp = sum(inputs[f"o{idx}"] * self.s_dict_time[j, idx] for idx in range(N))
334336
response[port] = resp
335337
return response
336338

simphony/time_domain/pole_residue_model.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class IIRModelBaseband(PoleResidueModel):
6363
def __init__(
6464
self, wvl_microns, center_wvl, s_params, sampling_period, order, options=None
6565
):
66-
if options == None:
66+
if options is None:
6767
self.options = BVF_Options()
6868
else:
6969
self.options = options
@@ -226,8 +226,6 @@ def compute_error(self):
226226
return np.max(np.abs(self.S - self.compute_response()))
227227

228228
def compute_time_response(self, sig=None, t=None):
229-
c = 299792458
230-
231229
sys = self.generate_sys_discrete()
232230

233231
if t is None:
@@ -266,7 +264,7 @@ def compute_lstsq_matrices(self, phi0, phi1):
266264
# This allows us to implement the Fast Vector Fitting algorithm:
267265
# https://scholar.googleusercontent.com/scholar?q=cache:u4aY-dn1tF8J:scholar.google.com/+piero+triverio+vector+fitting&hl=en&as_sdt=0,45
268266

269-
Q1, R11 = np.linalg.qr(A1)
267+
Q1, _ = np.linalg.qr(A1)
270268
R12 = Q1.conj().T @ A2
271269
Q2, R22 = np.linalg.qr(A2 - Q1 @ R12)
272270

simphony/time_domain/simulation.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
from __future__ import annotations
22

3+
from dataclasses import dataclass
34
from typing import TYPE_CHECKING
45

56
import jax.numpy as jnp
67
import matplotlib.pyplot as plt
7-
import networkx as nx
88
import numpy as np
99
import sax
1010
from jax import config, jit, lax
1111
from numpy.typing import ArrayLike
12+
from scipy.interpolate import interp1d
1213

14+
from simphony.exceptions import UndefinedActiveComponent
15+
from simphony.simulation import Simulation, SimulationResult
16+
from simphony.time_domain.pole_residue_model import BVF_Options, IIRModelBaseband
1317
from simphony.time_domain.time_system import (
1418
BlockModeComponent,
1519
SampleModeComponent,
1620
TimeSystem,
1721
TimeSystemIIR,
1822
)
19-
from simphony.utils import SPEED_OF_LIGHT
23+
from simphony.utils import SPEED_OF_LIGHT, dict_to_matrix
2024

2125
config.update("jax_enable_x64", True)
2226

23-
from dataclasses import dataclass
24-
25-
from scipy.interpolate import interp1d
26-
27-
from simphony.exceptions import UndefinedActiveComponent
28-
from simphony.simulation import Simulation, SimulationResult
29-
from simphony.time_domain.pole_residue_model import BVF_Options, IIRModelBaseband
30-
from simphony.utils import dict_to_matrix
31-
3227
if TYPE_CHECKING:
3328
from simphony.circuit.circuit import Circuit
3429

@@ -390,8 +385,6 @@ def _sample_mode_run(
390385
)
391386

392387
def _block_mode_run(self, t: ArrayLike, input_signals: dict) -> TimeResult:
393-
graph = nx.DiGraph()
394-
395388
# --------------------------------------------------------------
396389
# Turn self.td_netlist into a Networkx Graph
397390
# --------------------------------------------------------------
@@ -417,10 +410,7 @@ def _block_mode_run(self, t: ArrayLike, input_signals: dict) -> TimeResult:
417410
# Determine which exposed ports are Inputs and which are Outputs
418411
# --------------------------------------------------------------
419412

420-
exposed_ports = {
421-
"inputs": [],
422-
"outputs": [],
423-
}
413+
pass
424414

425415
# ------------------------------------------------------------------
426416
# HELPER: build immutable/tuple wiring maps (hashable for jit)

simphony/time_domain/time_system.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414

1515
class TimeSystem(ABC):
1616
def __init__(self, optical_ports, electrical_ports, logic_ports) -> None:
17-
if optical_ports == None:
17+
if optical_ports is None:
1818
self.optical_ports = []
1919
else:
2020
self.optical_ports = optical_ports
2121

22-
if electrical_ports == None:
22+
if electrical_ports is None:
2323
self.electrical_ports = []
2424
else:
2525
self.electrical_ports = electrical_ports
2626
# self.electrical_ports = electrical_ports
27-
if logic_ports == None:
27+
if logic_ports is None:
2828
self.logic_ports = []
2929
else:
3030
self.logic_ports = logic_ports
@@ -134,7 +134,7 @@ def my_dlsim(system, u, t=None, x0=None):
134134
return tout, yout, xout
135135

136136

137-
def my_dlsimworks(system, u, t=None, x0=None):
137+
def my_dlsimworks_jax(system, u, t=None, x0=None):
138138
out_samples = len(u)
139139
stoptime = (out_samples) * system.dt
140140

@@ -302,8 +302,6 @@ def run(self, inputs: dict, time_sim=True, **kwargs) -> ArrayLike:
302302
# if state_vector is not None:
303303
# self.state_vector = state_vector
304304

305-
first_key = next(iter(inputs))
306-
N = inputs[first_key].shape
307305
responses = {}
308306

309307
input = jnp.hstack([value.reshape(-1, 1) for value in inputs.values()])

simphony/time_domain/vector_fitting/s_domain.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import jax
2-
3-
jax.config.update("jax_enable_x64", True)
4-
from time import time
5-
62
import jax.numpy as jnp
73
import matplotlib.pyplot as plt
4+
import numpy as np
85
from scipy.constants import speed_of_light
96

107
from simphony.simulation.jax_tools import python_based_while_loop
118

9+
jax.config.update("jax_enable_x64", True)
10+
1211

1312
# @jax.jit
1413
def _initial_poles(model_order, frequency, alpha):
@@ -42,18 +41,13 @@ def _lstsq_matrices(model_order, transfer_function, phi0, phi1):
4241
M = jnp.zeros(((num_ports**2) * (model_order), (model_order)), dtype=complex)
4342
B = jnp.zeros(((num_ports**2) * (model_order)), dtype=complex)
4443

45-
A1 = phi0
46-
Q1, R11 = jnp.linalg.qr(A1)
47-
4844
iter = 0
4945
for i in range(num_ports):
5046
for j in range(num_ports):
5147
D = jnp.diag(transfer_function[:, i, j])
5248
A_block = jnp.hstack([phi0, -D @ phi1]) # never build the big matrix
5349
Q, R = jnp.linalg.qr(A_block, mode="reduced")
5450

55-
R11 = R[: model_order + 1, : model_order + 1]
56-
R12 = R[: model_order + 1, model_order + 1 :]
5751
R22 = R[model_order + 1 :, model_order + 1 :]
5852
Q2 = Q[:, model_order + 1 :]
5953

@@ -80,9 +74,6 @@ def _lstsq_matrices(model_order, transfer_function, phi0, phi1):
8074
return M, B
8175

8276

83-
import numpy as np
84-
85-
8677
def _full_lstsq_matrices(transfer_function, phi0, phi1):
8778
D = []
8879
V = []
@@ -395,9 +386,9 @@ def optimize_order(bias_fn, min_order, max_order):
395386
C_max_minus_1, *_ = bias_fn(max_order - 1)
396387
lambda_lower = jnp.abs(C_max_minus_1 - C_max)
397388
lambda_upper = C_min - C_max
398-
l = jnp.log10(lambda_lower)
399-
u = jnp.log10(lambda_upper)
400-
complexity_penalty = 10 ** (0.5 * (u + l))
389+
lower_log = jnp.log10(lambda_lower)
390+
upper_log = jnp.log10(lambda_upper)
391+
complexity_penalty = 10 ** (0.5 * (upper_log + lower_log))
401392

402393
# TODO: implement Golden Section Search
403394
# to minimize C - complexity_penalty * order
@@ -490,10 +481,7 @@ def main():
490481
)
491482
residues = jnp.reshape(residues[1:], (10, 1, 1))
492483
feedthrough = jnp.zeros((1, 1), dtype=complex)
493-
N = len(poles)
494-
495484
f = jnp.linspace(0.001, 10 / (2 * jnp.pi), 100)
496-
aortic_response = pole_residue_response(f, poles, residues, feedthrough)
497485

498486
_mzi, info = sax.circuit(
499487
netlist={
@@ -531,7 +519,6 @@ def mzi(wl=1.55):
531519
f_max = speed_of_light / 1.50e-6
532520
# f_min = speed_of_light / 1.565e-6
533521
# f_max = speed_of_light / 1.5350e-6
534-
f_center = 0.5 * (f_min + f_max)
535522
frequency = jnp.linspace(f_min, f_max, 1000)
536523

537524
plt.plot(
@@ -598,15 +585,12 @@ def main2():
598585
)
599586
residues = jnp.reshape(residues[1:], (10, 1, 1))
600587
feedthrough = jnp.zeros((1, 1), dtype=complex)
601-
N = len(poles)
602-
603588
f = jnp.linspace(0.001, 10 / (2 * jnp.pi), 100)
604589
response = pole_residue_response(f, poles, residues, feedthrough)
605590

606591
poles1, residues1, feedthrough1, error = vector_fitting(
607592
10, response, f, max_iterations=5
608593
)
609-
toc = time()
610594
H = pole_residue_response(f, poles1, residues1, feedthrough1)
611595
plt.plot(f, jnp.abs(H[:, 0, 0]) ** 2)
612596
plt.plot(f, jnp.abs(response[:, 0, 0]) ** 2, "r--")

simphony/time_domain/vector_fitting/z_domain.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ def _lstsq_matrices(model_order, transfer_function, phi0, phi1):
4141
)
4242
B = jnp.zeros(((num_inputs * num_outputs) * (model_order)), dtype=complex)
4343

44-
A1 = phi0
45-
Q1, R11 = jnp.linalg.qr(A1)
46-
4744
iter = 0
4845
# for i in range(num_ports):
4946
# for j in range(num_ports):
@@ -53,8 +50,6 @@ def _lstsq_matrices(model_order, transfer_function, phi0, phi1):
5350
A_block = jnp.hstack([phi0, -D @ phi1]) # never build the big matrix
5451
Q, R = jnp.linalg.qr(A_block, mode="reduced")
5552

56-
R11 = R[: model_order + 1, : model_order + 1]
57-
R12 = R[: model_order + 1, model_order + 1 :]
5853
R22 = R[model_order + 1 :, model_order + 1 :]
5954
Q2 = Q[:, model_order + 1 :]
6055

@@ -398,9 +393,9 @@ def optimize_order(bias_fn, min_order, max_order):
398393
C_max_minus_1, *_ = bias_fn(max_order - 1)
399394
lambda_lower = jnp.abs(C_max_minus_1 - C_max)
400395
lambda_upper = C_min - C_max
401-
l = jnp.log10(lambda_lower)
402-
u = jnp.log10(lambda_upper)
403-
complexity_penalty = 10 ** (0.5 * (u + l))
396+
lower_log = jnp.log10(lambda_lower)
397+
upper_log = jnp.log10(lambda_upper)
398+
complexity_penalty = 10 ** (0.5 * (upper_log + lower_log))
404399

405400
# TODO: implement Golden Section Search
406401
# to minimize C - complexity_penalty * order
@@ -818,8 +813,6 @@ def state_space_frequency_response_discrete(A, B, C, D, f, f_center, dt):
818813

819814

820815
def main():
821-
from time import time
822-
823816
import sax
824817

825818
from simphony.libraries import ideal
@@ -860,12 +853,9 @@ def main():
860853
sampling_frequency = 1e14
861854
model_order = 10
862855

863-
tic = time()
864856
poles, residues, feedthrough, error = optimize_order_vector_fitting_discrete(
865857
10, 50, s_params, frequency, f_center, sampling_frequency
866858
)
867-
toc = time()
868-
elapsed_time_1 = toc - tic
869859
model_order = len(poles)
870860
poles_eng, residues_eng, feedthrough_eng, erro = vector_fitting_discrete(
871861
model_order,
@@ -908,7 +898,7 @@ def main():
908898
plt.scatter(residues_eng[:, 0, 1].real, residues_eng[:, 0, 1].imag)
909899
plt.show()
910900

911-
H = pole_residue_response_discrete(
901+
pole_residue_response_discrete(
912902
f,
913903
f_center,
914904
sampling_frequency,

tox.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ exclude =
3434
.tox,
3535
extra,
3636
deprecated
37+
38+
per-file-ignores =
39+
simphony/time_domain/SSFM_old.py:E226,F841
40+
simphony/time_domain/examples/*.py:E203,E402
41+
simphony/time_domain/old_simulation.py:C901,E402,E711,F541
42+
simphony/time_domain/quicker_delete.py:E226,E402
43+
simphony/time_domain/tests/*.py:E402,F841

0 commit comments

Comments
 (0)