def get_merged_restrictions_and_priorities(X, y, finder, prefix, folder="data/", save=True): folder = mkdir(folder) entry_size = len(X[0]) restrictions = {} priorities = [] restrictions_filename = "{}{}_{}_merged_restrictions.pkl".format( folder, prefix, finder) priorities_filename = "{}{}_{}_merged_priorities.pkl".format( folder, prefix, finder) if file_exists(restrictions_filename) and file_exists(priorities_filename): print( "Loading: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) restrictions = load_data(restrictions_filename) priorities = load_data(priorities_filename) print("Done!") else: print("Creating restrictions...", end=" ") all_restrictions, _ = get_restrictions_and_priorities( X, y, finder, prefix) # images = get_images_by_label(X, y) labels = list(set(y)) merged = {} for label in labels: for key, value in all_restrictions[label].items(): if key not in merged: merged[key] = 0 merged[key] += value for key in merged.keys(): new_value = merged[key] / len(labels) if new_value > 0: restrictions[key] = new_value priorities = calculate_choice_priorities(restrictions, entry_size) print("Done!") if save: print( "Saving: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) save_data(restrictions, restrictions_filename) save_data(priorities, priorities_filename) print("Done!") return restrictions, priorities
def get_intersection_restrictions_and_priorities(X, y, finder, prefix, folder="data/", save=True): folder = mkdir(folder) entry_size = len(X[0]) restrictions = {} priorities = [] restrictions_filename = "{}{}_{}_intersection_restrictions.pkl".format( folder, prefix, finder) priorities_filename = "{}{}_{}_intersection_priorities.pkl".format( folder, prefix, finder) if file_exists(restrictions_filename) and file_exists(priorities_filename): print( "Loading: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) restrictions = load_data(restrictions_filename) priorities = load_data(priorities_filename) print("Done!") else: print("Creating restrictions...", end=" ") all_restrictions, _ = get_restrictions_and_priorities( X, y, finder, prefix) # images = get_images_by_label(X, y) labels = list(set(y)) for key, value in all_restrictions[labels[0]].items(): total = 0 for label, inner_restrictions in all_restrictions.items(): if key not in inner_restrictions: total = 0 break total += inner_restrictions[key] if total > 0: restrictions[key] = total / len(labels) priorities = calculate_choice_priorities(restrictions, entry_size) print("Done!") if save: print( "Saving: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) save_data(restrictions, restrictions_filename) save_data(priorities, priorities_filename) print("Done!") return restrictions, priorities
def get_exlusive_restrictions_and_priorities(X, y, finder, prefix, folder="data/", save=True): folder = mkdir(folder) entry_size = len(X[0]) restrictions = {} priorities = [] restrictions_filename = "{}{}_{}_exclusive_restrictions.pkl".format( folder, prefix, finder) priorities_filename = "{}{}_{}_exclusive_priorities.pkl".format( folder, prefix, finder) if file_exists(restrictions_filename) and file_exists(priorities_filename): print( "Loading: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) restrictions = load_data(restrictions_filename) priorities = load_data(priorities_filename) print("Done!") else: print("Creating restrictions...", end=" ") all_restrictions, _ = get_restrictions_and_priorities( X, y, finder, prefix) # images = get_images_by_label(X, y) labels = list(set(y)) for key, value in all_restrictions[labels[0]].items(): equal = True for label, inner_restrictions in all_restrictions.items(): if inner_restrictions.get(key, -1) != value: equal = False break if equal: restrictions[key] = value priorities = calculate_choice_priorities(restrictions, entry_size) print("Done!") if save: print( "Saving: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) save_data(restrictions, restrictions_filename) save_data(priorities, priorities_filename) print("Done!") return restrictions, priorities
def barplot_scores(desc, dataset_name, binarization_name, experiments, results_folder="results"): output_folder = mkdir("images/{}/".format(dataset_name)) df = concat_experiments_scores(dataset_name, binarization_name, experiments, results_folder) plot = sns.barplot(x="n", y="accuracy", hue="experiment", data=df) fig = plot.get_figure() fig.savefig("{}{}_{}.png".format(output_folder, binarization_name, desc)) fig.clf()
def get_restrictions_and_priorities(X, y, finder, prefix, folder="data/", save=True): folder = mkdir(folder) restrictions = {} priorities = {} restrictions_filename = "{}{}_{}_restrictions.pkl".format( folder, prefix, finder) priorities_filename = "{}{}_{}_priorities.pkl".format( folder, prefix, finder) if file_exists(restrictions_filename) and file_exists(priorities_filename): print( "Loading: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) restrictions = load_data(restrictions_filename) priorities = load_data(priorities_filename) print("Done!") else: print("Creating restrictions...", end=" ") images = get_images_by_label(X, y) for label, data in images.items(): restrictions[label] = calculate_restrictions(data, finder) # priorities[label] = calculate_priority_of_choice( # restrictions[label], len(data[0]) # ) priorities[label] = calculate_choice_priorities( restrictions[label], len(data[0])) print("Done!") if save: print( "Saving: {} and {} files...".format(restrictions_filename, priorities_filename), end=" ", ) save_data(restrictions, restrictions_filename) save_data(priorities, priorities_filename) print("Done!") return restrictions, priorities
def __init__( self, experiment_name, header_map, save=True, folder="results/", num_exec=20, miliseconds=True, ): self.experiment_name = experiment_name self.header_map = header_map self.save = save self.folder = mkdir(folder) self.num_exec = num_exec self.factor = 1 if miliseconds: self.factor = 1000 super().__init__()
def plot_metric_images(dataset_name, binarization_name, entropy=False, output_folder="images"): output_folder = mkdir("{}/{}/metric/".format(output_folder, dataset_name)) ds = get_dataset(dataset_name, binarization_name) prefix = "mental" if entropy: prefix = "entropy" mi = get_entropy_images(ds.X_train, ds.y_train) else: mi = get_mental_images(ds.X_train, ds.y_train) for key, value in mi.items(): io.imsave( "{}{}_{}_{}.png".format(output_folder, prefix, binarization_name, key), np.array(value * 255, dtype=np.uint8).reshape(ds.shape), )
def get_folder(self, dataset_name): folder_name = "{}/{}/".format(self.folder, dataset_name) return mkdir(folder_name)