Skip to content

Commit a9ed7a2

Browse files
committed
MNT use np.clip in motion energy extraction
1 parent b37843a commit a9ed7a2

4 files changed

Lines changed: 4 additions & 6 deletions

File tree

tutorials/movies_3T/06_extract_motion_energy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ def compute_motion_energy(luminance,
135135
# add some noise to deal with constant black areas
136136
luminance_batch = luminance[batch].copy()
137137
luminance_batch += np.random.randn(*luminance_batch.shape) * noise
138-
luminance_batch[luminance_batch < 0] = 0
139-
luminance_batch[luminance_batch > 100] = 100
138+
luminance_batch = np.clip(luminance_batch, 0, 100)
140139

141140
motion_energy[batch] = pyramid.project_stimulus(luminance_batch)
142141

tutorials/movies_4T/01_extract_motion_energy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ def compute_motion_energy(luminance,
135135
# add some noise to deal with constant black areas
136136
luminance_batch = luminance[batch].copy()
137137
luminance_batch += np.random.randn(*luminance_batch.shape) * noise
138-
luminance_batch[luminance_batch < 0] = 0
139-
luminance_batch[luminance_batch > 100] = 100
138+
luminance_batch = np.clip(luminance_batch, 0, 100)
140139

141140
motion_energy[batch] = pyramid.project_stimulus(luminance_batch)
142141

tutorials/notebooks/movies_3T/06_extract_motion_energy.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
},
9292
"outputs": [],
9393
"source": [
94-
"from scipy.signal import decimate\nfrom moten.pyramids import MotionEnergyPyramid\n\n# fixed experiment settings\nN_FRAMES_PER_SEC = 15\nN_FRAMES_PER_TR = 30\nN_TRS_PER_RUN = 300\n\n\ndef compute_motion_energy(luminance,\n batch_size=N_TRS_PER_RUN * N_FRAMES_PER_TR,\n noise=0.1):\n\n n_frames, height, width = luminance.shape\n\n # We create a pyramid instance, with the main motion-energy parameters.\n pyramid = MotionEnergyPyramid(stimulus_vhsize=(height, width),\n stimulus_fps=N_FRAMES_PER_SEC,\n spatial_frequencies=[0, 2, 4, 8, 16, 32])\n\n # We batch images run by run.\n motion_energy = np.zeros((n_frames, pyramid.nfilters))\n for ii, start in enumerate(range(0, n_frames, batch_size)):\n batch = slice(start, start + batch_size)\n print(\"run %d\" % ii)\n\n # add some noise to deal with constant black areas\n luminance_batch = luminance[batch].copy()\n luminance_batch += np.random.randn(*luminance_batch.shape) * noise\n luminance_batch[luminance_batch < 0] = 0\n luminance_batch[luminance_batch > 100] = 100\n\n motion_energy[batch] = pyramid.project_stimulus(luminance_batch)\n\n # decimate to the sampling frequency of fMRI responses\n motion_energy_decimated = decimate(motion_energy, N_FRAMES_PER_TR,\n ftype='fir', axis=0)\n return motion_energy_decimated\n\n\nmotion_energy_train = compute_motion_energy(luminance_train)\nmotion_energy_test = compute_motion_energy(luminance_test)"
94+
"from scipy.signal import decimate\nfrom moten.pyramids import MotionEnergyPyramid\n\n# fixed experiment settings\nN_FRAMES_PER_SEC = 15\nN_FRAMES_PER_TR = 30\nN_TRS_PER_RUN = 300\n\n\ndef compute_motion_energy(luminance,\n batch_size=N_TRS_PER_RUN * N_FRAMES_PER_TR,\n noise=0.1):\n\n n_frames, height, width = luminance.shape\n\n # We create a pyramid instance, with the main motion-energy parameters.\n pyramid = MotionEnergyPyramid(stimulus_vhsize=(height, width),\n stimulus_fps=N_FRAMES_PER_SEC,\n spatial_frequencies=[0, 2, 4, 8, 16, 32])\n\n # We batch images run by run.\n motion_energy = np.zeros((n_frames, pyramid.nfilters))\n for ii, start in enumerate(range(0, n_frames, batch_size)):\n batch = slice(start, start + batch_size)\n print(\"run %d\" % ii)\n\n # add some noise to deal with constant black areas\n luminance_batch = luminance[batch].copy()\n luminance_batch += np.random.randn(*luminance_batch.shape) * noise\n luminance_batch = np.clip(luminance_batch, 0, 100)\n\n motion_energy[batch] = pyramid.project_stimulus(luminance_batch)\n\n # decimate to the sampling frequency of fMRI responses\n motion_energy_decimated = decimate(motion_energy, N_FRAMES_PER_TR,\n ftype='fir', axis=0)\n return motion_energy_decimated\n\n\nmotion_energy_train = compute_motion_energy(luminance_train)\nmotion_energy_test = compute_motion_energy(luminance_test)"
9595
]
9696
},
9797
{

tutorials/notebooks/movies_4T/01_extract_motion_energy.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
},
9999
"outputs": [],
100100
"source": [
101-
"from scipy.signal import decimate\nfrom moten.pyramids import MotionEnergyPyramid\n\n# fixed experiment settings\nN_FRAMES_PER_SEC = 15\nN_FRAMES_PER_TR = 15\nN_TRS_PER_RUN = 600\n\n\ndef compute_motion_energy(luminance,\n batch_size=N_TRS_PER_RUN * N_FRAMES_PER_TR,\n noise=0.1):\n\n n_frames, height, width = luminance.shape\n\n # We create a pyramid instance, with the main motion-energy parameters.\n pyramid = MotionEnergyPyramid(stimulus_vhsize=(height, width),\n stimulus_fps=N_FRAMES_PER_SEC,\n spatial_frequencies=[0, 2, 4, 8, 16, 32])\n\n # We batch images run by run.\n motion_energy = np.zeros((n_frames, pyramid.nfilters))\n for ii, start in enumerate(range(0, n_frames, batch_size)):\n batch = slice(start, start + batch_size)\n print(\"run %d\" % ii)\n\n # add some noise to deal with constant black areas\n luminance_batch = luminance[batch].copy()\n luminance_batch += np.random.randn(*luminance_batch.shape) * noise\n luminance_batch[luminance_batch < 0] = 0\n luminance_batch[luminance_batch > 100] = 100\n\n motion_energy[batch] = pyramid.project_stimulus(luminance_batch)\n\n # decimate to the sampling frequency of fMRI responses\n motion_energy_decimated = decimate(motion_energy, N_FRAMES_PER_TR,\n ftype='fir', axis=0)\n return motion_energy_decimated\n\n\nmotion_energy_train = compute_motion_energy(luminance_train)\nmotion_energy_test = compute_motion_energy(luminance_test)"
101+
"from scipy.signal import decimate\nfrom moten.pyramids import MotionEnergyPyramid\n\n# fixed experiment settings\nN_FRAMES_PER_SEC = 15\nN_FRAMES_PER_TR = 15\nN_TRS_PER_RUN = 600\n\n\ndef compute_motion_energy(luminance,\n batch_size=N_TRS_PER_RUN * N_FRAMES_PER_TR,\n noise=0.1):\n\n n_frames, height, width = luminance.shape\n\n # We create a pyramid instance, with the main motion-energy parameters.\n pyramid = MotionEnergyPyramid(stimulus_vhsize=(height, width),\n stimulus_fps=N_FRAMES_PER_SEC,\n spatial_frequencies=[0, 2, 4, 8, 16, 32])\n\n # We batch images run by run.\n motion_energy = np.zeros((n_frames, pyramid.nfilters))\n for ii, start in enumerate(range(0, n_frames, batch_size)):\n batch = slice(start, start + batch_size)\n print(\"run %d\" % ii)\n\n # add some noise to deal with constant black areas\n luminance_batch = luminance[batch].copy()\n luminance_batch += np.random.randn(*luminance_batch.shape) * noise\n luminance_batch = np.clip(luminance_batch, 0, 100)\n\n motion_energy[batch] = pyramid.project_stimulus(luminance_batch)\n\n # decimate to the sampling frequency of fMRI responses\n motion_energy_decimated = decimate(motion_energy, N_FRAMES_PER_TR,\n ftype='fir', axis=0)\n return motion_energy_decimated\n\n\nmotion_energy_train = compute_motion_energy(luminance_train)\nmotion_energy_test = compute_motion_energy(luminance_test)"
102102
]
103103
},
104104
{

0 commit comments

Comments
 (0)