def read2DFMR2magFields(filepattern, path_field_after, path_field_before, path_frequency, path_real_signal, path_imag_signal, field_points=None, freq_points=None, tdms_file=None): """ Read a VNA- or Lock-In measurement with a field-before and a field-after channel. As an input this routine requires a field_before- and a field_after-channel and the groups respectively. For the frequency-channel and the signal-channels it assumes that these are in the same group. Parameters ---------- filepattern : string filename (relative) of tdms file. may include wilcards (see glob) path_* : tuple of strings path i.e. [group, channel] to the corresponding data nFieldPoints : integer, optional number of expected points in x direction, gets calculated if not specified nFrequencyPoints : integer, optional number of expected points in y direction, gets calculated if not specified Returns ------- x : dim. (nXPoints x 1) numpy array averaged field values from field_before and field_after y : dim. (nYPoints x 1) numpy array frequency values complexSignal : (nXPoints x nYPoints) complex numpy array signal as matrix tdms_file : (TdmsFile) loaded tdms file object Usage example (matplotlib): -------------- >>> x, y, aSignal, _ = read2DFMR2magFields(fname, signal_group, group_field_before, group_field_after, field_before_channel, field_after_channel, freq_channel, signal_channel, imag_channel, tdms_file=tdms_file) """ if tdms_file is None: files = glob(filepattern) if len(files) > 1: l.warn("Provided file pattern %s did return multiple results."%filepattern) if len(files) < 1: l.warn("Provided file pattern %s did not match any files."%filepattern) l.info("Reading file %s"%files[0]) tdms_file = TdmsFile(files[0]) x = average_channels(tdms_file, path_field_before, path_field_after, True) y = tdms_file.channel_data(*path_frequency) signal = tdms_file.channel_data(*path_real_signal) + 1j*tdms_file.channel_data(*path_imag_signal) return list(reshape(x, y, signal, field_points, freq_points)) + [tdms_file]
def f_open_tdms_2(filename): if filename == 'Input': filename = filedialog.askopenfilename() tdms_file = TdmsFile(filename) group_names = tdms_file.groups() # print('groups') # print(group_names) channel_object = tdms_file.group_channels(group_names[0]) # print('channel') # print(channel_object) channel_name = channel_object[0].channel # print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') # print(channel_name) # print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') data = tdms_file.channel_data(group_names[0], channel_name) # channel_object = tdms_file('Thiele_Versuche', 'AE Signal') # group_name = file.groups() # print(group_name[0]) # channel_name = file.group_channels(group_name[0]) # print(channel_name[0]) # canal = file.object(group_name[0], 'AE Signal') # group_name = file.groups() # group_name = group_name[0] # data = file.object('Thiele_Versuche', 'AE Signal') # data = file.channel_data(group_name[0], 'AE Signal') return data
def loadTDMSImages(self, file): tdms_file = TdmsFile(file) p = tdms_file.object().properties self.dimx = int(p['dimx']) self.dimy = int(p['dimy']) self.binning = int(p['binning']) self.frames = int(p['dimz']) self.exposure = float(p['exposure'].replace(',', '.')) images = tdms_file.channel_data('Image', 'Image') return images.reshape(self.frames, self.dimx, self.dimy)
def cut_log_spectra(fileinpaths, times, fileoutpaths_list, **kwargs): for i, fileinpath in enumerate(fileinpaths): fileoutpaths = fileoutpaths_list[i] tdmsfile = TF(fileinpath) for j, t in enumerate(times): fileoutpath = fileoutpaths[j] direc = os.path.split(fileoutpath)[0] if not os.path.exists(direc): os.makedirs(direc) root_object = RootObject(properties={}) try: with TdmsWriter(fileoutpath, mode='w') as tdms_writer: timedata = [ dt64(y) for y in tdmsfile.channel_data('Global', 'Time') ] idx1, idx2 = _get_indextime(timedata, t[0], t[1]) if idx1 == idx2: pass else: for group in tdmsfile.groups(): group_object = GroupObject(group, properties={}) if group == "Global": for channel in tdmsfile.group_channels(group): if channel.channel == 'Wavelength': channel_object = ChannelObject( channel.group, channel.channel, channel.data) else: channel_object = ChannelObject( channel.group, channel.channel, channel.data[idx1:idx2]) tdms_writer.write_segment([ root_object, group_object, channel_object ]) else: for channel_object in tdmsfile.group_channels( group)[idx1:idx2]: tdms_writer.write_segment([ root_object, group_object, channel_object ]) except ValueError as error: print(error) print('removing the file at: \n', fileoutpath) os.remove(fileoutpath)
def f_open_tdms_old(filename, channel): if filename == 'Input': filename = filedialog.askopenfilename() file = TdmsFile(filename) group_name = file.groups() group_name = group_name[0] # print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') # print(group_name) # print(file) # print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') data = file.channel_data(group_name, channel) return data
def tdms_to_json(directory, target_directory, name): ''' Parses the TDMS file name located in the directory to json file named like the tdms file ''' print("Parsing: " + directory + name + ".tdms") # load in data, take the TDMS data type as example tdms_file = TdmsFile(directory + name + ".tdms") groups = tdms_file.groups() df_list = [] #df_test = pd.DataFrame() df_list.append(pd.DataFrame()) for group in groups: print(group) for channel in tdms_file.group_channels(group): try: chan_name = str(channel).split("'/'")[1].replace("'>", "") data = tdms_file.channel_data(group, chan_name) chan_name = chan_name.replace("(", " ").replace( ")", " ").replace(" ", " ").replace(" ", "_") df_test = pd.DataFrame() #print(chan_name) # print(data) #time.sleep(0.5) df_test[chan_name] = chan_name #data df_list[-1][chan_name] = chan_name #data #df_test.to_json(target_directory + chan_name + ".json", orient='split') # print(chan_name) # print(data) #df_test.to_json(target_directory + chan_name + ".json", orient='split') #df_test.to_json(target_directory + chan_name + '_' + name + ".json", orient='split') with open( target_directory + chan_name + '_' + name + ".json", 'w') as fp: json.dump({chan_name: data.tolist()}, fp) except: print("An Error Occured at: X !") continue
def cut_powermeter(fileinpaths, times, fileoutpaths_list, **kwargs): """Cut up a power meter tdms file based on input times.""" localtz = tzlocal.get_localzone() for i in range(len(fileinpaths)): fileinpath = fileinpaths[i] fileoutpaths = fileoutpaths_list[i] tdmsfile = TF(fileinpath) for j in range(len(times)): time1 = times[j][0].astype('O') time1 = time1.replace(tzinfo=pytz.utc) #convert to datetime time2 = times[j][1].astype('O') time2 = time2.replace(tzinfo=pytz.utc) fileoutpath = fileoutpaths[j] direc = os.path.split(fileoutpath)[0] if not os.path.exists(direc): os.makedirs(direc) root_object = RootObject(properties={ #TODO root properties }) try: with TdmsWriter(fileoutpath, mode='w') as tdms_writer: for group in tdmsfile.groups(): timedata = tdmsfile.channel_data(group, 'Time_LV') for channel in tdmsfile.group_channels(group): if type(channel.data_type.size) == type(None): break #skips over non numeric channels channel_object = _cut_channel(channel, time1, time2, timedata=timedata) tdms_writer.write_segment( [root_object, channel_object]) timechannel = tdmsfile.object(group, 'Time_LV') timechannel_cut = _cut_datetime_channel( timechannel, time1, time2) tdms_writer.write_segment( [root_object, timechannel_cut]) except ValueError as error: print(error) print('removing the file at: \n', fileoutpath) os.remove(fileoutpath)
root_object_2 = rack_2.object() # Get groups (rack names) # automatize in the future group_names_1 = rack_1.groups() group_names_2 = rack_2.groups() print('List of groups of the rack_1 TDMS:', group_names_1[0]) print('List of groups of the rack_2 TDMS:', group_names_2[0]) # Get channel names of the group if len(group_names_1) == 1: channel_names_1 = rack_1.group_channels(group_names_1[0]) if len(group_names_2) == 1: channel_names_2 = rack_2.group_channels(group_names_2[0]) # Get the data from channels PT101 = rack_1.channel_data(group_names_1[0], "PT101") PT102 = rack_1.channel_data(group_names_1[0], "PT102") PT103 = rack_2.channel_data(group_names_2[0], "PT103") PT104 = rack_2.channel_data(group_names_2[0], "PT104") PT551 = rack_1.channel_data(group_names_1[0], "PT551") PT552 = rack_1.channel_data(group_names_1[0], "PT552") PT553 = rack_1.channel_data(group_names_1[0], "PT553") PT421 = rack_1.channel_data(group_names_1[0], "PT421") MT401S = rack_1.channel_data(group_names_1[0], "MT401S") MT401T = rack_1.channel_data(group_names_1[0], "MT401T") MT401J = rack_1.channel_data(group_names_1[0], "MT401J") MV101F = rack_1.channel_data(group_names_1[0], "MV101F") TTCEMEDIA = rack_1.channel_data(group_names_1[0], "TTCEMEDIA") # Synchronize relative times from both racks Time_1 = rack_1.channel_data(group_names_1[0], "System Time")
for i in range(nbSubjects): #looping through each subject subjectNo = str(i + 1).zfill(2) dir = 'V:\\S%s\\BBGraphData_postStim' % subjectNo #navigate to subject-specific folder filepath = list_files(dir, exact_file) tdms_file = TdmsFile(filepath) #opens the data from the tdms file data = deque( ) #another deque, this time for storing subject-specific data for parcel in range(nbParcels): #looping through each parcel real_values = [ ] #empty list that will contain the real values of a parcel for sample in range( nbTimeSamples): #going through each time sample #the real value for that time point is appended to the list real_values.append( tdms_file.channel_data( str(sample), 'Vertex Data (CSGreim)__real')[parcel]) data.append( real_values ) #the real values for the parcel are appended to the deque object a.append(data) #the real values for the subject are appended #side note: its important that the the order in which the values are appended to the deque object #is correct as that will determine the dimensions of the numpy array that's created. In this #case we first appended from each time point, then each parcel, then each subject. a = np.array( a, dtype=float ) #we convert it to a numpy array. dtype is float(64) for greater precision final = condition[0] + '_collapsed' #should match directory np.save(final, a) #the array is saved in the current working directory
def getPoiData(self, data_directory, poi_series_number, pmt=1): poi_name = 'points' + ('0000' + str(poi_series_number))[-4:] full_file_path = os.path.join(data_directory, 'points', poi_name, poi_name + '_pmt' + str(pmt) + '.tdms') if os.path.exists(full_file_path): tdms_file = TdmsFile(full_file_path) time_points = tdms_file.channel_data( 'PMT' + str(pmt), 'POI time') / 1e3 # msec -> sec poi_data_matrix = np.ndarray( shape=(len(tdms_file.group_channels('PMT' + str(pmt))[1:]), len(time_points))) poi_data_matrix[:] = np.nan for poi_ind in range( len(tdms_file.group_channels('PMT' + str(pmt))[1:]) ): # first object is time points. Subsequent for POIs poi_data_matrix[poi_ind, :] = tdms_file.channel_data( 'PMT' + str(pmt), 'POI ' + str(poi_ind) + ' ') # get poi locations in raw coordinates: poi_x = [ int(v) for v in tdms_file.channel_data('parameter', 'parameter')[21:] ] poi_y = [ int(v) for v in tdms_file.channel_data('parameter', 'value')[21:] ] poi_xy = np.array(list(zip(poi_x, poi_y))) # get Snap image and poi locations in snap coordinates metadata = self.getMetaData(data_directory, poi_series_number, 'poi') snap_name = metadata['Image']['name'].replace('"', '') snap_ct = 0 while ( ('points' in snap_name) and (snap_ct < 1000)): # used snap image from a previous POI scan snap_ct += 1 alt_dict = self.getMetaData(data_directory, int(snap_name[6:]), 'poi') temp_image = alt_dict.get('Image') if temp_image is not None: snap_name = temp_image['name'].replace('"', '') snap_image, snap_settings, poi_locations = self.getSnapImage( data_directory, snap_name, poi_xy, pmt=1) results_dict = { 'time_points': time_points, 'poi_data_matrix': poi_data_matrix, 'poi_xy': poi_xy, 'snap_image': snap_image, 'snap_settings': snap_settings, 'poi_locations': poi_locations } else: results_dict = {} return results_dict
def read2DSweep(filepattern, group, xChan, yChan, signalChan, xGroup=None, yGroup=None, nXPoints=None, nYPoints=None, tdms_file=None): """ Read 2D data from TDMS file, guesses number of points if possible. The re- turned data can be directly plotted with imshow() of guiqwt or matplotlib. The function assumes that x and y sweep values are the same for every scan. Therefore only vectors for x and y are returned, while the returned signal is an [len(x) x len(y)] array. Parameters ---------- filepattern : string filename (relative) of tdms file. may include wilcards (see glob) group : string datagroup name in tdms file xChan : string x channel name in tdms file xGroup : string, optional if set, the xdata is read from this group (prepend "Read." if neccessary) yChan : string y channel name in tdms file yGroup : string, optional if set, y data is read from this group (prepend "Read." if neccessary) signalChan : string signal channel name. signal has to come in len(x) blocks of len(y) entries nXPoints : integer, optional number of expected points in x direction, gets calculated if not specified nYPoints : integer, optional number of expected points in y direction, gets calculated if not specified tdms_file : TdmsFile, optional If set, file with filepattern is ignored and data from tdms_file is used Returns ------- vX : dim. (nXPoints x 1) numpy array x values vY : dim. (nYPoints x 1) numpy array y values aSignal : (nXPoints x nYPoints) numpy array signal as matrix tdms_file : TdmsFile TdmsFile object of read tdms. """ # Load data from TDMS file if not tdms_file: files = glob(filepattern) if len(files) > 1: l.warn("Provided file pattern %s did return multiple results."%filepattern) if len(files) < 1: l.warn("Provided file pattern %s did not match any files."%filepattern) l.info("Reading file %s"%files[0]) tdms_file = TdmsFile(files[0]) x = tdms_file.channel_data(xGroup or group, xChan) y = tdms_file.channel_data(yGroup or group, yChan) signal = tdms_file.channel_data(group, signalChan) return list(reshape(x, y, signal, nXPoints, nYPoints)) + [tdms_file]