Skip to content

Commit 5914c9e

Browse files
authored
Merge pull request #20 from Project-MONAI/moving-code-from-minimal
moving monai-minimal prototype code to the main repo
2 parents 42108b3 + a106c79 commit 5914c9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2609
-0
lines changed

.flake8

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[flake8]
2+
select = B,C,E,F,N,P,T4,W,B9
3+
max-line-length = 120
4+
# C408 ignored because we like the dict keyword argument syntax
5+
# E501 is not flexible enough, we're using B950 instead
6+
ignore =
7+
E203,E305,E402,E501,E721,E741,F403,F405,F821,F841,F999,W503,W504,C408,E302,W291,E303,
8+
# these ignores are from flake8-bugbear; please fix!
9+
B007,B008,
10+
# these ignores are from flake8-comprehensions; please fix!
11+
C400,C401,C402,C403,C404,C405,C407,C411,C413,C414,C415,C416
12+
per-file-ignores = __init__.py: F401
13+
exclude = docs/src,*.pyi,.git

.github/workflows/pythonapp.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Python application
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
- name: Set up Python 3.7
13+
uses: actions/setup-python@v1
14+
with:
15+
python-version: 3.7
16+
- name: Install dependencies
17+
run: |
18+
python -m pip install --upgrade pip
19+
pip install -r requirements.txt
20+
- name: Lint with flake8
21+
run: |
22+
pip install flake8
23+
pip install pep8-naming
24+
# stop the build if there are Python syntax errors or undefined names
25+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
26+
# exit-zero treats all errors as warnings.
27+
flake8 . --count --statistics
28+
# - name: Test with pytest
29+
# run: |
30+
# pip install pytest
31+
# pytest

.gitignore

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/
105+
examples/scd_lvsegs.npz

examples/cardiac_segmentation.ipynb

Lines changed: 295 additions & 0 deletions
Large diffs are not rendered by default.

monai/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# monai
3+
4+
The framework is divided into a few large modules:
5+
6+
* **application**: Contains the `NetworkManager` type, monitors, and utilities
7+
8+
* **data**: Contains the data stream definition types and example datasets
9+
10+
* **networks**: Contains network definitions, component definitions, and Pytorch specific utilities
11+
12+
* **test**: Contains the unit tests for the framework
13+
14+
* **utils**: Contains a set of utility files for doing namespace aliasing, auto module loading, and other facilities

monai/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os, sys
2+
from .utils.moduleutils import loadSubmodules
3+
4+
5+
__copyright__ = "(c) 2019 MONAI Consortium"
6+
__version__tuple__ = (0, 0, 1)
7+
__version__ = "%i.%i.%i" % (__version__tuple__)
8+
9+
__basedir__ = os.path.dirname(__file__)
10+
11+
12+
loadSubmodules(sys.modules[__name__], False) # load directory modules only, skip loading individual files
13+
loadSubmodules(sys.modules[__name__], True) # load all modules, this will trigger all export decorations

monai/application/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# Application
3+
4+
Contains the code for managing the training process. This includes the `NetworkManager` and its subclasses, monitors,
5+
and other facilities.
6+
7+
* **config**: This has a few facilities for configuration and diagnostic output.
8+
9+
* **metrics**: Defines metric tracking types.
10+
11+
* **handlers**: Defines handlers for implementing functionality at various stages in the training process.
12+
13+
* **engine**: Eventually will have Engine-derived classes for extending Ignite behaviour.

monai/application/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

monai/application/config/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os, sys
2+
from collections import OrderedDict
3+
import monai
4+
import numpy as np
5+
import torch
6+
7+
try:
8+
import ignite
9+
ignite_version=ignite.__version__
10+
except ImportError:
11+
ignite_version='NOT INSTALLED'
12+
13+
export = monai.utils.export("monai.application.config")
14+
15+
16+
@export
17+
def getConfigValues():
18+
output = OrderedDict()
19+
20+
output["MONAI version"] = monai.__version__
21+
output["Python version"] = sys.version.replace("\n", " ")
22+
output["Numpy version"] = np.version.full_version
23+
output["Pytorch version"] = torch.__version__
24+
output["Ignite version"] = ignite_version
25+
26+
return output
27+
28+
29+
@export
30+
def printConfig(file=sys.stdout):
31+
for kv in getConfigValues().items():
32+
print("%s: %s" % kv, file=file, flush=True)
33+
34+
35+
@export
36+
def setVisibleDevices(*devInds):
37+
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(map(str, devInds))

monai/application/engine/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

monai/application/handlers/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from collections import defaultdict
2+
3+
import monai
4+
5+
6+
@monai.utils.export("monai.application.handlers")
7+
@monai.utils.alias("metriclogger")
8+
class MetricLogger:
9+
def __init__(self,loss_transform=lambda x:x, metric_transform=lambda x:x):
10+
self.loss_transform=loss_transform
11+
self.metric_transform=metric_transform
12+
self.loss=[]
13+
self.metrics=defaultdict(list)
14+
15+
def attach(self,engine):
16+
return engine.add_event_handler(monai.application.engine.Events.ITERATION_COMPLETED,self)
17+
18+
def __call__(self,engine):
19+
self.loss.append(self.loss_transform(engine.state.output))
20+
21+
for m,v in engine.state.metrics.items():
22+
v=self.metric_transform(v)
23+
# # metrics may not be added on the first timestep, pad the list if this is the case
24+
# # so that each metric list is the same length as self.loss
25+
# if len(self.metrics[m])==0:
26+
# self.metrics[m].append([v[0]]*len(self.loss))
27+
28+
self.metrics[m].append(v)
29+

monai/application/metrics/__init__.py

Whitespace-only changes.

monai/data/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
# Data
3+
4+
This implements the data streams classes and contains a few example datasets. Data streams are iterables which produce
5+
single data items or batches thereof from source iterables (usually). Chaining these together is how data pipelines are
6+
implemented in the framework. Data augmentation routines are also provided here which can applied to data items as they
7+
pass through the stream, either singly or in parallel.
8+
9+
For example, the following stream reads image/segmentation pairs from `imSrc` (any iterable), applies the augmentations
10+
to convert the array format and apply simple augmentations (rotation, transposing, flipping, shifting) using mutliple
11+
threads, and wraps the whole stream in a buffering thread stream:
12+
13+
```
14+
def normalizeImg(im,seg):
15+
im=utils.arrayutils.rescaleArray(im)
16+
im=im[None].astype(np.float32)
17+
seg=seg[None].astype(np.int32)
18+
return im, seg
19+
20+
augs=[
21+
normalizeImg,
22+
augments.rot90,
23+
augments.transpose,
24+
augments.flip,
25+
partial(augments.shift,dimFract=5,order=0,nonzeroIndex=1),
26+
]
27+
28+
src=data.augments.augmentstream.ThreadAugmentStream(imSrc,200,augments=augs)
29+
src=data.streams.ThreadBufferStream(src)
30+
```
31+
32+
In this code, `src` is now going to yield batches of 200 images in a separate thread when iterated over. This can be
33+
fed directly into a `NetworkManager` class as its `src` parameter.
34+
35+
Module breakdown:
36+
37+
* **augments**: Contains definitions and stream types for doing data augmentation. An augment is simply a callable which
38+
accepts one or more Numpy arrays and returns the augmented result. The provided decorators are for adding probability
39+
and other facilities to a function.
40+
41+
* **readers**: Subclasses of `DataStream` for reading data from arrays and various file formats.
42+
43+
* **streams**: Contains the definitions of the stream classes which implement a number of operations on streams. The
44+
root of the stream classes is `DataStream` which provides a very simple iterable facility. It iterates over its `src`
45+
member, passes each item into its `generate()` generator method and yields each resulting value. This allows subclasses
46+
to implement `generate` to modify data as it moves through the stream. The `streamgen` decorator is provided to simplify
47+
this by being applied to a generator function to fill this role in a new object. Other subclasses implement buffering,
48+
batching, merging from multiple sources, cycling between sources, prefetching, and fetching data from the source in a
49+
separate thread.
50+

monai/data/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

monai/data/augments/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

0 commit comments

Comments
 (0)