Exemple #1
0
    def test_open_verify(self):
        """ Create a schema and read it back """
        with tempfile.NamedTemporaryFile() as f:
            fname = f.name + '.wiff'
            try:
                props = getprops()

                w = wiff.new(fname, props)
                w.close()

                # Opening is successful
                w = wiff.open(fname)

            finally:
                os.unlink(fname)
Exemple #2
0
def main1(fname):
    props = {
        'start': datetime.datetime.utcnow(),
        'end': datetime.datetime.utcnow(),
        'description':
        'Test WIFF file description and this one is even longer and crazier!',
        'fs': 500,
        'channels': [],
        'files': [],
    }
    leads = [
        'I', 'II', 'III', 'aVL', 'aVR', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5',
        'V6'
    ]
    for lead in leads:
        props['channels'].append({
            'name': lead,
            'bit': 12,
            'unit': 'uV',
            'comment': 'Lead ' + lead
        })

    # Create
    w = wiff.new(fname, props, force=True)

    # Then re-open just to test
    w = wiff.open(fname)

    w.set_file(fname)

    random.seed(0)

    v = w.new_segment(w.channels[0:2], segmentid=1)
    v.frame_space = 1006
    frames = []
    for i in range(100):
        frames.append((struct.pack(">H", random.getrandbits(12)),
                       struct.pack(">H", random.getrandbits(12))))
    w.add_frames(*frames)

    v = w.new_segment(w.channels[0:2], segmentid=2)
    v.frame_space = 1006
    frames = []
    for i in range(100):
        frames.append((struct.pack(">H", random.getrandbits(12)),
                       struct.pack(">H", random.getrandbits(12))))
    w.add_frames(*frames)
Exemple #3
0
def main2(fname):
    # Reopen again
    w = wiff.open(fname)

    w.set_file(fname)
    w.new_annotations()
    w.add_annotation(typ='M', fidx=(10, 20), marker='STAT')
    w.add_annotation(typ='C', fidx=(20, 30), comment='Hello annotation world')
    w.add_annotation(typ='M', fidx=(30, 40), marker='stat')
    w.add_annotation(typ='D', fidx=(30, 40), marker='stat', value=123456789)

    wavs = list(w._GetWAVE())

    v = w.new_segment(wavs[-1].channels, 3)
    v.frame_space = 1006
    frames = []
    for i in range(100):
        frames.append((struct.pack(">H", random.getrandbits(12)),
                       struct.pack(">H", random.getrandbits(12))))
    w.add_frames(*frames)

    print(w.dumps_str())
Exemple #4
0
def _main_dumpdata(args):
    w = wiff.open(args.FILE[0])

    print("# File=%s" % args.FILE[0])
    for i in w.meta:
        m = w.meta[i]
        print("# %s=%s" % (m.key, m.value))

    for i in w.recording:
        r = w.recording[i]
        print("# Recording %d" % i)
        for j in r.segment:
            s = r.segment[j]
            cset = s.channelset
            chans = [_.channel for _ in cset]
            b = s.blob

            if b.compression is not None:
                raise Exception(
                    "Unable to handle compression '%s' on segment %d" %
                    (b.compression, j))

            for x in range(s.fidx_end - s.fidx_start):
                off = s.stride * x
                f = funpack.funpack(b.data[off:off + s.stride], 'little')
                for c in chans:
                    if c.storage == 1:
                        print(f.u8())
                    elif c.storage == 2:
                        print(f.u16())
                    elif c.storage == 4:
                        print(f.u32())
                    elif c.storage == 8:
                        print(f.u64())
                    else:
                        raise NotImplemented(
                            "Unable to handle storage of %d bytes" % c.storage)
Exemple #5
0
def main3(fname):
    props = {
        'start': datetime.datetime.utcnow(),
        'end': datetime.datetime.utcnow(),
        'description':
        'Test WIFF file description and this one is even longer and crazier!',
        'fs': 500,
        'channels': [],
        'files': [],
    }
    leads = [
        'I', 'II', 'III', 'aVL', 'aVR', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5',
        'V6'
    ]
    for lead in leads:
        props['channels'].append({
            'name': lead,
            'bit': 12,
            'unit': 'uV',
            'comment': 'Lead ' + lead
        })

    # Create
    w = wiff.new(fname, props, force=True)

    # Then re-open just to test
    w = wiff.open(fname)

    w.set_file(fname)

    random.seed(0)

    v = w.new_segment(w.channels[0:2], segmentid=1)
    v.frame_space = 10000
    frames = []
    for i in range(10000):
        frames.append((struct.pack(">H", random.getrandbits(12)),
                       struct.pack(">H", random.getrandbits(12))))
    w.add_frames(*frames)

    base, ext = os.path.splitext(fname)
    annos_fname = base + '_anno' + ext

    w.new_file(annos_fname)
    w.new_annotations()
    w.add_annotation(typ='M', fidx=(10, 20), marker='STAT')
    w.add_annotation(typ='C', fidx=(20, 30), comment='Hello annotation world')
    w.add_annotation(typ='M', fidx=(30, 40), marker='stat')
    w.add_annotation(typ='D', fidx=(30, 40), marker='stat', value=123456789)

    print(w.dumps_str())

    annos = w.get_annotations()
    print(list(annos))
    annos = w.get_annotations(typ='M')
    print(list(annos))
    annos = w.get_annotations(fidx=200)
    print(list(annos))
    annos = w.get_annotations(fname=fname)
    print(list(annos))
    annos = w.get_annotations(fname=annos_fname)
    print(list(annos))

    frames = w.get_frames((0, 10))
    print(frames)
Exemple #6
0
def _main_info(args):
    w = wiff.open(args.FILE[0])

    s = os.stat(args.FILE[0])

    vals = []
    vals.append(('Filename', args.FILE[0]))
    vals.append(('Size', str(s.st_size)))
    vals.append((None, None))

    print("  ---------- File Information ----------")
    print_2col(vals)

    vals = []
    for i in w.meta:
        m = w.meta[i]
        vals.append((m.key, m.value))

    print("  ---------- Meta values ----------")
    print_2col(vals)

    for i in w.recording:
        r = w.recording[i]

        vals = []
        vals.append(('Start', r.start))
        vals.append(('End', r.end))
        vals.append(('Description', r.description))
        vals.append(('Sampling Rate', r.sampling))
        vals.append(('# Channels', len(r.channel)))
        vals.append(('Frame Start', r.frame_table.fidx_start))
        vals.append(('Frame End', r.frame_table.fidx_end))

        print()
        print("  ---------- Recording #%d ----------" % i)
        print_2col(vals)

    for i in w.channel:
        c = w.channel[i]

        vals = []
        vals.append(("Index", c.idx))
        vals.append(("Name", c.name))
        vals.append(("Bits", c.bits))
        vals.append(("Unit", c.unit))
        vals.append(("Storage", c.storage))
        vals.append(("Comment", c.comment))
        print()
        print("  ---------- Channel #%d ----------" % i)
        print_2col(vals)

    for i in w.segment:
        s = w.segment[i]
        vals = []
        vals.append(('Recording', s.id_recording))
        vals.append(('Frame Start', s.fidx_start))
        vals.append(('Frame End', s.fidx_end))
        vals.append(
            ('Channels', ",".join([str(_.channel.idx) for _ in s.channelset])))
        vals.append(('Stride', s.stride))
        vals.append(('Blod', s.id_blob))

        print()
        print("  ---------- Segment #%d ----------" % i)
        print_2col(vals)

    for i in w.blob:
        b = w.blob[i]
        vals = []
        vals.append(('Compression', b.compression))
        vals.append(('Data size', len(b.data)))

        print()
        print("  ---------- Blob #%d ----------" % i)
        print_2col(vals)

    for i in w.annotation:
        a = w.annotation[i]
        vals = []
        vals.append(('Recording', a.id_recording))
        vals.append(('Frame Start', a.fidx_start))
        vals.append(('Frame End', a.fidx_end))
        vals.append(('Type', a.type))

        if a.type == 'C':
            vals.append(('Comment', a.comment))
        elif a.type == 'M':
            vals.append(('Marker', a.marker))
        elif a.type == 'D':
            vals.append(('Marker', a.marker))
            vals.append(('Data', a.data))
        else:
            print("Unknown annotation: %s" % str(a))

        print()
        print("  ---------- Annotation #%d ----------" % i)
        print_2col(vals)