コード例 #1
0
ファイル: test_odin.py プロジェクト: pennmem/bptools
    def test_export(self, format):
        jfile = datafile('R1347D_jacksheet.txt')
        scheme = 'bipolar'
        area = 0.001
        ec = ElectrodeConfig.from_jacksheet(jfile, 'R1347D', scheme, area)
        ec.name = '8DEC2017L0M0STIM'

        anodes = ['LAD8', 'LPHCD8', 'LAHCD9', 'RAD8', 'LOFD8', 'RPHCD8']
        cathodes = ['LAD9', 'LPHCD9', 'LAHCD10', 'RAD9', 'LOFD9', 'RPHCD9']
        for args in zip(anodes, cathodes):
            ec.add_stim_channel(*args)

        if format == 'csv':
            with open(datafile("R1347D_8DEC2017L0M0STIM.csv"), 'r') as f:
                ref = f.read().split()
                new = ec.to_csv().split()
                for n, line in enumerate(ref):
                    assert line == new[n]
        else:
            with open(datafile('R1347D_8DEC2017L0M0STIM_bipolar.bin'),
                      'rb') as f:
                ref = f.read().split(b'|')
                new = ec.to_bin().split(b'|')
                for n, line in enumerate(ref):
                    if len(line) == 0:
                        break
                    assert line == new[n]
コード例 #2
0
ファイル: test_odin.py プロジェクト: pennmem/bptools
 def test_contacts_as_recarray(self):
     ec = ElectrodeConfig.from_jacksheet(datafile("simple_jacksheet.txt"))
     arr = ec.contacts_as_recarray()
     for i, num in enumerate(range(1, 36)):
         assert arr.jack_box_num[i] == num
     assert len(arr.contact_name) == 37
     assert len(arr.description) == 37
コード例 #3
0
def test_no_mux_crossing():
    pairs = create_pairs(datafile('mux_test_jacksheet.csv'))
    for pair in pairs.pair:
        if 'A' in pair:
            assert 'B' not in pair
        if 'B' in pair:
            assert 'A' not in pair
コード例 #4
0
ファイル: test_odin.py プロジェクト: pennmem/bptools
    def test_from_jacksheet(self, scheme, area):
        jfile = datafile('simple_jacksheet.txt')
        subject = "subject"

        if scheme == 'invalid':
            with pytest.raises(AssertionError):
                ElectrodeConfig.from_jacksheet(jfile, subject, scheme)
            return

        ec = ElectrodeConfig.from_jacksheet(jfile, subject, scheme, area=area)
        assert isinstance(ec.contacts[1].port, int)
        assert ec.subject == subject
        assert ec.num_contacts == 37
        assert ec.num_sense_channels == 37 if scheme == 'monopolar' else 35
        assert ec.num_stim_channels == 0

        if area == 0.5:
            for contact in ec.contacts.values():
                assert contact.area == area
        else:
            areas = ElectrodeConfig.read_area_file(area)
            regex = re.compile(r'(\d*[a-zA-Z]+)')
            for contact in ec.contacts.values():
                electrode = contact.label[:regex.match(contact.label).end()]
                area_ = areas[areas.label == electrode].area
                assert all(area_ == contact.area)
コード例 #5
0
def test_write_pairs():
    pairs = create_pairs(datafile('R1308T_jacksheet.txt'))

    # Unsupported file type
    with tempdir() as path:
        filename = osp.join(path, "test.xyz")
        with pytest.raises(ValueError):
            write_pairs(pairs, filename)

    # CSV files
    with tempdir() as path:
        filename = osp.join(path, 'test.csv')
        write_pairs(pairs, filename)

        df = pd.read_csv(filename)
        for column in pairs.columns:
            assert all(df[column] == pairs[column])

    # JSON files
    with tempdir() as path:
        filename = osp.join(path, 'pairs.json')

        # No subject
        with pytest.raises(ValueError):
            write_pairs(pairs, filename)

        write_pairs(pairs, filename, subject='R0000X')

        with open(filename, 'r') as f:
            data = json.loads(f.read())
            assert 'R0000X' in data
コード例 #6
0
def test_create_pairs(filename):
    pairs = create_pairs(datafile(filename))

    assert 'pair' in pairs.columns
    assert 'label1' in pairs.columns
    assert 'label2' in pairs.columns
    assert 'contact1' in pairs.columns
    assert 'contact2' in pairs.columns
    assert 'mux' in pairs.columns

    combos = np.array([sorted(pair.split('-')) for pair in pairs.pair])
    df = pd.DataFrame({'e1': combos[:, 0], 'e2': combos[:, 1]})

    # No contact should be paired with itself
    same = [df.loc[i, 'e1'] == df.loc[i, 'e2'] for i in df.index]
    assert not any(same)

    # No pairs should be repeated
    df = pd.DataFrame({'pair': ['-'.join(combo) for combo in combos]})
    assert len(df.pair.unique()) == len(df)

    # Labels should match pairs
    for n, row in pairs.iterrows():
        assert row.label1 in row.pair
        assert row.label2 in row.pair
コード例 #7
0
ファイル: test_odin.py プロジェクト: pennmem/bptools
    def test_creation_methods(self, odin, jacksheet, scheme):
        ec = ElectrodeConfig(filename=datafile(odin))
        ec2 = ElectrodeConfig.from_jacksheet(datafile(jacksheet),
                                             scheme=scheme)

        assert len(ec.contacts) == len(ec2.contacts)
        assert len(ec.sense_channels) == len(ec2.sense_channels)

        # n.b., stim channels shouldn't be defined for ec2
        assert len(ec2.stim_channels) == 0

        for i, _ in enumerate(ec.contacts):
            assert ec.contacts[i].label == ec2.contacts[i].label
            assert ec.contacts[i].port == ec2.contacts[i].port

        for i, _ in enumerate(ec.sense_channels):
            assert ec.sense_channels[i].name == ec2.sense_channels[i].name
            assert ec.sense_channels[i].contact == ec2.sense_channels[
                i].contact
            assert ec.sense_channels[i].ref == ec2.sense_channels[i].ref
コード例 #8
0
ファイル: test_jacksheet.py プロジェクト: pennmem/bptools
def test_read_jacksheet(filename, ignore_labels, standardize_labels):
    jacksheet = datafile(filename)
    js = read_jacksheet(jacksheet, ignore_labels=ignore_labels, standardize_labels=standardize_labels)

    if filename.startswith('simple'):
        assert len(js) == 37
    elif filename.startswith('R1308T'):
        assert len(js) == 128 if not ignore_labels else 126
    elif filename.startswith('R1306E'):
        assert len(js) == 122 if not ignore_labels else 120
        labels = js.label.values
        assert ('2RD1' in labels) if standardize_labels else ('2Rd1' in labels)

    assert 'electrode' in js.columns
コード例 #9
0
ファイル: test_odin.py プロジェクト: pennmem/bptools
    def test_read_config_file(self, extension):
        ec = ElectrodeConfig()
        filename = 'R1308T_14JUN2017L0M0STIM' + extension

        if extension == '.txt':
            with pytest.raises(RuntimeError):
                ec.read_config_file(datafile(filename))
            return

        ec.read_config_file(datafile(filename))

        assert ec.subject == 'R1308T'
        assert ec.version == '1.2'
        assert ec.name == '14JUN2017L0M0STIM'

        assert all([ch.name == ch.label for ch in ec.sense_channels])
        assert all([ch.name == ch.label for ch in ec.stim_channels])

        assert len(ec.contacts) == 126
        assert len(ec.sense_channels) == 126
        assert len(ec.stim_channels) == 3

        ec2 = ElectrodeConfig(datafile(filename))
        assert ec.subject == ec2.subject
        assert ec.version == ec2.version
        assert ec.name == ec2.name
        assert all(
            [ec.contacts[i] == ec2.contacts[i] for i in ec.contacts.keys()])
        assert all([
            ec.sense_channels[i] == ec2.sense_channels[i]
            for i in range(len(ec.sense_channels))
        ])
        assert all([
            ec.stim_channels[i] == ec2.stim_channels[i]
            for i in range(len(ec.stim_channels))
        ])
コード例 #10
0
def test_pairs_to_json():
    pairs = create_pairs(datafile('R1308T_jacksheet.txt'))

    with tempdir() as path:
        pairs_to_json(pairs, 'R1308T', path)
        pair_list = pairs.pair.tolist()
        outfile = osp.join(path, 'pairs.json')
        assert osp.exists(outfile)

        with open(outfile, 'r') as f:
            result = json.loads(f.read())
            assert 'R1308T' in result.keys()
            assert 'pairs' in result['R1308T'].keys()
            for key in result['R1308T']['pairs']:
                assert key in pair_list
コード例 #11
0
ファイル: test_odin.py プロジェクト: pennmem/bptools
    def test_add_stim_channel(self):
        ec = ElectrodeConfig.from_jacksheet(datafile('simple_jacksheet.txt'))
        assert len(ec.stim_channels) == 0

        ec.add_stim_channel('A1', 'A2')
        assert len(ec.stim_channels) == 1
        stim = ec.stim_channels[0]
        assert stim.name == 'A1_A2'
        assert stim.anode == 1
        assert stim.cathode == 2

        with pytest.raises(ContactNotFoundError):
            ec.add_stim_channel('C1', 'A2')

        with pytest.raises(ContactNotFoundError):
            ec.add_stim_channel('A1', 'C2')

        with pytest.raises(ContactNotFoundError):
            ec.add_stim_channel('C1', 'C3')
コード例 #12
0
ファイル: test_odin.py プロジェクト: pennmem/bptools
def test_make_odin_config(scheme, format):
    subject = 'R1308T' if scheme == 'monopolar' else 'R1347D'
    filename = '{:s}_jacksheet.txt'.format(subject)
    prefix = 'R1308T_14JUN2017L0M0STIM' if subject == 'R1308T' else 'R1347D_8DEC2017L0M0STIM'
    output_filename = prefix + '.' + format
    jfile = datafile(filename)

    if scheme == 'monopolar':
        stim_channels = [('LB6', 'LB7'), ('LC7', 'LC8'), ('LB5', 'LB6')]
    else:
        stim_channels = [('LAD8', 'LAD9'), ('LPHCD8', 'LPHCD9'),
                         ('LAHCD9', 'LAHCD10'), ('RAD8', 'RAD9'),
                         ('LOFD8', 'LOFD9'), ('RPHCD8', 'RPHCD9')]

    makeconf = partial(make_odin_config,
                       jfile,
                       prefix,
                       0.001,
                       stim_channels=stim_channels,
                       scheme=scheme,
                       format=format)

    # Printing to stdout
    makeconf()

    # Saving to a directory
    with tempdir() as path:
        makeconf(path=path)
        outfile = osp.join(path, output_filename)
        assert osp.exists(outfile)

        # Verify that each primary sense channel is only listed once
        if format == 'csv':
            config = read_sense_config(outfile)
            assert len(config.c1) == len(config.c1.unique())
            assert all(config.c1 == config.c1.unique())
        elif format == 'bin':
            reffile = '{}_{}.bin'.format(prefix, scheme)
            with open(resource_filename('bptools.test.data', reffile),
                      'rb') as ref:
                with open(outfile, 'rb') as gen:
                    rlines = ref.read()
                    glines = gen.read()

            rlines = rlines.split(b'|')
            glines = glines.split(b'|')
            for i, line in enumerate(rlines):
                if not len(line):
                    break

                # We don't really care if the comment section differs
                assert line.split(b'#')[0] == glines[i].split(b'#')[0]

    # Explicitly specifiying leads to include
    good_leads = [n for n in range(1, 4)]
    with tempdir() as path:
        outfile = osp.join(path, output_filename)
        makeconf(path=path, good_leads=good_leads)

        if format == 'csv':
            config = read_sense_config(outfile)
            if scheme == 'bipolar':
                assert len(config) == 2
            else:
                assert len(config) == 3
        elif format == 'bin':
            pass  # FIXME
コード例 #13
0
def test_create_monopolar_pairs():
    pairs = create_monopolar_pairs(datafile('simple_jacksheet.txt'))
    assert all(pairs.contact2 == 0)
    print(pairs)
    assert all(pairs.pair[i] == "{}-CR".format(pairs.label1[i])
               for i in range(len(pairs)))
コード例 #14
0
def test_muxes():
    """Test that muxes are labeled properly."""
    pairs = create_monopolar_pairs(datafile("simple_jacksheet.txt"))
    assert all(pairs.mux[:32] == 0)
    assert all(pairs.mux[32:] == 1)
コード例 #15
0
        write_pairs(pairs, filename)

        df = pd.read_csv(filename)
        for column in pairs.columns:
            assert all(df[column] == pairs[column])

    # JSON files
    with tempdir() as path:
        filename = osp.join(path, 'pairs.json')

        # No subject
        with pytest.raises(ValueError):
            write_pairs(pairs, filename)

        write_pairs(pairs, filename, subject='R0000X')

        with open(filename, 'r') as f:
            data = json.loads(f.read())
            assert 'R0000X' in data
            # Not testing remainder since covered in test_pairs_to_json


if __name__ == "__main__":
    from bptools.jacksheet import read_jacksheet
    from bptools.pairs import create_monopolar_pairs

    js = read_jacksheet(datafile('simple_jacksheet.txt'))
    print(create_monopolar_pairs(datafile('simple_jacksheet.txt')))
    print(create_pairs(datafile('simple_jacksheet.txt')))
    # print(create_pairs(datafile('R1306E_jacksheet.txt')))