This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Adds radiography requirements 1. and 2.a #24
Open
DavidFair
wants to merge
1
commit into
master
Choose a base branch
from
Radiography_req_1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .operations import fits_to_variable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import glob | ||
| import os | ||
| import numpy as np | ||
| import scipp as sc | ||
| from astropy.io import fits | ||
|
|
||
|
|
||
| def _load_fits(fit_dir): | ||
| if not os.path.isdir(fit_dir): | ||
| raise RuntimeError(fit_dir + " is not directory") | ||
| stack = [] | ||
| path_length = len(fit_dir) + 1 | ||
| filenames = sorted(glob.glob(fit_dir + "/*.fits")) | ||
| nfiles = len(filenames) | ||
| count = 0 | ||
| print(f"Loading {nfiles} files from '{fit_dir}'") | ||
| for filename in filenames: | ||
| count += 1 | ||
| print('\r{0}: Image {1}, of {2}'.format(filename[path_length:], count, nfiles), end="") | ||
| with fits.open(os.path.join(fit_dir, filename)) as hdu: | ||
| data = hdu[0].data | ||
| stack.append(np.flipud(data)) | ||
|
|
||
| if len(stack) == 1: | ||
| # Fold away a dim | ||
| stack = stack[0] | ||
|
|
||
| return np.array(stack, dtype=np.float64) | ||
|
|
||
|
|
||
| def fits_to_variable(fits_dir, average_data=False): | ||
| """ | ||
| Loads all fits images from the directory into a scipp Variable. | ||
| """ | ||
| stack = _load_fits(fits_dir) | ||
| if average_data: | ||
| stack = stack.mean(axis=0) | ||
|
|
||
| if len(stack.shape) == 3: | ||
| return sc.Variable(["slice", "x", "y"], values=stack, variances=stack) | ||
|
|
||
| elif len(stack.shape) == 2: | ||
| return sc.Variable(["x", "y"], values=stack, variances=stack) | ||
| else: | ||
| raise IndexError("Expected 2 or 3 dimensions," | ||
| f" found {len(stack.shape)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": { | ||
| "collapsed": true, | ||
| "pycharm": { | ||
| "name": "#%% md\n" | ||
| } | ||
| }, | ||
| "source": [ | ||
| "# How to start\n", | ||
| "\n", | ||
| "Before starting you must:\n", | ||
| "- Ensure that `scipp` and `mantid` are on your `PYTHONPATH`.\n", | ||
| "- Generate the `config.py` file using `make_config.py`. Refer to the `README.md` or `python make_config.py --help` for information.\n", | ||
| "\n", | ||
| "For `scipp` and `mantid` follow instructions at: https://scipp.readthedocs.io/en/latest/getting-started/installation.html." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Some PYTHONPATH sanity checks\n", | ||
| "try:\n", | ||
| " import scipp\n", | ||
| " print(\"scipp found\")\n", | ||
| "except ImportError as e:\n", | ||
| " print(\"scipp is not available in the PYTHONPATH\")\n", | ||
| " raise e\n", | ||
| "\n", | ||
| "try:\n", | ||
| " import mantid\n", | ||
| " print(\"mantid found\")\n", | ||
| "except ImportError as e:\n", | ||
| " print(\"mantid is not available in the PYTHONPATH\")\n", | ||
| " raise e\n", | ||
| "\n", | ||
| "try:\n", | ||
| " import scippconfig\n", | ||
| " print(\"config found\")\n", | ||
| "except ImportError as e:\n", | ||
| " print(\"config is not available. Make sure you have generated it with `make_config.py`.\")\n", | ||
| " raise e" | ||
| ], | ||
| "metadata": { | ||
| "collapsed": false, | ||
| "pycharm": { | ||
| "name": "#%%\n" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import os\n", | ||
| "import radiography\n", | ||
| "import scipp as sc\n", | ||
| "import numpy as np" | ||
| ], | ||
| "metadata": { | ||
| "collapsed": false, | ||
| "pycharm": { | ||
| "name": "#%%\n" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "outputs": [], | ||
| "source": [ | ||
| "experiment_dir = scippconfig.script_root\n", | ||
| "\n", | ||
| "data_dir = os.path.join(experiment_dir, 'radiography', 'IMAT_sample')\n", | ||
| "\n", | ||
| "# Since we don't have reference data pretend this folder contains our ref\n", | ||
| "dark_field_dir = os.path.join(data_dir, \"angle0\")\n", | ||
| "flat_field_dir = os.path.join(data_dir, \"Flats\", \"RB1730044\", \"Tomo_test\", \"OpenBeam_aft1\")\n", | ||
| "sample_dir = os.path.join(data_dir, \"angle1\")" | ||
| ], | ||
| "metadata": { | ||
| "collapsed": false, | ||
| "pycharm": { | ||
| "name": "#%%\n" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "source": [ | ||
| "raw_data = sc.Dataset()\n", | ||
| "raw_data[\"sample\"] = radiography.fits_to_variable(sample_dir)\n", | ||
| "\n", | ||
| "# Set coords based on sample dims, as reference needs to match\n", | ||
| "raw_data.coords[\"projection\"] = sc.Variable([\"projection\"], unit=sc.units.dimensionless, values=np.arange(0, raw_data[\"sample\"].shape[0]))\n", | ||
| "raw_data.coords[\"x\"] = sc.Variable([\"x\"], unit=sc.units.dimensionless, values=np.arange(0, raw_data[\"sample\"].shape[1]))\n", | ||
| "raw_data.coords[\"y\"] = sc.Variable([\"y\"], unit=sc.units.dimensionless, values=np.arange(0, raw_data[\"sample\"].shape[2]))" | ||
| ], | ||
| "metadata": { | ||
| "collapsed": false, | ||
| "pycharm": { | ||
| "name": "#%%\n" | ||
| } | ||
| }, | ||
| "execution_count": null, | ||
| "outputs": [] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "outputs": [], | ||
| "source": [ | ||
| "dark_field = radiography.fits_to_variable(dark_field_dir, average_data=True)\n", | ||
| "flat_field = radiography.fits_to_variable(flat_field_dir, average_data=True)\n", | ||
| "\n", | ||
| "# This blows through 32GB of memory as raw_data[\"sample\"] is already ~17 GB\n", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you implement below in place to avoid memory overhead of temporaries? or similar |
||
| "raw_data[\"transmission\"] = (raw_data[\"sample\"] - dark_field) / (flat_field - dark_field)" | ||
| ], | ||
| "metadata": { | ||
| "collapsed": false, | ||
| "pycharm": { | ||
| "name": "#%%\n", | ||
| "is_executing": true | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# TODO WIP below\n", | ||
| "roi_x = (100, 200)\n", | ||
| "roi_y = (100, 200)\n", | ||
| "\n", | ||
| "raw_data[\"roi_sample\"] = raw_data[\"sample\"]\n", | ||
| "\n", | ||
| "raw_data.masks[\"sample\"] = sc.Variable(['x', 'y'], shape=(raw_data.coords[\"x\"].shape[0], raw_data.coords[\"y\"].shape[0]),\n", | ||
| " dtype=sc.dtype.bool)" | ||
| ], | ||
| "metadata": { | ||
| "collapsed": false, | ||
| "pycharm": { | ||
| "name": "#%%\n" | ||
| } | ||
| } | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "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 | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Download data (specify own-cloud location) as a step?