import SimpleITK as sitk

from downloaddata import fetch_data, fetch_data_all

print(sitk.Version())

"""
Explanation: <h1 align="center">Testing SimpleITK Setup</h1>

Check that SimpleITK and auxilliary program(s) are correctly installed in your environment, and that you have the SimpleITK version which you expect (<b>requires network connectivty</b>).
You can optionally download all of the data used in the notebooks in advance. This step is only necessary if you expect to run the notebooks without network connectivity.
End of explanation
"""


from __future__ import print_function
import importlib
from distutils.version import LooseVersion

required_packages = {'IPython' : '4.0.0',
                     'numpy' : '1.9.2',
                     'matplotlib' : '1.4.2',
                     'ipywidgets' : '4.0.x' 
                    }

problem_packages = list()
# Iterate over the required packages: If the package is not installed
# ignore the exception. If it is installed check the version and remove
# from dictionary. In the end the dictionary contains the packages
# that are not installed.
for package, required_version in required_packages.items():
    try:
        p = importlib.import_module(package)        
        # Current release of ipywidgets has a bug with the __version__
        # attribute. This was fixed in master, so for now we do not
        # check ipywidgets version.
        if package != 'ipywidgets':
            installed_version = LooseVersion(p.__version__)
            required_version = LooseVersion(required_version)
            if installed_version < required_version:
                print('{0} - required version: {1} installed version: {2}'.format(
                        p.__name__, required_version, installed_version))
                problem_packages.append(package)    
    except ImportError:
        problem_packages.append(package)
    
if len(problem_packages) is 0:
    print('All is well.')
else:
    print('The following packages are required but not installed: ' \
          + ', '.join(problem_packages))

"""
Explanation: The following cell checks that all expected packages and correct versions are installed. SimpleITK may possibly work with other versions of these packages, but these are the ones we rely on.
End of explanation
"""


# Uncomment the line below to change the default external viewer to your viewer of choice and test that it works.
#%env SITK_SHOW_COMMAND /Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP 

# Retrive an image from the network, read it and display using the external viewer
sitk.Show(sitk.ReadImage(fetch_data("SimpleITK.jpg")))

"""
Explanation: We expect that you have an external image viewer installed. The default viewer is <a href="http://imagej.net/Welcome">ImageJ</a>. If you have another viewer (i.e. ITK-SNAP or 3D Slicer) you will need to set an environment variable to point to it. This can be done from within a notebook as shown below.
End of explanation
"""


fetch_data_all("Data", "Data/manifest.json")

"""
Explanation: Download all of the data in advance if you expect to be working offline (may take a couple of minutes).
End of explanation
"""


