-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from BEL-Public/release-0.7.0
- Loading branch information
Showing
16 changed files
with
210 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/usr/bin/env python | ||
""" | ||
mffdiff.py | ||
Compare the content of two MFF files | ||
""" | ||
from os.path import join | ||
from subprocess import check_output | ||
from mffpy import Reader | ||
from argparse import ArgumentParser | ||
|
||
char_limit = 50 | ||
|
||
parser = ArgumentParser(description=""" | ||
compare the contents of two MFF files | ||
""") | ||
parser.add_argument('leftpath', type=str, help="MFF file to compare") | ||
parser.add_argument('rightpath', type=str, help="second MFF file to compare") | ||
args = parser.parse_args() | ||
|
||
left_mff = Reader(args.leftpath) | ||
right_mff = Reader(args.rightpath) | ||
|
||
|
||
def getnested(instance, props, callback=None): | ||
"""get nested property of instance | ||
This is a recursive function to access a nested property, e.g. for | ||
`instance.a.b.c`, props is `['a', 'b', 'c']`. `callback` is optionally | ||
applied to the final value. | ||
Parameters | ||
---------- | ||
instance: | ||
instance that has a property `props[0]` | ||
props: | ||
nested property split into a list | ||
callback: | ||
is optionally applied to the output value | ||
""" | ||
if props: | ||
val = getattr(instance, props[0]) | ||
return getnested(val, props[1:], callback) | ||
|
||
return callback(instance) if callback else instance | ||
|
||
|
||
def compare_raw(filename): | ||
"""compare file in MFF using `$ diff`""" | ||
leftfile = join(args.leftpath, filename) | ||
rightfile = join(args.rightpath, filename) | ||
try: | ||
check_output(['diff', leftfile, rightfile]) | ||
status = 'match' | ||
except BaseException: | ||
status = 'diff' | ||
|
||
print(f">>> {status} @ file '{filename}'") | ||
|
||
|
||
def compare(prop: str, callback=None, info: str = ''): | ||
"""compare property between left_mff and right_mff | ||
Parameters | ||
---------- | ||
prop: | ||
string specifying a nested property of `mffpy.Reader` instance | ||
callback: | ||
post processing of the value of `prop` | ||
info: | ||
additional note of the nested property | ||
""" | ||
props = prop.split('.') | ||
try: | ||
left_value = getnested(left_mff, props, callback) | ||
right_value = getnested(right_mff, props, callback) | ||
left_msg = str(left_value)[:char_limit] | ||
right_msg = str(right_value)[:char_limit] | ||
if left_value != right_value: | ||
msg = f'\t{left_msg} != {right_msg}' | ||
status = 'diff' | ||
else: | ||
msg = '' | ||
status = 'match' | ||
|
||
except BaseException as err: | ||
status = 'error' | ||
msg = str(err)[:char_limit] + '\n' | ||
left_value = None | ||
|
||
print(f">>> {status} @ reader_instance.{prop} " | ||
f"{': ' + info if info else ''}") | ||
if msg: | ||
print(msg) | ||
|
||
return left_value if status == 'match' else None | ||
|
||
|
||
simple_props = ['mff_flavor', 'sampling_rates', 'durations', 'startdatetime', | ||
'units', 'num_channels', 'categories.categories'] | ||
|
||
print(f"Comparing {args.leftpath} with {args.rightpath}") | ||
for prop in simple_props: | ||
compare(prop) | ||
|
||
epoch_count = compare('epochs.epochs', lambda e: len(e), 'len(..)') | ||
if epoch_count is not None: | ||
for i in range(epoch_count): | ||
compare('epochs.epochs', lambda e: e[i].content, f"[{i}].content") | ||
|
||
out = compare_raw('signal1.bin') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Copyright 2019 Brain Electrophysiology Laboratory Company LLC | ||
Licensed under the ApacheLicense, Version 2.0(the "License"); | ||
you may not use this module except in compliance with the License. | ||
You may obtain a copy of the License at: | ||
http: // www.apache.org / licenses / LICENSE - 2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
ANY KIND, either express or implied. | ||
""" | ||
|
||
from glob import glob | ||
import os.path as op | ||
from zipfile import ZipFile, ZIP_STORED | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(scope='session', autouse=True) | ||
def ensure_mfz(): | ||
"""Ensure that the mfz file exists.""" | ||
fname = op.join( | ||
op.dirname(__file__), '..', '..', 'examples', 'example_1.mfz') | ||
if not op.isfile(fname): | ||
with ZipFile(fname, mode='w', compression=ZIP_STORED) as zf: | ||
for content_filename in glob(op.join(fname[:-3] + 'mff', '*')): | ||
arc_filename = op.basename(content_filename) | ||
zf.write(content_filename, arcname=arc_filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.