-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Implement basic helper tool
- Loading branch information
Showing
5 changed files
with
122 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__/ |
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,57 @@ | ||
from pathlib import Path | ||
import click | ||
import pynwb | ||
from .base import get_cases_in_namespace | ||
|
||
DEFAULT_SAMPLES_PATH = str(Path.home() / ".cache" / "nwb-healthstatus") | ||
|
||
@click.group() | ||
def main(): | ||
pass | ||
|
||
@main.group() | ||
def sample(): | ||
pass | ||
|
||
@sample.command() | ||
#@click.option("-e", "--environment") | ||
@click.option("--overwrite", is_flag=True) | ||
@click.option("--samples-path", type=click.Path(file_okay=False), default=DEFAULT_SAMPLES_PATH) | ||
@click.argument("casefile", type=click.Path(exists=True, dir_okay=False), nargs=-1) | ||
def create(casefile, overwrite, samples_path): | ||
sample_dir = Path(samples_path) | ||
for path in casefile: | ||
p = Path(path) | ||
producer = p.resolve().parent.name | ||
namespace = {} | ||
exec(p.read_text(), namespace) | ||
for casecls in get_cases_in_namespace(namespace): | ||
case = casecls() | ||
filepath = sample_dir / producer / case.FILENAME | ||
filepath.parent.mkdir(parents=True, exist_ok=True) | ||
if overwrite or not filepath.exists(): | ||
nwbfile = case.create() | ||
with pynwb.NWBHDF5IO(str(filepath), "w") as io: | ||
io.write(nwbfile) # , cache_spec=cache_spec) | ||
|
||
@sample.command() | ||
#@click.option("-e", "--environment") | ||
@click.option("--samples-path", type=click.Path(exists=True, file_okay=False), default=DEFAULT_SAMPLES_PATH) | ||
@click.argument("casefile", type=click.Path(exists=True, dir_okay=False), nargs=-1) | ||
def test(casefile, samples_path): | ||
sample_dir = Path(samples_path) | ||
for path in casefile: | ||
p = Path(path) | ||
producer = p.resolve().parent.name | ||
namespace = {} | ||
exec(p.read_text(), namespace) | ||
for casecls in get_cases_in_namespace(namespace): | ||
case = casecls() | ||
filepath = sample_dir / producer / case.FILENAME | ||
with pynwb.NWBHDF5IO(str(filepath), mode='r') as io: | ||
## TODO: Capture and display possible warnings | ||
obj = io.read() | ||
case.test(obj) | ||
|
||
if __name__ == "__main__": | ||
main() |
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,51 @@ | ||
from abc import ABC, abstractmethod | ||
from functools import reduce | ||
from inspect import isclass | ||
from operator import or_ | ||
from types import ModuleType | ||
from typing import ClassVar, Iterator, Set, Type | ||
from hdmf.container import Container | ||
import pynwb | ||
|
||
class SampleCase(ABC): | ||
#: Set of extensions needed by the sample case | ||
EXTENSIONS: ClassVar[Set[str]] | ||
|
||
#: Basename for the sample file created by the case | ||
FILENAME: ClassVar[str] | ||
|
||
@abstractmethod | ||
def create(self) -> pynwb.NWBFile: | ||
""" Creates a sample NWB file """ | ||
... | ||
|
||
@abstractmethod | ||
def test(self, data: Container) -> None: | ||
""" | ||
Takes the data read from a sample file and asserts that it contains | ||
what it should | ||
""" | ||
... | ||
|
||
@classmethod | ||
def __subclasshook__(cls, C): | ||
if ( | ||
cls is SampleCase | ||
and {"EXTENSIONS", "FILENAME", "create", "test"} | ||
<= reduce(or_, (B.__dict__.keys() for B in C.__mro__)) | ||
): | ||
return True | ||
return NotImplemented | ||
|
||
|
||
def get_cases_in_module(module: ModuleType) -> Iterator[Type[SampleCase]]: | ||
for name in dir(module): | ||
obj = getattr(module, name) | ||
if isclass(obj) and issubclass(obj, SampleCase): | ||
yield obj | ||
|
||
|
||
def get_cases_in_namespace(namespace: dict) -> Iterator[Type[SampleCase]]: | ||
for obj in namespace.values(): | ||
if isclass(obj) and issubclass(obj, SampleCase): | ||
yield obj |
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,3 @@ | ||
click | ||
hdmf | ||
pynwb |