本文整理匯總了Python中pydpiper.core.stages.Stages.defer方法的典型用法代碼示例。如果您正苦於以下問題:Python Stages.defer方法的具體用法?Python Stages.defer怎麽用?Python Stages.defer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pydpiper.core.stages.Stages
的用法示例。
在下文中一共展示了Stages.defer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: lsq6_pipeline
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def lsq6_pipeline(options):
# TODO could also allow pluggable pipeline parts e.g. LSQ6 could be substituted out for the modified LSQ6
# for the kidney tips, etc...
output_dir = options.application.output_directory
pipeline_name = options.application.pipeline_name
# TODO this is tedious and annoyingly similar to the registration chain and MBM ...
lsq6_dir = os.path.join(output_dir, pipeline_name + "_lsq6")
processed_dir = os.path.join(output_dir, pipeline_name + "_processed")
imgs = get_imgs(options.application)
s = Stages()
# TODO this is quite tedious and duplicates stuff in the registration chain ...
resolution = (options.registration.resolution or
get_resolution_from_file(
s.defer(registration_targets(lsq6_conf=options.lsq6,
app_conf=options.application,
reg_conf=options.registration)).registration_standard.path))
# FIXME: why do we have to call registration_targets *outside* of lsq6_nuc_inorm? is it just because of the extra
# options required?
targets = s.defer(registration_targets(lsq6_conf=options.lsq6,
app_conf=options.application,
reg_conf=options.registration,
first_input_file=imgs[0]))
# This must happen after calling registration_targets otherwise it will resample to options.registration.resolution
options.registration = options.registration.replace(resolution=resolution)
lsq6_result = s.defer(lsq6_nuc_inorm(imgs=imgs,
resolution=resolution,
registration_targets=targets,
lsq6_dir=lsq6_dir,
lsq6_options=options.lsq6))
return Result(stages=s, output=lsq6_result)
示例2: asymmetry_pipeline
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def asymmetry_pipeline(options):
output_dir = options.application.output_directory
pipeline_name = options.application.pipeline_name
processed_dir = os.path.join(output_dir, pipeline_name + "_processed")
s = Stages()
#imgs_ = [MincAtom(f, pipeline_sub_dir=processed_dir) for f in options.application.files]
imgs_ = get_imgs(options.application)
check_MINC_input_files([img.path for img in imgs_])
imgs = pd.Series(imgs_, index=[img.filename_wo_ext for img in imgs_])
flipped_imgs = imgs.apply(lambda img: s.defer(volflip(img))) # TODO add flags to control flip axis ...
# TODO ugly - MincAtom API should allow this somehow without mutation (also, how to pass into `volflip`, etc.?)
for f_i in flipped_imgs:
f_i.output_sub_dir += "_flipped"
check_MINC_input_files(imgs.apply(lambda img: img.path))
grouped_files_df = pd.DataFrame({'file' : pd.concat([imgs, flipped_imgs])}).assign(group=lambda df: df.index)
two_level_result = s.defer(two_level(grouped_files_df, options=options))
return Result(stages=s, output=two_level_result)
示例3: build_model
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def build_model(imgs,
conf,
nlin_dir,
nlin_prefix,
initial_target,
output_name_wo_ext = None):
s = Stages()
mincify = base_build_model.ToMinc
imgs = tuple(s.defer(mincify.from_mnc(img)) for img in imgs)
result = s.defer(base_build_model.build_model(imgs=imgs, conf=conf,
nlin_dir=nlin_dir, nlin_prefix=nlin_prefix,
initial_target=s.defer(mincify.from_mnc(initial_target))
#output_name_wo_ext=output_name_wo_ext
))
def wrap_output_xfmh(xfmh):
return XfmHandler(source=s.defer(mincify.to_mnc(xfmh.source)) if xfmh.source else None,
target=s.defer(mincify.to_mnc(xfmh.target)) if xfmh.target else None,
resampled=s.defer(mincify.to_mnc(xfmh.resampled)) if xfmh.has_resampled() else None,
xfm=s.defer(mincify.to_mni_xfm(xfmh.xfm)),
inverse=wrap_output_xfmh(xfmh.inverse) if xfmh.has_inverse() else None)
return Result(stages=s, output=WithAvgImgs(avg_imgs=[s.defer(mincify.to_mnc(img))
for img in result.avg_imgs],
avg_img=s.defer(mincify.to_mnc(result.avg_img)),
output=[wrap_output_xfmh(x)
for x in result.output]))
示例4: nlin_part
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def nlin_part(xfm : XfmHandler, inv_xfm : Optional[XfmHandler] = None) -> Result[XfmHandler]:
"""
*** = non linear deformations
--- = linear (affine) deformations
Input:
xfm : ******------>
inv_xfm : <******------ (optional)
Calculated:
inv_lin_xfm : <------
Returned:
concat : ******------> +
<------
equals : ******>
Compute the nonlinear part of a transform as follows:
go forwards across xfm and then backwards across the linear part
of the inverse xfm (by first calculating the inverse or using the one supplied)
Finally, use minc_displacement to compute the resulting gridfile of the purely
nonlinear part.
The optional inv_xfm (which must be the inverse!) is an optimization -
we don't go looking for an inverse by filename munging and don't programmatically
keep a log of operations applied, so any preexisting inverse must be supplied explicitly.
"""
s = Stages()
inv_xfm = inv_xfm or s.defer(invert_xfmhandler(xfm))
inv_lin_part = s.defer(lin_from_nlin(inv_xfm))
xfm = s.defer(concat_xfmhandlers([xfm, inv_lin_part]))
return Result(stages=s, output=xfm)
示例5: scale_transform
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def scale_transform(xfm, scale, newname_wo_ext):
s = Stages()
defs = s.defer(as_deformation(transform=xfm.xfm, reference=xfm.source))
scaled_defs = (defs.xfm.newname(newname_wo_ext) if newname_wo_ext else
defs.xfm.newname_with_suffix("_scaled_%s" % scale))
s.defer(CmdStage(cmd=['c3d', '-scale', str(scale), defs.path, "-o", scaled_defs.path],
inputs=(defs,), outputs=(scaled_defs,)))
return Result(stages=s, output=scaled_defs)
示例6: convert
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def convert(infile : ImgAtom, out_ext : str) -> Result[ImgAtom]:
s = Stages()
outfile = infile.newext(ext=out_ext)
if infile.mask is not None:
outfile.mask = s.defer(convert(infile.mask, out_ext=out_ext))
if infile.labels is not None:
outfile.mask = s.defer(convert(infile.labels, out_ext=out_ext))
s.add(CmdStage(inputs=(infile,), outputs=(outfile,),
cmd = ['c3d', infile.path, '-o', outfile.path]))
return Result(stages=s, output=outfile)
示例7: average_transforms
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def average_transforms(xfms, avg_xfm):
s = Stages()
defs = [s.defer(as_deformation(transform=xfm.xfm, reference_image=xfm.source)) for xfm in xfms]
#avg_img = NotImplemented
avg = imageToXfm(s.defer(average_images(defs,
avg_file=xfmToImage(avg_xfm),
#output_dir=os.path.join(defs[0].pipeline_sub_dir,
# defs[0].output_sub_dir,
# "transforms")
)))
return Result(stages=s, output=avg)
示例8: NLIN_pipeline
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def NLIN_pipeline(options):
# if options.application.files is None:
# raise ValueError("Please, some files! (or try '--help')") # TODO make a util procedure for this
output_dir = options.application.output_directory
pipeline_name = options.application.pipeline_name
# TODO this is tedious and annoyingly similar to the registration chain and MBM and LSQ6 ...
processed_dir = os.path.join(output_dir, pipeline_name + "_processed")
nlin_dir = os.path.join(output_dir, pipeline_name + "_nlin")
resolution = (options.registration.resolution # TODO does using the finest resolution here make sense?
or min([get_resolution_from_file(f) for f in options.application.files]))
imgs = get_imgs(options.application)
# imgs = [MincAtom(f, pipeline_sub_dir=processed_dir) for f in options.application.files]
# determine NLIN settings by overriding defaults with
# any settings present in protocol file, if it exists
# could add a hook to print a message announcing completion, output files,
# add more stages here to make a CSV
initial_target_mask = MincAtom(options.nlin.target_mask) if options.nlin.target_mask else None
initial_target = MincAtom(options.nlin.target, mask=initial_target_mask)
full_hierarchy = get_nonlinear_configuration_from_options(nlin_protocol=options.nlin.nlin_protocol,
reg_method=options.nlin.reg_method,
file_resolution=resolution)
s = Stages()
nlin_result = s.defer(nlin_build_model(imgs, initial_target=initial_target, conf=full_hierarchy, nlin_dir=nlin_dir))
# TODO return these?
inverted_xfms = [s.defer(invert_xfmhandler(xfm)) for xfm in nlin_result.output]
if options.stats.calc_stats:
# TODO: put the stats part behind a flag ...
determinants = [s.defer(determinants_at_fwhms(
xfm=inv_xfm,
inv_xfm=xfm,
blur_fwhms=options.stats.stats_kernels))
for xfm, inv_xfm in zip(nlin_result.output, inverted_xfms)]
return Result(stages=s,
output=Namespace(nlin_xfms=nlin_result,
avg_img=nlin_result.avg_img,
determinants=determinants))
else:
# there's no consistency in what gets returned, yikes ...
return Result(stages=s, output=Namespace(nlin_xfms=nlin_result, avg_img=nlin_result.avg_img))
示例9: determinants_at_fwhms
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def determinants_at_fwhms(xfms : List[XfmHandler], # TODO change to pd.Series to get indexing (hence safer inv_xfm)?
blur_fwhms : str, # TODO: change back to List[float]; should unblurred dets be found automatically?
inv_xfms : Optional[List[XfmHandler]] = None) \
-> Result[pd.DataFrame]: # TODO how to write down a Pandas type here ?!
"""
The most common way to use this function is by providing
it with transformations that go from the final average
to an individual. I.e.:
*** = non linear deformations
--- = linear (affine) deformations
xfm = final-nlin ******------> individual_input
inv_xfm = final-nlin <******------ individual_input
Takes a transformation (xfm) containing
both lsq12 (scaling and shearing, the 6-parameter
rotations/translations should not be part of this) and
non-linear parts of a subject to a common/shared average
and returns the determinants of both the (forward) nonlinear
part of the xfm at the given fwhms as well as the determinants
of the full (forward) transformation. The inverse transform
may optionally be specified to avoid its recomputation (e.g.,
when passing an inverted xfm to determinants_at_fwhms,
specify the original here).
"""
s = Stages()
inv_xfms = [s.defer(invert_xfmhandler(xfm)) for xfm in xfms] if inv_xfms is None else inv_xfms
fwhms = [float(x) for x in blur_fwhms.split(',')]
df = pd.DataFrame([{"xfm" : xfm, "inv_xfm" : inv_xfm, "fwhm" : fwhm,
"nlin_det" : nlin_det, "log_nlin_det" : nlin_log_det,
"full_det" : full_det, "log_full_det" : full_log_det }
for fwhm in fwhms + [0] # was: None, but this turns to NaN in Pandas ...
for xfm, inv_xfm in zip(xfms, inv_xfms)
for full_det_and_log_det in
[s.defer(det_and_log_det(displacement_grid=s.defer(minc_displacement(xfm)),
fwhm=fwhm,
annotation="_abs"))]
for full_det, full_log_det in [(full_det_and_log_det.det, full_det_and_log_det.log_det)]
for nlin_det_and_log_det in
[s.defer(det_and_log_det(displacement_grid=s.defer(nlin_displacement(xfm,
inv_xfm=inv_xfm)),
fwhm=fwhm,
annotation="_rel"))]
for nlin_det, nlin_log_det in [(nlin_det_and_log_det.det, nlin_det_and_log_det.log_det)]])
# TODO this is terrible, and should probably be done with joins, but one gets the idea ...
# TODO remove 'inv_xfm' column?
# TODO the return of this function is 'everything', not really just 'determinants_at_fwhms' ...
return Result(stages=s, output=df)
示例10: nlin_displacement
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def nlin_displacement(xfm : XfmHandler, inv_xfm : Optional[XfmHandler] = None) -> Result[MincAtom]:
"""
See: nlin_part().
This returns the nonlinear part of the input
transformation (xfm) in the form of a grid file (vector field).
All transformations are encapsulated in this field (linear parts
that are normally specified in the .xfm file are placed in the
vector field)
"""
s = Stages()
return Result(stages=s,
output=s.defer(minc_displacement(
s.defer(nlin_part(xfm, inv_xfm=inv_xfm)))))
示例11: cortical_thickness_pipeline
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def cortical_thickness_pipeline(options):
s = Stages()
#imgs = [MincAtom(name, pipeline_sub_dir=os.path.join(options.application.output_directory,
# options.application.pipeline_name + "_processed"))
# for name in options.application.files]
pipeline_sub_dir = os.path.join(options.application.output_directory,
options.application.pipeline_name + "_processed")
#def atom(atom_type, file):
# return atom_type(file, pipeline_sub_dir=pipeline_sub_dir) # TODO output_sub_dir, ....
# TODO are all these fields actually used? If not, omit from CSV?
xfms = (pd.read_csv(options.thickness.xfm_csv)
.apply(axis=1, # TODO fill out <..>Atom(...) fields ...
func=lambda row: XfmHandler(
source=MincAtom(row.source, pipeline_sub_dir=pipeline_sub_dir),
target=MincAtom(row.target, pipeline_sub_dir=pipeline_sub_dir),
resampled=None, #MincAtom(row.resampled, pipeline_sub_dir=pipeline_sub_dir),
xfm=XfmAtom(row.xfm, pipeline_sub_dir=pipeline_sub_dir))))
# TODO better way to unpack?
result = s.defer(cortical_thickness(xfms=xfms,
atlas=NotImplemented,
label_mapping=options.thickness.label_mapping,
atlas_fwhm=options.thickness.atlas_fwhm,
thickness_fwhm=options.thickness.thickness_fwhm))
return Result(stages=s, output=result)
示例12: mincblob
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def mincblob(op : str, grid : MincAtom, subdir : str = "tmp") -> Result[MincAtom]:
"""
Low-level mincblob wrapper with the one exception being the determinant option. By
default the inner clockwork of mincblob subtracts 1 from all determinant values that
are being calculated. As such, 1 needs to be added to the result of the mincblob call.
We will do that here, because it makes most sense here.
>>> stages = mincblob('determinant', MincAtom("/images/img_1.mnc", pipeline_sub_dir="/tmp")).stages
>>> [s.render() for s in stages]
['mincblob -clobber -determinant /images/img_1.mnc /tmp/img_1/img_1_determinant.mnc']
"""
if op not in ["determinant", "trace", "translation", "magnitude"]:
raise ValueError('mincblob: invalid operation %s' % op)
# if we are calculating the determinant, the first file produced is a temp file:
if op == "determinant":
out_file = grid.newname_with_suffix("_temp_det", subdir=subdir)
else:
out_file = grid.newname_with_suffix('_' + op, subdir=subdir)
stage = CmdStage(inputs=(grid,), outputs=(out_file,),
cmd=['mincblob', '-clobber', '-' + op, grid.path, out_file.path])
s = Stages([stage])
# now create the proper determinant if that's what was asked for
if op == "determinant":
result_file = s.defer(mincmath(op='add',
const=1,
vols=[out_file],
subdir=subdir,
new_name=grid.filename_wo_ext + "_det"))
else:
result_file = out_file
return Result(stages=s, output=result_file)
示例13: tamarack_pipeline
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def tamarack_pipeline(options):
output_dir = options.application.output_directory
pipeline_name = options.application.pipeline_name
#processed_dir = os.path.join(output_dir, pipeline_name + "_processed")
first_level_dir = os.path.join(output_dir, pipeline_name + "_first_level")
s = Stages()
with open(options.application.csv_file, 'r') as f:
files_df = (pd.read_csv(filepath_or_buffer=f,
usecols=['group', 'filename'])
.assign(file=lambda df:
df.apply(axis="columns",
func=lambda r:
MincAtom(r.filename.strip(),
pipeline_sub_dir=os.path.join(first_level_dir,
"%s_processed" % r.group.strip())))))
check_MINC_input_files(files_df.file.apply(lambda img: img.path))
#grouped_files_df = pd.DataFrame({'file' : pd.concat([imgs])}).assign(group=lambda df: df.index)
tamarack_result = s.defer(tamarack(files_df, options=options))
tamarack_result.first_level_results.applymap(maybe_deref_path).to_csv("first_level_results.csv", index=False)
tamarack_result.resampled_determinants.applymap(maybe_deref_path).to_csv("resampled_determinants.csv", index=False)
tamarack_result.overall_determinants.applymap(maybe_deref_path).to_csv("overall_determinants.csv", index=False)
return Result(stages=s, output=tamarack_result)
示例14: determinant
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def determinant(displacement_grid : MincAtom) -> Result[MincAtom]:
"""
Takes a displacement field (deformation grid, vector field, those are
all the same thing) and calculates the proper determinant (mincblob()
takes care of adding 1 to the silly output of running mincblob directly)
"""
s = Stages()
det = s.defer(mincblob(op='determinant', grid=displacement_grid))
return Result(stages=s, output=det)
示例15: average_images
# 需要導入模塊: from pydpiper.core.stages import Stages [as 別名]
# 或者: from pydpiper.core.stages.Stages import defer [as 別名]
def average_images(imgs : Sequence[ImgAtom],
dimensions : int = 3,
normalize : bool = False,
output_dir : str = '.',
name_wo_ext : str = "average",
out_ext : Optional[str] = None,
avg_file : Optional[ITKImgAtom] = None) -> Result[ITKImgAtom]:
s = Stages()
if len(imgs) == 0:
raise ValueError("`AverageImages` arg `imgs` is empty (can't average zero files)")
ext = out_ext or imgs[0].ext
# the output_dir basically gives us the equivalent of the pipeline_sub_dir for
# regular input files to a pipeline, so use that here
avg = avg_file or ImgAtom(name=os.path.join(output_dir, '%s.todo' % name_wo_ext),
orig_name=None,
pipeline_sub_dir=output_dir)
avg.ext = ext
# if all input files have masks associated with them, add the combined mask to
# the average:
# TODO what if avg_file has a mask ... should that be used instead? (then rename avg -> avg_file above)
all_inputs_have_masks = all((img.mask for img in imgs))
if all_inputs_have_masks:
combined_mask = (ImgAtom(name=os.path.join(avg_file.dir, '%s_mask.todo' % avg_file.filename_wo_ext),
orig_name=None,
pipeline_sub_dir=avg_file.pipeline_sub_dir)
if avg_file is not None else
ImgAtom(name=os.path.join(output_dir, '%s_mask.todo' % name_wo_ext),
orig_name=None,
pipeline_sub_dir=output_dir))
combined_mask.ext = ext
s.defer(max(imgs=sorted({img_inst.mask for img_inst in imgs}),
out_img=combined_mask))
avg.mask = combined_mask
s.add(CmdStage(inputs = imgs,
outputs = (avg,),
cmd = ["AverageImages", str(dimensions), avg.path, "%d" % normalize]
+ [img.path for img in imgs]))
return Result(stages=s, output=avg)