def test_read_data(self): ''' This test reads data from simple_touchstone.s2p and compares with known true values. ''' filename = os.path.join(self.test_dir, 'simple_touchstone.s2p') touch = Touchstone(filename) f, s = touch.get_sparameter_arrays() z0 = complex(touch.resistance) f_true = npy.array([1.00000000e+09, 1.10000000e+09]) s_true = npy.array([[[1.+2.j, 5.+6.j], [3.+4.j, 7.+8.j]], [[9.+10.j, 13.+14.j], [11.+12.j, 15.+16.j]]]) z0_true = 50+50j self.assertTrue((f == f_true).all()) self.assertTrue((s == s_true).all()) self.assertTrue((z0 == z0_true))
def test_read_from_fid(self): ''' This tests reading touch stone data from a file object as compared with a string path and name of the file. ''' with open(os.path.join(self.test_dir, 'simple_touchstone.s2p')) as fid: touch = Touchstone(fid) f, s = touch.get_sparameter_arrays() z0 = complex(touch.resistance) f_true = npy.array([1.00000000e+09, 1.10000000e+09]) s_true = npy.array([[[1.+2.j, 5.+6.j], [3.+4.j, 7.+8.j]], [[9.+10.j, 13.+14.j], [11.+12.j, 15.+16.j]]]) z0_true = 50+50j self.assertTrue((f == f_true).all()) self.assertTrue((s == s_true).all()) self.assertTrue((z0 == z0_true))
def test_read_from_fid(self): fid = open(os.path.join(self.test_dir, 'simple_touchstone.s2p')) touch= Touchstone(fid) f,s = touch.get_sparameter_arrays() z0 = complex(touch.resistance) f_true = npy.array([ 1.00000000e+09, 1.10000000e+09]) s_true = npy.array([ [[ 1. +2.j, 5. +6.j], [ 3. +4.j, 7. +8.j]], [[ 9.+10.j, 13.+14.j], [ 11.+12.j, 15.+16.j]] ]) z0_true = 50+50j self.assertTrue((f==f_true).all()) self.assertTrue((s==s_true).all()) self.assertTrue((z0==z0_true))
def to_csv(self, csv_filename, dB=True, angle='rad', unwrap=False): csv_filename = self.root_node.gui.dialogs.constant_handler_ASK_SAVEAS_FILENAME( csv_filename, filetypes=[('CSV Files', '*.csv'), ('All Files', '*.*')], defaultextension='.csv', initialdir=os.path.split(self.filename)[0]) s2p = Touchstone(str(self.filename)) f, s = s2p.get_sparameter_arrays() if angle == 'rad': convert_unit = lambda x: x elif angle in ('deg', 'degree'): convert_unit = numpy.rad2deg else: raise TypeError('Angle unit not supported.') if dB: get_mag = lambda x: 20 * numpy.log10(numpy.abs(x)) else: get_mag = numpy.abs if unwrap: get_unwrap = numpy.unwrap else: get_unwrap = lambda x: x X, Y, Z = s.shape data_list = [] for z in range(Z): for y in range(Y): data_list.append( (get_mag(s[:, y, z]), convert_unit(get_unwrap(numpy.angle(s[:, y, z]))))) with open(csv_filename, 'wb') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Freq'] + ['Abs', 'Angle'] * len(data_list)) for index, freq in enumerate(f): row = [freq] for item in data_list: row.append(item[0][index]) row.append(item[1][index]) writer.writerow(row)
def to_csv(self, csv_filename, dB=True, angle='rad', unwrap=False): csv_filename = self.root_node.dialogs.support_ask_saveas_filename( csv_filename, filetypes=[('CSV Files', '*.csv'), ('All Files', '*.*')], defaultextension='.csv', initialdir=os.path.split(self.filename)[0] ) s2p = Touchstone(str(self.filename)) f, s = s2p.get_sparameter_arrays() if angle == 'rad': convert_unit = lambda x: x elif angle in ('deg', 'degree'): convert_unit = numpy.rad2deg else: raise TypeError('Angle unit not supported.') if dB: get_mag = lambda x: 20*numpy.log10(numpy.abs(x)) else: get_mag = numpy.abs if unwrap: get_unwrap = numpy.unwrap else: get_unwrap = lambda x: x X, Y, Z = s.shape data_list = [] for z in range(Z): for y in range(Y): data_list.append((get_mag(s[:, y, z]), convert_unit(get_unwrap(numpy.angle(s[:, y, z]))))) with open(csv_filename, 'wb') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Freq'] + ['Abs', 'Angle']*len(data_list)) for index, freq in enumerate(f): row = [freq] for item in data_list: row.append(item[0][index]) row.append(item[1][index]) writer.writerow(row)
def load(self, filename): kwargs = {} if self.__filename: kwargs['initialdir'] = os.path.split(self.__filename)[0] kwargs['filetypes'] = [('Touchstone Files', '*.s2p'), ('All Files', '*.*')] filename = self.root_node.gui.dialogs.constant_handler_ASK_OPEN_FILENAME( filename, **kwargs) if not filename: return s2p = Touchstone(str(filename)) f, s = s2p.get_sparameter_arrays() self.__freq_range = f X, Y, Z = s.shape for y in range(Y): for z in range(Z): self.current_data = s[:, z, y] self.plot_current_data() self.__filename = filename
def load(self, filename): kwargs = {} if self.__filename: kwargs['initialdir'] = os.path.split(self.__filename)[0] kwargs['filetypes'] = [('Touchstone Files', '*.s2p'), ('All Files', '*.*')] filename = self.root_node.dialogs.support_ask_open_filename( filename, **kwargs ) if not filename: return s2p = Touchstone(str(filename)) f, s = s2p.get_sparameter_arrays() self.__freq_range = f X, Y, Z = s.shape for y in range(Y): for z in range(Z): self.current_data = s[:, z, y] self.plot_current_data() self.__filename = filename