JaneliaSciComp/bigstream

rerunning the same code block in jupyter notebook

Closed this issue · 4 comments

I found that rerunning the same code (e.g. global affine alignment and transformation) in a Jupyter notebook generated different results.

It looks like the new alignment code still somehow remembers the previous alignment. Is this the case? Below is an example code. If you rerun this repeatedly in a jupyter notebook, the new cost function/metric would not start decreasing from the beginning (~0 cross correlation), but it seemed to remember where it was last time.

Could you clarify this? Thanks a lot!

# define alignment steps
ransac_kwargs = {
    'blob_sizes':[2, 8],
    'cc_radius':12,
    'match_threshold':0.4,
    'nspots':10000,
}

affine_kwargs = {
    'alignment_spacing':4.0,
    'shrink_factors':(1,),
    'smooth_sigmas':(4.,),
    'optimizer_args':{
        'learningRate':0.25,
        'minStep':0.,
        'numberOfIterations':400,
    },
    'metric':'C',
}


steps = [
    ('ransac', ransac_kwargs,),
    ('affine', affine_kwargs,),
]

mov = r2
fix = r1

# align
affine = alignment_pipeline(
    fix, mov,
    fix_spacing_s4,
    mov_spacing_s4,
    steps,
    # fix_mask=fix_mask,
    # mov_mask=mov_mask,
)

# apply affine only
affine_aligned = apply_transform(
    fix, mov,
    fix_spacing_s4, mov_spacing_s4,
    transform_list=[affine,],
)

# write results
np.savetxt(outdir+'/affine_r2tor1.mat', affine)
tifffile.imwrite(outdir+'/affine_r2tor1.tiff', affine_aligned)

Hi Fangming,

Thanks for adding these questions here. Some of them are known issues with workarounds. Like the rerun-gives-different-results issue. The SimpleITK ImageRegistrationMethod has some kind of memory, even when all python references to it are removed. Unfortunately the work around for now is to just restart the Jupyter notebook kernel. You need to do this between each run, otherwise you don’t know if changes are due to the internal state of the IRM or due to any parameter changes you made. I will find the cause of this and fix it soon.

I’m leaving for a two week trip on Monday and won’t be working at all during that time but I will look into these issues when I’m back and send any help that I can.

@GFleishman Thanks! No worries. Yes that is how I am doing now: I restart the kernel for each run. It works fine!

It just took me a while to notice this, as I was experimenting with different parameters and got inconsistent results that puzzled me for a while.

@FangmingXie FYI I identified and fixed a bug today that was retaining previous registration information and using it to initialize subsequent registrations. I'm not sure if this was the only place where a memory was being stored that interfered with subsequent alignments, but so far in all my testing the memory issue seems to be resolved so I'm closing this issue.
The bug fix is released on PyPI, bigstream versions 1.2.2 and later should have this bug fixed.

Thanks!