Repository: scikit-rf/scikit-rf

Issue:
Network.delay() swap ports when nports == 2
### Discussed in https://github.com/scikit-rf/scikit-rf/discussions/1068
The ports are swapped when using `Network.delay` on a 2-port network.

Let's do a single frequency point 2-port open and add a delay of pi rad:
```Python
import skrf as rf
import numpy as np

# n-port open
nports = 2
f = rf.Frequency(1, 1, 1, 'GHz')
s = np.identity(nports, dtype = complex)
z0 = np.array([50] * nports)
z0[0] = 25
n = rf.Network(frequency = f, s = s, z0 = z0, name = 'original')
print(n.name)
print(f"{np.angle(n.s[0])}")
print(f"z0:{n.z0[0]}")

# apply pi delay
n = n.delay(np.pi, unit = 'rad', port = 0)
n.name = 'theta = pi delay'
print(n.name)
print(f"{np.angle(n.s[0])}")
print(f"z0:{n.z0[0]}")
```
The output is:
```
original
[[0. 0.]
 [0. 0.]]
z0:[25.+0.j 50.+0.j]
theta = pi delay
[[ 0.          0.        ]
 [ 0.         -3.14159265]]
z0:[50.+0.j 25.+0.j]
```
The pi rad delay is applied but the ports are swapped (see the 25 ohm port moving from 0 to 1). This is due to `Network.connect` when cascading the delay:
https://github.com/scikit-rf/scikit-rf/blob/b67a5c2dacaf7415991c2606716ade9d5a140eab/skrf/network.py#L3426-L3427
https://github.com/scikit-rf/scikit-rf/blob/b67a5c2dacaf7415991c2606716ade9d5a140eab/skrf/network.py#L4994-L5002
Should be `if ntwkB.nports == 2 and ntwkA.nports >= 2 and num == 1:`

Discussion:

Patch:
diff --git a/skrf/calibration/calibration.py b/skrf/calibration/calibration.py
index 0d28a554..3317b274 100644
--- a/skrf/calibration/calibration.py
+++ b/skrf/calibration/calibration.py
@@ -5232,7 +5232,7 @@ class LMR16(SixteenTerm):
         if switch_terms is None:
             warn('No switch terms provided', stacklevel=2)
 
-        if type(ideals) == Network:
+        if isinstance(ideals, Network):
             ideals = [ideals]
         if len(ideals) != 1:
             raise ValueError("One ideal must be given: Through or reflect definition.")
diff --git a/skrf/network.py b/skrf/network.py
index 0cb40a63..05ab5c3a 100644
--- a/skrf/network.py
+++ b/skrf/network.py
@@ -4991,7 +4991,7 @@ def connect(ntwkA: Network, k: int, ntwkB: Network, l: int, num: int = 1) -> Net
         ntwkC = innerconnect(ntwkC, k, ntwkA.nports - 1 + l, num - 1)
 
     # if ntwkB is a 2port, then keep port indices where you expect.
-    if ntwkB.nports == 2 and ntwkA.nports > 2 and num == 1:
+    if ntwkB.nports == 2 and ntwkA.nports >= 2 and num == 1:
         from_ports = list(range(ntwkC.nports))
         to_ports = list(range(ntwkC.nports))
         to_ports.pop(k)


Test patch:
diff --git a/skrf/tests/test_network.py b/skrf/tests/test_network.py
index b8a7ed41..53044ab6 100644
--- a/skrf/tests/test_network.py
+++ b/skrf/tests/test_network.py
@@ -468,7 +468,7 @@ class NetworkTestCase(unittest.TestCase):
         line = med.line(1, unit='m')
         line.z0 = [10, 20]
 
-        for nport_portnum in [3,4,5,6,7,8]:
+        for nport_portnum in [1,2,3,4,5,6,7,8]:
 
             # create a Nport network with port impedance i at port i
             nport = rf.Network()