Пример #1
0
    def get_valid_out_paths(self,
                            sample,
                            presets_collected,
                            sub_dirs=('scores', 'out_segms'),
                            file_extensions=('.yml', '.h5'),
                            overwrite_previous=False):
        experiment_dir = self.get("exp_path")

        # Compose output file name:
        filename = sample
        for preset in presets_collected:
            filename += "__{}".format(preset)
        post_fix = self.get('postproc_config/save_name_postfix', None)
        if post_fix is not None:
            filename = filename + "__" + post_fix

        ID = str(np.random.randint(1000000000))
        out_file_paths = []
        for file_ext, dir_type in zip(file_extensions, sub_dirs):
            # Create directories:
            dir_path = os.path.join(experiment_dir, dir_type)
            segm_utils.check_dir_and_create(dir_path)

            # Backup old file, it already exists:
            candidate_file = os.path.join(dir_path, filename + file_ext)
            if os.path.exists(candidate_file) and not overwrite_previous:
                candidate_file = candidate_file.replace(
                    file_ext, "__{}{}".format(ID, file_ext))
                # Renaming previous file is not thread safe...
                # shutil.move(candidate_file, candidate_file.replace(file_ext, "__{}{}".format(ID, file_ext)))
            out_file_paths.append(candidate_file)

        return out_file_paths
Пример #2
0
    def save_infer_output(self, output):
        print("Saving....")
        if self.get("export_path") is not None:
            dir_path = os.path.join(
                self.get("export_path"),
                self.get("name_experiment", default="generic_experiment"))
        else:
            try:
                # Only works for my experiments saving on trendyTukan, otherwise it will throw an error:
                trendyTukan_path = get_trendytukan_drive_dir()
            except ValueError:
                raise ValueError(
                    "TrendyTukan drive not found. Please specify an `export_path` in the config file."
                )
            dir_path = os.path.join(
                trendyTukan_path, "projects/pixel_embeddings",
                self.get("name_experiment", default="generic_experiment"))
        check_dir_and_create(dir_path)
        filename = os.path.join(
            dir_path,
            "predictions_sample_{}.h5".format(self.get("loaders/infer/name")))
        print("Writing to ", self.get("inner_path_output", 'data'))
        writeHDF5(output.astype(np.float16), filename,
                  self.get("inner_path_output", 'data'))
        print("Saved to ", filename)

        # Dump configuration to export folder:
        self.dump_configuration(os.path.join(dir_path,
                                             "prediction_config.yml"))
Пример #3
0
 def save_infer_output(self, output):
     import h5py
     import numpy as np
     print("Saving....")
     from segmfriends.utils.various import check_dir_and_create
     dir_path = os.path.join(
         get_trendytukan_drive_path(), "projects/pixel_embeddings",
         self.get("name_experiment", default="generic_experiment"))
     check_dir_and_create(dir_path)
     with h5py.File(
             os.path.join(
                 dir_path, "predictions_sample_{}.h5".format(
                     self.get("loaders/infer/name"))), 'w') as f:
         f.create_dataset('data',
                          data=output.astype(np.float16),
                          compression='gzip')
Пример #4
0
    def save_infer_output(self, output):
        import h5py
        import numpy as np
        print("Saving....")
        from segmfriends.utils.various import check_dir_and_create
        dir_path = os.path.join(
            get_trendytukan_drive_path(), "projects/pixel_embeddings",
            self.get("name_experiment", default="generic_experiment"))
        check_dir_and_create(dir_path)
        filename = os.path.join(
            dir_path,
            "predictions_sample_{}.h5".format(self.get("loaders/infer/name")))
        print("Writing to ", self.get("inner_path_output", 'data'))
        from segmfriends.utils.various import writeHDF5
        writeHDF5(output.astype(np.float16), filename,
                  self.get("inner_path_output", 'data'))
        print("Saved to ", filename)

        # Dump configuration to export folder:
        self.dump_configuration(os.path.join(dir_path,
                                             "prediction_config.yml"))
Пример #5
0
    def build_experiment(self):
        # Create dirs if not existing:
        exp_name = self.get("experiment_name", ensure_exists=True)
        exp_path = os.path.join(self.get("proj_dir_path"), exp_name)
        self.set("exp_path", exp_path)
        exp_path_exists = segm_utils.check_dir_and_create(exp_path)

        # Check paths where to get affinities:
        if self.get("volume_config/affinities/path") is None:
            # Check if an experiment folder is given:
            if self.get("affs_exp_name") is not None:
                affs_dir_path = os.path.join(self.get("proj_dir_path"),
                                             self.get("affs_exp_name"))
            else:
                assert exp_path_exists, "None affs path has been passed and none were found in exp folder!"
                affs_dir_path = exp_path
            self.set("affinities_dir_path", affs_dir_path)
Пример #6
0
def scatter_plot(results_collected, project_directory, exp_name):
    colors = {
        'max': {
            'False': {
                'True': 'C4',
                'False': 'C0'
            }
        },
        'mean': {
            'False': {
                'True': 'C5',
                'False': 'C1'
            },
            'True': {
                'True': 'C6',
                'False': 'C8'
            }
        },
        'sum': {
            'False': {
                'False': 'C2'
            },
            'True': {
                'False': 'C3'
            }
        },
    }

    ncols, nrows = 1, 1

    list_all_keys = [
        ['score_WS', 'adapted-rand'],
        # ['score_WS', "vi-merge"],
        # ['score_WS', "vi-split"],
        # ['energy'],
        # ['runtime']
    ]

    for all_keys in list_all_keys:

        selected_prob = 0.02
        label_names = []
        print('\n')
        print(all_keys)

        # Find best values for every crop:
        for sample in CREMI_crop_slices:
            cumulated_values = {'True': [None, None], 'False': [None, None]}
            counter = 0
            for crop in CREMI_crop_slices[sample]:
                for subcrop in CREMI_sub_crops_slices:

                    if sample != 'B' or crop != CREMI_crop_slices['B'][
                            5] or subcrop != CREMI_sub_crops_slices[5]:
                        continue

                    results_collected_crop = results_collected[sample][crop][
                        subcrop]

                    matplotlib.rcParams.update({'font.size': 10})
                    f, ax = plt.subplots(ncols=ncols,
                                         nrows=nrows,
                                         figsize=(7, 7))

                    for agglo_type in [
                            ty for ty in ['sum', 'max', 'mean']
                            if ty in results_collected_crop
                    ]:
                        for non_link in [
                                ty for ty in ['False', 'True']
                                if ty in results_collected_crop[agglo_type]
                        ]:
                            for local_attraction in [
                                    ty for ty in ['False']
                                    if ty in results_collected_crop[agglo_type]
                                [non_link]
                            ]:

                                # if agglo_type == 'sum':
                                #     continue

                                sub_dict = results_collected_crop[agglo_type][
                                    non_link][local_attraction]
                                probs = []
                                VI_split = []
                                VI_merge = []
                                runtimes = []
                                error_bars_split = []
                                error_bars_merge = []
                                for noise_factor in sub_dict:
                                    multiple_VI_split = []
                                    multiple_VI_merge = []
                                    multiple_runtimes = []
                                    for ID in sub_dict[noise_factor]:
                                        if sub_dict[noise_factor][ID][
                                                "edge_prob"] != 1.:
                                            continue
                                        multiple_VI_split.append(
                                            return_recursive_key_in_dict(
                                                sub_dict[noise_factor][ID],
                                                ['score_WS', "adapted-rand"]))
                                        multiple_VI_merge.append(
                                            return_recursive_key_in_dict(
                                                sub_dict[noise_factor][ID],
                                                ['score_WS', "vi-merge"]))
                                        multiple_runtimes.append(
                                            return_recursive_key_in_dict(
                                                sub_dict[noise_factor][ID],
                                                ['runtime']))
                                    if len(multiple_VI_split) == 0:
                                        continue
                                    probs.append(float(noise_factor))

                                    multiple_VI_split = np.array(
                                        multiple_VI_split)
                                    VI_split.append(multiple_VI_split.mean())
                                    error_bars_split.append(
                                        multiple_VI_split.std())

                                    multiple_VI_merge = np.array(
                                        multiple_VI_merge)
                                    VI_merge.append(multiple_VI_merge.mean())
                                    error_bars_merge.append(
                                        multiple_VI_merge.std())

                                    multiple_runtimes = np.array(
                                        multiple_runtimes)
                                    runtimes.append(multiple_runtimes.mean())

                                    # ax.scatter(multiple_VI_merge, multiple_VI_split, s=np.ones_like(multiple_VI_merge)*noise_factor * 500,
                                    #            c=colors[agglo_type][non_link][local_attraction], marker='o',
                                    #            alpha=0.3)

                                if len(probs) == 0:
                                    continue
                                probs = np.array(probs)

                                error_bars_split = np.array(error_bars_split)
                                VI_split = np.array(VI_split)

                                error_bars_merge = np.array(error_bars_merge)
                                VI_merge = np.array(VI_merge)

                                runtimes = np.array(runtimes)

                                # if (agglo_type=='mean' and non_link == 'True') or (agglo_type=='max' and local_attraction=='True'):
                                #     continue

                                # Compose plot label:
                                plot_label_1 = agglo_type
                                plot_label_2 = " + cannot-link " if eval(
                                    non_link) else " "
                                plot_label_3 = "(local edges attractive)" if eval(
                                    local_attraction) else ""

                                if all_keys[-1] == 'runtime':
                                    error_bars_split = None

                                # if all_keys[-1] == 'energy':
                                #     values = -values

                                # print(runtimes.min(), runtimes.max())
                                # runtimes -= 0.027
                                # runtimes /= 0.2
                                # runtimes = (1 - runtimes) * 500
                                # print(runtimes.min(), runtimes.max())

                                ax.scatter(VI_merge,
                                           VI_split,
                                           s=probs * probs * probs,
                                           c=colors[agglo_type][non_link]
                                           [local_attraction],
                                           marker='o',
                                           alpha=0.3,
                                           label=plot_label_1 + plot_label_2 +
                                           plot_label_3)

                                # ax.errorbar(VI_merge, VI_split, xerr=error_bars_merge ,yerr=error_bars_split, fmt='.',
                                #             color=colors[agglo_type][non_link][local_attraction], alpha=0.3)

                    if all_keys[-1] == 'runtime':
                        ax.set_yscale("log", nonposy='clip')
                        # ax.set_yscale("log", nonposy='clip')
                    ax.set_yscale("log", nonposy='clip')
                    import segmfriends.vis as vis_utils
                    # vis_utils.set_log_tics(ax, [-2, 0], [10], format="%.2f", axis='y')
                    # vis_utils.set_log_tics(ax, [-2, 0], [10], format="%.2f", axis='x')

                    # ax.set_xticks(np.arange(0, 1, step=0.1))
                    ax.legend(loc='upper right')
                    ax.set_xlabel("Variation of information - merge")
                    # ax.set_ylabel("Variation of information - split")
                    ax.set_ylabel("Adapted RAND")
                    # ax.set_ylim([0.027, 0.052])
                    # ax.set_xlim([0.15, 0.35])
                    # ax.set_title("Variation of Information on CREMI sample {}".format(sample))

                    plot_dir = os.path.join(project_directory, exp_name,
                                            "plots")
                    check_dir_and_create(plot_dir)
                    f.savefig(os.path.join(
                        plot_dir,
                        'noise_plot_{}_deep_z_scatter_plots.pdf'.format(
                            sample)),
                              format='pdf')
def run_agglo(problem_name=None, update_rule='mean', CLC=False):
    saving_path = os.path.join(get_trendytukan_drive_path(), "projects/agglo_cluster_compare/bigFruitFlyGraphs/scores")
    check_dir_and_create(saving_path)
    saving_path_2 = os.path.join(saving_path,
                                 problem_name.replace(".txt", ''))

    check_dir_and_create(saving_path_2)
    result_file_path = os.path.join(saving_path_2,
                                    "{}_{}.json".format(update_rule, CLC))
    if os.path.exists(result_file_path):
        print(result_file_path)
        print("Skip agglo ", update_rule, CLC, problem_name)
        return

    uvIds = all_data[problem_name][:,:2].astype('uint64')
    edge_weights = all_data[problem_name][:, 2].astype('float64')


    print("Building graph")
    graph = UndirectedGraph(uvIds.max())
    graph.insertEdges(uvIds)

    # Run agglo:
    tick = time.time()
    print("Start agglo ", update_rule, CLC, problem_name)
    try:
        nodeSeg, _ = runGreedyGraphEdgeContraction(graph, edge_weights, update_rule=update_rule, add_cannot_link_constraints=CLC)
    except RuntimeError:
        print("Nifty Exception on {} {} {}!".format(problem_name, update_rule, CLC))
    tock = time.time()

    edge_labels = graph.nodesLabelsToEdgeLabels(nodeSeg)
    MC_energy = (edge_weights * edge_labels).sum()

    print(tock-tick)
    print(MC_energy)

    new_results = {}
    new_results[update_rule] = {}
    new_results[update_rule][CLC] = {}

    new_results[update_rule][CLC]['MC_energy'] = MC_energy
    new_results[update_rule][CLC]['runtime'] = tock - tick


    with global_lock:
        # if os.path.exists(result_file_path):
        #     try:
        #         with open(result_file_path, 'r') as f:
        #             result_dict = json.load(f)
        #     except Exception:
        #         result_dict = {}
        #         print("Exception raised on {}, {}!!".format(problem_name, update_rule) )
        # else:
        #     result_dict = {}
        #
        # result_dict = recursive_dict_update(new_results, result_dict)

        with open(result_file_path, 'w') as f:
            try:
                json.dump(new_results, f, indent=4, sort_keys=True)
            except Exception:
                print("Exception again!")
        print("saved")
Пример #8
0
#     for c in range(nb_col):
#         collected_array[r, c] = collected_results[r][c]

# collected_results = np.array([np.array(item, dtype="str") for item in collected_results], dtype="str")

if len(collected_results) == 0:
    raise ValueError("No scores collected")
assert all(len(row) == len(collected_results[0]) for row in collected_results)
# if any(len(row) != len(collected_results[0]) for row in collected_results):
#     # Collapse first columns?
#     for i, row in enumerate(collected_results):
#         collected_results[i] = ['__'.join(row[:nb_first_columns])] + row[nb_first_columns:]
#     nb_first_columns = 1

collected_results = np.array(collected_results, dtype="str")
collected_results = collected_results[collected_results[:, sorting_column_idx + max_nb_columns].argsort()]
ID = np.random.randint(255000)
print(ID)
from segmfriends.utils.various import check_dir_and_create
export_dir = os.path.join(project_dir, "collected_scores")
check_dir_and_create(export_dir)
# print(collected_results)
if LATEX_OUTPUT:
    np.savetxt(os.path.join(export_dir, "collected_cremi_{}{}.csv".format(ID, POSTFIX_FILE)), collected_results, delimiter=' & ',
           fmt='%s',
           newline=' \\\\\n')
else:
    np.savetxt(os.path.join(export_dir, "collected_cremi_{}{}.csv".format(ID, POSTFIX_FILE)), collected_results, delimiter=';',
               fmt='%s',
               newline=' \n')