class ImageDownloadApiTests(unittest.TestCase): def __init__(self, *args, **kwargs): super(ImageDownloadApiTests, self).__init__(*args, **kwargs) self.ida = None def setUp(self): self.ida = ImageDownloadApi() def tearDown(self): self.ida = None def test_api_doc_url_download_image_downsampled(self): ''' Notes ----- See: `Experimental Overview and Metadata `<http://help.brain-map.org/display/mouseconnectivity/API#API-ExperimentalOverviewandMetadata>_ , link labeled 'Download image downsampled by factor of 6 using default thresholds'. ''' expected = 'http://api.brain-map.org/api/v2/section_image_download/126862575?downsample=6&range=0,932,0,1279,0,4095' path = '126862575.jpg' self.ida.retrieve_file_over_http = \ MagicMock(name='retrieve_file_over_http') section_image_id = 126862575 self.ida.download_section_image(section_image_id, downsample=6, range=[0,932, 0,1279, 0,4095]) self.ida.retrieve_file_over_http.assert_called_once_with(expected, path) def test_api_doc_url_download_image_full_resolution(self): ''' Notes ----- See: `Experimental Overview and Metadata `<http://help.brain-map.org/display/mouseconnectivity/API#API-ExperimentalOverviewandMetadata>_ , link labeled 'Download a region of interest at full resolution using default thresholds'. ''' expected = 'http://api.brain-map.org/api/v2/section_image_download/126862575?left=19045&top=11684&width=1000&height=1000&range=0,932,0,1279,0,4095' path = '126862575.jpg' self.ida.retrieve_file_over_http = \ MagicMock(name='retrieve_file_over_http') section_image_id = 126862575 self.ida.download_section_image(section_image_id, left=19045, top=11684, width=1000, height=1000, range=[0,932, 0,1279, 0,4095]) self.ida.retrieve_file_over_http.assert_called_once_with(expected, path)
def download_section(savepath, section_id, downsample): # Downloading all of the images from a section data set image_api = ImageDownloadApi() input_directory = str(section_id) + '_input' output_directory = str(section_id) + '_output' format_str = '.jpg' section_images = image_api.section_image_query(section_id) section_image_ids = [si['id'] for si in section_images] # You have probably noticed that the AllenSDK has a logger which notifies you of file downloads. # Since we are downloading ~300 images, we don't want to see messages for each one. # The following line will temporarily disable the download logger.(optional) logging.getLogger( 'allensdk.api.api.retrieve_file_over_http').disabled = True for section_image_id in section_image_ids: file_name = str(section_image_id) + format_str input_file_path = os.path.join(savepath, input_directory, file_name) output_file_path = os.path.join(savepath, output_directory, file_name) Manifest.safe_make_parent_dirs(input_file_path) image_api.download_section_image(section_image_id, file_path=input_file_path, downsample=downsample, expression=0) Manifest.safe_make_parent_dirs(output_file_path) image_api.download_section_image(section_image_id, file_path=output_file_path, downsample=downsample, expression=1) # re-enable the logger (optional) logging.getLogger( 'allensdk.api.api.retrieve_file_over_http').disabled = False file_names = os.listdir(os.path.join(savepath, input_directory)) print(len(file_names))
def download_brain_slice(df): # create an image download API image_api = ImageDownloadApi() format_str = ".jpg" # You have probably noticed that the AllenSDK has a logger which notifies you of file downloads. # Since we are downloading ~300 images, we don't want to see messages for each one. # The following line will temporarily disable the download logger. logging.getLogger("allensdk.api.api.retrieve_file_over_http").disabled = True # get parameters path, downsample, indices = ask_parameters_for_downloading(df) print( "Downloads initiated", end="...", file=sys.stderr, flush=True, ) for index in indices: # from indices, get experiment id and gene symbol from df exp_id = df["Experiment"][index] # set the dirname as the gene symbol dirname = df["Gene Symbol"][index] plane = df["Plane"][index] section_data_set_id = exp_id section_image_directory = os.path.join(path, dirname) # get the image ids for all of the images in this data set section_images = image_api.section_image_query( section_data_set_id ) # Should be a dicionary of the features of section images section_image_ids = [ si["id"] for si in section_images ] # Take value of 'id' from the dictionary # Create a progress bar pbar_image = tqdm(total=len(section_image_ids), desc=dirname + " " + plane) for section_image_id in section_image_ids: file_name = str(section_image_id) + format_str file_path = os.path.join(section_image_directory, file_name) Manifest.safe_make_parent_dirs(file_path) # Check if the file is already downloaded, which happens if the downloads have been interrupted. saved_file_names = os.listdir(section_image_directory) if file_name in saved_file_names: pass else: image_api.download_section_image( section_image_id, file_path=file_path, downsample=downsample ) pbar_image.update() pbar_image.close() # re-enable the logger logging.getLogger("allensdk.api.api.retrieve_file_over_http").disabled = False print( "Downloads completed.", file=sys.stderr, flush=True, )
section_image_ids = [si['id'] for si in section_images] print(len(section_image_ids)) # You have probably noticed that the AllenSDK has a logger which notifies you of file downloads. # Since we are downloading ~300 images, we don't want to see messages for each one. # The following line will temporarily disable the download logger.(optional) logging.getLogger('allensdk.api.api.retrieve_file_over_http').disabled = True for section_image_id in section_image_ids: file_name = str(section_image_id) + format_str file_path = os.path.join(section_image_directory, file_name) Manifest.safe_make_parent_dirs(file_path) image_api.download_section_image(section_image_id, file_path=file_path, downsample=downsample,expression = expression, view='expression', colormap='expression') # re-enable the logger (optional) logging.getLogger('allensdk.api.api.retrieve_file_over_http').disabled = False file_names = os.listdir(section_image_directory) print(len(file_names))