def make_ft_axis(length, dt, undersampling = 0, normalized_to_period = 0, zero_in_middle = False, flag_verbose = False): """ fourier transform the time axis 20101204/RB: started 20130131/RB: now uses speed of light value from Crocodile.Resources.Constants INPUT: length: amount of samples dt: time between samples undersampling: normalized_to_period: will normalize the spectrum to 1 for the specified period. If the number is 0, the spectrum will not be normalized. zero_in_middle: determines the frequency axes. OUTPUT: A frequency axis. """ DEBUG.verbose("make FT axis", flag_verbose) if normalized_to_period == 0: resolution = 1 / ( CONST.wavenumberToInvFs * length * dt) else: resolution = normalized_to_period / (length * dt) array = numpy.arange((undersampling)*length/2, (undersampling+1)*length/2)*resolution if zero_in_middle == False: return numpy.concatenate((array,-numpy.flipud(array))) else: return numpy.concatenate((-numpy.flipud(array), array))
def execute(args): if args.skip1 == False: suite = unittest.TestLoader().loadTestsFromTestCase(Test_setting_file_stuff) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[0], True)
def make_numpy_ndarray(val): """ make a numpy.ndarray out of val, used by make_coordinates() Types of val that are accepted: int, float, string: make it a list and then numpy.ndarray. list: make it a numpy.ndarray tuple: convert to a list, then numpy.ndarray numpy.ndarray: return directly Not accepted: dict CHANGELOG: 20130317/RB: started """ if type(val) == numpy.ndarray: return val elif type(val) == list: return numpy.array(val) elif type(val) == dict: DEBUG.printError("Value shouldn't be a dict or tuple", inspect.stack()) return False elif type(val) == tuple: return numpy.array(list(val)) else: return numpy.array([val])
def execute(args): if args.skip1 == False: suite = unittest.TestLoader().loadTestsFromTestCase(Test_find_axes) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[0], True) if args.skip2 == False: suite = unittest.TestLoader().loadTestsFromTestCase(Test_make_contours_2d) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[1], True) if args.skip3 == False: suite = unittest.TestLoader().loadTestsFromTestCase(Test_find_axes_indices) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[2], True) if args.skip4 == False: suite = unittest.TestLoader().loadTestsFromTestCase(Test_truncate_data) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[3], True) if args.skip5 == False: suite = unittest.TestLoader().loadTestsFromTestCase(Test_find_subplots) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[4], True)
def truncate_data(data, x_axis, y_axis, x_min_i, x_max_i, y_min_i, y_max_i, flag_verbose = False): """ Truncate data in a non-default way. x_axis = [1,2,3,4,5] x_min_i = 0 if x_max_i == 2: x_axis = [1,2] if x_max_i == 4: x_axis = [1,2,3,4] if x_max_i == -1: x_axis = [1,2,3,4,5] CHANGELOG: 20130213: was in Plotting.contourplot function, moved to Functions module, now handles x_max_i == -1 """ DEBUG.verbose("Truncate data", flag_verbose) if x_max_i == -1: DEBUG.verbose(" x_max == -1", flag_verbose) data = data[:,x_min_i:] x_axis = x_axis[x_min_i:] else: DEBUG.verbose(" x_max != -1", flag_verbose) data = data[:,x_min_i:x_max_i] x_axis = x_axis[x_min_i:x_max_i] if y_max_i == -1: DEBUG.verbose(" y_max == -1", flag_verbose) data = data[y_min_i:,:] y_axis = y_axis[y_min_i:] else: DEBUG.verbose(" y_max != -1", flag_verbose) data = data[y_min_i:y_max_i,:] y_axis = y_axis[y_min_i:y_max_i] return data, x_axis, y_axis
def test_time_stamp_False(self): """ This is correct """ DEBUG.verbose("\nWarning is intentional", True) self.dc.time_stamp = False self.assertEqual(self.dc.time_stamp, "False")
def test_time_stamp_5_int(self): """ This is correct """ DEBUG.verbose("\nWarning is intentional", True) self.dc.time_stamp = 12345 self.assertEqual(self.dc.time_stamp, "12345")
def test_import_data_no_data(self): """ folder exists, LV_file_format.1 exists and is correct, but contains no other data """ self.mess.path = "Test_resources/petw_test_folder_2/" DEBUG.verbose("\nError about importing is intentional", True) res = self.mess.import_data(flag_verbose = self.flag_verbose) self.assertFalse(res)
def test_phase_rad_nan(self): """ None sets phase_degrees to init value """ DEBUG.verbose("\nWarning is intentional", True) self.dc.phase_rad = None self.assertEqual(self.dc.phase_degrees, None) self.assertEqual(self.dc.phase_rad, None)
def test_2p_1m(self): """ mess[0].s = 1 + [1].s: 2 - [2].s: 3 (1 + 2) / 2 - 3 = -1.5 """ DEBUG.verbose("\nWarning about zeropad intentional", True) mer = PEME.pe_merge("Test", class_plus = [self.mess[0], self.mess[1]], class_min = [self.mess[2]], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(mer.s == -1.5))
def test_phase_rad_true(self): """ True should be interpreted as 1 """ DEBUG.verbose("\nWarning is intentional", True) self.dc.phase_rad = True self.assertAlmostEqual(self.dc.phase_degrees, 1 * 180 / numpy.pi) self.assertEqual(self.dc.phase_rad, 1)
def test_phase_rad_nan(self): """ numpy.nan should give error, no value is set """ DEBUG.verbose("\nWarning is intentional", True) self.dc.phase_rad = numpy.nan self.assertEqual(self.dc.phase_degrees, None) self.assertEqual(self.dc.phase_rad, None)
def test_phase_rad_false(self): """ False should be interpreted as 0 """ DEBUG.verbose("\nWarning is intentional", True) self.dc.phase_rad = False self.assertEqual(self.dc.phase_degrees, 0) self.assertEqual(self.dc.phase_rad, 0)
def test_phase_degrees_none(self): """ None should will reset phase_degrees to init value """ DEBUG.verbose("\nWarning is intentional", True) self.dc.phase_degrees = None self.assertEqual(self.dc.phase_degrees, None) self.assertEqual(self.dc.phase_rad, None)
def test_import_data_incorrect_path(self): """ Path is incorrect, no data imported. Returns False """ self.mess.path = "Test_resources/FolderDoesNotExist/" DEBUG.verbose("\nError that directory can't found is intentional", True) res = self.mess.import_data(flag_verbose = self.flag_verbose) self.assertFalse(res)
def test_zeropad_to_r_uninit(self): """ without r set, it should give an error """ DEBUG.verbose("\nError is intentional", True) self.dc.zeropad_to = 40 self.assertEqual(self.dc.zeropad_to, None) self.assertEqual(self.dc.zeropad_by, 1.0)
def test_import_data_wrong_file_format(self): """ LV_file_format.666 should not be recognized. """ self.mess.path = "Test_resources/petw_test_folder_1/" DEBUG.verbose("\nError about unknown file format is intentional", True) res = self.mess.import_data(flag_verbose = self.flag_verbose) self.assertFalse(res)
def test_object_with_sub_type_unknown_sub_type(self): """ Test if objects with sub_type 'x' are found - there should be none. This raises a warning. The resulting array has length zero. """ sub_type = "x" DEBUG.verbose("\nWarning is intentional", True) array = self.oa.list_objects_with_sub_type(sub_type = sub_type, flag_verbose = self.flag_verbose) self.assertTrue(len(array) == 0)
def test_add_array_with_objects_2(self): """ Add unique and a non-unique object to the array """ temp = [self.a, self.c] DEBUG.verbose("\nWarning is intentional", True) self.oa.add_array_with_objects(temp, flag_verbose = self.flag_verbose) self.assertTrue(["a", "b", "c"] == self.oa.obj_id_array)
def test_rs_1(self): """ This should work correct r and s exist """ DEBUG.verbose("\nTwo intentional zeropad warnings", True) mer = PEME.pe_merge("Test", class_plus = [self.mess[0]], class_min = [self.mess[1]], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(mer.s))
def test_add_object_with_same_obj_id(self): """ Add object with same obj_id """ a = OA.testobject("Boot", "a", "power", flag_verbose = self.flag_verbose) DEBUG.verbose("\nError is intentional", True) result = self.oa.add_object(a, flag_verbose = self.flag_verbose) self.assertTrue(["a", "b", "c"] == self.oa.obj_id_array and result == False)
def test_add_object_without_obj_id(self): """ Add object without obj_id """ oa = OA.objectarray("test_new") DEBUG.verbose("\nError is intentional", True) result = oa.add_object(1, flag_verbose = self.flag_verbose) self.assertFalse(result)
def linear(data, x_axis, x_range = [0, 0], y_range = [0, 0], ax = False, x_label = "", y_label = "", title = "", legend = "", plot_real = True, flag_verbose = False): DEBUG.verbose("linear plot", flag_verbose) if plot_real: data = numpy.real(data) # make the x-axis if x_range == [0, 0]: x_min = x_axis[0] x_max = x_axis[-1] else: x_min = x_range[0] x_max = x_range[1] # select the appropriate data range x_min_i = numpy.where(x_axis > x_min)[0][0] x_max_i = numpy.where(x_axis < x_max)[0][-1] x_axis = x_axis[x_min_i:x_max_i] data = data[x_min_i:x_max_i] # make sure there is an axis-object if ax == False: fig = plt.figure() ax = fig.add_subplot(111) flag_show = True else: flag_show = False # the actual plot ax.plot(x_axis, data, label = legend) ax.set_xlim(x_min, x_max) if y_range != [0,-1]: ax.set_ylim(y_range[0], y_range[1]) ax.set_xlabel(x_label) ax.set_ylabel(y_label) ax.set_title(title) if legend != "": plt.legend() if flag_show: plt.show() return True
def test_obj_id_str(self): oa = OA.objectarray("test") a = OA.testobject("Auto", "a", "power", flag_verbose = self.flag_verbose) oa.add_object(a, flag_verbose = self.flag_verbose) DEBUG.verbose("\nOverwrite warning is intentional", True) oa.save_objectarray(self.path_and_filename, flag_overwrite = True, flag_verbose = self.flag_verbose)
def test_reverse_edges(self): """ The function does no sanity-checking. """ val_min = 27.0 val_max = 22.0 DEBUG.verbose("\nWarning is intentional", True) res = FU.find_axes_indices(self.axis, val_min, val_max) self.assertEqual(res, (7,4))
def test_zeropad_to_nan(self): """ set zeropad_to to numpy.nan """ self.dc.r = [numpy.ones((10,20)), numpy.ones((10,20))] DEBUG.verbose("\nError is intentional", True) self.dc.zeropad_to = numpy.nan self.assertEqual(self.dc.zeropad_to, None) self.assertEqual(self.dc.zeropad_by, 1.0)
def fourier(array, zero_in_middle = False, first_correction = False, zeropad_to = None, window_function = "none", window_length = 0, flag_plot = False, flag_verbose = False): """ A Fourier transform for any dimension. INPUT: - array (x-dimensions ndarray): to be FFT'd - zero_in_middle (BOOL): for FFT the zero-time should be the first element of the array. If the zero is in the middle, it will be shifted first - first_correction (BOOL): if the first element of the array has to be halved, check this as True - zeropad_to (number): Length of the transformed axis of the output. If n is smaller than the length of the input, the input is cropped. If it is larger, the input is padded with zeros. If n is None, the length of the input (along the axis specified by axis) is used. OUTPUT: array (x-dimensions ndarray): Fourier transformed array CHANGELOG: 20101204 RB: started 20110909 RB: added zeropadding """ DEBUG.verbose("FFT", flag_verbose) # shift time = 0 to first element if zero_in_middle == True: array = numpy.fft.ifftshift(array) # half the first element if first_correction == True: dim = len(numpy.shape(array)) if dim == 1: array[0] /= 2 elif dim == 2: array[0,:] /= 2 array[:,0] /= 2 elif dim > 2: DEBUG.printError("Correction of the first element is not done!", inspect.stack()) # window function if window_function != "none": array = window_functions(array, window_function, window_length, flag_plot = flag_plot) # the fft array = numpy.fft.fft(array, n = zeropad_to) # move the array back if it was shifted if zero_in_middle == True: array = numpy.fft.fftshift(array) return array
def test_phase_degrees_uninit_1(self): """ mess[0].phase_degrees = None s=[0] to suppress zeropad warning """ self.mess[0].s = [0] DEBUG.verbose("\nIntentional phase warning", True) self.mess[0].phase_degrees = None DEBUG.verbose("\nIntentional phase warning", True) mer = PEME.pe_merge("Test", class_plus = [self.mess[0]], class_min = [self.mess[1]], flag_verbose = self.flag_verbose) self.assertEqual(mer.phase_degrees, None)
def test_obj_id_unicode(self): """ This testcase may be unnecesary. If you use from __future__ import unicode_literals, then somes strings are suddenly unicode, giving problems when saving the database. However, in Python 3.3 they removed this. Without the __future__ thingy it also works fine in Python 2.7. """ oa = OA.objectarray("test") l = ["auto"] a = OA.testobject(l[0], "a", "power", flag_verbose = self.flag_verbose) oa.add_object(a, flag_verbose = self.flag_verbose) DEBUG.verbose("\nOverwrite warning is intentional", True) oa.save_objectarray(self.path_and_filename, flag_overwrite = True, flag_verbose = self.flag_verbose)
def test_phase_degrees_uninit_2(self): """ mess[1].phase_degrees = None s=[0] to suppress zeropad warning Tests if the order makes a difference. It shouldn't """ self.mess[0].s = [0] DEBUG.verbose("\nIntentional phase warning", True) self.mess[1].phase_degrees = None DEBUG.verbose("\nError is intentional", True) mer = PEME.pe_merge("Test", class_plus = [self.mess[0]], class_min = [self.mess[1]], flag_verbose = self.flag_verbose) self.assertEqual(mer.phase_degrees, None)
def execute(args): if args.skip1 == False: suite = unittest.TestLoader().loadTestsFromTestCase( Test_make_coordinates) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[0], True) if args.skip2 == False: suite = unittest.TestLoader().loadTestsFromTestCase( Test_find_longest_list) unittest.TextTestRunner(verbosity=1).run(suite) else: DEBUG.verbose("Skipping: " + suite_list[1], True)