def map( imbin_file, som_file, map_file, trans_file, som_width, som_height, cpu_only=False, nrot=360, numthreads=10, overwrite=False, log=True, ): # /// Map through SOM \\\ imgs = pu.ImageReader(imbin_file) if not os.path.exists(map_file) or overwrite: map_imbin( imbin_file, som_file, map_file, trans_file, som_width, som_height, numthreads=numthreads, cpu=cpu_only, nrot=nrot, log=log, ) som = pu.SOM(som_file) mapping = pu.Mapping(map_file) transform = pu.Transform(trans_file) somset = pu.SOMSet(som, mapping, transform) return somset
def init_somset(som_file, map_file=None, trans_file=None): """Initialize the SOMSet, which contains the SOM, mapping, and transform files. Args: som_file (str): Name of the SOM file. map_file (str, optional): Name of the mapping file. Defaults to None, in which case it follows the default naming scheme. trans_file (str, optional): Name of the transform file. Defaults to None. Returns: pu.SOMSet: A container holding the SOM, mapping, and transform files. """ som = pu.SOM(som_file) if map_file is None: map_file = som_file.replace("SOM", "MAP") mapping = pu.Mapping(map_file) if trans_file is None: trans_file = map_file.replace("MAP", "TRANSFORM") transform = pu.Transform(trans_file) somset = pu.SOMSet(som=som, mapping=mapping, transform=transform) return somset
def __init__( self, som: Union[str, pu.SOM], results: Union[str, Dict[tuple, Annotation]] = None, save: Union[bool, str, None] = True, ): """An object to help manage and annotate a SOM and its neurons Arguments: som {Union[str, pu.SOM]} -- SOM file to annotate as a PINK binary Keyword Arguments: results {Union[bool, str, Dict[tuple, Annotation]]} -- A path to a pickled Annotator object, an existing Dict of appropriate mappings for neurons and their corresponding Annotations. If None a new Dict is created. If `True` will attempt to load from a default path (SOM path with `.results.pkl`). (default: {None}) save {Union[bool, str, None]} -- Action to save (default: {True}) Returns: [type] -- [description] """ self.som = pu.SOM(som) if isinstance(som, str) else som logger.info(f"Loaded SOM {som}...") self.results: Dict[tuple, Annotation] = {} if results not in [None, False]: if isinstance(results, str): try: with open(results, "rb") as infile: self.results = pickle.load(infile) except FileNotFoundError: logger.warn( f"Results file {results} not found. Continuing with an empty result set." ) elif isinstance(results, dict): self.results = results elif results == True: try: with open(f"{self.som.path}.{ANT_SUFFIX}", "rb") as infile: self.results = pickle.load(infile) except FileNotFoundError: logger.warn( f"Results file {self.som.path}.{ANT_SUFFIX} not found. Continuing with an empty result set. " ) else: raise ValueError( f"Expected either a path of a pickled Annotator or a Dict are accepted, got {type(results)}" ) self.save: Union[str, None] = None if save == True: self.save = f"{self.som.path}.{ANT_SUFFIX}" elif isinstance(save, str): self.save = save
# /// Select Sample (single tile) \\\ print("Loading the VLASS catalogue, specifying complex components") radio_cat_file = os.path.join(radio_path, radio_component_catalogue) radio_cat = vdl.load_vlass_catalogue(radio_cat_file, complex=True, NN_dist=72, SNR_min=None) print("Creating the tile catalogue") subtile_file = os.path.join(radio_path, vlass_subtiles) subtile_cat = Table.read(subtile_file, format="csv").to_pandas() tile_cat = vdl.vlass_tile(subtile_cat) print("Loading the SOM and annotations") som = pu.SOM(som_file) annotation = pu.Annotator(som.path, results=annotations_file) # sdss = sky_chunk(df, (120, 240), (-10, 50)) tiles = tile_cat.Tile.values ti = int(sys.argv[1]) if len(sys.argv) >= 2 else 0 num_tiles = int(sys.argv[2]) if len(sys.argv) >= 3 else 1 tiles = tiles[ti:ti + num_tiles] for tile_id in tiles: print(f"Processing tile {tile_id}") imbin_file, map_file, trans_file = cxc.binary_names(tile_id, path=out_bin_path) # Load VLASS and unWISE catalogues
print(f" \includegraphics[width=0.75\\textwidth]{{{base}.png}}", file=outfile) print(f" \includegraphics[width=0.75\\textwidth]{{{base}_img0.png}}", file=outfile) print(f" \includegraphics[width=0.75\\textwidth]{{{base}_img1.png}}", file=outfile) print(f" \caption{{{caption}}}", file=outfile) print(f" \label{{fig:{label}}}", file=outfile) print(f"\end{{figure*}}", file=outfile) if len(sys.argv) < 5: print(f"USAGE: {sys.argv[0]} IMG SOM MAP TRANSFORM") imgs = pu.ImageReader(sys.argv[1]) som = pu.SOM(sys.argv[2]) mapping = pu.Mapping(sys.argv[3]) trans = pu.Transform(sys.argv[4]) somset = pu.SOMSet(som, mapping, trans) path = pu.PathHelper("Inspection") outfile = open("latex_imgs.tex", "w") ### SOM 2D histogram ### som_counts(somset, True) plt.savefig(f"som_stats.png") plt.clf() caption = f"The total number of images within the {imgs.data.shape[0]} input images mapped to each neuron in the SOM." latex_figure("som_stats.png", caption, "som_stats", outfile) print("", file=outfile) ########################
def run_all( catalogues, som_file, unique_id, image_cutout_path, bin_path="", img_size=(2, 150, 150), numthreads=10, annotation=None, annotations_file=None, remove_tile_cutouts=False, cpu_only=False, sorter_mode="area_ratio", pix_scale=0.6, som_mask=None, ): """Run the preprocess, map, and collate steps for a single sample. Args: catalogues (tuple): DataFrames of the radio and ir catalogues som_file (str): Name of the SOM file unique_id (str): Unique identifier for the sample (tile id, ssid, etc) image_cutout_path (str): Path to the directory containing the image cutouts annotations_file (str, optional): Name of the SOM annotations file. Defaults to a name based on the SOM file name. Returns: [type]: [description] """ radio_cat, ir_cat = catalogues # Preprocess imbin_file, map_file, trans_file = binary_names(unique_id, bin_path) print(f"Preprocessing the sample: {imbin_file}") imgs = preprocess( radio_cat, imbin_file, img_size=img_size, tile_cutout_path=image_cutout_path, remove_tile_cutouts=remove_tile_cutouts, ) print("...done") # Map print(f"Mapping...") som = pu.SOM(som_file) w, h, d = som.som_shape somset = map( imbin_file, som_file, map_file, trans_file, w, h, numthreads=numthreads, cpu_only=cpu_only, ) somset.som.bmu_mask = som_mask print("...done") # Collate if annotation is None: if annotations_file is None: annotations_file = f"{som_file}.results.pkl" annotation = pu.Annotator(som.path, results=annotations_file) print(f"Collating...") comp_cat, src_cat = collate( radio_cat, ir_cat, imgs, somset, annotation, sorter_mode=sorter_mode, pix_scale=pix_scale, ) print("...done") return comp_cat, src_cat