"""Atlas-related ("register") tasks for MagellanMapper"""importosimportrefromtypingimportOptional,Sequencefrommagmap.ioimportexport_regionsfrommagmap.settingsimportconfigfrommagmap.statsimportvols_logger=config.logger.getChild(__name__)
[docs]defbuild_labels_diff_images(paths:Optional[Sequence[str]]=None):"""Build labels difference images or regional heat maps for given metrics. Replaces each label in an atlas labels image with the metric value. :class:`magmap.settings.config.PlotLabels.X_COL` in :attr:`magmap.settings.config.plot_labels` can be used to change the metric column, which otherwise defaults to "vals.effect". Args: paths: Paths to volume stat files output from the R pipeline or output from the ``--register vol_stats`` pipeline (:meth:`magmap.atlas.register.volumes_by_id`). """# set the measurement columncol_meas=config.plot_labels[config.PlotLabels.X_COL]ifnotcol_meas:col_meas="vals.effect"ifpaths:# set up metrics from filenames after first (image) filename;# extract metrics from R stats filename formatpath_dfs=pathsmetrics=[re.search(r"vols_stats_(.*).csv",p)forpinpath_dfs]metrics=[m.group(1)ifmelsemforminmetrics]ifnotmetrics[0]:# fall back to the measurement column, eg for metrics from the# vol_stats pipelinemetrics[0]=col_measelse:# set up default metrics and assume corresponding CSVs are in# current working directorymetrics=(vols.LabelMetrics.EdgeDistSum.name,vols.LabelMetrics.CoefVarNuc.name,vols.LabelMetrics.CoefVarIntens.name,vols.LabelMetrics.NucCluster.name,vols.LabelMetrics.NucClusNoise.name,#vols.MetricCombos.HOMOGENEITY.value[0], )path_dfs=[f"vols_stats_{m}.csv"forminmetrics]forpath_df,metricinzip(path_dfs,metrics):ifnotos.path.exists(path_df):# check for existing R stats file_logger.warn(f"{path_df} not found, skipping")continueifnotmetric:# check for extracted metric name_logger.warn(f"Metric not found from {path_df}, skipping")continue# generate difference imagecol_wt=vols.get_metric_weight_col(metric)export_regions.map_metric_to_labels_img(config.filename,path_df,col_meas,None,config.prefix,config.show,config.labels_level,metric,col_wt)