예제 #1
0
def open_streamframe_from_xiofile(filepath,
                                  sensorname,
                                  window_size=5,
                                  with_fields=None,
                                  without_fields=None,
                                  discard_duplicates=True,
                                  start_time=0,
                                  end_time=0,
                                  relative=True,
                                  timestamp_offset=0):
    """Import data for one sensor out of a XIOFile and return a
       StreamFrame indexed with timestamps. By default, the timestamps
       are made relative. Optionally, and offset can be added to
       relative timestamps or the raw timestamps can be kept.

       Arguments:
       filepath             --  Path + filename of the XIOFile to be imported.
       sensorname           --  Name of the sensor to be imported.
       window_size,
       with_fields,
       without_fields,
       discard_duplicates   --  Parameters for quantizing (see Quantize
                                function)
       start_time,
       end_time,
       relative             -- Parameters for xio_quicklinegen.
       timestamp_offset     -- is zero by default and makes timestamps relative
                               Set to any non_integer value, timestamp_offset
                               will leave the timestamps raw. Any non-zero
                               integer value will be added as offset to the
                               relative timestamps

    """
    infile = XIOFile(filepath, 'r', indexing=False)
    stream = pd.DataFrame(
        quantize(infile.xio_quicklinegen(start_time, end_time, True, relative),
                 sensorname,
                 window_size,
                 with_fields,
                 without_fields,
                 discard_duplicates,
                 enumerate_fields=True))
    stream.dropna(subset=['time'], inplace=True)
    stream = stream[:-1]
    if len(stream) < 1:
        return stream
    stream.index = stream['time'].map(lambda x: int(x))
    if type(timestamp_offset) == int:
        stream.index -= infile.min_time
        stream.index += timestamp_offset
    else:
        print "non-int offset in input: raw timestamps from the file will be" +\
               " used"
    infile.xiofile_close()
    stream.index.name = None
    return stream
예제 #2
0
 def test_writing_in_xiofile(self):
     #Read from previously written to file
     self.failUnlessEqual(
         self.i.xio_getline(0), '<irio:sfint32 value="35" '
         'sensorName="xioFileClass/desk3/skeletonCount" timestamp='
         '"13765381"></irio:sfint32>\n')
     #Read from previously formatted XIO line (legacy mode)
     self.failUnlessEqual(
         self.i.xio_getline(1), '<irio:sfint32 value="37" '
         'sensorName="xioFileClass/desk3/skeletonCount" timestamp='
         '"13765381"></irio:sfint32>\n')
     #Read from previously formatted XIO line (venice mode)
     self.failUnlessEqual(
         self.m.xio_getline(0), '<sfint32 value="37" '
         'timestamp="13765381" sensorName="desk3/skeletonCount"/>\n')
     #Test the mode of writing to a file
     self.failUnlessEqual(XIOFile('data/trywriting.xio.gz', 'w').mode, 'w')
예제 #3
0
    def test_xiofile_unindexed(self):
        self.failUnlessEqual(self.g.mode, 'r')
        self.failUnlessEqual(self.g.indexed, False)
        self.failUnlessEqual(self.g.min_time, 1376571532081L)
        self.failUnlessEqual(
            [ln for ln in self.g.xio_quicklinegen(0, 13)][6], {
                'valuetype': 'sffloat',
                'fieldname': 'soundAmplitude',
                'sensorname': 'dsglab-desk-3',
                'value': -1.0,
                'time': 1376571532081L
            })
        self.failUnlessEqual(
            len([z for z in self.q.xio_quicklinegen(0, 6, on_errors='stop')]),
            4)

        self.failUnlessEqual(self.g.xio_quicksearch(15),
                             (False, 1376571532081L))
        self.failUnlessEqual(self.g.xio_quicksearch(1376571532082),
                             (True, 1376571532082L))
        #Test that unindexed is now the default mode
        self.failUnlessEqual(XIOFile('data/types.xio.gz').indexed, False)
        self.failUnlessEqual(type(self.nr), XIOFile)
예제 #4
0
 def setUp(self):
     #Test of an indexed file
     self.f = XIOFile('data/types.xio.gz', indexing=True)
     #Test of an indexed file with a preset number of lines
     self.j = XIOFile('data/types.xio.gz', 'r', 10, indexing=True)
     #Test of an an-unidexed file, now default
     self.g = XIOFile('data/types.xio.gz', indexing=False)
     #Test write mode (old legacy format)
     self.h = XIOFile('data/trywriting.xio.gz', 'w', fileformat='legacy')
     self.h.xio_writeline('<irio:sfint32 value="35" '
                          'sensorName="xioFileClass/desk3/skeletonCount" '
                          'timestamp="13765381"></irio:sfint32>\n')
     self.h.xio_writeline(
         self.h.xio_formatline('sfint32', 37, 'desk3', 'skeletonCount',
                               13765381))
     self.h.xiofile_close()
     #Test write mode (new default venice format)
     self.l = XIOFile('data/trywriting2.xio.gz', 'w')
     self.l.xio_writeline(
         self.l.xio_formatline('sfint32', 37, 'desk3', 'skeletonCount',
                               13765381))
     self.l.xiofile_close()
     #Test write mode, by opening the file previously written to
     self.i = XIOFile('data/trywriting.xio.gz', 'r', indexing=True)
     self.m = XIOFile('data/trywriting2.xio.gz', 'r', indexing=True)
     #Test copy utility, copy part of the file to a new file
     xiofile_quickcopy('data/types.xio.gz', 'data/newtypes.xio.gz', 0, 5)
     #Test result of copy, by loading the copy
     self.k = XIOFile('data/newtypes.xio.gz', 'r', indexing=True)
     #Test of an un-compressed file
     self.n = XIOFile('data/types.xio', indexing=True)
     #File to specifically test line ranges
     self.o = XIOFile('data/linestest.xio.gz', indexing=True)
     #Similar for unindexed mode
     self.p = XIOFile('data/linestest.xio.gz')
     self.n = XIOFile('data/types.xio', indexing=True)
     self.q = XIOFile('data/parsing5.xio.gz', indexing=True)
     self.nr = XIOFile('data/unreadable.xio.gz')
예제 #5
0
    def setUp(self):
        self.f = open_streamframe_from_xiofile('data/fseeksmaller.xio.gz',
                                               "lab-labtop/irioKinect 2",
                                               window_size=5, with_fields=[],
                                               without_fields=[],
                                               discard_duplicates=True,
                                               start_time=0, end_time=13,
                                               relative=True,
                                               timestamp_offset=10)

        self.f2 = open_streamframe_from_xiofile('data/fseeksmaller.xio.gz',
                                                "lab-labtop/irioKinect",
                                                window_size=5, with_fields=[],
                                                without_fields=[],
                                                discard_duplicates=True,
                                                start_time=0, end_time=13,
                                                relative=True,
                                                timestamp_offset=10)
        #self.outtake_from_stream = self.f.ix[1341393414826]['framenumber']
        self.fraw = open_streamframe_from_xiofile('data/fseeksmaller.xio.gz',
                                                  "lab-labtop/irioKinect 2",
                                                  window_size=5, with_fields=[],
                                                  without_fields=[],
                                                  discard_duplicates=True,
                                                  start_time=0, end_time=13,
                                                  relative=True,
                                                  timestamp_offset='raw')
        self.ff = open_streamframe_from_xiofile('data/fseeksmaller.xio.gz',
                                                "lab-labtop/irioKinect 2",
                                                window_size=5, with_fields=[],
                                                without_fields=[],
                                                discard_duplicates=True,
                                                start_time=0, end_time=13,
                                                relative=True)

        save_streamframe_to_xiofile({"lab-labtop/irioKinect 2": self.f},
                                    'data/sf_to_xio.xio.gz')

        save_streamframe_to_xiofile({"lab-labtop/irioKinect 2": self.f,
                                     "lab-labtop/irioKinect": self.f2},
                                    'data/sf_to_xio2.xio.gz')

        self.rsn = open_streamframe_from_xiofile('data/fseeksmaller.xio.gz',
                                                "wrong/sensor/name")

        self.outtake_from_xio = XIOFile('data/sf_to_xio.xio.gz',
                                        indexing=True)

        self.outtake_from_xio_2 = XIOFile('data/sf_to_xio2.xio.gz',
                                          indexing=True)

        self.q = [ex for ex in quantize(self.outtake_from_xio.xio_quicklinegen\
                                                  (0, 13, True, True),
                                  "lab-labtop/irioKinect 2")][0]['soundAngle']

        self.ivf = open_intervalframe_from_textgrid('data/r1_12_15with'
                                                    'Point.TextGrid',
                                                    encoding='utf-8',
                                                    asobjects = False,
                                         include_empty_intervals = False)['P']
        self.cv = convert_pointtier_to_streamframe(self.ivf)
        self.pf = convert_streamframe_to_pointtier(self.f)
        self.outtake_from_pf = \
          convert_streamframe_to_pointtier(self.f)['soundAngle'].ix[0]['time']

        self.if_from_tg = open_intervalframe_from_textgrid('data/r1-20120704-'
                          'cam1-head-zm.TextGrid',
                          encoding='utf-8',
                          asobjects = False,
                          include_empty_intervals = False)

        self.if_from_tg_tier =  self.if_from_tg.values()[0]

        save_intervalframe_to_textgrid(self.if_from_tg,
                                       'data/testif.TextGrid',
                                       encoding='utf-8')
        self.tg = tgt.read_textgrid('data/testif.TextGrid',
                                    encoding='utf-8',
                                    include_empty_intervals=False)

        self.ic1 = open_intervalframe_from_increco('data/test.inc_reco')

        self.ic2 = open_intervalframe_from_increco('data/test.inc_reco',
                                                   lastonly=True)
예제 #6
0
def save_streamframe_to_xiofile(framedict, filepath):
    """Save many streamframes to a single XIOFile.

    Arguments:
    framedict   --  a dict of streamframes, keys are the
                    sensornames
    filepath    --  Path + filename of the file to be written.
    sensorname  --  Name of the sensor.

    """

    #create a joined streamframe
    frame = None
    for key in framedict.keys():
        if len(framedict[key]) < 1:
            continue
        sensorframe = framedict[key].copy(deep=True)
        sensorframe['sensorname'] = key
        if frame is None:
            frame = sensorframe
        else:
            frame = frame.append(sensorframe)

    if len(frame) < 1:
        print "invalid data!"
        return

    frame.sort_index(inplace=True)

    outfile = XIOFile(filepath, 'w')
    types = {
        'float': 'sffloat',
        'SFVec3f': 'sfvec3f',
        'SFVec2f': 'sfvec2f',
        'SFRotation': 'sfrotation',
        'MFVec3f': 'mfvec3f',
        'MFVec2f': 'mfvec2f',
        'MFRotation': 'mfrotation',
        'MFString': 'mfstring',
        'MFFloat': 'mffloat',
        'bool': 'sfbool',
        'int': 'sfint32',
        'str': 'sfstring'
    }

    for row in frame.iterrows():  #each row of the dataframe
        timestamp = int(row[0])
        fields = row[1]
        sensorname = fields['sensorname']

        for fieldname in row[1].index:  #fieldname becomes name of each column
            if fieldname in ['time', 'sensorname']:
                continue
            for key in types:
                if key in str(type(row[1][fieldname])):
                    otype = types[key]
                    value = row[1][fieldname]
                    if str(value).lower() != 'nan':
                        outfile.xio_writeline(
                            outfile.xio_formatline(otype, value, sensorname,
                                                   fieldname, timestamp))
    outfile.xiofile_close()
    return