Code Monkey home page Code Monkey logo

ptycho_gui's Introduction

BNL NSLS-II in-house ptychography software

Introduction

Installation

While one can pip install this pacakge directly, most likely the non-Python dependencies will not be available. For the time being, therefore, we recommend using Conda.

Installation on NSLS-II Beamline Machines

The instruction below is for admins who have the root priviledge to install the software at the system level so that all users logging in the machine can run the software directly without any setup.

Fully Automatic Installation (recommended)

Note: The instructions for automatic installation are out of date and may not work as expected.

  1. Create a new conda environment named ptycho_production: sudo /opt/conda/bin/conda create -p /opt/conda_envs/ptycho_production python=3.6 nsls2ptycho (If you need beamline-specific packages, such as hxntools for HXN, append the package names in the conda create command. This helps resolve possible conflict/downgrade issues.) The conda environment ptycho_production is activated under the hood using the run-ptycho script to be installed in the last step.
  2. fix_conda_privileges.sh
  3. sudo -i (switch to root)
  4. /opt/conda_envs/ptycho_production/bin/pip install 'cupy-cudaXX>=6.0.0b3', where XX is your CUDA toolkit version, available from nvcc --version
  5. If needed, copy the script run-ptycho in the root of this repo to /usr/local/bin/: sudo cp ./run-ptycho /usr/local/bin/

To update the software, simple do sudo conda update -n ptycho_production nsls2ptycho

Manual Installation

  1. Create a new conda environment named ptycho_production: sudo conda create -n ptycho_production -c conda-forge python=3.9 pyfftw pyqt=5 numpy scipy matplotlib pillow h5py databroker openmpi mpi4py cython. If you need beamline-specific packages, such as hxntools for HXN, append the package names in the conda create command. This helps resolve possible conflict/downgrade issues. The conda environment ptycho_production is activated under the hood using the run-ptycho script to be installed in Step 9.
  2. Activate the environment: conda activate ptycho_production.
  3. Install additional packages using pip: pip install posix_ipc.
  4. Make sure you are granted access to the backend, currently hosted in this private GitHub repo.
  5. Create a temporary workspace: mkdir /tmp/build_ptycho; cd /tmp/build_ptycho
  6. Clone from the mirror of this repo: git clone --recursive https://github.com/NSLS-II/ptycho_gui.git (During the process git may prompt you to enter your GitHub login and password for cloning the backend.)
  7. Move this repo to /usr/local/: sudo mv ./ptycho_gui /usr/local/; cd /usr/local/ptycho_gui; rmdir /tmp/build_ptycho
  8. Install the GUI in "develop" mode: sudo /opt/conda_envs/ptycho_production/bin/pip install -e .
  9. Copy the script run-ptycho to /usr/local/bin/: sudo cp ./run-ptycho /usr/local/bin/

To update the software, simple go to the code location and do git pull there. Since we installed in the develop mode (with -e flag) the files are symlinked to the conda env, so any updates we do to the code will be immediately up online. This can also work as a way to do "hot fixes".

cd /usr/local/ptycho_gui/
sudo git pull origin master    # update frontend
cd ./nsls2ptycho/core/ptycho/
sudo git pull origin master    # update backend

Installation on Personal Machines

The procedure is similar to Manual Installation outlined above, except that it does not require sudo:

  1. Make sure you are granted access to the backend, currently hosted in this private GitHub repo
  2. git clone --recursive https://github.com/NSLS-II/ptycho_gui.git (during the process git will prompt you to enther your GitHub id and password for cloning the backend)
  3. Create the conda environment as described in steps 1-3 of instructions for Manual Installation (do not use sudo). Alternatively, the needed packages may be installed in the existing Conda environment.
  4. Activate the Conda environment.
  5. Enter the cloned directory: cd ./ptycho_gui.
  6. pip install . (or pip install -e .).

Execution

  1. Start the GUI: run-ptycho
  2. Spawn two MPI processes without GUI: mpirun -n 2 run-ptycho-backend input_file

Conventions

  1. The GUI writes a config file to ~/.ptycho_gui/
  2. Once the working directory is specified in the GUI, it assumes that all HDF5 files are stored there, and the outputs are written to working_dir/recon_results/SXXXXX/, where XXXXX is the scan-number string.
  3. A few compiled .cubin files are stored with the Python code

References

  • High-Performance Multi-Mode Ptychography Reconstruction on Distributed GPUs, Z. Dong, Y.-L. L. Fang et al., 2018 NYSDS, DOI:10.1109/NYSDS.2018.8538964

For users using the new solvers mADMM, PM, and APG, you are advised to cite additionlly the following paper:

License

MIT (subject to change)

Users are encouraged to cite the references above.

Maintainer

ptycho_gui's People

Contributors

dmgav avatar leofang avatar sungsooha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ptycho_gui's Issues

Pass the param dict directly to the backend?

Currently we maintain, effectively, 4 distinct dicts:

  1. https://github.com/leofang/ptycho_gui/blob/b64326d7d0db5f05bda684a55f295bf1fdeb57e2/nsls2ptycho/core/ptycho_param.py#L23
  2. An instance of the above Param() class here: https://github.com/leofang/ptycho_gui/blob/b64326d7d0db5f05bda684a55f295bf1fdeb57e2/nsls2ptycho/ptycho_gui.py#L104-L108
  3. configparser: https://github.com/leofang/ptycho/blob/b0b39bd849ad65842659e29dc446f9629fa1433e/utils.py#L8
  4. The backend ptycho_trans class https://github.com/leofang/ptycho/blob/b0b39bd849ad65842659e29dc446f9629fa1433e/ptycho_trans_ml.py#L79

This has caused a significant headache every time we want to pass new attributes from the fronted to the backend: We have to update all the four files above plus https://github.com/leofang/ptycho/blob/master/recon_ptycho_gui.py to get it work, which is too tedious from the maintenance point of view.

The goal is to maintain a single dictionary across both frontend and backend. Steps to resolve this are:

  • Instantiate the backend ptycho_trans class directly in the frontend, and dump its attributes to disk as before
  • Eliminate the intermediate Param class
  • Investigate the possibility of making the instance in-memory for the backend to reuse
  • Eliminate the usage of configparser

Release checklist for v1.1.4

Checklist

  • Bump software version
  • Write the changelog for the new release
  • Assign relevant issues and PRs to the corresponding milestone
  • Regenerate the C files from the Cython sources (if they are changed)
  • Update the submodule for the backend (if it is changed)
  • Mirror both the frontend and the backend to the NSLS-II internal Gitlab
  • Update NSLS-II Conda recipe and build the Conda package

problematic mmap size of shared memory on OS X

Although the Python mmap doc says

class mmap.mmap(fileno, length, flags=MAP_SHARED, prot=PROT_WRITE|PROT_READ, access=ACCESS_DEFAULT[, offset])
(Unix version) Maps length bytes from the file specified by the file descriptor fileno, and returns a mmap object. If length is 0, the maximum length of the map will be the current size of the file when mmap is called.

When it comes to Mac OS X, the length=0 rule seems to not respected and causes an OSError: [Errno 22] Invalid argument. We should instead set length to be the size of the allocated shared memory, available through shm.size. See, e.g., https://github.com/osvenskan/posix_ipc/blob/46eae821f56f364a3afd6d68b3562adeced3794a/tests/test_memory.py#L135-L165.

Close all windows at once

A special method should be added such that when clicking the x button on the main window, all windows will be closed (preferably with a confirmation dialogue). This would eliminate the nuisance of multiple clicking every time.

unify API for accessing databroker and provide implementation for other beamlines

Currently the GUI is tied to @NSLS-II-HXN, but other beamlines (such as @NSLS-II-CSX) may also want to use it. We should define some API for flexible coupling to beamline-specific databroker processing functions.

Part of the work will involve creating a switch of some kind to select beamline profiles. Also, defer imports of beamline-specific packages to the corresponding databroker wrappers (e.g., don't import hxntools when the main window starts).

Replace GPU buttons by a text field

For better flexibility, the GPU buttons (0, 1, 2, 3) should be replaced by a text field so that arbitrary selection of GPUs (ex: 0, 2-4, 6, 8; similar to how batch processing is specified) can be done. This also frees the limitation of 4 GPUs and avoids the need of auto detection (by giving the users full control).

Release checklist for v1.1.3

Checklist

  • Bump software version
  • Write the changelog for the new release
  • Assign relevant issues and PRs to the corresponding milestone
  • Regenerate the C files from the Cython sources (if they are changed)
  • Update the submodule for the backend (if it is changed)
  • Mirror both the frontend and the backend to the NSLS-II internal Gitlab
  • Update NSLS-II Conda recipe and build the Conda package

add more guards to the GUI

A number of examples:

  • disable algorithms not yet implemented in GPU when the GPU flag is checked
  • disable algorithms that do not support multi-mode (or multi-slice) if the corresponding flag is checked
  • warn for too large GPU batch size by estimating the GPU memory usage

support fully automatic build

In setup.py (and/or accompanying codes) we should:

  • detect existence of GPU and test if CUDA driver & toolkit are properly installed
  • detect existence of MPI library
  • detect existence of FFTW library
  • compile the CUDA code into ~/.ptycho_gui/ (can be avoided if we use RawKernel.compile() in the backend, see cupy/cupy/pull/1889); this would also involve moving configure.sh to the backend.
  • detect current beamline and install beamline-specific packages for accessing databroker
  • Choose the right CuPy version to install if GPU is to be used
  • other things?

support batch cropping

@xiaojinghuang requested this feature so that once we identify the ROI and hot pixels for the head of a series of scans, we can use batch mode to extract data from databroker (using the same ROI and hot pixels) and run the reconstruction.

better way to retrieve scan coordinates from databroker?

Which is better, robust, and perhaps backward compatible?

  1. from @xiaojinghuang's save_data_h5_mll_new.py
    if np.abs(angle) <= 45.:
        x = np.array(df['dssx'])
    else:
        x = np.array(df['dssz'])
    y = np.array(df['dssy'])
    points = np.zeros((2,num_frame))
    points[0,:] = x#[:500]
    points[1,:] = y#[:500]
  1. Current approach:
    https://github.com/leofang/ptycho_gui/blob/e1103377937fdfc89692096824c42b3df17b6801/nsls2ptycho/core/HXN_databroker.py#L40
    https://github.com/leofang/ptycho_gui/blob/e1103377937fdfc89692096824c42b3df17b6801/nsls2ptycho/core/HXN_databroker.py#L44
    https://github.com/leofang/ptycho_gui/blob/e1103377937fdfc89692096824c42b3df17b6801/nsls2ptycho/core/HXN_databroker.py#L85-L89

Attn: @xiaojinghuang

Release checklist for v1.3.0

Checklist

  • Bump software version
  • Write the changelog for the new release
  • Assign relevant issues and PRs to the corresponding milestone
  • Regenerate the C files from the Cython sources (if they are changed)
  • Update the submodule for the backend (if it is changed)
  • Mirror both the frontend and the backend to the NSLS-II internal Gitlab
  • Update NSLS-II Conda recipe and build the Conda package

better shared memory handling?

If the GUI crashes very violently, it may be possible that the shared memory is not cleaned up. Try do a search (based on ownership) and clean up at GUI start time?

Challenges:

  • this seems to work only for Linux (with /dev/shm)?

Is max size of shared memory segments limited on OS X?

On Mac OS X the default maximum size for each shared memory segment is 4MB:

leofang@Leos-MacBook-Pro:~/$ sysctl -A | grep shm
kern.sysv.shmmax: 4194304
kern.sysv.shmmin: 1
kern.sysv.shmmni: 32
kern.sysv.shmseg: 8
kern.sysv.shmall: 1024
machdep.pmap.hashmax: 21
security.mac.posixshm_enforce: 1
security.mac.sysvshm_enforce: 1
leofang@Leos-MacBook-Pro:~/$ ipcs -M
IPC status from <running system> as of Wed May 15 11:09:08 EDT 2019
shminfo:
	shmmax: 4194304	(max shared memory segment size)
	shmmin:       1	(min shared memory segment size)
	shmmni:      32	(max number of shared memory identifiers)
	shmseg:       8	(max shared memory segments per process)
	shmall:    1024	(max amount of shared memory in pages)

This will have to be considered if the reconstructed arrays are too large.

That being said, so far I don't find this causes any problem:

import posix_ipc
s1 = posix_ipc.SharedMemory('/xxx', flags=posix_ipc.O_CREAT, size=4194304+1) # 1 byte larger than maximum

Refs:
http://www.spy-hill.com/help/apple/SharedMemory.html
https://stackoverflow.com/questions/36595754/shared-memory-folder-in-mac-error-shm-invalid-argument

Release checklist for v1.2.0

Checklist

  • Bump software version
  • Write the changelog for the new release
  • Assign relevant issues and PRs to the corresponding milestone
  • Regenerate the C files from the Cython sources (if they are changed)
  • Update the submodule for the backend (if it is changed)
  • Mirror both the frontend and the backend to the NSLS-II internal Gitlab
  • Update NSLS-II Conda recipe and build the Conda package

load data from databroker and create an in-memory H5

Starting from v2.9.0, h5py supports in-memory H5, which means one can allocate a segment of shared memory, within which a H5 object can live. If the frontend can do this, it will help avoid reading large data from disk in the backend.

Thoughts:

  • to actually save time, we want to reuse the same H5 many times before closing the shared memory. Is this the most common use case?
  • before killing the in-memory object, should we write it to disk? (I think so)

H5 worker thread cannot be terminated at will

This is a follow-up of #26. The following code snippet was commented out because it would freeze the whole window:

#if self.roiWindow._worker_thread.isRunning():
# self.roiWindow._worker_thread.terminate()
# self.roiWindow._worker_thread.wait()

The offending call is thread.terminate(). As a result, currently during batch processing if one clicks "Stop", the H5 worker will still finish up the work in the background. Only the whole processing queue is killed.

If a more recent Qt/PyQt5 is available (at least 5.10) we should revisit this problem. I suspect it's the underlying implementation causing the problem.

Release checklist for v1.3.1

Checklist

  • Bump software version
  • Write the changelog for the new release
  • Assign relevant issues and PRs to the corresponding milestone
  • Regenerate the C files from the Cython sources (if they are changed)
  • Update the submodule for the backend (if it is changed)
  • Mirror both the frontend and the backend to the NSLS-II internal Gitlab
  • Update NSLS-II Conda recipe and build the Conda package

Bug in plotting errors

Observation from #20

matplotlib v3.0.3 doesn't seem to behave properly on line plots. The errors aren't plotted at all.

This is due to an unexpected code path introduced in 1d873d4. After initializing the mmap, the string "init_mmap" was mistakenly passed to the error windows, so subsequent data were (silently) rejected.

This does no harm:

Not sure if this is related: every time the start button is clicked, the following warning is always shown:

'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'. Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

stop sending irrelevant information to backend

Currently only intensity frames and scan positions are read from H5 in the backend, and the rest is read from the GUI. Therefore, it is possible that we are asking for more info than we actually need for completing the reconstruction.

TODO:

  • determine what belongs to "irrelevant information"
  • remove those elements from both frontend and backend (related: #7)
  • modify the functions involving reading data from databroker and writing to H5 (related: #9)

expand the monitor window

TODO:

  • add at least two more panels so that we can see amplitudes and phases for probe and object simultaneously
  • add a panel for showing how scanning points are split across MPI processes (ex: paint them in different colors)
  • add a panel for plotting the psi error

bug in image cropping

@xiaojinghuang reported that the cropped images do not seem right starting this Tuesday or Wednesday. While the production code was not changed this week, it might be related to the recent fix 96d5586.

Decouple HXN's functionalities from the GUI

A number of HXN-specific setups are deeply buried in the GUI. To extend the software they need to be modularized. This is the first step toward enabling different beamline configurations.

Relevant: #10, #32.

Pass true MPI size to ScanWindow

Currently we pass num_processes to ScanWindow:
https://github.com/leofang/ptycho_gui/blob/6e9ea6d5aea1a5f3642fd1acc20a8ba2c2d62718/nsls2ptycho/ptycho_gui.py#L550
However, if the MPI machine file is used, the actual MPI size is counted during initialization:
https://github.com/leofang/ptycho_gui/blob/9e62e62f409b495e83c8a24640f221704990eb58/nsls2ptycho/core/ptycho_recon.py#L99-L125
This workflow needs to be changed/refactored so that the correct size is passed to ScanWindow.

May be relevant: #21

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.