Skip to content

Commit

Permalink
added code to apply bidi offset correction manually
Browse files Browse the repository at this point in the history
  • Loading branch information
zhounapeuw committed Oct 3, 2022
1 parent 696655f commit a4a4faa
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions napeca/bidi_correction_manual.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import os\n",
"import h5py\n",
"import sima\n",
"from sima import sequence\n",
"from sima.imaging import ImagingDataset\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import bidi_offset_correction\n",
"import sima_motion_bidi_correction as bidi_funcs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fname = '22_1_19_gck_cgm_3_sacchari-001' # h5 file name (without the extension)\n",
"fdir = r'D:\\bruker_data\\Jennifer\\22_1_19_gck_cgm_3_sacchari-001' # root folder of h5 to cut\n",
"data_format = 'sima'\n",
"manual_offset_value = 10 # in pixels (I found this to be pixels you measure by eye +1)\n",
"\n",
"num_save_frames = 300 # use the following for saving and applying offset to all frames: data_n_meta['data'].shape[0] "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load an object referencing the motion-corrected data\n",
"data_n_meta = bidi_funcs.load_sima_or_h5_object(fdir, fname, data_format)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# mean_subsample_img = np.squeeze(np.nanmean(data_n_meta['data'][::40,...], axis=0))\n",
"# my_bidi_corr_obj = bidi_offset_correction.bidi_offset_correction(mean_subsample_img) # initialize data to object\n",
"\n",
"# my_bidi_corr_obj.compute_mean_image() # compute mean image across time\n",
"# bidi_offset = my_bidi_corr_obj.determine_bidi_offset() # calculated bidirectional offset via fft cross-correlation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# applies an offset to odd lines. Every time you run this, it will update the sima offsets file!!!\n",
"bidi_funcs.apply_bidi_corr_to_sima_offsets(fdir, fname, -manual_offset_value-1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# show mean image\n",
"dataset = sima.ImagingDataset.load(os.path.join(fdir, os.path.splitext(fname)[0] + '_mc.sima'))\n",
"sequence_data = dataset.sequences[0][:num_save_frames,...]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# comment this out if you don't want to wait for the mean image to process and show in this notebook\n",
"plt.figure(figsize=(7,7))\n",
"plt.imshow(np.nanmean(np.squeeze(sequence_data), axis=0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# saves an h5 based on the motion correction shifts calculated by sima\n",
"data_to_save = np.empty([num_save_frames, dataset.frame_shape[1], dataset.frame_shape[2]])\n",
"frame_iter1 = iter(sequence_data)\n",
"\n",
"# these next few lines just fill in the empty spaces\n",
"fill_gapscaller = bidi_funcs.fill_gaps(0, sequence_data, frame_iter1)\n",
"fill_gapscaller.send(None)\n",
"\n",
"for frame_num in range(num_save_frames):\n",
" data_to_save[frame_num, ...] = fill_gapscaller.send(frame_num).astype('uint8')\n",
"\n",
"# save to an h5\n",
"sima_mc_bidi_outpath = os.path.join(fdir, fname + '_sima_mc.h5')\n",
"h5_write_bidi_corr = h5py.File(sima_mc_bidi_outpath, 'w')\n",
"h5_write_bidi_corr.create_dataset('imaging', data=data_to_save.astype('uint8'))\n",
"h5_write_bidi_corr.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit a4a4faa

Please sign in to comment.