diff --git a/Examples/IPythonNotebooks/doc/IPythonNotebooks.dox b/Examples/IPythonNotebooks/doc/IPythonNotebooks.dox new file mode 100644 index 0000000000..5aeff46798 --- /dev/null +++ b/Examples/IPythonNotebooks/doc/IPythonNotebooks.dox @@ -0,0 +1,19 @@ +/** \page IPythonNotebooks Folder for examplary IPython notebooks. + +This folder contains little (or not so little) IPython notebooks, mainly for tutorial purposes. + +\par +E.g. the notebook SimpleSegmentationManipulation.ipynb in the subfolder segmentation shows how we can load and manipulate a segmentation in an IPython notebook and redisplay it in MITK. + +\par Other usefull notebooks + +SimpleITK: +a huge collection on how to do image processing with SimpleITK. + +VTK: +A nice blog post/notebook on how to use vtk in python. + + + +*/ diff --git a/Examples/PythonPackageExample/LICENSE b/Examples/PythonPackageExample/LICENSE new file mode 100644 index 0000000000..91aacba82c --- /dev/null +++ b/Examples/PythonPackageExample/LICENSE @@ -0,0 +1,38 @@ +======================================================================= +Copyright (c) 2003-2012 German Cancer Research Center, +Division of Medical and Biological Informatics +All rights reserved. + +Redistribution and use in source and binary forms, with or +without modification, are permitted provided that the +following conditions are met: + + * Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the + following disclaimer in the documentation and/or other + materials provided with the distribution. + + * Neither the name of the German Cancer Research Center, + nor the names of its contributors may be used to endorse + or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +======================================================================= \ No newline at end of file diff --git a/Examples/PythonPackageExample/Makefile b/Examples/PythonPackageExample/Makefile new file mode 100644 index 0000000000..bd393cbc38 --- /dev/null +++ b/Examples/PythonPackageExample/Makefile @@ -0,0 +1,11 @@ +init: + pip install -r requirements.txt + +test: + python -m unittest discover + +install_develop: + python setup.py develop + +install: + python setup.py install diff --git a/Examples/PythonPackageExample/README.rst b/Examples/PythonPackageExample/README.rst new file mode 100644 index 0000000000..81fd013040 --- /dev/null +++ b/Examples/PythonPackageExample/README.rst @@ -0,0 +1,68 @@ +Sample Module Repository +======================== + +This simple project is an example repo for Python projects. + +It is adapted from [this github example](https://github.com/kennethreitz/samplemod/tree/master). +[Learn more](). + +And tries to follow the instructions on [this guide](). + +The actual code can be found in sample/thinker.py. It implements a toy class +which "thinks" random thoughts. +The tests for this class are located in sample/tests. Python will automatically +discover tests if you name them test_*.py +Look at test_advanced.py for a simple unittest example. + +## install dependencies +either use: +``` +pip install -r requirements.txt +``` +or +``` +make init +``` + +## install this package +You have two choices: +1. install a development version. This means that if you make +changes to the packages code you make will affect the code you write which is +based on this package. +2. "normal" install. This will install the current version. + +After you installed your package, it can be imported in your via import sample +where ever you are. + +### install development version: +either use: +``` +python setup.py develop +``` +or +``` +make install_develop +``` + +### "normal" install +either use: +``` +python setup.py install +``` +or +``` +make install +``` + +## run tests +either run: +``` +python -m unittest discover +``` +or +``` +make test +``` + +## execute tutorial +In your console, navigate to the tutorials subfolder and start ipython notebook. diff --git a/Examples/PythonPackageExample/doc/pythonpackageexample.dox b/Examples/PythonPackageExample/doc/pythonpackageexample.dox new file mode 100644 index 0000000000..c674d8a325 --- /dev/null +++ b/Examples/PythonPackageExample/doc/pythonpackageexample.dox @@ -0,0 +1,73 @@ +/** \page Python package example. + +See README.rst for more info. +This is text was copied from README.rst (maybe not updated): + +Sample Module Repository +======================== + +This simple project is an example repo for Python projects. + +It is adapted from [this github example](https://github.com/kennethreitz/samplemod/tree/master). +[Learn more](). + +And tries to follow the instructions on [this guide](). + +The actual code can be found in sample/thinker.py. It implements a toy class +which "thinks" random thoughts. +The tests for this class are located in sample/tests. Python will automatically +discover tests if you name them test_*.py +Look at test_advanced.py for a simple unittest example. + +## install dependencies +either use: +``` +pip install -r requirements.txt +``` +or +``` +make init +``` + +## install this package +You have two choices: +1. install a development version. This means that if you make +changes to the packages code you make will affect the code you write which is +based on this package. +2. "normal" install. This will install the current version. + +After you installed your package, it can be imported in your via import sample +where ever you are. + +### install development version: +either use: +``` +python setup.py develop +``` +or +``` +make install_develop +``` + +### "normal" install +either use: +``` +python setup.py install +``` +or +``` +make install +``` + +## run tests +either run: +``` +python -m unittest discover +``` +or +``` +make test +``` + + +*/ diff --git a/Examples/PythonPackageExample/requirements.txt b/Examples/PythonPackageExample/requirements.txt new file mode 100644 index 0000000000..f3c7e8e6ff --- /dev/null +++ b/Examples/PythonPackageExample/requirements.txt @@ -0,0 +1 @@ +nose diff --git a/Examples/PythonPackageExample/samplepackage/__init__.py b/Examples/PythonPackageExample/samplepackage/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Examples/PythonPackageExample/samplepackage/helpers.py b/Examples/PythonPackageExample/samplepackage/helpers.py new file mode 100644 index 0000000000..e06baccab5 --- /dev/null +++ b/Examples/PythonPackageExample/samplepackage/helpers.py @@ -0,0 +1,24 @@ +""" +=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +=================================================================== +""" + +# this is a list of thoughts which is used by the thinker +thoughts = ["itk", "genius", "core", "blue", + "vtk", "grand", "berry", "delicious"] + +# it is only in a separate file to show how to +# import other modules in thinker.py diff --git a/Examples/PythonPackageExample/samplepackage/tests/__init__.py b/Examples/PythonPackageExample/samplepackage/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Examples/PythonPackageExample/samplepackage/tests/test_advanced.py b/Examples/PythonPackageExample/samplepackage/tests/test_advanced.py new file mode 100644 index 0000000000..a277354e63 --- /dev/null +++ b/Examples/PythonPackageExample/samplepackage/tests/test_advanced.py @@ -0,0 +1,53 @@ +""" +=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +=================================================================== +""" + +# -*- coding: utf-8 -*- + +import unittest + +from samplepackage.thinker import Thinker + + +class AdvancedTestSuite(unittest.TestCase): + """Advanced test cases.""" + + def setUp(self): + # like in JUnit/CppUnit we can define setUp and tearDown methods + # create a thinker with 5 thoughts: + self.nr_thoughts = 5 + self.thinker = Thinker(self.nr_thoughts) + + def test_thoughts(self): + """ + test if the correct number of thoughts is produced + :return: + """ + self.assertEquals(self.nr_thoughts, len(self.thinker.think()), + "correct number of thoughts is produced") + + def test_constructor(self): + """ + test if trying to construct a thinker without argument raises an error + :return: + """ + with self.assertRaises(TypeError): + Thinker() + + +if __name__ == '__main__': + unittest.main() diff --git a/Examples/PythonPackageExample/samplepackage/tests/test_basic.py b/Examples/PythonPackageExample/samplepackage/tests/test_basic.py new file mode 100644 index 0000000000..ae6f2303e2 --- /dev/null +++ b/Examples/PythonPackageExample/samplepackage/tests/test_basic.py @@ -0,0 +1,34 @@ +""" +=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +=================================================================== +""" + +# -*- coding: utf-8 -*- + + +import unittest + + +class BasicTestSuite(unittest.TestCase): + """Basic test cases. This is just a fake, go to test_advanced for a real + test""" + + def test_absolute_truth_and_meaning(self): + assert True + + +if __name__ == '__main__': + unittest.main() diff --git a/Examples/PythonPackageExample/samplepackage/thinker.py b/Examples/PythonPackageExample/samplepackage/thinker.py new file mode 100644 index 0000000000..9807f11d8f --- /dev/null +++ b/Examples/PythonPackageExample/samplepackage/thinker.py @@ -0,0 +1,76 @@ +''' +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ +''' + + +# -*- coding: utf-8 -*- + +# packages/modules included in pythons go here +import random +import logging + +# installed packages go here + +# our own modules/packages go here +from helpers import thoughts + + +# a free floating function +def get_thought(): + """Get a random thought.""" + return random.choice(thoughts) + + +class Thinker: + """ + A class which produces thoughts + """ + + def __init__(self, number_of_thoughts): + """ + This is a constructor. Define variables here. + If you do it in the classes namespace instead it will be equivalent to + a static variable in C++ + + :param number_of_thoughts: the number of thoughts we want to produce + :return: + """ + # underscore is standard for members in python: + self.number_of_thoughts = number_of_thoughts + + def think(self): + """ + Member function which uses the number_of_thoughts to "think + :return: a list of thoughts, length depending on your number of thoughts + """ + thought_list = [] + try: + thought_list = [get_thought() for _ in range(self.number_of_thoughts)] + except IndexError: + logging.error("Just showing off the logging module here") + return thought_list + + def think_out_loud(self): + """ + Think and print these thoughts on the console. + :return: + """ + current_thoughts = self.think() + print "currently I'm thinking of..." + for thought in current_thoughts: + print thought + diff --git a/Examples/PythonPackageExample/setup.py b/Examples/PythonPackageExample/setup.py new file mode 100755 index 0000000000..0061927670 --- /dev/null +++ b/Examples/PythonPackageExample/setup.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +from setuptools import setup, find_packages + + +with open('README.rst') as f: + readme = f.read() + +with open('LICENSE') as f: + license = f.read() + +setup( + name='samplepackage', + version='0.0.2', + description='Sample package adapted from Python-Guide.org for MITK', + long_description=readme, + # if you want, put your own name here + # (this would likely result in people sending you emails) + author='MBI@DKFZ', + author_email='mitk-users@lists.sourceforge.net', + url='http://mitk.org', + license=license, + packages=find_packages(exclude=('tests', 'doc', 'tutorials')), + # the requirements to install this project. + # Since this one is so simple this is empty. + install_requires=[] + # a more sophisticated project might have something like: + #install_requires=['numpy>=1.11.0', 'scipy>=0.17', 'scikit-learn'] +) + diff --git a/Examples/PythonPackageExample/tutorials/ThinkerExample.ipynb b/Examples/PythonPackageExample/tutorials/ThinkerExample.ipynb new file mode 100644 index 0000000000..0c25cbf1f1 --- /dev/null +++ b/Examples/PythonPackageExample/tutorials/ThinkerExample.ipynb @@ -0,0 +1,148 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is a very short example showing how a ipython notebook tutorial could look like" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# The next three line tell python where to look for the code\n", + "# in case the package was installed (e.g. make install) they are not necessary.\n", + "import os\n", + "import sys\n", + "sys.path.insert(0, os.path.abspath('..'))\n", + "\n", + "# import our great new class\n", + "from samplepackage.thinker import Thinker" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "currently I'm thinking of...\n", + "vtk\n", + "delicious\n", + "blue\n", + "berry\n", + "blue\n" + ] + } + ], + "source": [ + "# create a thinker and let it think\n", + "initial_nr_thoughts = 5\n", + "thinker = Thinker(initial_nr_thoughts)\n", + "thinker.think_out_loud()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "currently I'm thinking of...\n", + "core\n", + "vtk\n", + "core\n", + "genius\n", + "vtk\n" + ] + } + ], + "source": [ + "# our thinker will produce new thoughts every time we call it\n", + "thinker.think_out_loud()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "currently I'm thinking of...\n", + "core\n", + "genius\n" + ] + } + ], + "source": [ + "# we can also manipulate the number of thoughts it thinks\n", + "thinker.number_of_thoughts = 2\n", + "thinker.think_out_loud()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "That's it, what a great tutorial!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}