Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes/enhancement to ALS readers #25

Merged
merged 9 commits into from
May 12, 2016
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ coverage.xml
*.sublime-workspace
docs/trunk/*
.spyder*
.idea

# Francesco's #
################
Expand Down
103 changes: 52 additions & 51 deletions dxchange/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
logger = logging.getLogger(__name__)


def read_als_832(fname, ind_tomo=None, normalized=False):
def read_als_832(fname, ind_tomo=None, normalized=False, sino=None):
"""
Read ALS 8.3.2 standard data format.

Expand All @@ -104,6 +104,9 @@ def read_als_832(fname, ind_tomo=None, normalized=False):
8.3.2 has a plugin that normalization is preferred to be
done with prior to tomopy reconstruction.

sino : {sequence, int}, optional
Specify sinograms to read. (start, end, step)

Returns
-------
ndarray
Expand All @@ -120,8 +123,6 @@ def read_als_832(fname, ind_tomo=None, normalized=False):
fname = os.path.abspath(fname)

if not normalized:
fname = fname.split(
'output')[0] + fname.split('/')[len(fname.split('/')) - 1]
tomo_name = fname + '_0000_0000.tif'
flat_name = fname + 'bak_0000.tif'
dark_name = fname + 'drk_0000.tif'
Expand Down Expand Up @@ -157,7 +158,8 @@ def read_als_832(fname, ind_tomo=None, normalized=False):
ind_dark = list(range(0, ndark))

# Read image data from tiff stack.
tomo = dxreader.read_tiff_stack(tomo_name, ind=ind_tomo, digit=4)
tomo = dxreader.read_tiff_stack(tomo_name, ind=ind_tomo, digit=4,
slc=(sino, None))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know this code, but that sometime you are using (sino, None) and other time (None, sino) is a really bad code smell.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell it does smell bad ... thank you for point it out. For some reason we are flipping the order between datasets consisting of multiple files (tiff, xrm) vs single files (hdf, netcdf etc.). We should make it consistent.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dgursoy fix seems easy, just swap the loop indexing at

def _shape_after_slice(shape, slc):

but will make it not back compatible. Suggestions?


if not normalized:

Expand Down Expand Up @@ -190,9 +192,10 @@ def read_als_832(fname, ind_tomo=None, normalized=False):
dy, dz = _arr.shape
flat = np.zeros((dx, dy, dz))
flat[m] = _arr
flat = dxreader._slice_array(flat, None)
flat = dxreader._slice_array(flat, (None, sino))
else:
flat = dxreader.read_tiff_stack(flat_name, ind=ind_flat, digit=4)
flat = dxreader.read_tiff_stack(flat_name, ind=ind_flat, digit=4,
slc=(sino, None))

# Adheres to 8.3.2 flat/dark naming conventions:
# ----Darks----
Expand All @@ -215,7 +218,7 @@ def read_als_832(fname, ind_tomo=None, normalized=False):
dy, dz = _arr.shape
dark = np.zeros((dx, dy, dz))
dark[m] = _arr
dark = dxreader._slice_array(dark, None)
dark = dxreader._slice_array(dark, (None, sino))
else:
flat = np.ones(1)
dark = np.zeros(1)
Expand Down Expand Up @@ -263,63 +266,61 @@ def read_als_832h5(fname, ind_tomo=None, ind_flat=None, ind_dark=None,
Indices of flat field data within tomography projection list
"""

dgroup = dxreader.find_dataset_group(fname)
dname = dgroup.name.split('/')[-1]
with dxreader.find_dataset_group(fname) as dgroup:
dname = dgroup.name.split('/')[-1]

tomo_name = dname + '_0000_0000.tif'
flat_name = dname + 'bak_0000.tif'
dark_name = dname + 'drk_0000.tif'
tomo_name = dname + '_0000_0000.tif'
flat_name = dname + 'bak_0000.tif'
dark_name = dname + 'drk_0000.tif'

# Read metadata from dataset group attributes
keys = list(dgroup.attrs.keys())
if 'nangles' in keys:
nproj = int(dgroup.attrs['nangles'])
if 'i0cycle' in keys:
inter_bright = int(dgroup.attrs['i0cycle'])
if 'num_bright_field' in keys:
nflat = int(dgroup.attrs['num_bright_field'])
else:
nflat = dxreader._count_proj(dgroup, flat_name, nproj,
inter_bright=inter_bright)
if 'num_dark_fields' in keys:
ndark = int(dgroup.attrs['num_dark_fields'])
else:
ndark = dxreader._count_proj(dgroup, dark_name, nproj)
# Read metadata from dataset group attributes
keys = list(dgroup.attrs.keys())
if 'nangles' in keys:
nproj = int(dgroup.attrs['nangles'])
if 'i0cycle' in keys:
inter_bright = int(dgroup.attrs['i0cycle'])
if 'num_bright_field' in keys:
nflat = int(dgroup.attrs['num_bright_field'])
else:
nflat = dxreader._count_proj(dgroup, flat_name, nproj,
inter_bright=inter_bright)
if 'num_dark_fields' in keys:
ndark = int(dgroup.attrs['num_dark_fields'])
else:
ndark = dxreader._count_proj(dgroup, dark_name, nproj)

# Create arrays of indices to read projections, flats and darks
if ind_tomo is None:
ind_tomo = list(range(0, nproj))
ind_dark = list(range(0, ndark))
group_dark = [nproj - 1]
ind_flat = list(range(0, nflat))

if inter_bright > 0:
group_flat = list(range(0, nproj, inter_bright))
if group_flat[-1] != nproj - 1:
group_flat.append(nproj - 1)
elif inter_bright == 0:
group_flat = [0, nproj - 1]
else:
group_flat = None
# Create arrays of indices to read projections, flats and darks
if ind_tomo is None:
ind_tomo = list(range(0, nproj))
ind_dark = list(range(0, ndark))
group_dark = [nproj - 1]
ind_flat = list(range(0, nflat))

tomo = dxreader.read_hdf5_stack(
dgroup, tomo_name, ind_tomo, slc=(proj, sino))
if inter_bright > 0:
group_flat = list(range(0, nproj, inter_bright))
if group_flat[-1] != nproj - 1:
group_flat.append(nproj - 1)
elif inter_bright == 0:
group_flat = [0, nproj - 1]
else:
group_flat = None

flat = dxreader.read_hdf5_stack(dgroup, flat_name, ind_flat, slc=(None, sino),
out_ind=group_flat)
tomo = dxreader.read_hdf5_stack(
dgroup, tomo_name, ind_tomo, slc=(proj, sino))

dark = dxreader.read_hdf5_stack(dgroup, dark_name, ind_dark, slc=(None, sino),
out_ind=group_dark)
flat = dxreader.read_hdf5_stack(dgroup, flat_name, ind_flat, slc=(None, sino),
out_ind=group_flat)

group_flat = dxreader._map_loc(ind_tomo, group_flat)
dark = dxreader.read_hdf5_stack(dgroup, dark_name, ind_dark, slc=(None, sino),
out_ind=group_dark)

return tomo, flat, dark, group_flat
return tomo, flat, dark


def read_anka_topotomo(
fname, ind_tomo, ind_flat, ind_dark, proj=None, sino=None):
"""
Read ANKA TOMO-TOMO standard data format.
Read ANKA TOPO-TOMO standard data format.

Parameters
----------
Expand Down
6 changes: 4 additions & 2 deletions dxchange/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import re
import math
import struct
from contextlib import contextmanager
import dxchange.writer as writer
from dxchange.dtype import empty_shared_array

Expand Down Expand Up @@ -600,6 +601,7 @@ def _list_file_stack(fname, ind, digit):
list_fname.append(str(body + '{0:0={1}d}'.format(m, digit) + ext))
return list_fname

@contextmanager
def find_dataset_group(fname):
"""
Finds the group name containing the stack of projections datasets within
Expand All @@ -614,8 +616,8 @@ def find_dataset_group(fname):
-------
h5py.Group
"""
h5object = h5py.File(fname, 'r')
return _find_dataset_group(h5object)
with h5py.File(fname, 'r') as h5object:
yield _find_dataset_group(h5object)


def _find_dataset_group(h5object):
Expand Down