+ "import numpy as np\nfrom moten.io import imagearray2luminance\n\nfrom voxelwise_tutorials.progress_bar import bar\nfrom voxelwise_tutorials.io import load_hdf5_array\n\n\ndef compute_luminance(run_name, size=(96, 96), batch_size=100):\n\n stimuli_file = os.path.join(directory, 'stimuli', run_name)\n\n # get the number of images in the stimuli file\n with h5py.File(stimuli_file, 'r') as f:\n n_images = f['stimuli'].shape[0]\n\n # compute the luminance on each batch\n luminance = np.zeros((n_images, *size))\n for start in bar(range(0, n_images, batch_size),\n title=f'compute_luminance({run_name})'):\n # load the batch of images\n batch = slice(start, start + batch_size)\n images = load_hdf5_array(stimuli_file, key='stimuli', slice=batch)\n\n # ``imagearray2luminance`` uses uint8 arrays\n if images.dtype != 'uint8':\n images = np.int_(np.clip(images, 0, 1) * 255).astype(np.uint8)\n\n # convert RGB images to a single luminance channel\n luminance[batch] = imagearray2luminance(images, size=size)\n\n return luminance\n\n\nluminance_train = np.concatenate(\n [compute_luminance(f\"train_{ii:02d}.hdf\") for ii in range(12)])\nluminance_test = compute_luminance(\"test.hdf\")"
0 commit comments