def update_metadata(self): mpl4 = self.get_megacube_path('mpl-4/drpall-v1_5_1.fits') mpl9 = self.get_megacube_path('mpl-9/drpall-v2_7_1.fits') mpl4_metadata = mclass().get_metadata(mpl4) self.stdout.write('Fetched MPL4') mpl9_metadata = mclass().get_metadata(mpl9) self.stdout.write('Fetched MPL9') columns = self.get_model_fields(Image) columns.remove('id') for column in list(mpl4_metadata): if column not in columns: del mpl4_metadata[column] for column in list(mpl9_metadata): if column not in columns: del mpl9_metadata[column] self.stdout.write('Merged the two MPL file data') dict_metadata = self.merge_same_keys_dict(mpl4_metadata, mpl9_metadata) list_metadata = self.dict_list_to_list_dict(dict_metadata) self.stdout.write( 'Transformed the dictionary of lists to a list of dictionaries') for row in list_metadata: filename = 'manga-%s-MEGA.fits.fz' % row['plateifu'] megacube = self.get_megacube_path(filename) if os.path.isfile(megacube): try: self.stdout.write('File {} was found!'.format(filename)) new_image = Image() for key in row.keys(): setattr(new_image, key, row[key]) # Adding the filename to table setattr(new_image, 'megacube', filename) new_image.save() self.stdout.write('Inserted its metadata into database') # Verifying that the there's not duplicates except IntegrityError: pass self.stdout.write('Finished the database integration')
def test(self, request, pk=None): galaxy = self.get_object() megacube = self.get_megacube_path(galaxy.megacube) data = mclass().image_by_hud(megacube, 'Adev') z = mclass().image_data_to_array(data, ) return Response(z)
def exctract_original_image(self): """ It extracts the Origimal Image (zero) data from 'FLUX' HUD for each image and save them in a very small JSON file in the path /images/megacube_parts/megacube_{JOB_ID}/original_image.json. """ self.stdout.write("".ljust(100, '-')) self.stdout.write('Started Original Image Extraction') images = Image.objects.all() for image in images: self.stdout.write('Extracting Original Image [%s]' % str(image.id)) megacube = self.get_megacube_path(image.megacube) cube_data = mclass().get_original_cube_data(megacube) content = dict({ 'z': cube_data, 'title': 'FLUX', }) filename = 'original_image.json' self.write_in_megacube_path( image.megacube.split('.fits.fz')[0], filename, content) self.stdout.write('Finished Original Image Extraction!')
def extract_megacube_header(self): """ It extracts the Megacube Header from 'PoPBins' HUD for each image and save them in a very small JSON file in the path /images/megacube_parts/megacube_{JOB_ID}/cube_header.json. """ self.stdout.write("".ljust(100, '-')) self.stdout.write('Started Megacube Header Extraction') images = Image.objects.all() for image in images: self.stdout.write('Extracting Megacube Header [%s]' % str(image.id)) megacube = self.get_megacube_path(image.megacube) content = repr(mclass().get_headers(megacube, 'PoPBins')).split('\n') filename = 'cube_header.json' self.write_in_megacube_path( image.megacube.split('.fits.fz')[0], filename, content) self.stdout.write('Finished Megacube Header Extraction!')
def vecs_by_position(self, request, pk=None): """ Returns the Vecs by an X, Y position. Args: <br> x ([number]): position X on Image. <br> y ([number]): position Y on Image. <br> Returns: <br> [dict]: a dictionary with 'x', 'y', 'm' and 'mlegend'. <br> - x ([list[number]]) <br> - y ([list[number]]) <br> - m ([list[number]]) <br> - mlegend ([list[string]]) """ params = request.query_params if 'x' not in params: raise Exception("Parameter x is required") if 'y' not in params: raise Exception("Parameter y is required") galaxy = self.get_object() megacube = self.get_megacube_path(galaxy.megacube) vecs = mclass().vecs_by_position(megacube, int(params['x']), int(params['y'])) return Response(vecs)
def extract_list_hud(self): """ It extracts the List of HUDs from 'PoPBins' HUD for each image and save them in a very small JSON file in the path /images/megacube_parts/megacube_{JOB_ID}/list_hud.json. """ self.stdout.write("".ljust(100, '-')) self.stdout.write('Started List Of HUD Extraction') images = Image.objects.all() for image in images: self.stdout.write('Extracting List Of HUD [%s]' % str(image.id)) megacube = self.get_megacube_path(image.megacube) cube_header = mclass().get_headers(megacube, 'PoPBins') cube_data = mclass().get_cube_data(megacube, 'PoPBins') cube_comments = mclass().get_comments(megacube, 'PoPBins') lHud = mclass().get_all_hud(cube_header, cube_data) dHud = list() for hud in lHud: # TODO: recuperar o display name para cada HUD dHud.append({ 'name': hud, 'display_name': hud, 'comment': cube_comments[hud] }) dHud = sorted(dHud, key=lambda i: i['display_name']) content = ({'hud': dHud}) filename = 'list_hud.json' self.write_in_megacube_path( image.megacube.split('.fits.fz')[0], filename, content) self.stdout.write('Finished List Of HUD Extraction!')
def flux_by_position(self, request, pk=None): """ Returns the Flux, Lambda and Synt by an X, Y position. Args: <br> x ([number]): position X on Image. <br> y ([number]): position Y on Image. Returns: <br> [dict]: a dictionary with the 'flux', 'lamb' and 'synt'. <br> - flux ([list[number]]) <br> - lamb ([list[number]]) <br> - synt ([list[number]]) """ params = request.query_params if 'x' not in params: raise Exception("Parameter x is required") if 'y' not in params: raise Exception("Parameter y is required") galaxy = self.get_object() megacube = self.get_megacube_path(galaxy.megacube) flux, lamb = mclass().flux_by_position(megacube, int(params['x']), int(params['y'])) synt, lamb2 = mclass().synt_by_position(megacube, int(params['x']), int(params['y'])) result = dict({ 'flux': flux.tolist(), 'lamb': lamb.tolist(), 'synt': synt.tolist(), }) return Response(result)
def extract_image_heatmap(self): """ It extracts all the Image Heatmaps from 'PoPBins' HUD for each image and for each HUD saved in the file /images/megacube_parts/megacube_{JOB_ID}/list_hud.json and save them in very small JSON files in the path /images/megacube_parts/megacube_{JOB_ID}/image_heatmap_{HUD}.json.json. """ self.stdout.write("".ljust(100, '-')) self.stdout.write('Started Image Heatmap Extraction') images = Image.objects.all() for i, image in enumerate(images): t0 = datetime.now() self.stdout.write('Started Image Heatmap By ID [%s]: [%s]' % (image.id, t0.strftime("%Y-%m-%d %H:%M:%S"))) megacube = self.get_megacube_path(image.megacube) cube_header = mclass().get_headers(megacube, 'PoPBins') cube_data = mclass().get_cube_data(megacube, 'PoPBins') lHud = mclass().get_all_hud(cube_header, cube_data) for hud in lHud: self.stdout.write('Extracting Image Heatmap [%s] Of HUD [%s]' % (str(image.id), str(hud))) megacube = self.get_megacube_path(image.megacube) image_data = mclass().image_by_hud(megacube, hud) # z = mclass().image_data_to_array(image_data) content = dict({ 'z': image_data, 'title': hud, }) filename = 'image_heatmap_%s.json' % hud self.write_in_megacube_path( image.megacube.split('.fits.fz')[0], filename, content) # End time of Image Heatmap t1 = datetime.now() self.stdout.write('Finished Image Heatmap By ID [%s]: [%s]' % (image.id, t0.strftime("%Y-%m-%d %H:%M:%S"))) self.stdout.write('Progress: [%s/%s]' % (i + 1, len(images))) tdelta = t1 - t0 self.stdout.write( 'Execution Time By ID [%s]: [%s]' % (image.id, humanize.naturaldelta(tdelta, minimum_unit="seconds"))) self.stdout.write("".ljust(100, '-')) self.stdout.write('Finished Image Heatmap Extraction!')