Skip to content

Commit

Permalink
Refs #53. first try
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin J committed Jul 26, 2016
1 parent cb14987 commit ee28e35
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
12 changes: 7 additions & 5 deletions python/imars3d/CT.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,19 @@ def recon(self, workdir=None, outdir=None, **kwds):
if self.clean_on_the_fly:
cropped.removeAll()
# correct tilt
for i in range(3):
MAX_TILT_ALLOWED = 0.05
NROUNDS = 3
for i in range(NROUNDS):
tilt_corrected, tilt = i3.correct_tilt(
pre, workdir=os.path.join(workdir, 'tilt-correction-%s' % i),
max_npairs=None, parallel=self.parallel_preprocessing)
if self.clean_on_the_fly:
pre.removeAll()
if abs(tilt) < .5: break
if abs(tilt) < MAX_TILT_ALLOWED: break
pre = tilt_corrected
continue
if abs(tilt) >= .5:
raise RuntimeError("failed to bring tilt down to less than .5 degrees in 3 rounds")
if abs(tilt) >= MAX_TILT_ALLOWED:
raise RuntimeError("failed to bring tilt down to less than %s degrees in %s rounds" % (MAX_TILT_ALLOWED, NROUNDS))
# reconstruct
self.reconstruct(tilt_corrected, workdir=workdir, outdir=outdir, **kwds)
return
Expand Down Expand Up @@ -217,7 +219,7 @@ def sniff(self):
return

CT_pattern_cache = "CT_PATTERN"
CT_angles_cache = "CT_ANGLES"
CT_angles_cache = "CT_ANGLES.npy"
def find_CT(self):
pattern_cache_path = os.path.join(self.workdir, self.CT_pattern_cache)
angles_cache_path = os.path.join(self.workdir, self.CT_angles_cache)
Expand Down
20 changes: 12 additions & 8 deletions python/imars3d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ def correct_tilt(ct_series, workdir='work', max_npairs=10, parallel=True):
tiltcalc = components.TiltCalculation(workdir=workdir, max_npairs=max_npairs)
tilt = tiltcalc(ct_series)

tiltcorrected_series = io.ImageFileSeries(
os.path.join(workdir, "tiltcorrected_%07.3f.tiff"),
identifiers = ct_series.identifiers,
name = "Tilt corrected CT", mode = 'w',
)
tiltcorr = components.TiltCorrection(tilt=tilt)
tiltcorr(ct_series, tiltcorrected_series, parallel=parallel)
return tiltcorrected_series, tilt
# only correct if the tilt is large
if abs(tilt)>0.002:
tiltcorrected_series = io.ImageFileSeries(
os.path.join(workdir, "tiltcorrected_%07.3f.tiff"),
identifiers = ct_series.identifiers,
name = "Tilt corrected CT", mode = 'w',
)
tiltcorr = components.TiltCorrection(tilt=tilt)
tiltcorr(ct_series, tiltcorrected_series, parallel=parallel)
return tiltcorrected_series, tilt
return ct_series, tilt


@dec.timeit
def correct_intensity_fluctuation(ct_series, workdir='work'):
Expand Down
23 changes: 19 additions & 4 deletions python/imars3d/tilt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

import os, numpy as np
import logging
from . import use_centers, phasecorrelation

def compute(ct_series, workdir, max_npairs=10, calculator=None):
def compute(ct_series, workdir, max_npairs=10):
from . import use_centers
calculator = use_centers.Calculator(sigma=10, maxshift=200)
tilt = _compute(
ct_series, os.path.join(workdir, 'testrun'), max_npairs=10,
calculator=calculator)
if abs(tilt) > 0.8:
calculator = phasecorrelation.PhaseCorrelation()
tilt = _compute(
ct_series, workdir, max_npairs=max_npairs,
calculator=calculator)
return tilt

def _compute(ct_series, workdir, max_npairs=10, calculator=None):
logger = logging.getLogger("imars3d.tilt")
tilt_out = os.path.join(workdir, "tilt.out")
# cached value?
Expand All @@ -13,7 +27,6 @@ def compute(ct_series, workdir, max_npairs=10, calculator=None):
if not calculator:
# from . import phasecorrelation
# calculator = phasecorrelation.PhaseCorrelation()
from . import use_centers
calculator = use_centers.Calculator(sigma=10, maxshift=200)
img = lambda angle: ct_series.getImage(angle)
# find opposite pairs
Expand Down Expand Up @@ -67,11 +80,13 @@ def check(tilt, img0, img180):
def apply(tilt, img, outimg, save=True):
"""apply tilt to the given image
"""
from scipy import ndimage
# from scipy import ndimage
from skimage.transform import rotate
import numpy as np

data = img.getData()
data = ndimage.rotate(data, -tilt)
# data = ndimage.rotate(data, -tilt)
data = rotate(data, -tilt)
outimg.data = data
if save:
outimg.save()
Expand Down
2 changes: 1 addition & 1 deletion python/imars3d/tilt/use_centers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, logging_dir=None, **opts):
def __call__(self, img0, img180):
slope, intercept = computeTilt(img0, img180, workdir=self.logging_dir, **self.opts)
# print (slope, np.arctan(slope))
return np.arctan(slope)*180./np.pi, 1.0
return .7 * np.arctan(slope)*180./np.pi, 1.0


def computeTilt(img0, img180, workdir=None, **kwds):
Expand Down

0 comments on commit ee28e35

Please sign in to comment.