def AtlasWriteOutput(cf, atlas, m_array, nodeSubjectsIds, isReporter): # save initial momenta for all individuals for itsub in range(len(nodeSubjectsIds)): common.SaveITKField( m_array[itsub], cf.io.outputPrefix + str(nodeSubjectsIds[itsub]).replace('.', '_') + "_m0.mhd") # save the atlas if isReporter: common.SaveITKImage(atlas, cf.io.outputPrefix + "atlas.mhd")
def main(): secNum = sys.argv[1] mkyNum = sys.argv[2] channel = sys.argv[3] region = str(sys.argv[4]) conf_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/confocal/src_registration/' side_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/side_light_microscope/src_registration/' save_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/confocal/sidelight_registered/' # DIC = '/home/sci/blakez/Reflect Affine/DIC_to_Reflect.txt' src_pt = conf_dir + 'M{0}/section_{1}/{2}/section_{1}_confocal_relation_with_sidelight.txt'.format(mkyNum, secNum, region) tar_pt = side_dir + 'M{0}/section_{1}/section_{1}_sidelight_relation_with_confocal.txt'.format(mkyNum, secNum) # SID = '/home/sci/blakez/Reflect Affine/sidelight_to_DIC.txt' src_im = common.LoadITKImage(conf_dir + 'M{0}/section_{1}/{3}/Ch{2}/M{0}_{1}_LGN_RHS_Ch{2}_z00.tif'.format(mkyNum, secNum, channel, region)) # tar_im = common.LoadITKImage('M{0}/{1}/Crop_ThirdNerve_EGFP_z16.tiff'.format(mkyNum, secNum)) # The points need to be chosen in the origin corrected sidescape for downstream purposes affine = load_and_solve(tar_pt, src_pt) out_grid = bb_grid_solver(src_im, affine) z_stack = [] num_slices = len(glob.glob(conf_dir + 'M{0}/section_{1}/{3}/Ch{2}/*'.format(mkyNum, secNum, channel, region))) for z in range(0, num_slices): src_im = common.LoadITKImage(conf_dir + 'M{0}/section_{1}/{4}/Ch{2}/M{0}_{1}_LGN_RHS_Ch{2}_z{3}.tif'.format(mkyNum, secNum, channel, str(z).zfill(2), region)) aff_im = ca.Image3D(out_grid, ca.MEM_HOST) cc.ApplyAffineReal(aff_im, src_im, affine) common.SaveITKImage(aff_im, save_dir + 'M{0}/section_{1}/{4}/Ch{2}/M{0}_01_section_{1}_LGN_RHS_Ch{2}_conf_aff_sidelight_z{3}.tiff'.format(mkyNum, secNum, channel, str(z).zfill(2), region)) z_stack.append(aff_im) print('==> Done with {0}/{1}'.format(z, num_slices - 1)) stacked = cc.Imlist_to_Im(z_stack) stacked.setSpacing(ca.Vec3Df(out_grid.spacing()[0], out_grid.spacing()[1], 0.03/num_slices)) common.SaveITKImage(stacked, save_dir + 'M{0}/section_{1}/{3}/Ch{2}/M{0}_01_section_{1}_Ch{2}_conf_aff_sidelight_stack.nrrd'.format(mkyNum, secNum, channel, region)) common.DebugHere() if channel==0: cc.WriteGrid(stacked.grid(), save_dir + 'M{0}/section_{1}/{2}/affine_registration_grid.txt'.format(mkyNum, secNum, region))
def GeoRegWriteOuput(subjectId, cf, p, t, Imsmts, cpinds, cpstates, msmtinds, gradAtMsmts, EnergyHistory): # save initial image and momenta for regression geodesic common.SaveITKImage(p.I0, cf.io.outputPrefix + subjectId + "I0.mhd") common.SaveITKField(p.m0, cf.io.outputPrefix + subjectId + "m0.mhd") # save residual images for regression geodesic # TODO: # write Energy details energyFilename = cf.io.outputPrefix + subjectId + "Energy.csv" with open(energyFilename, 'w') as f: csv_writer = csv.writer(f, delimiter='\t') csv_writer.writerows(EnergyHistory)
def intensity_normalization_histeq(args): for i in range(0, len(args.input_images)): image = common.LoadITKImage(args.output_images[i], ca.MEM_HOST) grid = image.grid() image_np = common.AsNPCopy(image) nan_mask = np.isnan(image_np) image_np[nan_mask] = 0 image_np /= np.amax(image_np) # perform histogram equalization if needed if args.histeq: image_np[image_np != 0] = exposure.equalize_hist( image_np[image_np != 0]) image_result = common.ImFromNPArr(image_np, ca.MEM_HOST) image_result.setGrid(grid) common.SaveITKImage(image_result, args.output_images[i])
def HGMWriteOutput(cf, groupState, tDiscGroup, isReporter): # save initial momenta for residual geodesics, p, for all individuals for i in range(len(groupState.t)): if tDiscGroup[i].J is not None: common.SaveITKField( tDiscGroup[i].p0, cf.io.outputPrefix + str(tDiscGroup[i].subjectId).replace('.', '_') + "_p0.mhd") # write individual's energy history energyFilename = cf.io.outputPrefix + str( tDiscGroup[i].subjectId).replace('.', '_') + "ResidualEnergy.csv" HGMWriteEnergyHistoryToFile(tDiscGroup[i].Energy, energyFilename) # save initial image and momenta for group gedoesic if isReporter: common.SaveITKImage(groupState.I0, cf.io.outputPrefix + "I0.mhd") common.SaveITKField(groupState.m0, cf.io.outputPrefix + "m0.mhd") # write energy history energyFilename = cf.io.outputPrefix + "TotalEnergyHistory.csv" HGMWriteEnergyHistoryToFile(groupState.EnergyHistory, energyFilename)
def MatchingImageMomentaWriteOuput(cf, geodesicState, EnergyHistory, m0, n1): grid = geodesicState.J0.grid() mType = geodesicState.J0.memType() # save momenta for the gedoesic common.SaveITKField(geodesicState.p0, cf.io.outputPrefix + "p0.mhd") # save matched momenta for the geodesic if cf.vectormomentum.matchImOnly: m0 = common.LoadITKField(cf.study.m, mType) ca.CoAd(geodesicState.p, geodesicState.rhoinv, m0) common.SaveITKField(geodesicState.p, cf.io.outputPrefix + "m1.mhd") # momenta match energy if cf.vectormomentum.matchImOnly: vecdiff = ca.ManagedField3D(grid, mType) ca.Sub_I(geodesicState.p, n1) ca.Copy(vecdiff, geodesicState.p) geodesicState.diffOp.applyInverseOperator(geodesicState.p) momentaMatchEnergy = ca.Dot(vecdiff, geodesicState.p) / ( float(geodesicState.p0.nVox()) * geodesicState.SigmaSlope * geodesicState.SigmaSlope) # save energy energyFilename = cf.io.outputPrefix + "testMomentaMatchEnergy.csv" with open(energyFilename, 'w') as f: print >> f, momentaMatchEnergy # save matched image for the geodesic tempim = ca.ManagedImage3D(grid, mType) ca.ApplyH(tempim, geodesicState.J0, geodesicState.rhoinv) common.SaveITKImage(tempim, cf.io.outputPrefix + "I1.mhd") # save energy energyFilename = cf.io.outputPrefix + "energy.csv" MatchingImageMomentaWriteEnergyHistoryToFile(EnergyHistory, energyFilename)
def main(): secNum = sys.argv[1] mkyNum = sys.argv[2] region = str(sys.argv[3]) # channel = sys.argv[3] ext = 'M{0}/section_{1}/{2}/'.format(mkyNum, secNum, region) ss_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/side_light_microscope/' conf_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/confocal/' memT = ca.MEM_DEVICE try: with open( ss_dir + 'src_registration/M{0}/section_{1}/M{0}_01_section_{1}_regions.txt' .format(mkyNum, secNum), 'r') as f: region_dict = json.load(f) f.close() except IOError: region_dict = {} region_dict[region] = {} region_dict['size'] = map( int, raw_input("What is the size of the full resolution image x,y? "). split(',')) region_dict[region]['bbx'] = map( int, raw_input( "What are the x indicies of the bounding box (Matlab Format x_start,x_stop? " ).split(',')) region_dict[region]['bby'] = map( int, raw_input( "What are the y indicies of the bounding box (Matlab Format y_start,y_stop? " ).split(',')) if region not in region_dict: region_dict[region] = {} region_dict[region]['bbx'] = map( int, raw_input( "What are the x indicies of the bounding box (Matlab Format x_start,x_stop? " ).split(',')) region_dict[region]['bby'] = map( int, raw_input( "What are the y indicies of the bounding box (Matlab Format y_start,y_stop? " ).split(',')) img_region = common.LoadITKImage( ss_dir + 'src_registration/M{0}/section_{1}/M{0}_01_section_{1}_{2}.tiff'. format(mkyNum, secNum, region), ca.MEM_HOST) ssiSrc = common.LoadITKImage( ss_dir + 'src_registration/M{0}/section_{1}/frag0/M{0}_01_ssi_section_{1}_frag0.nrrd' .format(mkyNum, secNum), ca.MEM_HOST) bfi_df = common.LoadITKField( ss_dir + 'Blockface_registered/M{0}/section_{1}/frag0/M{0}_01_ssi_section_{1}_frag0_to_bfi_real.mha' .format(mkyNum, secNum), ca.MEM_DEVICE) # Figure out the same region in the low resolution image: There is a transpose from here to matlab so dimensions are flipped low_sz = ssiSrc.size().tolist() yrng_raw = [(low_sz[1] * region_dict[region]['bbx'][0]) / np.float(region_dict['size'][0]), (low_sz[1] * region_dict[region]['bbx'][1]) / np.float(region_dict['size'][0])] xrng_raw = [(low_sz[0] * region_dict[region]['bby'][0]) / np.float(region_dict['size'][1]), (low_sz[0] * region_dict[region]['bby'][1]) / np.float(region_dict['size'][1])] yrng = [np.int(np.floor(yrng_raw[0])), np.int(np.ceil(yrng_raw[1]))] xrng = [np.int(np.floor(xrng_raw[0])), np.int(np.ceil(xrng_raw[1]))] low_sub = cc.SubVol(ssiSrc, xrng, yrng) # Figure out the grid for the sub region in relation to the sidescape originout = [ ssiSrc.origin().x + ssiSrc.spacing().x * xrng[0], ssiSrc.origin().y + ssiSrc.spacing().y * yrng[0], 0 ] spacingout = [ (low_sub.size().x * ssiSrc.spacing().x) / (img_region.size().x), (low_sub.size().y * ssiSrc.spacing().y) / (img_region.size().y), 1 ] gridout = cc.MakeGrid(img_region.size().tolist(), spacingout, originout) img_region.setGrid(gridout) only_sub = np.zeros(ssiSrc.size().tolist()[0:2]) only_sub[xrng[0]:xrng[1], yrng[0]:yrng[1]] = np.squeeze(low_sub.asnp()) only_sub = common.ImFromNPArr(only_sub) only_sub.setGrid(ssiSrc.grid()) # Deform the only sub region to only_sub.toType(ca.MEM_DEVICE) def_sub = ca.Image3D(bfi_df.grid(), bfi_df.memType()) cc.ApplyHReal(def_sub, only_sub, bfi_df) def_sub.toType(ca.MEM_HOST) # Now have to find the bounding box in the deformation space (bfi space) if 'deformation_bbx' not in region_dict[region]: bb_def = np.squeeze(pp.LandmarkPicker([np.squeeze(def_sub.asnp())])) bb_def_y = [bb_def[0][0], bb_def[1][0]] bb_def_x = [bb_def[0][1], bb_def[1][1]] region_dict[region]['deformation_bbx'] = bb_def_x region_dict[region]['deformation_bby'] = bb_def_y with open( ss_dir + 'src_registration/M{0}/section_{1}/M{0}_01_section_{1}_regions.txt' .format(mkyNum, secNum), 'w') as f: json.dump(region_dict, f) f.close() # Now need to extract the region and create a deformation and image that have the same resolution as the img_region deform_sub = cc.SubVol(bfi_df, region_dict[region]['deformation_bbx'], region_dict[region]['deformation_bby']) common.DebugHere() sizeout = [ int( np.ceil((deform_sub.size().x * deform_sub.spacing().x) / img_region.spacing().x)), int( np.ceil((deform_sub.size().y * deform_sub.spacing().y) / img_region.spacing().y)), 1 ] region_grid = cc.MakeGrid(sizeout, img_region.spacing().tolist(), deform_sub.origin().tolist()) def_im_region = ca.Image3D(region_grid, deform_sub.memType()) up_deformation = ca.Field3D(region_grid, deform_sub.memType()) img_region.toType(ca.MEM_DEVICE) cc.ResampleWorld(up_deformation, deform_sub, ca.BACKGROUND_STRATEGY_PARTIAL_ZERO) cc.ApplyHReal(def_im_region, img_region, up_deformation) ss_out = ss_dir + 'Blockface_registered/M{0}/section_{1}/{2}/'.format( mkyNum, secNum, region) if not pth.exists(pth.expanduser(ss_out)): os.mkdir(pth.expanduser(ss_out)) common.SaveITKImage( def_im_region, pth.expanduser(ss_out) + 'M{0}_01_section_{1}_{2}_def_to_bfi.nrrd'.format( mkyNum, secNum, region)) common.SaveITKImage( def_im_region, pth.expanduser(ss_out) + 'M{0}_01_section_{1}_{2}_def_to_bfi.tiff'.format( mkyNum, secNum, region)) del img_region, def_im_region, ssiSrc, deform_sub # Now apply the same deformation to the confocal images conf_grid = cc.LoadGrid( conf_dir + 'sidelight_registered/M{0}/section_{1}/{2}/affine_registration_grid.txt' .format(mkyNum, secNum, region)) cf_out = conf_dir + 'blockface_registered/M{0}/section_{1}/{2}/'.format( mkyNum, secNum, region) # confocal.toType(ca.MEM_DEVICE) # def_conf = ca.Image3D(region_grid, deform_sub.memType()) # cc.ApplyHReal(def_conf, confocal, up_deformation) for channel in range(0, 4): z_stack = [] num_slices = len( glob.glob(conf_dir + 'sidelight_registered/M{0}/section_{1}/{3}/Ch{2}/*.tiff'. format(mkyNum, secNum, channel, region))) for z in range(0, num_slices): src_im = common.LoadITKImage( conf_dir + 'sidelight_registered/M{0}/section_{1}/{3}/Ch{2}/M{0}_01_section_{1}_LGN_RHS_Ch{2}_conf_aff_sidelight_z{4}.tiff' .format(mkyNum, secNum, channel, region, str(z).zfill(2))) src_im.setGrid( cc.MakeGrid( ca.Vec3Di(conf_grid.size().x, conf_grid.size().y, 1), conf_grid.spacing(), conf_grid.origin())) src_im.toType(ca.MEM_DEVICE) def_im = ca.Image3D(region_grid, ca.MEM_DEVICE) cc.ApplyHReal(def_im, src_im, up_deformation) def_im.toType(ca.MEM_HOST) common.SaveITKImage( def_im, cf_out + 'Ch{2}/M{0}_01_section_{1}_{3}_Ch{2}_conf_def_blockface_z{4}.tiff' .format(mkyNum, secNum, channel, region, str(z).zfill(2))) if z == 0: common.SaveITKImage( def_im, cf_out + 'Ch{2}/M{0}_01_section_{1}_{3}_Ch{2}_conf_def_blockface_z{4}.nrrd' .format(mkyNum, secNum, channel, region, str(z).zfill(2))) z_stack.append(def_im) print('==> Done with Ch {0}: {1}/{2}'.format( channel, z, num_slices - 1)) stacked = cc.Imlist_to_Im(z_stack) stacked.setSpacing( ca.Vec3Df(region_grid.spacing().x, region_grid.spacing().y, conf_grid.spacing().z)) common.SaveITKImage( stacked, cf_out + 'Ch{2}/M{0}_01_section_{1}_{3}_Ch{2}_conf_def_blockface_stack.nrrd' .format(mkyNum, secNum, channel, region)) if channel == 0: cc.WriteGrid( stacked.grid(), cf_out + 'deformed_registration_grid.txt'.format( mkyNum, secNum, region))
def Matching(cf): if cf.compute.useCUDA and cf.compute.gpuID is not None: ca.SetCUDADevice(cf.compute.gpuID) if os.path.isfile(cf.io.outputPrefix + 'm0.mhd'): print cf.io.outputPrefix return () # if os.path.isfile(cf.io.outputPrefix+'m0.mhd'): # return(); # else: # print cf.io.outputPrefix; # return(); # prepare output directory common.Mkdir_p(os.path.dirname(cf.io.outputPrefix)) # Output loaded config if cf.io.outputPrefix is not None: cfstr = Config.ConfigToYAML(MatchingConfigSpec, cf) with open(cf.io.outputPrefix + "parsedconfig.yaml", "w") as f: f.write(cfstr) mType = ca.MEM_DEVICE if cf.compute.useCUDA else ca.MEM_HOST I0 = common.LoadITKImage(cf.study.I0, mType) I1 = common.LoadITKImage(cf.study.I1, mType) #ca.DivC_I(I0,255.0) #ca.DivC_I(I1,255.0) grid = I0.grid() It = ca.Image3D(grid, mType) ca.ThreadMemoryManager.init(grid, mType, 1) #common.DebugHere() # TODO: need to work on these t = [x * 1. / cf.optim.nTimeSteps for x in range(cf.optim.nTimeSteps + 1)] checkpointinds = range(1, len(t)) checkpointstates = [(ca.Field3D(grid, mType), ca.Field3D(grid, mType)) for idx in checkpointinds] p = MatchingVariables(I0, I1, cf.vectormomentum.sigma, t, checkpointinds, checkpointstates, cf.vectormomentum.diffOpParams[0], cf.vectormomentum.diffOpParams[1], cf.vectormomentum.diffOpParams[2], cf.optim.Niter, cf.optim.stepSize, cf.optim.maxPert, cf.optim.nTimeSteps, integMethod=cf.optim.integMethod, optMethod=cf.optim.method, nInv=cf.optim.NIterForInverse, plotEvery=cf.io.plotEvery, plotSlice=cf.io.plotSlice, quiverEvery=cf.io.quiverEvery, outputPrefix=cf.io.outputPrefix) print(p.stepSize) RunMatching(p) # write output if cf.io.outputPrefix is not None: # reset all variables by shooting once, may have been overwritten CAvmCommon.IntegrateGeodesic(p.m0,p.t,p.diffOp,\ p.m, p.g, p.ginv,\ p.scratchV1, p.scratchV2,p. scratchV3,\ p.checkpointstates, p.checkpointinds,\ Ninv=p.nInv, integMethod = p.integMethod) ca.ApplyH(It, I0, p.ginv) common.SaveITKField(p.m0, cf.io.outputPrefix + "m0.mhd") common.SaveITKField(p.ginv, cf.io.outputPrefix + "phiinv.mhd") #common.SaveITKField(p.g, cf.io.outputPrefix+"phi.mhd") common.SaveITKImage(It, cf.io.outputPrefix + "I1.mhd")
def write_result(result, output_prefix): common.SaveITKImage(result['I1'], output_prefix+"I1.mhd") common.SaveITKField(result['phiinv'], output_prefix+"phiinv.mhd")
def GeodesicShooting(cf): # prepare output directory common.Mkdir_p(os.path.dirname(cf.io.outputPrefix)) # Output loaded config if cf.io.outputPrefix is not None: cfstr = Config.ConfigToYAML(GeodesicShootingConfigSpec, cf) with open(cf.io.outputPrefix + "parsedconfig.yaml", "w") as f: f.write(cfstr) mType = ca.MEM_DEVICE if cf.useCUDA else ca.MEM_HOST #common.DebugHere() I0 = common.LoadITKImage(cf.study.I0, mType) m0 = common.LoadITKField(cf.study.m0, mType) grid = I0.grid() ca.ThreadMemoryManager.init(grid, mType, 1) # set up diffOp if mType == ca.MEM_HOST: diffOp = ca.FluidKernelFFTCPU() else: diffOp = ca.FluidKernelFFTGPU() diffOp.setAlpha(cf.diffOpParams[0]) diffOp.setBeta(cf.diffOpParams[1]) diffOp.setGamma(cf.diffOpParams[2]) diffOp.setGrid(grid) g = ca.Field3D(grid, mType) ginv = ca.Field3D(grid, mType) mt = ca.Field3D(grid, mType) It = ca.Image3D(grid, mType) t = [ x * 1. / cf.integration.nTimeSteps for x in range(cf.integration.nTimeSteps + 1) ] checkpointinds = range(1, len(t)) checkpointstates = [(ca.Field3D(grid, mType), ca.Field3D(grid, mType)) for idx in checkpointinds] scratchV1 = ca.Field3D(grid, mType) scratchV2 = ca.Field3D(grid, mType) scratchV3 = ca.Field3D(grid, mType) # scale momenta to shoot cf.study.scaleMomenta = float(cf.study.scaleMomenta) if abs(cf.study.scaleMomenta) > 0.000000: ca.MulC_I(m0, float(cf.study.scaleMomenta)) CAvmCommon.IntegrateGeodesic(m0,t,diffOp, mt, g, ginv,\ scratchV1,scratchV2,scratchV3,\ keepstates=checkpointstates,keepinds=checkpointinds, Ninv=cf.integration.NIterForInverse, integMethod = cf.integration.integMethod) else: ca.Copy(It, I0) ca.Copy(mt, m0) ca.SetToIdentity(ginv) ca.SetToIdentity(g) # write output if cf.io.outputPrefix is not None: # scale back shotmomenta before writing if abs(cf.study.scaleMomenta) > 0.000000: ca.ApplyH(It, I0, ginv) ca.CoAd(mt, ginv, m0) ca.DivC_I(mt, float(cf.study.scaleMomenta)) common.SaveITKImage(It, cf.io.outputPrefix + "I1.mhd") common.SaveITKField(mt, cf.io.outputPrefix + "m1.mhd") common.SaveITKField(ginv, cf.io.outputPrefix + "phiinv.mhd") common.SaveITKField(g, cf.io.outputPrefix + "phi.mhd") GeodesicShootingPlots(g, ginv, I0, It, cf) if cf.io.saveFrames: SaveFrames(checkpointstates, checkpointinds, I0, It, m0, mt, cf)
print 'Attempting to make volumes' ssi_reg_vol = cc.Imlist_to_Im(reg_list) ssi_aff_vol = cc.Imlist_to_Im(aff_list) bfi_org_vol = cc.Imlist_to_Im(bfi_list) ssi_reg_vol.setSpacing( ca.Vec3Df(ssi_reg_vol.spacing()[0], ssi_reg_vol.spacing()[1], 0.030)) ssi_aff_vol.setSpacing( ca.Vec3Df(ssi_aff_vol.spacing()[0], ssi_aff_vol.spacing()[1], 0.030)) bfi_org_vol.setSpacing( ca.Vec3Df(bfi_org_vol.spacing()[0], bfi_org_vol.spacing()[1], 0.030)) print 'Volumes Created and Attempting to Save' common.SaveITKImage( ssi_reg_vol, out_path + 'M{0}_01_section_{1}_to_section_{2}_reg_ssi_stack.nrrd'.format( monkeyNum, slicestart, slicefinish)) common.SaveITKImage( ssi_aff_vol, out_path + 'M{0}_01_section_{1}_to_section_{2}_aff_ssi_stack.nrrd'.format( monkeyNum, slicestart, slicefinish)) common.SaveITKImage( bfi_org_vol, out_path + 'M{0}_01_section_{1}_to_section_{2}_org_bfi_stack.nrrd'.format( monkeyNum, slicestart, slicefinish))
def predict_image(args): if (args.use_CPU_for_shooting): mType = ca.MEM_HOST else: mType = ca.MEM_DEVICE # load the prediction network predict_network_config = torch.load(args.prediction_parameter) prediction_net = create_net(args, predict_network_config) batch_size = args.batch_size patch_size = predict_network_config['patch_size'] input_batch = torch.zeros(batch_size, 2, patch_size, patch_size, patch_size).cuda() # start prediction for i in range(0, len(args.moving_image)): common.Mkdir_p(os.path.dirname(args.output_prefix[i])) if (args.affine_align): # Perform affine registration to both moving and target image to the ICBM152 atlas space. # Registration is done using Niftireg. call([ "reg_aladin", "-noSym", "-speeeeed", "-ref", args.atlas, "-flo", args.moving_image[i], "-res", args.output_prefix[i] + "moving_affine.nii", "-aff", args.output_prefix[i] + 'moving_affine_transform.txt' ]) call([ "reg_aladin", "-noSym", "-speeeeed", "-ref", args.atlas, "-flo", args.target_image[i], "-res", args.output_prefix[i] + "target_affine.nii", "-aff", args.output_prefix[i] + 'target_affine_transform.txt' ]) moving_image = common.LoadITKImage( args.output_prefix[i] + "moving_affine.nii", mType) target_image = common.LoadITKImage( args.output_prefix[i] + "target_affine.nii", mType) else: moving_image = common.LoadITKImage(args.moving_image[i], mType) target_image = common.LoadITKImage(args.target_image[i], mType) #preprocessing of the image moving_image_np = preprocess_image(moving_image, args.histeq) target_image_np = preprocess_image(target_image, args.histeq) grid = moving_image.grid() moving_image_processed = common.ImFromNPArr(moving_image_np, mType) target_image_processed = common.ImFromNPArr(target_image_np, mType) moving_image.setGrid(grid) target_image.setGrid(grid) predict_transform_space = False if 'matlab_t7' in predict_network_config: predict_transform_space = True # run actual prediction prediction_result = util.predict_momentum(moving_image_np, target_image_np, input_batch, batch_size, patch_size, prediction_net, predict_transform_space) m0 = prediction_result['image_space'] m0_reg = common.FieldFromNPArr(prediction_result['image_space'], mType) registration_result = registration_methods.geodesic_shooting( moving_image_processed, target_image_processed, m0_reg, args.shoot_steps, mType, predict_network_config) phi = common.AsNPCopy(registration_result['phiinv']) phi_square = np.power(phi, 2) for sample_iter in range(1, args.samples): print(sample_iter) prediction_result = util.predict_momentum( moving_image_np, target_image_np, input_batch, batch_size, patch_size, prediction_net, predict_transform_space) m0 += prediction_result['image_space'] m0_reg = common.FieldFromNPArr(prediction_result['image_space'], mType) registration_result = registration_methods.geodesic_shooting( moving_image_processed, target_image_processed, m0_reg, args.shoot_steps, mType, predict_network_config) phi += common.AsNPCopy(registration_result['phiinv']) phi_square += np.power( common.AsNPCopy(registration_result['phiinv']), 2) m0_mean = np.divide(m0, args.samples) m0_reg = common.FieldFromNPArr(m0_mean, mType) registration_result = registration_methods.geodesic_shooting( moving_image_processed, target_image_processed, m0_reg, args.shoot_steps, mType, predict_network_config) phi_mean = registration_result['phiinv'] phi_var = np.divide(phi_square, args.samples) - np.power( np.divide(phi, args.samples), 2) #save result common.SaveITKImage(registration_result['I1'], args.output_prefix[i] + "I1.mhd") common.SaveITKField(phi_mean, args.output_prefix[i] + "phiinv_mean.mhd") common.SaveITKField(common.FieldFromNPArr(phi_var, mType), args.output_prefix[i] + "phiinv_var.mhd")
def main(): # Extract the Monkey number and section number from the command line global frgNum global secOb mkyNum = sys.argv[1] secNum = sys.argv[2] frgNum = int(sys.argv[3]) write = True # if not os.path.exists(os.path.expanduser('~/korenbergNAS/3D_database/Working/configuration_files/SidescapeRelateBlockface/M{0}/section_{1}/include_configFile.yaml'.format(mkyNum,secNum))): # cf = initial(secNum, mkyNum) try: secOb = Config.Load( secSpec, pth.expanduser( '~/korenbergNAS/3D_database/Working/configuration_files/SidescapeRelateBlockface/M{0}/section_{1}/include_configFile.yaml' .format(mkyNum, secNum))) except IOError as e: try: temp = Config.LoadYAMLDict(pth.expanduser( '~/korenbergNAS/3D_database/Working/configuration_files/SidescapeRelateBlockface/M{0}/section_{1}/include_configFile.yaml' .format(mkyNum, secNum)), include=False) secOb = Config.MkConfig(temp, secSpec) except IOError: print 'It appears there is no configuration file for this section. Please initialize one and restart.' sys.exit() if frgNum == int(secOb.yamlList[frgNum][-6]): Fragmenter() try: secOb = Config.Load( secSpec, pth.expanduser( '~/korenbergNAS/3D_database/Working/configuration_files/SidescapeRelateBlockface/M{0}/section_{1}/include_configFile.yaml' .format(mkyNum, secNum))) except IOError: print 'It appeas that the include yaml file list does not match your fragmentation number. Please check them and restart.' sys.exit() if not pth.exists( pth.expanduser(secOb.ssiOutPath + 'frag{0}'.format(frgNum))): common.Mkdir_p( pth.expanduser(secOb.ssiOutPath + 'frag{0}'.format(frgNum))) if not pth.exists( pth.expanduser(secOb.bfiOutPath + 'frag{0}'.format(frgNum))): common.Mkdir_p( pth.expanduser(secOb.bfiOutPath + 'frag{0}'.format(frgNum))) if not pth.exists( pth.expanduser(secOb.ssiSrcPath + 'frag{0}'.format(frgNum))): os.mkdir(pth.expanduser(secOb.ssiSrcPath + 'frag{0}'.format(frgNum))) if not pth.exists( pth.expanduser(secOb.bfiSrcPath + 'frag{0}'.format(frgNum))): os.mkdir(pth.expanduser(secOb.bfiSrcPath + 'frag{0}'.format(frgNum))) frgOb = Config.MkConfig(secOb.yamlList[frgNum], frgSpec) ssiSrc, bfiSrc, ssiMsk, bfiMsk = Loader(frgOb, ca.MEM_HOST) #Extract the saturation Image from the color iamge bfiHsv = common.FieldFromNPArr( matplotlib.colors.rgb_to_hsv( np.rollaxis(np.array(np.squeeze(bfiSrc.asnp())), 0, 3)), ca.MEM_HOST) bfiHsv.setGrid(bfiSrc.grid()) bfiSat = ca.Image3D(bfiSrc.grid(), bfiHsv.memType()) ca.Copy(bfiSat, bfiHsv, 1) #Histogram equalize, normalize and mask the blockface saturation image bfiSat = cb.HistogramEqualize(bfiSat, 256) bfiSat.setGrid(bfiSrc.grid()) bfiSat *= -1 bfiSat -= ca.Min(bfiSat) bfiSat /= ca.Max(bfiSat) bfiSat *= bfiMsk bfiSat.setGrid(bfiSrc.grid()) #Write out the blockface region after adjusting the colors with a format that supports header information if write: common.SaveITKImage( bfiSat, pth.expanduser(secOb.bfiSrcPath + 'frag{0}/M{1}_01_bfi_section_{2}_frag{0}_sat.nrrd'. format(frgNum, secOb.mkyNum, secOb.secNum))) #Set the sidescape grid relative to that of the blockface ssiSrc.setGrid(ConvertGrid(ssiSrc.grid(), bfiSat.grid())) ssiMsk.setGrid(ConvertGrid(ssiMsk.grid(), bfiSat.grid())) ssiSrc *= ssiMsk #Write out the sidescape masked image in a format that stores the header information if write: common.SaveITKImage( ssiSrc, pth.expanduser(secOb.ssiSrcPath + 'frag{0}/M{1}_01_ssi_section_{2}_frag{0}.nrrd'. format(frgNum, secOb.mkyNum, secOb.secNum))) #Update the image parameters of the sidescape image for future use frgOb.imSize = ssiSrc.size().tolist() frgOb.imOrig = ssiSrc.origin().tolist() frgOb.imSpac = ssiSrc.spacing().tolist() updateFragOb(frgOb) #Find the affine transform between the two fragments bfiAff, ssiAff, aff = Affine(bfiSat, ssiSrc, frgOb) updateFragOb(frgOb) #Write out the affine transformed images in a format that stores header information if write: common.SaveITKImage( bfiAff, pth.expanduser( secOb.bfiOutPath + 'frag{0}/M{1}_01_bfi_section_{2}_frag{0}_aff_ssi.nrrd'.format( frgNum, secOb.mkyNum, secOb.secNum))) common.SaveITKImage( ssiAff, pth.expanduser( secOb.ssiOutPath + 'frag{0}/M{1}_01_ssi_section_{2}_frag{0}_aff_bfi.nrrd'.format( frgNum, secOb.mkyNum, secOb.secNum))) bfiVe = bfiAff.copy() ssiVe = ssiSrc.copy() cc.VarianceEqualize_I(bfiVe, sigma=frgOb.sigVarBfi, eps=frgOb.epsVar) cc.VarianceEqualize_I(ssiVe, sigma=frgOb.sigVarSsi, eps=frgOb.epsVar) #As of right now, the largest pre-computed FFT table is 2048, so resample onto that grid for registration regGrd = ConvertGrid( cc.MakeGrid(ca.Vec3Di(2048, 2048, 1), ca.Vec3Df(1, 1, 1), ca.Vec3Df(0, 0, 0)), ssiSrc.grid()) ssiReg = ca.Image3D(regGrd, ca.MEM_HOST) bfiReg = ca.Image3D(regGrd, ca.MEM_HOST) cc.ResampleWorld(ssiReg, ssiVe) cc.ResampleWorld(bfiReg, bfiVe) #Create the default configuration object for IDiff Matching and then set some parameters idCf = Config.SpecToConfig(IDiff.Matching.MatchingConfigSpec) idCf.compute.useCUDA = True idCf.io.outputPrefix = '/home/sci/blakez/IDtest/' #Run the registration ssiDef, phi = DefReg(ssiReg, bfiReg, frgOb, ca.MEM_DEVICE, idCf) #Turn the deformation into a displacement field so it can be applied to the large tif with C++ code affV = phi.copy() cc.ApplyAffineReal(affV, phi, np.linalg.inv(frgOb.affine)) ca.HtoV_I(affV) #Apply the found deformation to the input ssi ssiSrc.toType(ca.MEM_DEVICE) cc.HtoReal(phi) affPhi = phi.copy() ssiBfi = ssiSrc.copy() upPhi = ca.Field3D(ssiSrc.grid(), phi.memType()) cc.ApplyAffineReal(affPhi, phi, np.linalg.inv(frgOb.affine)) cc.ResampleWorld(upPhi, affPhi, bg=2) cc.ApplyHReal(ssiBfi, ssiSrc, upPhi) # ssiPhi = ca.Image3D(ssiSrc.grid(), phi.memType()) # upPhi = ca.Field3D(ssiSrc.grid(), phi.memType()) # cc.ResampleWorld(upPhi, phi, bg=2) # cc.ApplyHReal(ssiPhi, ssiSrc, upPhi) # ssiBfi = ssiSrc.copy() # cc.ApplyAffineReal(ssiBfi, ssiPhi, np.linalg.inv(frgOb.affine)) # #Apply affine to the deformation # affPhi = phi.copy() # cc.ApplyAffineReal(affPhi, phi, np.linalg.inv(frgOb.affine)) if write: common.SaveITKImage( ssiBfi, pth.expanduser( secOb.ssiOutPath + 'frag{0}/M{1}_01_ssi_section_{2}_frag{0}_def_bfi.nrrd'.format( frgNum, secOb.mkyNum, secOb.secNum))) cc.WriteMHA( affPhi, pth.expanduser( secOb.ssiOutPath + 'frag{0}/M{1}_01_ssi_section_{2}_frag{0}_to_bfi_real.mha'. format(frgNum, secOb.mkyNum, secOb.secNum))) cc.WriteMHA( affV, pth.expanduser( secOb.ssiOutPath + 'frag{0}/M{1}_01_ssi_section_{2}_frag{0}_to_bfi_disp.mha'. format(frgNum, secOb.mkyNum, secOb.secNum))) #Create the list of names that the deformation should be applied to # nameList = ['M15_01_0956_SideLight_DimLED_10x_ORG.tif', # 'M15_01_0956_TyrosineHydroxylase_Ben_10x_Stitching_c1_ORG.tif', # 'M15_01_0956_TyrosineHydroxylase_Ben_10x_Stitching_c2_ORG.tif', # 'M15_01_0956_TyrosineHydroxylase_Ben_10x_Stitching_c3_ORG.tif'] # appLarge(nameList, affPhi) common.DebugHere()
def Fragmenter(): tmpOb = Config.Load( frgSpec, pth.expanduser( '~/korenbergNAS/3D_database/Working/configuration_files/SidescapeRelateBlockface/M{0}/section_{1}/section_{1}_frag0.yaml' .format(secOb.mkyNum, secOb.secNum))) dictBuild = {} #Load in the whole image so that the fragment can cropped out ssiSrc, bfiSrc, ssiMsk, bfiMsk = Loader(tmpOb, ca.MEM_HOST) #Because some of the functions only woth with gray images bfiGry = ca.Image3D(bfiSrc.grid(), bfiSrc.memType()) ca.Copy(bfiGry, bfiSrc, 1) lblSsi, _ = ndimage.label(np.squeeze(ssiMsk.asnp()) > 0) lblBfi, _ = ndimage.label(np.squeeze(bfiMsk.asnp()) > 0) seedPt = np.squeeze(pp.LandmarkPicker([lblBfi, lblSsi])) subMskBfi = common.ImFromNPArr(lblBfi == lblBfi[seedPt[0, 0], seedPt[0, 1]].astype('int8'), sp=bfiSrc.spacing(), orig=bfiSrc.origin()) subMskSsi = common.ImFromNPArr(lblSsi == lblSsi[seedPt[1, 0], seedPt[1, 1]].astype('int8'), sp=ssiSrc.spacing(), orig=ssiSrc.origin()) bfiGry *= subMskBfi bfiSrc *= subMskBfi ssiSrc *= subMskSsi #Pick points that are the bounding box of the desired subvolume corners = np.array( pp.LandmarkPicker( [np.squeeze(bfiGry.asnp()), np.squeeze(ssiSrc.asnp())])) bfiCds = corners[:, 0] ssiCds = corners[:, 1] #Extract the region from the source images bfiRgn = cc.SubVol(bfiSrc, xrng=[bfiCds[0, 0], bfiCds[1, 0]], yrng=[bfiCds[0, 1], bfiCds[1, 1]]) ssiRgn = cc.SubVol(ssiSrc, xrng=[ssiCds[0, 0], ssiCds[1, 0]], yrng=[ssiCds[0, 1], ssiCds[1, 1]]) #Extract the region from the mask images rgnMskSsi = cc.SubVol(subMskSsi, xrng=[ssiCds[0, 0], ssiCds[1, 0]], yrng=[ssiCds[0, 1], ssiCds[1, 1]]) rgnMskBfi = cc.SubVol(subMskBfi, xrng=[bfiCds[0, 0], bfiCds[1, 0]], yrng=[bfiCds[0, 1], bfiCds[1, 1]]) dictBuild['rgnBfi'] = np.divide( bfiCds, np.array(bfiSrc.size().tolist()[0:2], 'float')).tolist() dictBuild['rgnSsi'] = np.divide( ssiCds, np.array(ssiSrc.size().tolist()[0:2], 'float')).tolist() #Check the output directory for the source files of the fragment if not pth.exists( pth.expanduser(secOb.ssiSrcPath + 'frag{0}'.format(frgNum))): os.mkdir(pth.expanduser(secOb.ssiSrcPath + 'frag{0}'.format(frgNum))) if not pth.exists( pth.expanduser(secOb.bfiSrcPath + 'frag{0}'.format(frgNum))): os.mkdir(pth.expanduser(secOb.bfiSrcPath + 'frag{0}'.format(frgNum))) #Check the output directory for the mask files of the fragment if not pth.exists( pth.expanduser(secOb.ssiMskPath + 'frag{0}'.format(frgNum))): os.mkdir(pth.expanduser(secOb.ssiMskPath + 'frag{0}'.format(frgNum))) if not pth.exists( pth.expanduser(secOb.bfiMskPath + 'frag{0}'.format(frgNum))): os.mkdir(pth.expanduser(secOb.bfiMskPath + 'frag{0}'.format(frgNum))) dictBuild[ 'ssiSrcName'] = 'frag{0}/M{1}_01_ssi_section_{2}_frag1.tif'.format( frgNum, secOb.mkyNum, secOb.secNum) dictBuild[ 'bfiSrcName'] = 'frag{0}/M{1}_01_bfi_section_{2}_frag1.mha'.format( frgNum, secOb.mkyNum, secOb.secNum) dictBuild[ 'ssiMskName'] = 'frag{0}/M{1}_01_ssi_section_{2}_frag1_mask.tif'.format( frgNum, secOb.mkyNum, secOb.secNum) dictBuild[ 'bfiMskName'] = 'frag{0}/M{1}_01_bfi_section_{2}_frag1_mask.tif'.format( frgNum, secOb.mkyNum, secOb.secNum) #Write out the masked and cropped images so that they can be loaded from the YAML file #The BFI region needs to be saved as color and mha format so that the grid information is carried over. common.SaveITKImage( ssiRgn, pth.expanduser(secOb.ssiSrcPath + dictBuild['ssiSrcName'])) cc.WriteColorMHA( bfiRgn, pth.expanduser(secOb.bfiSrcPath + dictBuild['bfiSrcName'])) common.SaveITKImage( rgnMskSsi, pth.expanduser(secOb.ssiMskPath + dictBuild['ssiMskName'])) common.SaveITKImage( rgnMskBfi, pth.expanduser(secOb.bfiMskPath + dictBuild['bfiMskName'])) frgOb = Config.MkConfig(dictBuild, frgSpec) updateFragOb(frgOb) return None