-
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.
bug fixes + add anatomist plot class + data provider + average meshes (…
…#13) * fix setup.py bugs after info.py * increment version_micro * add build/ folder to .gitignore * update install requirements * add volume-to-point-loud to CLI * add Anatomist class * add region overlaps to data provider * small doc revision * add recipes for pcpm meshes * refactor
- Loading branch information
Showing
17 changed files
with
483 additions
and
89 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 |
---|---|---|
|
@@ -7,7 +7,8 @@ __pycache__/ | |
*.py[cod] | ||
scripts/ | ||
|
||
*.ipynb | ||
venv | ||
|
||
.pytest_cache/ | ||
.pytest_cache/ | ||
build/ | ||
examples_local/ |
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,19 @@ | ||
import functools | ||
import logging | ||
log = logging.getLogger("Dico_toolbox") | ||
|
||
try: | ||
from soma import aims as _aims | ||
_HAS_AIMS = True | ||
except ImportError: | ||
_HAS_AIMS = False | ||
log.warn("Can not import pyAims, are you in a brainvisa environment?") | ||
|
||
def _with_brainvisa(fun): | ||
@functools.wraps(fun) | ||
def wrapper(*args, **kwargs): | ||
if not _HAS_AIMS: | ||
raise RuntimeError( | ||
"This function is only available in a brainvisa environment") | ||
return fun(*args, **kwargs) | ||
return wrapper |
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,41 @@ | ||
import os | ||
import logging | ||
import anatomist.api as anatomist | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
os.environ["QT_API"] = "pyqt5" | ||
|
||
message_lines = ["NOTES for jupyter users:" | ||
"Remember to set '%gui qt' in a cell of your notebook" | ||
"To hide anatomist logs, use the '%%capture' magic command at the beginning of the cell"] | ||
|
||
|
||
class Anatomist(): | ||
info_displayed = False | ||
instance = None | ||
windows = {} | ||
|
||
def __init__(self): | ||
log.warning("/n".join(message_lines)) | ||
self.instance = anatomist.Anatomist() | ||
|
||
def new_window_3D(self, name="Default"): | ||
w = self.instance.createWindow("3D", geometry=[1200, 350, 500, 500]) | ||
self.windows[name] = w | ||
|
||
def close(self): | ||
self.instance.close() | ||
|
||
def add_objects(self, objects, window_name="Default"): | ||
if type(objects) == dict: | ||
pass | ||
elif type(objects) in [list, tuple]: | ||
# list to dict | ||
objects = {str(n): obj for n, obj in enumerate(object)} | ||
|
||
for name, obj in objects.items(): | ||
m = self.instance.toAObject(obj) | ||
m.name = name | ||
m.addInWindows(self.windows[window_name]) |
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 @@ | ||
from .volume_to_point_cloud import volume_to_point_cloud |
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,73 @@ | ||
import argparse | ||
import os | ||
from glob import glob | ||
from tqdm import tqdm | ||
import dico_toolbox as dtb | ||
import numpy as np | ||
from multiprocessing import Pool, cpu_count | ||
|
||
import logging | ||
log = logging.getLogger(__name__) | ||
|
||
try: | ||
from soma import aims | ||
except: | ||
ImportError( | ||
"pyAims could not be imported. Cannot use this tool outside a brainvisa environment.") | ||
|
||
|
||
def get_fname(path): return os.path.basename(path).split('.')[0] | ||
|
||
|
||
def volume_to_point_cloud(path): | ||
"""convert a volume into a point-colud.""" | ||
|
||
fname = get_fname(path) | ||
|
||
try: | ||
vol = aims.read(path) | ||
point_cloud = dtb.convert.volume_to_bucket_numpy(vol) | ||
error_mgs = None | ||
except Exception as e: | ||
point_cloud = None | ||
error_mgs = str(e) | ||
|
||
return {"name": fname, "point-cloud": point_cloud, "error_mgs": error_mgs} | ||
|
||
|
||
def main(*args, **kwargs): | ||
parser = argparse.ArgumentParser( | ||
description="Convert specified AIMS volume files into point clouds and store them in one compressed numpy file.") | ||
parser.add_argument( | ||
"input_path", help="The path to the volume to convert (wildcards are admitted m e.g. *.nii)", nargs='*', type=str) | ||
parser.add_argument("-o", "--output_path", | ||
help="The path of the output file containing the bucket", type=str) | ||
args = parser.parse_args() | ||
|
||
out_path = args.output_path | ||
if out_path is None: | ||
out_path = "point_clouds.npz" | ||
base_out_dir = os.path.dirname(out_path) | ||
|
||
# check output path validity | ||
if not os.path.isdir(base_out_dir): | ||
log.critical(f'"{base_out_dir}" is not a valid directory') | ||
return 1 | ||
|
||
fun = volume_to_point_cloud | ||
|
||
with Pool(cpu_count() - 3) as pool: | ||
out = list(tqdm( | ||
pool.imap(fun, args.input_path), total=len(args.input_path))) | ||
|
||
pcs = {d['name']: d['point-cloud'] for d in out if d['error_mgs'] is None} | ||
errors = [d['error_mgs'] for d in out if d['error_mgs'] is not None] | ||
|
||
print("Creating output file...", end='') | ||
|
||
np.savez_compressed(out_path, **pcs) | ||
print("Done.") | ||
|
||
if len(errors) > 0: | ||
error_str = [f"There were {len(errors)} ERRORS:", *errors] | ||
log.error("\n".join(error_str)) |
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,2 @@ | ||
from . import nomenclature | ||
from . import data |
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,29 @@ | ||
|
||
import json | ||
import re | ||
|
||
class JsonData: | ||
"""Basic handling of json data""" | ||
|
||
def __init__(self, json_string): | ||
self.data = json.loads(json_string) | ||
|
||
def _filter_list(self, regex, iterable): | ||
"""Filter list items by regular expression""" | ||
return list(filter(regex.search, iterable)) | ||
|
||
def _filter_dict(self, regex, dictionnary): | ||
"""Filter dictionnary items by regular expression""" | ||
return {k:v for k,v in dictionnary.items() if regex.search(k)} | ||
|
||
|
||
def _filter(self, regular_expression, object): | ||
"""Filter objects by reuglar expression""" | ||
result = None | ||
regex = re.compile(regular_expression) | ||
if isinstance(object, dict): | ||
result = self._filter_dict(regex, object) | ||
elif isinstance(object, list): | ||
result = self._filter_list(regex, object) | ||
|
||
return result |
Oops, something went wrong.