def test_can_read_tdms_file_properties_after_writing():
    test_time = np.datetime64('2019-11-19T15:30:00')

    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)

    file_properties = tdms_file.properties
    b_output = tdms_file["group_name"]

    assert "prop1" in file_properties, "prop1 not found"
    assert "prop2" in file_properties, "prop2 not found"
    assert "prop3" in b_output.properties, "prop3 not found"
    assert "prop4" in b_output.properties, "prop4 not found"
    assert file_properties["prop1"] == "foo"
    assert file_properties["prop2"] == 3
    assert b_output.properties["prop3"] == 1.2345
    assert b_output.properties["prop4"] == test_time
Example #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)
Example #3
0
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 tdmswriter:
                    for group in tdmsfile.groups().remove('Global'):
                        idx1, idx2 = _get_indextime(timedate, t[0], t[1])
                        for channel in file.group_channels(group)[idx1, idx2]:
                            tdms_writer.write_segment([root_object, channel])
                    for channel in tdmsfile.group_channels('Global'):
                        if channel.channel == 'Wavelength':
                            channel_object = channel
                        else:
                            channel_object = _cut_channel(channel,
                                                          time[0],
                                                          time[1],
                                                          timedata=None)
                        tdms_writer.write_segment(
                            [root_object, channel_object])
            except ValueError as error:
                print(error)
                print('removing the file at: \n', fileoutpath)
                os.remove(fileoutpath)
Example #4
0
def SPEtoTDMS_seq(spefilepath,meastype):
    """convert a sequential SPE file (image or spectral) into a Tdms file. """
    if isinstance(spefilepath,bytes): #The labview addin passes a bytes instead of string. 
        spefilepath = spefilepath.decode("utf-8")
    
    folder = os.path.splitext(os.path.dirname(spefilepath))[0]
    base = os.path.splitext(os.path.basename(spefilepath))[0]
    tdmsfilepath = os.path.join(folder,base + ".tdms")

    spe_file = sl.load_from_files([spefilepath])

    frames  = spe_file.data   
    gatedelays = _get_gatedelays(spe_file)
    
    root_object = RootObject(properties={})    
    common_group_object = GroupObject("Common", properties={})
    
    with TdmsWriter(tdmsfilepath) as tdms_writer:   
        channel_object = ChannelObject("Common", "Gate Delays" ,gatedelays, properties={})
        tdms_writer.write_segment([root_object,common_group_object,channel_object])
    
    if(meastype == 0): 
        wavelength = spe_file.wavelength
        with TdmsWriter(tdmsfilepath, mode = 'a') as tdms_writer: 
            channel_object = ChannelObject("Common", "Wavelength" ,wavelength, properties={})
            tdms_writer.write_segment([root_object,common_group_object,channel_object])   
        write_spectra(tdmsfilepath, root_object, frames,wavelength )
    if(meastype == 1):
        write_image(tdmsfilepath, root_object, frames )
Example #5
0
def _write_dataframe(tdms_writer, dataframe, name):
    """Write a dataframe to a tdms group."""
    root_object = RootObject(properties={})
    i = 0
    for column in dataframe.iteritems():
        column = column[1].as_matrix()
        channel_object = ChannelObject(name, name + "_" + str(i), column)
        tdms_writer.write_segment([root_object, channel_object])
        i = i + 1
Example #6
0
    def write_array_data(self,
                         filepath,
                         groupName,
                         rawData,
                         tags={},
                         sampleInfo={
                             "batchId": None,
                             "batchName": '',
                             "sampleId": None
                         }):
        nextIndexOfChannel = 0
        hasGroup = False
        root_object = RootObject(properties=sampleInfo)
        group_object = GroupObject(groupName,
                                   properties={'data_form': 'array'})
        channel_object = ChannelObject(groupName,
                                       'm0',
                                       rawData,
                                       properties=tags)
        if os.path.exists(filepath):
            tdms_file = TdmsFile.read(filepath)
            original_groups = tdms_file.groups()
            original_channels = [
                chan for group in original_groups for chan in group.channels()
            ]

            try:
                hasGroup = tdms_file[groupName] is not None
            except KeyError:
                hasGroup = False
            print(f'has group? {hasGroup}')
            if hasGroup:
                channels = tdms_file[groupName].channels()
                print(channels)
                nextIndexOfChannel = len(channels)
            channelName = f'm{nextIndexOfChannel}'
            channel_object = ChannelObject(groupName,
                                           channelName,
                                           rawData,
                                           properties=tags)
            with TdmsWriter(filepath, mode='a') as tdms_writer:
                # root_object = RootObject(tdms_file.properties)
                # channels_to_copy = [chan for chan in original_channels]
                # channels_to_copy.append(channel_object)
                # tdms_writer.write_segment([root_object] + original_groups + channels_to_copy)
                # Write first segment
                tdms_writer.write_segment(
                    [root_object, group_object, channel_object])
        else:
            with TdmsWriter(filepath) as tdms_writer:
                # Write first segment
                tdms_writer.write_segment(
                    [root_object, group_object, channel_object])
Example #7
0
    def copy_tdms(self, filePath, targetPath):
        original_file = TdmsFile(filePath)
        original_groups = original_file.groups()
        original_channels = [
            chan for group in original_groups for chan in group.channels()
        ]

        with TdmsWriter(targetPath) as copied_file:
            root_object = RootObject(original_file.properties)
            channels_to_copy = [chan for chan in original_channels]
            copied_file.write_segment([root_object] + original_groups +
                                      channels_to_copy)
Example #8
0
File: spe.py Project: msb002/mhdpy
def parse_lasertiming(fileinpaths, **kwargs):
    """Takes in a series of SPE sequential images and outputs a tdms file of the maximum of each image."""
    intensities, timestamps, gatedelays = _lasertiming(fileinpaths)
    folder = os.path.split(fileinpaths[0])[0]
    fileoutpath = os.path.join(folder, 'PIMax_Timing_Parsed.tdms')

    with TdmsWriter(fileoutpath, mode='w') as tdms_writer:
        root_object = RootObject(properties={})
        channel_object = ChannelObject('Gate Delays', 'Gate Delays',
                                       gatedelays)
        tdms_writer.write_segment([root_object, channel_object])
        _write_dataframe(tdms_writer, intensities, "MaxIntensities")
        _write_dataframe(tdms_writer, timestamps, "Timestamps")
Example #9
0
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)
Example #10
0
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)
Example #11
0
def save_as_tdms(file_path, pcd):
    
    points = np.asarray(pcd.points)
    colors = np.asarray(pcd.colors) * 255
    x, y, z = points.T.astype('float32')
    r, g, b = colors.T.astype('uint8')
    
    
    root_obj = RootObject(properties={
            'Version': 1, 
            'Day': 11, 
            'Month': 12, 
            'Year': 2018, 
            'Pipe ID': 584})
    
    group_laser = GroupObject('Laser Data', properties={
            'Fields': 'X Y Z',
            'Type': 'F F F',
            'Points': len(x)
            })
    
    group_color = GroupObject('Colour Data', properties={
            'Fields': 'R G B',
            'Type': 'U8 U8 U8',
            'Points': len(r)
            })
    
    channel_x = ChannelObject('Laser Data', 'X', x)
    channel_y = ChannelObject('Laser Data', 'Y', y)
    channel_z = ChannelObject('Laser Data', 'Z', z)
    
    channel_r = ChannelObject('Colour Data', 'R', r)
    channel_g = ChannelObject('Colour Data', 'G', g)
    channel_b = ChannelObject('Colour Data', 'B', b)
    
    with TdmsWriter(file_path) as tdms_writer:
        tdms_writer.write_segment([
                root_obj,
                group_laser, group_color,
                channel_x, channel_y, channel_z,
                channel_r, channel_g, channel_b])
Example #12
0
    def write_waveform_data(self,
                            filepath,
                            groupName,
                            time_data=[],
                            y_data=[],
                            tags={},
                            sampleInfo={
                                "batchId": None,
                                "batchName": '',
                                "sampleId": None
                            }):
        nextIndexOfChannel = 0
        if os.path.exists(filepath):
            self.open(filepath)
            nextIndexOfChannel = self.how_many_channels_in_group_name(
                groupName) + 1
            self.close()

        time_channelName = f'm{nextIndexOfChannel}_t'
        value_channelName = f'm{nextIndexOfChannel}_y'
        root_object = RootObject(properties=sampleInfo)
        group_object = GroupObject(groupName,
                                   properties={'data_form': 'waveform'})
        with TdmsWriter(filepath) as tdms_writer:
            # Write time data segment
            time_channel_object = ChannelObject(groupName,
                                                time_channelName,
                                                time_data,
                                                properties={})
            tdms_writer.write_segment(
                [root_object, group_object, time_channel_object])
            # Write value data segment
            value_channel_object = ChannelObject(groupName,
                                                 value_channelName,
                                                 y_data,
                                                 properties=tags)
            tdms_writer.write_segment(
                [root_object, group_object, value_channel_object])
Example #13
0
def cut_log_file(fileinpaths, times, fileoutpaths_list, **kwargs):
    """
    Cuts up a log file based on the supplied times.
    
    This function assumes that the channels are waveforms.
    """

    for i, fileinpath in enumerate(fileinpaths):

        fileoutpaths = fileoutpaths_list[i]
        tdmsfile = TF(fileinpath)
        for j in range(len(times)):
            time1 = times[j][0]
            time2 = times[j][1]

            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():
                        for channel in tdmsfile.group_channels(group):
                            channel_object = _cut_channel(channel,
                                                          time1,
                                                          time2,
                                                          timedata=None)
                            tdms_writer.write_segment(
                                [root_object, channel_object])
            except ValueError as error:
                print(error)
                print('removing the file at: \n', fileoutpath)
                os.remove(fileoutpath)
Example #14
0
def cut_log_file(fileinpaths, times, fileoutpaths_list, **kwargs):
    """
    Cuts up a log file based on the supplied times.
    
    This function assumes that the channels are waveforms.
    """

    for i, fileinpath in enumerate(fileinpaths):

        fileoutpaths = fileoutpaths_list[i]
        tdmsfile = TF(fileinpath)
        for j in range(len(times)):
            time1 = times[j][0]
            time2 = times[j][1]

            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
            })

            timegroupwritten = False

            try:
                with TdmsWriter(fileoutpath, mode='w') as tdms_writer:
                    for group in tdmsfile.groups():
                        if 'TimeChannelName' in kwargs:
                            if 'TimeGroupName' in kwargs:
                                timegroup = kwargs['TimeGroupName']
                            else:
                                timegroup = group
                                timegroupwritten = False
                            timechannel = tdmsfile.object(
                                timegroup, kwargs['TimeChannelName'])
                            timedata = timechannel.data
                            timedata = np.array(
                                list(map(lambda x: np.datetime64(x),
                                         timedata)))
                            timedata = timedata.astype('M8[us]')

                            if timegroupwritten == False:
                                timechannel_cut = _cut_datetime_channel(
                                    timechannel, time1, time2)
                                tdms_writer.write_segment(
                                    [root_object, timechannel_cut])
                                timegroupwritten = True

                            waveform = False
                        else:
                            waveform = True

                        for channel in tdmsfile.group_channels(group):
                            # if type(channel.data_type.size) == type(None): break #skips over non numeric channels
                            if channel.data_type == nptdms.types.DoubleFloat:
                                if waveform:
                                    timedata = channel.time_track(
                                        absolute_time=True)
                                idx1, idx2 = _get_indextime(
                                    timedata, time1, time2)
                                channel_object = _cut_channel(
                                    channel, idx1, idx2, waveform)
                                tdms_writer.write_segment(
                                    [root_object, channel_object])
            except ValueError as error:
                print(error)
                print('removing the file at: \n', fileoutpath)
                os.remove(fileoutpath)
Example #15
0
# PyPI imports
import numpy as np
from nptdms import TdmsWriter, RootObject, GroupObject, ChannelObject

# Define time epoch (UTC)
test_epoch = datetime(2020, 7, 14, 12, 30, 30, 79060, tzinfo=timezone.utc)
test_epoch_standard = datetime(1904, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc)

test_time = 1  # in seconds

# Generate PXI1_LF .tdms
samp_freq = 1000
num_elem = test_time * samp_freq

root_object = RootObject(properties={
    "rack_name": "PXI1_LF",
    "sampling_freq": samp_freq
})
group_object = GroupObject("group_1")

data_st = np.linspace(0, 1, num_elem, endpoint=True)
ts = (test_epoch - test_epoch_standard).total_seconds()
data_at = data_st + ts

data_rp = np.heaviside(data_st, 0.25) - np.heaviside(data_st, 0.75)

all_data = [data_st, data_at, data_rp]

tdms_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..',
                         'tests', 'test_inputs')
tdms_name = os.path.join(tdms_path, "example_01.tdms")
with TdmsWriter(tdms_name) as tdms_writer:
Example #16
0
chn_g = original_file['Data']['Pixel ch 2'][:] - bg_green
chn_r = chn_r - chn_g * ratio_r2g
# chn_r = np.array([int(i) for i in chn_r])
chn_r = np.where(chn_r > 0, chn_r, 0)
# chn_g = np.array([int(i) for i in chn_g])
chn_g = np.where(chn_g > 0, chn_g, 0)
chn_b = original_file['Data']['Pixel ch 3'][:]
# chn_b = np.array([int(i) for i in chn_b])

# original_channels[0,:] = chn_r
#
# print (original_channels)
# print(len(chn_r))
with TdmsWriter('C:\\Users\\86183\\Desktop\\' + name + '-corrected' +
                '.tdms') as copied_file:
    root_object = RootObject(original_file.properties)
    # 这里必须加号
    copied_file.write_segment([root_object] + original_groups)
    ch_1 = ChannelObject("Data",
                         "Pixel ch 1",
                         chn_r,
                         properties={
                             "Channel_name": 'Red',
                             "NI_ArrayColumn": 7,
                         })
    ch_2 = ChannelObject("Data",
                         "Pixel ch 2",
                         chn_g,
                         properties={
                             "Channel_name": 'Green',
                             "NI_ArrayColumn": 8,