Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skrf.NetworkSet.interpolate_from_params raises exception if any identical parameter values are in a network set where as skrf.NetworkSet.interpolate_from_network using the same parameters does not. #999

Open
Sionwage opened this issue Jan 17, 2024 · 1 comment · May be fixed by #1012
Labels
Feature Request Wished Feature Improvements Improvements of existing feature NetworkSet

Comments

@Sionwage
Copy link

Describe the bug

rf.NetworkSet.interpolate_from_params raises an exception if a network set has two identical values for a parameter being interpolated.

To Reproduce
Steps to reproduce the behavior:

Create a network set with a parameter having two or more identical values that is to be interpolated.

In my case (loading a folder of s-parameter measurements):

import skrf as rf
import matplotlib.pyplot as plt
from pprint import pprint

from pathlib import Path
import os

folder = '../KaptonData/'

data_files = os.listdir(folder)
param_data = [1, 2, 2, 3, 4, 20, 50, 100]
ntwk_files = [rf.Network(Path(folder+file, params{'spacing':param})) for (file, param) in zip(data_files, param_data)]

ntwk_set = rf.NetworkSet(ntwk_files)

ntwk_set.interpolate_from_params('spacing', 5.0)

Output is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[11], line 1
----> 1 ntwk_set.interpolate_from_params('spacing', 5.0)

File ~\AppData\Local\pypoetry\Cache\virtualenvs\python-model-2taDPn6c-py3.12\Lib\site-packages\skrf\networkSet.py:1308, in NetworkSet.interpolate_from_params(self, param, x, sub_params, interp_kind)
   1306 # interpolating the sub-NetworkSet matching the passed sub-parameters
   1307 sub_ns = self.sel(sub_params)
-> 1308 interp_ntwk = sub_ns.interpolate_from_network(sub_ns.coords[param],
   1309                                               x, interp_kind)
   1311 return interp_ntwk

File ~\AppData\Local\pypoetry\Cache\virtualenvs\python-model-2taDPn6c-py3.12\Lib\site-packages\skrf\networkSet.py:1041, in NetworkSet.interpolate_from_network(self, ntw_param, x, interp_kind)
   1039 # Interpolating the scattering parameters
   1040 s = npy.array([self[idx].s for idx in range(len(self))])
-> 1041 f = interp1d(ntw_param, s, axis=0, kind=interp_kind)
   1042 ntw.s = f(x)
   1044 return ntw

File ~\AppData\Local\pypoetry\Cache\virtualenvs\python-model-2taDPn6c-py3.12\Lib\site-packages\scipy\interpolate\_interpolate.py:494, in interp1d.__init__(self, x, y, kind, axis, copy, bounds_error, fill_value, assume_sorted)
    490 def __init__(self, x, y, kind='linear', axis=-1,
    491              copy=True, bounds_error=None, fill_value=np.nan,
    492              assume_sorted=False):
    493     """ Initialize a 1-D linear interpolation class."""
--> 494     _Interpolator1D.__init__(self, x, y, axis=axis)
    496     self.bounds_error = bounds_error  # used by fill_value setter
    497     self.copy = copy

File ~\AppData\Local\pypoetry\Cache\virtualenvs\python-model-2taDPn6c-py3.12\Lib\site-packages\scipy\interpolate\_polyint.py:56, in _Interpolator1D.__init__(self, xi, yi, axis)
     54 self.dtype = None
     55 if yi is not None:
---> 56     self._set_yi(yi, xi=xi, axis=axis)

File ~\AppData\Local\pypoetry\Cache\virtualenvs\python-model-2taDPn6c-py3.12\Lib\site-packages\scipy\interpolate\_polyint.py:126, in _Interpolator1D._set_yi(self, yi, xi, axis)
    124     shape = (1,)
    125 if xi is not None and shape[axis] != len(xi):
--> 126     raise ValueError("x and y arrays must be equal in length along "
    127                      "interpolation axis.")
    129 self._y_axis = (axis % yi.ndim)
    130 self._y_extra_shape = yi.shape[:self._y_axis]+yi.shape[self._y_axis+1:]

ValueError: x and y arrays must be equal in length along interpolation axis.

Expected behavior
I would expect this function to run like this:

ntwk_set.interpolate_from_network(param_data, 5.0) which returns 2-Port Network: '20231214-101306-1 mil spacing', 20.0-250.0 MHz, 1601 pts, z0=[50.+0.j 50.+0.j]

This when plotted looks correct compared to a measurement of a 5.0 mil spacing VNA measurement.

Data
I believe the solution to the problem is on line 1308 of networkSet.py.

For my above code:

The value of ntwk_set.coords['spacing'] is [1, 2, 3, 4, 100, 50, 20] vs the value of ntwk_set.params_values['spacing'] is [1, 2, 2, 3, 4, 20, 50, 100].

I think line 1308 should be using sub_ns.params_values[param] instead of sub_ns.coords[param].

Screenshots
If applicable, add screenshots to help explain your problem.

None

System

  • OS: window
  • scikit-rf version 0.30.0

Additional context
Add any other context about the problem here if relevant.

@Sionwage
Copy link
Author

Digging into the module and how scipy expects data, it looks like another issue could be having duplicate data values for a parameter based on the interpolation kind to use. Linear might be ok with duplicates but quadratic and cubic would not.

Would there be any value in modifying the function to turn the networks that are duplicates of a parameter value and substituting the mean_s of that NetworkSet to prevent duplicate parameters being used in line 1041 of networkSet.py? Or perhaps even omitting extra networks after the first one with a warning message?

Other item is that scipy.interp1d's parameter of kind can take an int instead of a string that could be good to add to the function snippet.

@jhillairet jhillairet linked a pull request Jan 27, 2024 that will close this issue
@jhillairet jhillairet added Feature Request Wished Feature NetworkSet Improvements Improvements of existing feature labels Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Wished Feature Improvements Improvements of existing feature NetworkSet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants