Exemple #1
0
    def test_can_write_numpy_timestamp_data_with_dates(self):
        tzinfo = None
        if pytz:
            tzinfo = pytz.utc
        input_data = np.array([
            '2017-07-09',
            '2017-07-09',
            '2017-07-09'], dtype='datetime64')

        segment = ChannelObject("group", "timedata", input_data)

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([segment])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        output_data = tdms_file.object("group", "timedata").data

        self.assertEqual(len(output_data), 3)
        self.assertEqual(
            output_data[0], datetime(2017, 7, 9, 0, 0, 0, 0, tzinfo))
        self.assertEqual(
            output_data[1], datetime(2017, 7, 9, 0, 0, 0, 0, tzinfo))
        self.assertEqual(
            output_data[2], datetime(2017, 7, 9, 0, 0, 0, 0, tzinfo))
Exemple #2
0
    def test_can_read_tdms_file_properties_after_writing(self):
        test_time = datetime.utcnow()
        if pytz:
            test_time = test_time.replace(tzinfo=pytz.utc)

        a_segment = RootObject(properties={
            "prop1": "foo",
            "prop2": 3,
        })
        b_segment = GroupObject("group_name", properties={
            "prop3": 1.2345,
            "prop4": test_time,
        })

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([a_segment, b_segment])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        a_output = tdms_file.object()
        b_output = tdms_file.object("group_name")

        self.assertTrue("prop1" in a_output.properties, msg="prop1 not found")
        self.assertTrue("prop2" in a_output.properties, msg="prop2 not found")
        self.assertTrue("prop3" in b_output.properties, msg="prop3 not found")
        self.assertTrue("prop4" in b_output.properties, msg="prop4 not found")
        self.assertEqual(a_output.properties["prop1"], "foo")
        self.assertEqual(a_output.properties["prop2"], 3)
        self.assertEqual(b_output.properties["prop3"], 1.2345)
        self.assertEqual(b_output.properties["prop4"], test_time)
Exemple #3
0
    def test_can_append_to_file_using_path(self):
        input_1 = np.linspace(0.0, 1.0, 10)
        input_2 = np.linspace(1.0, 2.0, 10)
        segment_1 = ChannelObject("group", "a", input_1)
        segment_2 = ChannelObject("group", "a", input_2)

        tempdir = tempfile.mkdtemp()
        temppath = "%s/test_file.tdms" % tempdir
        try:
            with TdmsWriter(temppath) as tdms_writer:
                tdms_writer.write_segment([segment_1])
            with TdmsWriter(temppath, 'a') as tdms_writer:
                tdms_writer.write_segment([segment_2])

            tdms_file = TdmsFile(temppath)

            output = tdms_file.object("group", "a").data

            self.assertEqual(len(output), 20)
            np.testing.assert_almost_equal(
                output, np.concatenate([input_1, input_2]))

        finally:
            if os.path.exists(temppath):
                os.remove(temppath)
            os.rmdir(tempdir)
Exemple #4
0
    def test_can_write_timestamp_data(self):
        tzinfo = None
        if pytz:
            tzinfo = pytz.utc
        input_data = [
            datetime(2017, 7, 9, 12, 35, 0, 0, tzinfo),
            datetime(2017, 7, 9, 12, 36, 0, 0, tzinfo),
            datetime(2017, 7, 9, 12, 37, 0, 0, tzinfo),
            ]

        segment = ChannelObject("group", "timedata", input_data)

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([segment])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        output_data = tdms_file.object("group", "timedata").data

        self.assertEqual(len(output_data), 3)
        self.assertEqual(output_data[0], input_data[0])
        self.assertEqual(output_data[1], input_data[1])
        self.assertEqual(output_data[2], input_data[2])
Exemple #5
0
def import_tdmsfile_to_tempodb(file_path, series_key_base=None):
    # Parse the TDMS file and get a handle to the object
    tdmsfile = TdmsFile(file_path)

    # Logging options
    show_properties = True
    show_data = False
    show_time = False
    import_data = True

    count = 0
    level = 0
    root = tdmsfile.object()
    display('/', level)
    if show_properties:
        display_properties(root, level)
    for group in tdmsfile.groups():
        level = 1
        group_obj = tdmsfile.object(group)
        display("%s" % group_obj.path, level)
        if show_properties:
            display_properties(group_obj, level)
        for channel in tdmsfile.group_channels(group):
            level = 2
            display("%s" % channel.path, level)
            if show_properties:
                level = 3
                display("data type: %s" % channel.data_type.name, level)
                display_properties(channel, level)

            if show_data:
                level = 3
                data = channel.data
                display("data: %s" % data, level)

            if show_time:
                level = 3
                time = channel.time_track()
                display("time: %s" % time, level)

            if import_data:
                level = 3
                try:
                    if series_key_base:
                        series_key = "%s-%i" % (series_key_base, count)
                        count += 1
                        # "Paul-Python-TDMS-1"
                    else:
                        # series_key_base = "%s-%s-%s" % os.path.basename(os.path.splitext(file_path))[0], group_obj.
                        series_key = channel.path

                    import_channel_to_tempodb(channel, series_key)

                except KeyError as ke:
                    display("There is no embedded time data in this channel.", level)
                    print ke
            print
        print
 def load_tdms(self):
     """ Looks for .tdms files in folder and loads the first one
     it finds
     """
     for fname in os.listdir(self.folder):
         ext = os.path.splitext(fname)[1]            
         if ext=='.tdms': self.tdms = TdmsFile(self.folder+fname)
     if not self.tdms: print 'Tdms file not found' 
Exemple #7
0
        def _load(self, filename=None, *args, **kargs):
            """TDMS file loader routine.

            Args:
                filename (string or bool): File to load. If None then the existing filename is used,
                    if False, then a file dialog will be used.

            Returns:
                A copy of the itself after loading the data.
            """
            if filename is None or not filename:
                self.get_filename("r")
            else:
                self.filename = filename
            # Open the file and read the main file header and unpack into a dict
            try:
                f = TdmsFile(self.filename)

                column_headers = []
                data = np.array([])

                for grp in f.objects.keys():
                    if grp == "/":
                        pass  # skip the rooot group
                    elif grp == "/'TDI Format 1.5'":
                        metadata = f.object("TDI Format 1.5")
                        for k, v in metadata.properties.items():
                            self.metadata[k] = self.metadata.string_to_type(str(v))
                    else:
                        if f.objects[grp].has_data:
                            chnl = grp.split("/")[-1]
                            chnl.strip().strip("'")
                            column_headers.append(chnl)
                            if data.size == 0:
                                data = f.objects[grp].data
                            else:
                                data = np.column_stack([data, f.objects[grp].data])
                self.data = data
                self.column_headers = column_headers
            except Exception:
                from traceback import format_exc

                raise Core.StonerLoadError("Not a TDMS File \n{}".format(format_exc()))

            return self
def convert_tdms(fileName,tempo,env):
    if tempo:    
        time.sleep(20)    
    path=env.path
    tdms_file=TdmsFile(os.path.join(path,fileName+'.tdms'))
   # tdms_file=TdmsFile(r'D:\DATA\00838_Data.tdms')
    hdf5=h5py.File(path+os.sep+fileName+'.h5','w')
    #channel=tdms_file.object('PXI M6251','Lang_U')
    #group=tdms_file.object('PXI M6251')
    grouplist=tdms_file.groups()
    #print grouplist
    for i in grouplist:
        group=tdms_file.object(i)
        grouph=hdf5.create_group(i)
        print group.path
        if group.path=='/\'PXI M6251\'':
            nbchannels=group.properties['Nchannel']
            tstart=group.properties['Tstart']
            sampling=group.properties['SampleTime']
        if group.path=='/\'Tektronix\'':    
            tstart=group.properties['Tstart']
            #sampling=group.properties['SampleTime']
            sampling=1/1.25e9
            nbchannels=group.properties['Nchannel']
        if group.path=='/\'S7\'':
            nbchannels=group.properties['Nchannel']
            tstart=0.
            sampling=1.
        #print nbchannels,tstart,sampling
        grouph.attrs['Nchannel']=nbchannels
        grouph.attrs['Tstart']=tstart
        grouph.attrs['sampling']=1/float(sampling)
        liste=tdms_file.group_channels(i)
    
        for j in liste:
            grouph.create_dataset(re.sub('[\']','',j.path),data=j.data,compression="gzip")
            
#    conn=sqlite3.connect('ishtar')
#    curs=conn.cursor()
#    curs.execute('insert into shots values(?,?,?,?,?)',(int(fileName[0:-5]),fileName,0.,0.,0.))
#    conn.commit()
    hdf5.create_group('Process')
    hdf5.close()
    env.process.addFile(fileName)
Exemple #9
0
def test_indexing_channel_after_read_data():
    """ Test indexing into a channel after reading all data
    """
    test_file, expected_data = scenarios.chunked_segment().values
    with test_file.get_tempfile() as temp_file:
        tdms_file = TdmsFile.read(temp_file.file)
    for ((group, channel), expected_channel_data) in expected_data.items():
        channel_object = tdms_file[group][channel]
        assert channel_object[0] == expected_channel_data[0]
        compare_arrays(channel_object[:], expected_channel_data)
Exemple #10
0
    def test_can_write_string_data(self):
        input_data = [
            "hello world",
            u"\u3053\u3093\u306b\u3061\u306f\u4e16\u754c"]

        segment = ChannelObject("group", "string_data", input_data)

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([segment])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        output_data = tdms_file.object("group", "string_data").data

        self.assertEqual(len(output_data), 2)
        self.assertEqual(output_data[0], input_data[0])
        self.assertEqual(output_data[1], input_data[1])
Exemple #11
0
def test_indexing_channel_with_integer(index):
    """ Test indexing into a channel with an integer index
    """
    test_file, expected_data = scenarios.chunked_segment().values
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel),
                 expected_channel_data) in expected_data.items():
                channel_object = tdms_file[group][channel]
                assert channel_object[index] == expected_channel_data[index]
def read_cal_TDMS(tdms_filename, porttype='2-port'):
	from nptdms import TdmsFile
	tdms_file = TdmsFile(tdms_filename)

	## list groups
	#print(tdms_file.groups())

	## scan properties
	#print(tdms_file.object('Parameters').properties)

	## 2-port data
	twoport = tdms_file.group_channels('2-port')

	cal_paramsR = {}
	cal_paramsI = {}
	for idx in range(0,len(twoport)):
		tmpvar = twoport[idx]
		path = [item.replace('\'', '') for item in tmpvar.path.split('/') if item!='']
		name = path[len(path)-1].split('_')
		term = name[0]
		realORimag = name[1]

		values = tmpvar.data
		bins = tmpvar.time_track()

		if realORimag=='X':
			cal_paramsR[term] = values
		elif realORimag=='Y':
			cal_paramsI[term] = values
		else:
			raise('ParseNameError')
		#
	#

	cal_params = {}
	for each in cal_paramsR.keys():
		cal_params[each] = cal_paramsR[each] + 1j*cal_paramsI[each]
	#

	#from collections import namedtuple
	#return namedtuple('GenericDict', cal_params.keys())(**cal_params)

	return cal_params
    def _init_data_with_tdms(self, tdms_filename):
        """Initializes the current RT-DC dataset with a tdms file.
        """
        tdms_file = TdmsFile(str(tdms_filename))
        # time is always there
        table = "Cell Track"
        # Edit naming.dclab2tdms to add features
        for arg in naming.tdms2dclab:
            try:
                data = tdms_file.object(table, arg).data
            except KeyError:
                pass
            else:
                if data is None or len(data) == 0:
                    # Ignore empty features. npTDMS treats empty
                    # features in the following way:
                    # - in nptdms 0.8.2, `data` is `None`
                    # - in nptdms 0.9.0, `data` is an array of length 0
                    continue
                self._events[naming.tdms2dclab[arg]] = data

        # Set up configuration
        tdms_config = Configuration(
            files=[self.path.with_name(self._mid + "_para.ini"),
                   self.path.with_name(self._mid + "_camera.ini")],
        )
        dclab_config = Configuration()
        for section in naming.configmap:
            for pname in naming.configmap[section]:
                meta = naming.configmap[section][pname]
                typ = dfn.config_funcs[section][pname]
                if isinstance(meta, tuple):
                    osec, opar = meta
                    if osec in tdms_config and opar in tdms_config[osec]:
                        val = tdms_config[osec].pop(opar)
                        dclab_config[section][pname] = typ(val)
                else:
                    dclab_config[section][pname] = typ(meta)

        self.config = dclab_config
        self._complete_config_tdms(tdms_config)

        self._init_filters()
Exemple #14
0
 def __init__(self, fname):
     tdms_file = TdmsFile(fname)["main"]
     t0 = tdms_file["t0"][:]
     t0 -= t0[0]
     t0 /= 1e6
     self.tdms_file = tdms_file
     self.t0 = t0
     self.Nrecords = len(self.t0)
     self.colletion_name = "Collection channel is not set!"
     self.collection = []
Exemple #15
0
 def _parseFile(self):
     if self.filename.lower().endswith('.tdms'):
         tdms = TdmsFile(self.filename)
         self.time = {}
         self.data = {}
         self.groups = tdms.groups()
         self.channels = {}
         for g in self.groups:
             self.time[g] = {}
             self.data[g] = {}
             self.channels[g] = tdms.group_channels(g)
             for c in self.channels[g]:
                 if c.has_data:
                     props = c.properties
                     self.time[g][props["NI_ChannelName"]] = c.time_track()
                     self.data[g][props["NI_ChannelName"]] = c.data
     elif self.filename.lower().endswith('.txt'):
         fid = open(self.filename, "r")
         if "<Mach-1 File>" in fid.readline():
             contents = fid.readlines()
             fid.close()
             self.time = OrderedDict()
             self.data = OrderedDict()
             self.channels = OrderedDict()
             info_blocks = [i for i, j in izip(count(), contents) if "<INFO>" in j or "<END INFO>" in j]
             info_blocks = izip(islice(info_blocks, 0, None, 2), islice(info_blocks, 1, None, 2))
             data_blocks = [i for i, j in izip(count(), contents) if "<DATA>" in j or "<END DATA>" in j]
             data_blocks = izip(islice(data_blocks, 0, None, 2), islice(data_blocks, 1, None, 2))
             self.groups = range(1, len(list(info_blocks))+1)
             for i, ind in enumerate(data_blocks):
                 g = self.groups[i]
                 header = contents[ind[0]+1].rstrip("\r\n").split("\t")
                 self.channels[g] = header
                 data = contents[ind[0]+2:ind[1]]
                 for j, d in enumerate(data):
                     data[j] = d.rstrip("\r\n").split("\t")
                 data = np.array(data, float)
                 self.time[g] = OrderedDict()
                 self.data[g] = OrderedDict()
                 for j, c in enumerate(self.channels[g][1:]):
                     self.time[g][c] = data[:, 0]
                     self.data[g][c] = data[:, j+1]
Exemple #16
0
    def load_sdata(self, del_data=True, skip_cal=False):
        if (del_data == True):
            self.delete_data()

        self.prop['fns'] = filedialog.askopenfilenames(
            filetypes=[("TDMS", "*.tdms")],
            title='open s-polarized excitation data')
        self.prop['Nf'] = len(self.prop['fns'])

        self.prop['PxCols'] = list(
            tdms(self.prop['fns'][0]).properties.items())[11][1]
        self.prop['PxRows'] = list(
            tdms(self.prop['fns'][0]).properties.items())[12][1]

        self.sIs = np.zeros(
            (self.prop['PxCols'], self.prop['PxRows'], self.prop['Nf']))
        self.sIp = np.zeros(
            (self.prop['PxCols'], self.prop['PxRows'], self.prop['Nf']))

        for i in range(self.prop['Nf']):
            sDat = tdms.read(self.prop['fns'][i]).groups()[0].channels(
            )[1].data  #channel 2 is PBS reflection path -> s-polarized light
            pDat = tdms.read(self.prop['fns'][i]).groups()[0].channels(
            )[2].data  #channel 3 is PBS transmission path -> p-polarized light

            for j in range(self.prop['PxCols']):
                self.sIs[j, :,
                         i] = sDat[(j *
                                    self.prop['PxRows']):((j + 1) *
                                                          self.prop['PxRows'])]
                self.sIp[j, :,
                         i] = pDat[(j *
                                    self.prop['PxRows']):((j + 1) *
                                                          self.prop['PxRows'])]

        print(np.mean(self.sIp))
        print(np.mean(self.sIs))

        # beware of doulbe processing when using calibrate function
        if (skip_cal == False):
            self.axelrod()
            self.s_sensitivity(missing=False)
    def parameterSearch(self,TDMSPath):
        config = ConfigParser.ConfigParser()
        config.read(u'config.txt')
        
        #This section reads the needed information from the config file 
        startFreq = config.getint(u'Frequencies',u'Start Frequency')
        stopFreq = config.getint(u'Frequencies',u'Stop Frequency')
        stepFreq = config.getint(u'Frequencies',u'Step Frequency')
        NUM_POINTS = config.getint(u'Symbolic Constants',u'Num Points')
        JUMP_BACK = config.getint(u'Symbolic Constants',u'Jump Back')
        
        TDMS_Time = []
        TDMS_Data = []
        TDMSfiles = os.listdir(TDMSPath)
        TDMSfiles = [file for file in TDMSfiles if file.endswith(u'.tdms')]
        for file in TDMSfiles: #Loop through all TDMS files in order to get the data within each
            path = TDMSPath + u'/' + file
            TDMS = TdmsFile(path) #Function that reads the specific TDMS file
            group = TDMS.groups()[0]
            channel = TDMS.object(group, u'Dev1/ai0') #returns a channel type
            data = channel.data
            time = channel.time_track()
            
            #Determining starting point to read file
            #if highest point is more than JUMP_BACK points into the data, start there
            if numpy.argmax(data)>JUMP_BACK:
                start = numpy.argmax(data)-JUMP_BACK
            else: start = 0 #otherwise start at the beginning of the file

            t = time[start:start+NUM_POINTS]  #time information from the start point to the end point
            s = data[start:start+NUM_POINTS] #data from the start point to the end point

            TDMS_Time.append(t) #add the TDMS files data to the set of all TDMS files data
            TDMS_Data.append(s)

        #Now that the data has all been found it can be set using the InputData class

        InputData.Set_Start_Freq(startFreq)
        InputData.Set_Stop_Freq(stopFreq)
        InputData.Set_Step_Freq(stepFreq)
        InputData.Set_TDMS_Time(TDMS_Time)
        InputData.Set_TDMS_Data(TDMS_Data)
Exemple #18
0
def tdms2csv(tdmsFolderName):
    tdmsFiles = []
    tdmsFolderNameFull = originTDMSFolder + '/' + tdmsFolderName + '/data'

    if not os.path.exists(tdmsFolderNameFull):
        return

    for file in os.listdir(tdmsFolderNameFull):
        if file.endswith(".tdms"):
            tdmsFiles += [file]
    #%% Check if the destination foldername exists
    destinationFolderName = destinationCSVFolder + '/' + tdmsFolderName

    if not os.path.exists(destinationFolderName):
        os.makedirs(destinationFolderName)

    #%%
    directoryIndex = 1
    for file in tdmsFiles:
        print(file)
        tdms_file = TdmsFile(tdmsFolderNameFull + '/' + file)

        directory = destinationCSVFolder + '/' + tdmsFolderName + '/data_' + str(
            directoryIndex)
        directoryIndex += 1

        if not os.path.exists(directory):
            os.makedirs(directory)

        for channelName in channelNames:
            channel = tdms_file.object('data', channelName)
            data = channel.data
            s = channel.property('wf_start_time')
            samplingIncrement = channel.property('wf_increment')

            np.savetxt(directory + '/' + channelName + '.csv',
                       data,
                       delimiter=',')
            np.savetxt(directory + '/timingMetaData.csv', [[
                s.hour, s.minute, s.second, s.microsecond, samplingIncrement
            ]],
                       delimiter=',')
def signal_plot(PDname=None, method='diff'):
    if PDname is None:
        PDname = FindFile('Photodiode')
    PDfile = TdmsFile(PDname)
    PDdata = PDfile.as_dataframe(time_index=True, absolute_time=False)
    num_tests = int(len(PDdata.columns) / 4)
    plot_num = test_enter(num_tests)
    new_data = PDdata[PDdata.columns[plot_num * 4:(plot_num * 4) + 4]]

    new_data.columns = [
        'Test {0} '.format(plot_num) + 'PD1',
        'Test {0} '.format(plot_num) + 'PD2',
        'Test {0} '.format(plot_num) + 'PD3',
        'Test {0} '.format(plot_num) + 'PD4'
    ]

    new_data.index.name = 'Time (s)'
    # Determine the values of the 1st finite difference
    diff_val = new_data.diff()
    # Plot the desired signals
    new_data.plot(linewidth=3)
    # Plot locations of the maximum values of the signals
    plt.plot(new_data.idxmax(),
             new_data.max(),
             marker='o',
             linestyle='None',
             markersize=8,
             label='Max',
             color='black',
             markerfacecolor='black')
    # Plot locations of the maximum values of the 1st finite difference
    plt.plot(diff_val.idxmax(),
             np.diag(new_data.loc[diff_val.idxmax(), new_data.columns]),
             marker='s',
             linestyle='None',
             markersize=8,
             label='Grad',
             color='black')
    plt.legend()
    plt.show()
    #    PDdata.plot(y=list(PDdata.columns[plot_num*4:(plot_num*4)+4]))
    return new_data
def test_stream_interleaved_data_channel(benchmark):
    """ Benchmark streaming channel data from an interleaved data file
    """
    with TdmsFile.open(
            get_interleaved_file().get_bytes_io_file()) as tdms_file:
        channel = tdms_file['group']['channel3']
        channel_data = benchmark(stream_chunks, channel)

        channel_data = np.concatenate(channel_data)
        expected_data = np.repeat([3], 10000)
        np.testing.assert_equal(channel_data, expected_data)
def test_index_interleaved_data_channel(benchmark):
    """ Benchmark reading a data from a interleaved data file using integer indices
    """
    with TdmsFile.open(
            get_interleaved_file().get_bytes_io_file()) as tdms_file:
        channel = tdms_file['group']['channel3']
        channel_data = np.zeros(10000, dtype=channel.dtype)
        benchmark(index_values, channel, channel_data)

        expected_data = np.repeat([3], 10000)
        np.testing.assert_equal(channel_data, expected_data)
Exemple #22
0
def f_open_tdms(filename, channel):
    if filename == 'Input':
        filename = filedialog.askopenfilename()

    file = TdmsFile.read(filename)
    all_groups = file.groups()
    group = all_groups[0]
    data_channel = group[channel]
    data = data_channel[:]

    return data
Exemple #23
0
def test_read_data_after_close_throws():
    """ Trying to read after opening and closing without reading data should throw
    """
    test_file, expected_data = scenarios.single_segment_with_one_channel().values
    group, channel = list(expected_data.keys())[0]
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            pass
        with pytest.raises(RuntimeError) as exc_info:
            tdms_file[group][channel].read_data()
        assert "Cannot read data after the underlying TDMS reader is closed" in str(exc_info.value)
Exemple #24
0
def test_indexing_channel_with_invalid_type_raises_error(index):
    """ Test indexing into a channel with an invalid index type
    """
    test_file, expected_data = scenarios.chunked_segment().values
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel), expected_channel_data) in expected_data.items():
                channel_object = tdms_file[group][channel]
                with pytest.raises(TypeError) as exc_info:
                    _ = channel_object[index]
                assert "Invalid index type" in str(exc_info.value)
Exemple #25
0
def test_indexing_channel_with_zero_step_raises_error():
    """ Test indexing into a channel with a slice with zero step size raises an error
    """
    test_file, expected_data = scenarios.chunked_segment().values
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel), expected_channel_data) in expected_data.items():
                channel_object = tdms_file[group][channel]
                with pytest.raises(ValueError) as exc_info:
                    _ = channel_object[::0]
                assert str(exc_info.value) == "Step size cannot be zero"
Exemple #26
0
def test_stream_channel_data_chunks(test_file, expected_data):
    """Test streaming chunks of data for a single channel from a TDMS file
    """
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel), expected_channel_data) in expected_data.items():
                actual_data = []
                for chunk in tdms_file[group][channel].data_chunks():
                    assert chunk.offset == len(actual_data)
                    actual_data.extend(chunk[:])
                compare_arrays(actual_data, expected_channel_data)
Exemple #27
0
    def test_can_write_multiple_segments(self):
        input_1 = np.linspace(0.0, 1.0, 10)
        input_2 = np.linspace(2.0, 3.0, 10)

        segment_1 = ChannelObject("group", "a", input_1)
        segment_2 = ChannelObject("group", "a", input_2)

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([segment_1])
            tdms_writer.write_segment([segment_2])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        output_data = tdms_file.object("group", "a").data

        expected_data = np.append(input_1, input_2)
        self.assertEqual(len(output_data), len(expected_data))
        self.assertTrue((output_data == expected_data).all())
Exemple #28
0
def test_lazily_read_raw_channel_data():
    """Test reading raw channel data lazily"""

    test_file, expected_data = scenarios.single_segment_with_one_channel(
    ).values
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel), expected_data) in expected_data.items():
                actual_data = tdms_file[group][channel].read_data(scaled=False)
                assert actual_data.dtype == expected_data.dtype
                compare_arrays(actual_data, expected_data)
Exemple #29
0
def test_lazily_read_channel_data_with_channel_data_method():
    """Test reading channel data lazily using the channel_data method of TdmsFile
    """
    test_file, expected_data = scenarios.single_segment_with_two_channels(
    ).values
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel), expected_data) in expected_data.items():
                actual_data = tdms_file.channel_data(group, channel)
                assert actual_data.dtype == expected_data.dtype
                np.testing.assert_almost_equal(actual_data, expected_data)
Exemple #30
0
def test_invalid_length_in_read_data_throws():
    """ Exception is thrown when reading a subset of data with an invalid length
    """
    test_file, expected_data = scenarios.single_segment_with_one_channel(
    ).values
    group, channel = list(expected_data.keys())[0]
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            with pytest.raises(ValueError) as exc_info:
                tdms_file[group][channel].read_data(0, -5)
            assert "length must be non-negative" in str(exc_info.value)
Exemple #31
0
    def test_can_write_multiple_segments(self):
        input_1 = np.linspace(0.0, 1.0, 10)
        input_2 = np.linspace(2.0, 3.0, 10)

        segment_1 = ChannelObject("group", "a", input_1)
        segment_2 = ChannelObject("group", "a", input_2)

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([segment_1])
            tdms_writer.write_segment([segment_2])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        output_data = tdms_file.object("group", "a").data

        expected_data = np.append(input_1, input_2)
        self.assertEqual(len(output_data), len(expected_data))
        self.assertTrue((output_data == expected_data).all())
Exemple #32
0
def test_indexing_channel_with_invalid_integer_raises_error(index):
    """ Test indexing into a channel with an invalid integer index
    """
    test_file, expected_data = scenarios.chunked_segment().values
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel),
                 expected_channel_data) in expected_data.items():
                channel_object = tdms_file[group][channel]
                with pytest.raises(IndexError):
                    _ = channel_object[index]
Exemple #33
0
def read_noise_tdms(filename, sampleRate=51200):

    # read metadata and TDMS channel data
    noisedata = TdmsFile(filename)

    # parse start time of recording
    # strip off folders
    startTime = filename.split("/")[-1].split("_")[:2]
    # join date (YYYYMMDD) to time (HHMMSS)
    startTime = "_".join(startTime)

    # convert tdms data to pd.DataFrame
    noisedf = noisedata.as_dataframe()

    # Make time index
    noisedf.index = pd.TimedeltaIndex(data=1 / sampleRate *
                                      np.arange(len(noisedf)),
                                      unit="s")

    return noisedf, startTime
Exemple #34
0
def tdms_extract(entry):
    try:
        if (entry.endswith('.tdms')):
            tdms_file = TdmsFile(entry)
            for group in tdms_file.groups():
                data = tdms_file.object(group).as_dataframe()
                if group == 'Events':
                    data["TimeStamp"] = data["TimeStamp"].astype(
                        float).values - float(data["TimeStamp"].values[0])
                if group == 'Channels':
                    data["TimeStamp (sec)"] = data[
                        "TimeStamp (sec)"].values - data[
                            "TimeStamp (sec)"].values[0]
                if not os.path.exists('C:/Niagara/UEF/' + group):
                    os.makedirs('C:/Niagara/UEF/' + group)
                name = entry.split('\\')[-1]
                data.to_csv('C:/Niagara/UEF/' + group + "/" + name[:-5] +
                            ".csv")
    except Exception as e:
        pass
def testTdmsFile():
    tdms = TdmsFile(
        "/Volumes/RAID-0/LockheedMartin/TDMS_200120_12-40_2020-01-20 ATRQ Build 2/Slice00122.tdms"
    )
    # tdms.as_hdf('/tmp/Slice00122.h5')

    properties = tdms.properties
    for property in properties:
        print(f'PROPERTY: {property} = {properties[property]}')

    objects = tdms.objects
    for obj in objects:
        print(f'OBJECT: {obj}')

    groups = tdms.groups()
    for part in groups:
        print(f'GROUP: {part}')
        # get the data from each group's channel and make a CSV
        channels = tdms.group_channels(part)

        # make a 2D array, and populate it with the arrays in this loop.
        groupCSV = []
        areaCol = []
        xCol = []
        yCol = []
        paramCol = []
        intensityCol = []
        laserCol = []
        csvCount = 0
        # copy each channel's data to its respective frame

        for channel in channels:
            print(f'  CHANNEL: {channel}')
            names = []
            for i in channels:
                wordList = str(i).split("/")
                name = wordList[-1]
                name = name.strip(">")
                name = name.strip("'")
                names.append(name)
            colNames = names
Exemple #36
0
def test_read_data_after_open_in_read_mode_throws():
    """ Trying to read channel data after reading all data initially should throw
    """
    test_file, expected_data = scenarios.single_segment_with_one_channel(
    ).values
    group, channel = list(expected_data.keys())[0]
    with test_file.get_tempfile() as temp_file:
        tdms_file = TdmsFile.read(temp_file.file)
        with pytest.raises(RuntimeError) as exc_info:
            tdms_file[group][channel].read_data()
        assert "Cannot read data after the underlying TDMS reader is closed" in str(
            exc_info.value)
Exemple #37
0
def test_read_channel_data(test_file, expected_data):
    """Test reading data"""

    with test_file.get_tempfile() as temp_file:
        tdms_data = TdmsFile.read(temp_file.file)

    for ((group, channel), expected_data) in expected_data.items():
        channel_obj = tdms_data[group][channel]
        actual_data = channel_obj.data
        assert actual_data.dtype == expected_data.dtype
        assert channel_obj.dtype == expected_data.dtype
        compare_arrays(actual_data, expected_data)
Exemple #38
0
def test_iterate_channel_data_in_read_mode():
    """Test iterating over channel data after reading all data
    """
    test_file, expected_data = scenarios.chunked_segment().values

    with test_file.get_tempfile() as temp_file:
        tdms_file = TdmsFile.read(temp_file.file)
        for ((group, channel), expected_channel_data) in expected_data.items():
            actual_data = []
            for value in tdms_file[group][channel]:
                actual_data.append(value)
            compare_arrays(actual_data, expected_channel_data)
Exemple #39
0
def test_reading_subset_of_data_for_scenario(test_file, expected_data, offset,
                                             length):
    """Test reading a subset of a channel's data
    """
    assume(any(offset <= len(d) for d in expected_data.values()))
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel), expected_data) in expected_data.items():
                actual_data = tdms_file[group][channel].read_data(
                    offset, length)
                compare_arrays(actual_data,
                               expected_data[offset:offset + length])
Exemple #40
0
def test_indexing_scaled_channel_with_integer():
    """ Test indexing into a channel with an integer index when the channel is scaled
    """
    test_file, expected_data = scenarios.scaled_data().values
    with test_file.get_tempfile() as temp_file:
        with TdmsFile.open(temp_file.file) as tdms_file:
            for ((group, channel), expected_channel_data) in expected_data.items():
                channel_object = tdms_file[group][channel]
                values = []
                for i in range(len(channel_object)):
                    values.append(channel_object[i])
                compare_arrays(values, expected_channel_data)
Exemple #41
0
    def read(self):
        # File information below
        tdms_file = TdmsFile(self.fname + '.tdms')  # Reads a tdms file.
        group_name = "Trap"  # Get the group name
        channels = tdms_file.group_channels(
            group_name)  # Get the channel object

        self.ch = np.zeros(
            (len(channels),
             self.N_total))  # Make a 2D array (ch, timetrace) for trap data
        for i, channel in enumerate(channels):
            self.ch[i, ] = channel.data[range(self.N_total)]

        self.QPDx = self.ch[0] - np.mean(self.ch[0])
        self.QPDy = self.ch[1] - np.mean(self.ch[1])
        self.QPDs = self.ch[2]
        self.PZTx = (self.ch[3] - np.mean(self.ch[3])) * PZT_nm2V[0]
        self.PZTy = (self.ch[4] - np.mean(self.ch[4])) * PZT_nm2V[1]

        self.QPDx = self.QPDx / self.QPDs
        self.QPDy = self.QPDy / self.QPDs
Exemple #42
0
    def test_can_read_tdms_file_after_writing(self):
        a_input = np.linspace(0.0, 1.0, 100)
        b_input = np.linspace(0.0, 100.0, 100)

        a_segment = ChannelObject("group", "a", a_input)
        b_segment = ChannelObject("group", "b", b_input)

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([a_segment, b_segment])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        a_output = tdms_file.object("group", "a").data
        b_output = tdms_file.object("group", "b").data

        self.assertEqual(len(a_output), len(a_input))
        self.assertEqual(len(b_output), len(b_input))
        self.assertTrue((a_output == a_input).all())
        self.assertTrue((b_output == b_input).all())
Exemple #43
0
    def test_can_read_tdms_file_after_writing(self):
        a_input = np.linspace(0.0, 1.0, 100)
        b_input = np.linspace(0.0, 100.0, 100)

        a_segment = ChannelObject("group", "a", a_input)
        b_segment = ChannelObject("group", "b", b_input)

        output_file = BytesIO()
        with TdmsWriter(output_file) as tdms_writer:
            tdms_writer.write_segment([a_segment, b_segment])

        output_file.seek(0)
        tdms_file = TdmsFile(output_file)

        a_output = tdms_file.object("group", "a").data
        b_output = tdms_file.object("group", "b").data

        self.assertEqual(len(a_output), len(a_input))
        self.assertEqual(len(b_output), len(b_input))
        self.assertTrue((a_output == a_input).all())
        self.assertTrue((b_output == b_input).all())
Exemple #44
0
def check_for_same_length(tdms_operator: nptdms.TdmsFile) -> either.Either:
    """Checks whether all relevant channels of the Tdms file have the same
    length.
    """
    array_lengths = [[
        len(channel) for channel in group.channels() if len(channel) > 0
    ] for group in tdms_operator.groups()]
    array_lengths = np.array(array_lengths).flatten()
    all_lengths_equal = len(set(array_lengths)) == 1
    if not all_lengths_equal:
        return either.Left(ErrorCode.LENGTHERROR)
    return either.Right(tdms_operator)
def TDMS_to_dict(TDMS_filename, input_chan, output_chan):
    """Convert raw data from a Labview VI into a dictionary of Python dictionaries
    Usage:
    TDMS_filename (string): full path to the input data filename
    input_chan (list of int): List of channel to be saved
    output_chan (list of strings): Identifier for each of the saved channels
    """

    tdms_file = TdmsFile(TDMS_filename)
    list_out = {}
    for idx in range(len(input_chan)):
        channel_name = 'Dev2/ai' + str(input_chan[idx])
        pdb.set_trace()
        channel = tdms_file.object('Untitled', channel_name)
        data = channel.data
        time = channel.time_track()
        sr = 1 / (time[1] - time[0])
        entry = {'data': data, 'time': time, 'sr': sr}
        list_out[output_chan[idx]] = entry

    return list_out
Exemple #46
0
def lade_tdms(par, typ):
    """
    :type par: Parameter.Parameter
    :type typ: str
    """
    daten = []
    namen = []

    # Dateinamen aufteilen und numerisch sortieren:
    sorted_fnames = sorted(
        glob(os.path.join(par.verzeichnis, typ + '*.tdms')),  # alle Dateien in diesem Ordner mit der Endung TDMS
        key=lambda x: int(x.split(os.sep)[-1].split(typ)[1].split('.')[0])  # Zeilennummer hinter dem Typnamen
    )

    # Offenbar das falsche Verzeichnis, wenn gar keine TDMS-Dateien gefunden wurden
    if len(sorted_fnames) == 0:
        raise Fehler(mw_tdms[lang])

    verbleibend = par.messpunkte
    for tdms_fname in sorted_fnames:
        if verbleibend != 0:
            verbleibend -= 1
            tdms_file = TdmsFile(tdms_fname)
            channel = tdms_file.object('Untitled', 'Untitled')  # erster Name ist der Gruppenname, dann der Kanalname
            daten.append(np.array(channel.data))
            namen.append(tdms_fname.split(os.sep)[-1])
        else:
            break

    # Es fehlen Messdaten, die letzte Zeile wird einfach vervielfacht
    if verbleibend > 0:
        print("Fehlende Messdaten (" + typ + ")")
        while verbleibend > 0:
            verbleibend -= 1
            daten.append(daten[-1])

    return daten, namen
    def load_trace(mname):
        """Loads the traces and returns them as a dictionary

        Currently, only loading traces from tdms files is supported.
        This forces us to load the full tdms file into memory which
        takes some time.
        """
        tname = TraceColumn.find_trace_file(mname)

        # Initialize empty trace dictionary
        trace = {}

        if tname is None:
            pass
        elif tname.suffix == ".tdms":
            # Again load the measurement tdms file.
            # This might increase memory usage, but it is cleaner
            # when looking at code structure.
            mdata = TdmsFile(str(mname))
            sampleids = mdata.object("Cell Track", "FL1index").data

            # Load the trace data. The traces file is usually larger than the
            # measurement file.
            tdata = TdmsFile(str(tname))
            for trace_key in dfn.FLUOR_TRACES:
                group, ch = naming.tr_data_map[trace_key]
                try:
                    trdat = tdata.object(group, ch).data
                except KeyError:
                    pass
                else:
                    if trdat is not None and trdat.size != 0:
                        # Only add trace if there is actual data.
                        # Split only needs the position of the sections,
                        # so we remove the first (0) index.
                        trace[trace_key] = np.split(trdat, sampleids[1:])
        return trace
Exemple #48
0
    def test_can_write_tdms_objects_read_from_file(self):
        group_segment = GroupObject("group", properties={
            "prop1": "bar"
        })
        input_data = np.linspace(0.0, 1.0, 10)
        channel_segment = ChannelObject("group", "a", input_data, properties={
            "prop1": "foo",
            "prop2": 3,
        })

        tempdir = tempfile.mkdtemp()
        temppath = "%s/test_file.tdms" % tempdir
        try:
            with TdmsWriter(temppath) as tdms_writer:
                tdms_writer.write_segment([group_segment, channel_segment])

            tdms_file = TdmsFile(temppath)
            read_group = tdms_file.object("group")
            read_channel = tdms_file.object("group", "a")

            with TdmsWriter(temppath) as tdms_writer:
                tdms_writer.write_segment([read_group, read_channel])

            tdms_file = TdmsFile(temppath)
            read_group = tdms_file.object("group")
            read_channel = tdms_file.object("group", "a")

            self.assertFalse(read_group.has_data)
            self.assertEqual(read_group.properties["prop1"], "bar")

            self.assertEqual(len(read_channel.data), 10)
            np.testing.assert_almost_equal(read_channel.data, input_data)
            self.assertEqual(read_channel.properties["prop1"], "foo")
            self.assertEqual(read_channel.properties["prop2"], 3)

        finally:
            if os.path.exists(temppath):
                os.remove(temppath)
            os.rmdir(tempdir)
Exemple #49
0
#import channel list

channels = pd.read_csv(info_dir+'Channels.csv', index_col = 'Channel')

helmet_channels = pd.read_csv(info_dir+'Helmet_Channels.csv', index_col = 'Channel')

#import test descriptions

test_des = pd.read_csv(info_dir+'Test_Descriptions.csv',index_col = 'Test Name')

events_dict = {}
data_dict = {}

for test in test_des.index.values:
	print(test)
	data_df = TdmsFile(data_dir+test+'/'+test+'.tdms').as_dataframe()
	events_df = data_df.iloc[:,-2:].dropna()
	events_df.columns = ['Event','Time']



	#Ignition is taken as the first event followin background
	ignition = events_df['Time'][int(test_des['Ignition Event Index'][test])].split('-')[-1]
	hh,mm,ss = ignition.split(':')
	ignition = 3600*int(hh) + 60*int(mm) + int(ss)
	event_times = []
	for time in events_df['Time']:
		timestamp = time.split('-')[-1]
		hh,mm,ss = timestamp.split(':')
		timestamp = 3600*int(hh)+60*int(mm)+int(ss)-int(ignition)
		event_times.append(timestamp)
import numpy as np
import pandas as pandas
# lampTrain1 = np.genfromtxt('/Users/Chryssia/Desktop/SchoolStuff/Gatech/ECE4011/SeniorDesign/Scikit/MachineLearningData/Lamp/LampAverage_1.csv',delimiter=' ',dtype=None)

# Item ID
# 1. No load
# 2. Lamp
# 3. Hair Drier
# 4. Small Blender
# 5. Big Blender
# 6. Hand Mixer
# 7. TV Box
# 8. Hair Drier and Lamp

# Lamp Training
lamp_tdms = TdmsFile("Lamp_001.tdms")
a = lamp_tdms.object("Untitled","Untitled 5")

x_train=np.array([a.data])
y_train = np.array(['2'])
for x in range(1, 10):
    a = lamp_tdms.object("Untitled","Untitled "+str(x*5))
   
    np1=np.array([a.data])
    x_train = np.concatenate((x_train,np1),axis=0)
    y_train = np.concatenate((y_train,[2]),axis=0)

# Hair Drier Training
hairDrier_tdms = TdmsFile("HairDyer_001.tdms")
for x in range(1, 11):
    a = hairDrier_tdms.object("Untitled","Untitled "+str(x*5))
Exemple #51
0

def Reduce_data(data_X, data_Y, x1):
    Mx = np.column_stack((data_X[:, np.newaxis], data_Y[:, np.newaxis]))
    M2 = Red.rdp(Mx, x1)
    data_X2 = M2[:, 0].transpose()
    data_Y2 = M2[:, 1].transpose()
    return data_X2, data_Y2


def PandasResample(df, length):
    td = (df.index[-1] - df.index[0]) / (length - 1)
    return df.resample(td, how='mean').interpolate()  # Handle NaNs when upsampling


tdms_file = TdmsFile('data/10676_3531053.tdms')
data = tdms_file.as_dataframe()

channel = tdms_file.object("Unbenannt", "ProbenMitte")
# print channel.property('wf_increment')
# print channel.property('wf_start_time')
# print channel.time_track()

# kills alle leeren Spalten
data = data.dropna(axis=1, how='all')

# doppelte Spaltennamen aufloesen
name_col = []
for jh in data.columns:
    tdummy = jh.split('/')[-1]
    name_col.append(tdummy.replace("\'", ""))
# prop = ['C','C','C','B','B','B','A','A','A','A','A','A','B','B','B','C','C','C']

charts_to_skip = {10: ['Gas_A','Carbon_Monoxide_A'], 18:['13TC']}

experiment_info = pd.DataFrame({'Experiment':experiments,'Prop':prop})
experiment_info = experiment_info.set_index('Experiment')

for experiment in experiment_info.index:

	if experiment in charts_to_skip:
		skip_charts = charts_to_skip[experiment]
	else:
		skip_charts = []

	print ('Plotting Experiment ' + str(experiment))
	tdms_file = TdmsFile(data_location + 'Experiment_' + str(experiment) + '/Experiment_' + str(experiment) + '.tdms')

	# Read in channel list
	if experiment_info['Prop'][experiment] is 'A':
		channel_list = pd.read_csv(channel_location+'Channels_A.csv')
	if experiment_info['Prop'][experiment] is 'B':
		if experiment == 14 or 15:
			print ('Using alternate Channel List')
			channel_list = pd.read_csv(channel_location+'Channels_B_alt.csv')
		else:
			channel_list = pd.read_csv(channel_location+'Channels_B.csv')
	if experiment_info['Prop'][experiment] is 'C':
		if experiment==18:
			print ('Using alternate Channel List')
			channel_list = pd.read_csv(channel_location+'Channels_C_alt.csv')
		else:
Exemple #53
0
    def __init__(self, tdms_filename):
        """ Load tdms file and set all variables """
        # Kernel density estimator dictionaries
        self._KDE_Scatter = {}
        self._KDE_Contour = {}
        self._old_filters = {} # for comparison to new filters
        self._Downsampled_Scatter = {}
        self._polygon_filter_ids = []
        
        self.tdms_filename = tdms_filename
        self.name = os.path.split(tdms_filename)[1].split(".tdms")[0]
        self.fdir = os.path.dirname(tdms_filename)

        mx = os.path.join(self.fdir, self.name.split("_")[0])
        
        self.title = u"{} - {}".format(
                      GetProjectNameFromPath(tdms_filename),
                      os.path.split(mx)[1])
        
        f2hash = [ tdms_filename, mx+"_camera.ini", mx+"_para.ini" ]
        
        self.file_hashes = [(fname, _hashfile(fname)) for fname in f2hash]

        self.identifier = self.file_hashes[0][1]

        tdms_file = TdmsFile(tdms_filename)
        
        ## Set all necessary internal parameters as defined in
        ## definitions.py
        ## Note that this is meta-programming. If you want to add a
        ## different column from tdms files, then edit definitions.py:
        ## -> uid, axl, rdv, tfd
        # time is always there
        datalen = len(tdms_file.object("Cell Track", "time").data)
        for i, group in enumerate(dfn.tfd):
            table = group[0]
            if not isinstance(group[1], list):
                group[1] = [group[1]]
            func = group[2]
            args = []
            try:
                for arg in group[1]:
                    data = tdms_file.object(table, arg).data
                    args.append(data)
            except KeyError:
                # set it to zero
                func = lambda x: x
                args = [np.zeros(datalen)]
            finally:
                setattr(self, dfn.rdv[i], func(*args))

        # Plotting filters, set by "GetDownSampledScatter".
        # This is a nested filter which is applied after self._filter
        self._plot_filter = np.ones_like(self.time, dtype=bool)

        # Set array filters:
        # This is the filter that will be used for plotting:
        self._filter = np.ones_like(self.time, dtype=bool)
        # The filtering array for a general data event limit:
        self._filter_limit = np.ones_like(self._filter)
        attrlist = dir(self)
        # Find attributes to be filtered
        # These are the filters from which self._filter is computed
        inifilter = np.ones(data.shape, dtype=bool)
        for attr in attrlist:
            # only allow filterable attributes from global dfn.cfgmap
            if not dfn.cfgmap.has_key(attr):
                continue
            data = getattr(self, attr)
            if isinstance(data, np.ndarray):
                # great, we are dealing with an array
                setattr(self, "_filter_"+attr, inifilter.copy())
        self._filter_polygon = inifilter.copy()

        self.SetConfiguration()

        # Get video file name
        videos = []
        for f in os.listdir(self.fdir):
            if f.endswith(".avi") and f.startswith(self.name[:2]):
                videos.append(f)
        videos.sort()
        if len(videos) == 0:
            self.video = None
        else:
            # Defaults to first avi file
            self.video = videos[0]
            # g/q video file names. q comes first.
            for v in videos:
                if v.endswith("imag.avi"):
                    self.video = v
                    break
                # add this here, because fRT-DC measurements also contain
                # videos ..._proc.avi
                elif v.endswith("imaq.avi"):
                    self.video = v
                    break
        
        # Get contours
        self.contours = {}
        for f in os.listdir(self.fdir):
            if f.endswith("_contours.txt") and f.startswith(self.name[:2]):
                with open(os.path.join(self.fdir, f), "r") as c:
                    # read entire file
                    cdat = c.read(-1)
                for cont in cdat.split("Contour in frame"):
                    cont = cont.strip()
                    if len(cont) == 0:
                        continue
                    cont = cont.splitlines()
                    # the frame is the first number
                    frame = int(cont.pop(0))
                    cont = [ np.fromstring(c.strip("()"), sep=",") for c in cont ]
                    cont = np.array(cont, dtype=np.uint8)
                    self.contours[frame] = cont
#!/usr/bin/env python

from nptdms import TdmsFile
import glob
import re

# Update paths below to directory with tdms files (input_dir) and directory for csv outputs (output_dir)
input_dir = '/Users/prioberoi/Documents/nist/netZero/data/raw/'
output_dir = '/Users/prioberoi/Documents/nist/netZero/data/raw/'

for filename in glob.iglob(input_dir+'*.tdms'): #update filepath to location with .tdms files from LabView
    tdms_file = TdmsFile(filename)
    temp = tdms_file.as_dataframe(time_index=False, absolute_time=False)
    start_loc = re.search(' All Day.tdms', filename).start()-10
    stop_loc = re.search(' All Day.tdms', filename).start()
    temp.to_csv(path_or_buf=(output_dir+filename[start_loc:stop_loc]+".csv"), encoding='utf-8')
filedir = '/home/tiago/Data/Lab.local/PAG/probe/'
fname = 'SE-CSC-RAW-Ch11_.nex'
r = neo.io.NeuroExplorerIO(filename=(filedir + fname)) 
seg = r.read_segment(lazy=False, cascade=True)
data = np.array(seg.analogsignals[0])

filedir = '/home/tiago/Data/Lab.local/PAG/probe/'
fname = 'Analog Input Ch16_.nex'
f = neo.io.NeuroExplorerIO(filename=(filedir + fname)) 
fseg = f.read_segment(lazy=False, cascade=True)
frames = np.array(fseg.analogsignals[0])

# Get tdms file corresponding to the recording
filedir = '/home/tiago/Data/Lab.local/PAG/probe/'
fname = '02-07-2015-19-48-64207_VG2_SC_probe_t1.tdms'
tdms = TdmsFile(filedir+fname)


#############
# Pre-process
#############
# Get rid of probe starting artefact
i = frames<(-1000)
counter = np.arange(0,len(i))
dataStart = counter[i][-1]+100
frames = frames[dataStart:]

# Get the start of each frame
dframes = np.diff(frames)
counter = np.arange(0,len(dframes))
i = dframes>1000
Exemple #56
0
    def lade_tdms(self, datei):
        """
        :type datei: str
        :return: Die gemittelten Messwerte aus der angegebenen Datei
        :rtype: numpy.mutliarray.ndarray
        """
        # Beschnittene Daten (links: positiv, rechts: negativ)
        daten = np.zeros(self.par.messpunkte - self.par.bereich_links + self.par.bereich_rechts)
        try:
            tdat = TdmsFile(datei)
            tdms = tdat.object(tdat.groups()[0], 'Untitled')
        except (ValueError, IOError):
            print('Datei ' + datei + ' nicht auslesbar')
            return daten
        index_fehler = False
        for mittelung in range(self.par.mittelungen):
            try:
                """
                Mittelung (durch Addition)
                UND
                Begrenzung des Fitbereichs (zur Eliminierung von parasitären Frequenzpeaks) nach Angabe in GUI
                """
                start = mittelung * self.par.messpunkte
                links = start + self.par.bereich_links
                rechts = start + self.par.messpunkte + self.par.bereich_rechts
                daten += tdms.data[links:rechts]

                """if mittelung == 0:
                    name = raw_input('$')
                    import matplotlib.pyplot as plt
                    plt.title(name+ ": Einzelmessung")
                    plt.xlabel(u"Frequenz / Hz")
                    plt.ylabel(u"Amplitude / µV")
                    plt.plot(
                        self.frequenzen,
                        daten * (1000*1000/50/2.9),
                        antialiased=True
                    )
                    plt.show()
                elif mittelung == self.par.mittelungen-1:
                    name = raw_input('$')
                    import matplotlib.pyplot as plt
                    plt.title(name+ ": 200x gemittelt")
                    plt.xlabel(u"Frequenz / Hz")
                    plt.ylabel(u"Amplitude / µV")
                    plt.plot(
                        self.frequenzen,
                        daten / self.par.mittelungen * (1000*1000/180),
                        antialiased=True
                    )
                    plt.show()"""

            except (ValueError, IndexError):
                """
                In diesem Fall ist ein Messfehler aufgetreten. Das kann (sehr selten) passieren, weshalb der Fit
                dennoch funktionieren muss. Hier ist dann aber ein Einbruch in der Amplitude zu verzeichnen.
                """
                if not index_fehler:
                    index_fehler = True
                    print('Fehlende Messwerte in Datei ' + datei)
        return daten / self.par.mittelungen
    ncfile.createDimension('y',pixel)
    dd=ncfile.createVariable(name,np.dtype('int32').char,('x','y'))
    data_out2=np.int32(((data_out+abs(data_out.min()))/(data_out.max()-data_out.min()))*(np.power(2,31)-1))
    dd[:] = data_out2
    ncfile.close()

data=[]
phase=[]
messpunkte=200 # die Anzahl der Messpunkte pro Resonanzkurve
pixel=200	# anzahl der Resonanzkurven

############## TDMS Datei einlesen ''''''''''''''''''''''''''''''''''''''
fnames=glob("/home/sebadur/Dokumente/Studium/Master/Fit-GUI/Messung 06.11.14/amp*.tdms")    # alle dateien in diesem Ordner mit der Endung TDMS
sorted_fnames = sorted(fnames, key=lambda x: int(x.split('/')[-1].split('amp')[1].split('.')[0]))    # Dateiname aufgeteilt und Nummerisch sortiert
for i in range(pixel):
    tdms_file = TdmsFile(sorted_fnames[i])
    channel = tdms_file.object('Unbenannt', 'Untitled')     # erster Name ist der Gruppenname dann der Kanalname
    data.append(np.array(channel.data))

###################--phase-- ############################
fnames=glob("/home/sebadur/Dokumente/Studium/Master/Fit-GUI/Messung 06.11.14/phase*.tdms")    # alle dateien in diesem Ordner mit der Endung TDMS
sorted_fnames = sorted(fnames, key=lambda x: int(x.split('/')[-1].split('phase')[1].split('.')[0]))    # Dateiname aufgeteilt und Nummerisch sortiert
for i in np.arange(pixel):
    tdms_file = TdmsFile(sorted_fnames[i])
    channel = tdms_file.object('Unbenannt', 'Untitled')     # erster Name ist der Gruppenname dann der Kanalname
    #data[i]=np.array(channel.data)
    phase.append(np.array(channel.data))



if pixel!=len(data[1])/messpunkte:
Exemple #58
0
def chunker(seq, size):
    return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))


def Reduce_data(data_X, data_Y, x1):
    Mx = np.column_stack((data_X[:, np.newaxis], data_Y[:, np.newaxis]))
    M2 = Red.rdp(Mx, x1)
    data_X2 = M2[:, 0].transpose()
    data_Y2 = M2[:, 1].transpose()
    return data_X2, data_Y2


def PandasResample(df, length):
    td = (df.index[-1] - df.index[0]) / (length - 1)
    return df.resample(td, how='mean').interpolate()  # Handle NaNs when upsampling


tdms_file = TdmsFile('data/_1620291.tdms')
data = tdms_file.as_dataframe()
print data.columns

tizm = tdms_file.object("Unbenannt", "Zeit")
channel = tdms_file.object("Unbenannt", "OfenIO")
channel1 = tdms_file.object("Unbenannt", "T_X6")
channel2 = tdms_file.object("Unbenannt", "Soll")
# plt.plot(tizm.data,channel.data)
# ax.scatter(x, y, c=c, cmap=cmap)
plt.scatter(tizm.data, channel2.data, c=channel.data * 3., cmap=plt.cm.hot)
plt.show()
tdmsVoltageFilename = os.path.join(sys.argv[1], "Voltage.tdms")

tdmsCurrentFilename = os.path.join(sys.argv[1], "Current.tdms")

#print "made file names"

voltageTempNames = []
currentTempNames = []
timestamps = []
voltageGroupChannels = []
currentGroupChannels = []
voltageChannelNames = []
currentChannelNames = []


tdmsVoltageFile = TdmsFile(tdmsVoltageFilename)
tdmsCurrentFile = TdmsFile(tdmsCurrentFilename)

#print "called file names"
###### find group name
# look at meta file

#find "Log signal names:"
#extract group name and channel names

metaVoltageFile = open(metaVoltageFilename)
metaCurrentFile = open(metaCurrentFilename)
#print "successfully opened meta files"
voltageTempNames = []
currentTempNames = []
    bin_acc = 0.
    newcount = 0
    for count in range(0,len(datax)):
        if(datax[count] >= startbin + binsize):
            newx.append(startbin + binsize/2.)
            newy.append(bin_acc)
            newcount = newcount + 1
            startbin += binsize
            bin_acc = 0.
        bin_acc += abs(datay[count])
    return (newx,newy)


# reading TDMS file
filename = sys.argv[1]
datafile = TdmsFile(filename)  # TdmsFile is a function from the library/class npTDMS??

# get the group names
print filename
list_of_groups = datafile.groups() # groups is a function from npTDMS, what returns the names of the groups
# print list_of_groups[0]     it's only possible to print element 0, so the list has only one element
number_of_groups = len(list_of_groups)
# print number_of_groups   this gives a "1", so there is only one group
for groupname in list_of_groups:
    print groupname  # the groupname is "data"; it means to print every element of a list
    list_of_channels = datafile.group_channels(groupname) # group channels is a function from npTDMS, what returns a list of channel objects
    for channel in list_of_channels:
        print channel

# extracting first waveform
# getting voltages