diff --git a/CDTools/datasets.py b/CDTools/datasets.py index 2a5edf3..f6be09d 100644 --- a/CDTools/datasets.py +++ b/CDTools/datasets.py @@ -4,7 +4,11 @@ import torch as t from copy import copy from CDTools.tools import data as cdtdata +from CDTools.tools import plotting from torch.utils import data as torchdata +from matplotlib import pyplot as plt +from matplotlib.widgets import Slider +from matplotlib import ticker __all__ = ['CDataset', 'Ptycho_2D_Dataset'] @@ -178,3 +182,115 @@ class Ptycho_2D_Dataset(CDataset): super(Ptycho_2D_Dataset,self).to_cxi(cxi_file) cdtdata.add_data(cxi_file, self.patterns, axes=self.axes) cdtdata.add_ptycho_translations(cxi_file, self.translations) + + + def inspect(self): + fig, axes = plt.subplots(1,2,figsize=(8,5.3)) + fig.tight_layout(rect=[0.04, 0.09, 0.98, 0.96]) + axslider = plt.axes([0.15,0.06,0.75,0.03]) + + translations = self.translations.detach().cpu().numpy() + nanomap_values = self.patterns.sum(dim=(1,2)).detach().cpu().numpy() + + def update_colorbar(im): + # If the update brought the colorbar out of whack + # (say, from clicking back in the navbar) + # Holy fuck this was annoying. Sorry future for how + # crappy this solution is. + if hasattr(im, 'norecurse') and im.norecurse: + im.norecurse=False + return + + im.norecurse=True + im.colorbar.set_clim(vmin=np.min(im.get_array()),vmax=np.max(im.get_array())) + im.colorbar.ax.set_ylim(0,1) + im.colorbar.set_ticks(ticker.LinearLocator(numticks=5)) + im.colorbar.draw_all() + + + def update(idx): + idx = int(idx) % len(self) + fig.pattern_idx = idx + updating = True if len(axes[1].images) >= 1 else False + + inputs, output = self[idx] + meas_data = output.detach().cpu().numpy() + if hasattr(self, 'mask') and self.mask is not None: + mask = self.mask.detach().cpu().numpy() + else: + mask = 1 + + if not updating: + print('hi') + axes[0].set_title('Nanomap') + axes[1].set_title('Pattern') + + bbox = axes[0].get_window_extent().transformed(fig.dpi_scale_trans.inverted()) + + s = bbox.width * bbox.height / translations.shape[0] * 72**2 #72 is points per inch + s /= 4 # A rough value to make the size work out + s = np.ones(len(nanomap_values)) * s + + s[idx] *= 4 + + nanomap = axes[0].scatter(1e6 * translations[:,0],1e6 * translations[:,1],s=s,c=nanomap_values) + + axes[0].invert_xaxis() + axes[0].set_facecolor('k') + axes[0].set_xlabel('Translation x (um)') + axes[0].set_ylabel('Translation y (um)') + cb1 = plt.colorbar(nanomap, ax=axes[0], orientation='horizontal',format='%.2e',ticks=ticker.LinearLocator(numticks=5),pad=0.15,fraction=0.1) + cb1.ax.tick_params(labelrotation=20) + + meas = axes[1].imshow(meas_data * mask) + + cb2 = plt.colorbar(meas, ax=axes[1], orientation='horizontal',format='%.2e',ticks=ticker.LinearLocator(numticks=5),pad=0.15,fraction=0.1) + cb2.ax.tick_params(labelrotation=20) + cb2.ax.callbacks.connect('xlim_changed', lambda ax: update_colorbar(meas)) + + else: + axes[0].set_title('Nanomap') + bbox = axes[0].get_window_extent().transformed(fig.dpi_scale_trans.inverted()) + + s = bbox.width * bbox.height / translations.shape[0] * 72**2 #72 is points per inch + s /= 4 # A rough value to make the size work out + s = np.ones(len(nanomap_values)) * s + s[idx] *= 4 + + axes[0].clear() + nanomap = axes[0].scatter(1e6 * translations[:,0],1e6 * translations[:,1],s=s,c=nanomap_values) + axes[0].invert_xaxis() + axes[0].set_facecolor('k') + axes[0].set_xlabel('Translation x (um)') + axes[0].set_ylabel('Translation y (um)') + + + + meas = axes[1].images[-1] + meas.set_data(meas_data * mask) + update_colorbar(meas) + + + # This is dumb but the slider doesn't work unless a reference to it is + # kept somewhere... + self.slider = Slider(axslider, 'Pattern #', 0, len(self)-1, valstep=1, valfmt="%d") + self.slider.on_changed(update) + + def on_action(event): + if not hasattr(event, 'button'): + event.button = None + if not hasattr(event, 'key'): + event.key = None + + if event.key == 'up' or event.button == 'up': + update(fig.pattern_idx - 1) + elif event.key == 'down' or event.button == 'down': + update(fig.pattern_idx + 1) + self.slider.set_val(fig.pattern_idx) + plt.draw() + + fig.canvas.mpl_connect('key_press_event',on_action) + fig.canvas.mpl_connect('scroll_event',on_action) + update(0) + + diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..a42a7e0 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +SPHINXPROJ = CDTools +SOURCEDIR = source +BUILDDIR = build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..d22dfe8 --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,172 @@ +# -*- coding: utf-8 -*- +# +# Configuration file for the Sphinx documentation builder. +# +# This file does only contain a selection of the most common options. For a +# full list see the documentation: +# http://www.sphinx-doc.org/en/master/config + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +import os +import sys +sys.path.insert(0, os.path.abspath('../..')) + + +# -- Project information ----------------------------------------------------- + +project = 'CDTools' +copyright = '2019, Abraham Levitan' +author = 'Abraham Levitan' + +# The short X.Y version +version = '0.1' +# The full version, including alpha/beta/rc tags +release = '0.1.0' + + +# -- General configuration --------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +# +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.todo', + 'sphinx.ext.mathjax', + 'sphinx.ext.napoleon', + 'sphinxarg.ext', +] + + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# +# source_suffix = ['.rst', '.md'] +source_suffix = '.rst' + +# The master toctree document. +master_doc = 'index' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path . +exclude_patterns = [] + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = 'alabaster' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +# +# html_theme_options = {} + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Custom sidebar templates, must be a dictionary that maps document names +# to template names. +# +# The default sidebars (for documents that don't match any pattern) are +# defined by theme itself. Builtin themes are using these templates by +# default: ``['localtoc.html', 'relations.html', 'sourcelink.html', +# 'searchbox.html']``. +# +# html_sidebars = {} + + +# -- Options for HTMLHelp output --------------------------------------------- + +# Output file base name for HTML help builder. +htmlhelp_basename = 'ADCDdoc' + + +# -- Options for LaTeX output ------------------------------------------------ + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + (master_doc, 'ADCD.tex', 'ADCD Documentation', + 'Abraham Levitan', 'manual'), +] + + +# -- Options for manual page output ------------------------------------------ + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + (master_doc, 'adcd', 'ADCD Documentation', + [author], 1) +] + + +# -- Options for Texinfo output ---------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + (master_doc, 'ADCD', 'ADCD Documentation', + author, 'ADCD', 'One line description of project.', + 'Miscellaneous'), +] + + +# -- Extension configuration ------------------------------------------------- + +# -- Options for todo extension ---------------------------------------------- + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = True + + +autoclass_content = 'both' diff --git a/docs/source/index.rst b/docs/source/index.rst new file mode 100644 index 0000000..60636c3 --- /dev/null +++ b/docs/source/index.rst @@ -0,0 +1,23 @@ + +Introduction to CDTools +======================= + +CDTools is a python library for autodifferentiation-based coherent diffractive imaging reconstructions. The core of the library is a set of simple tools built in pytorch for basic operations relevant to coherent diffraction - math operations on complex numbers, subpixel shifts, propagators, and the like. In addition, a set of database types exist to help with easily loading and saving data to/from .cxi files. Finally, a collection of ptychography models are implemented which allow for a variety of different styles of ptychographic reconstructions. + + +Documentation Overview +====================== + +.. toctree:: + :maxdepth: 2 + + installation + + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/docs/source/installation.rst b/docs/source/installation.rst new file mode 100644 index 0000000..6bd191d --- /dev/null +++ b/docs/source/installation.rst @@ -0,0 +1,49 @@ +How to Get +========== + +CDTools can be downloaded from it's `MIT github page`_, and the relevant prerequisites can be downloaded via pip, conda, or most python package managers. + +.. _`MIT github page`: https://github.mit.edu/Scattering/ADCD + + +Access to the MIT github page can be granted to any member of the MIT community - if you are not a member, access can be arranged through a guest account on the github enterprise server + +Prerequisites +------------- + +CDTools has the following prerequisites: + + * `numpy `_ + * `scipy `_ + * `matplotlib `_ + * `pytorch `_ + * `python-dateutil `_ + * `h5py `_ + +All of these can be installed via pip or conda. It is required that pytorch is ilt with MKL, as that enables FFTs. Additionally, CUDA support in pytorch is recommended for running any serious reconstructions with the package. The code is written to be python 2.7+ compatible, although it is only tested in python 3. + +Finally, to run the tests, pytest is required, and to build the docs, sphinx and sphinx-argparse are required. + + +Installation +------------ + +CDTools can be installed via pip, although it is recommended to install it in development mode as changes to the code are pushed to the MIT github quite frequently. + +To install in developer mode, run the following command from the top level directory (the directory including the setup.py file) + +.. code:: bash + + $ pip install -e . + + +Run The Tests +------------- + +To ensure that the installation has worked correctly, it is recommended to run the unit tests. After ensuring that `pytest `_ is installed, run the following command from the top level directory: + +.. code:: bash + + $ pytest + + diff --git a/examples/gold_ball_ptycho.py b/examples/gold_ball_ptycho.py index 4928c6d..bdf70f3 100644 --- a/examples/gold_ball_ptycho.py +++ b/examples/gold_ball_ptycho.py @@ -32,5 +32,6 @@ with open('example_reconstructions/gold_balls.pickle', 'wb') as f: pickle.dump(model.save_results(dataset),f) model.inspect(dataset) +dataset.inspect() model.compare(dataset) plt.show() diff --git a/examples/specular_pinhole_ptycho.py b/examples/specular_pinhole_ptycho.py index 7d058cb..89708ff 100644 --- a/examples/specular_pinhole_ptycho.py +++ b/examples/specular_pinhole_ptycho.py @@ -41,5 +41,6 @@ for i, loss in enumerate(model.Adam_optimize(250, dataset,batch_size=5)): model.inspect(dataset) +dataset.inspect() model.compare(dataset) plt.show()