def generate_testdata(self): run_names = ["run_1", "run_2"] ground_truth = { "run_1": [ ["Description", "A", "B"], ["name_1", 1.0, -1.0], ["name_2", -0.5, 0.5], ], "run_2": [ ["Description", "A", "B"], ["name_1", 1.0, -1.0], ["name_2", -0.5, np.nan], ], } embedding_ground_truth = { "run_1": [[1.0, 0.5], [-0.5, 0.5]], "run_2": [[1.0, 0.5], [-0.5, 0.5]], } for run_name in run_names: subdir = os.path.join(self.logdir, run_name) writer = tf.compat.v2.summary.create_file_writer(subdir) data = ground_truth[run_name] python_result = [] python_annotations = [] python_classes = [] for row_index, row in enumerate(data): if row_index > 0: python_result.append([]) for col_index, column in enumerate(row): if row_index == 0: if col_index > 0: python_classes.append(column) else: if col_index == 0: python_annotations.append(column) else: python_result[len(python_result) - 1].append(column) with writer.as_default(): tensor_result = tf.convert_to_tensor(python_result) tensor_annotations = tf.convert_to_tensor(python_annotations) tensor_classes = tf.convert_to_tensor(python_classes) tensor_embeddings = tf.convert_to_tensor( embedding_ground_truth[run_name]) summary.npmi_values(tensor_result, 1) summary.npmi_annotations(tensor_annotations, 1) summary.npmi_metrics(tensor_classes, 1) summary.npmi_embeddings(tensor_embeddings, 1) writer.close()
def setup_run(logdir, run_name): # Getting a list of dummy annotations from a separate file. python_annotations = npmi_demo_data.MOUNTAINS # Writing out random nPMI values. python_classes = ["nPMI@SKIABLE", "nPMI@NOT_SKIABLE"] python_result = np.random.rand(len(python_annotations), len(python_classes)) python_result = python_result * 2.0 - 1.0 logdir = os.path.join(logdir, run_name) os.makedirs(logdir) writer = tf.summary.create_file_writer(logdir) with writer.as_default(): tensor_result = tf.convert_to_tensor(python_result) tensor_annotations = tf.convert_to_tensor(python_annotations) tensor_metrics = tf.convert_to_tensor(python_classes) summary.npmi_values(tensor_result, 1) summary.npmi_annotations(tensor_annotations, 1) summary.npmi_metrics(tensor_metrics, 1) writer.close()
def convert_file(file_path): """Converts a csv file to a logfile readable by the nPMI plugin. Args: file_path: the path to the csv file to be converted """ if not os.path.exists(file_path): print("No such file found. Conversion failed.") return metrics = [] annotations = [] values = [] with open(file_path) as csv_file: csv_reader = csv.reader(csv_file) metrics = next(csv_reader)[1:] if not contains_npmi_metric(metrics): print( "No metric is prefixed with nPMI@ or nPMI_diff@. No export generated." ) return for row in csv_reader: annotations.append(row[0]) values.append(row[1:]) values = np.array(values).astype(np.float) writer = tf.summary.create_file_writer(os.path.dirname(file_path)) with writer.as_default(): tensor_result = tf.convert_to_tensor(values) tensor_annotations = tf.convert_to_tensor(annotations) tensor_metrics = tf.convert_to_tensor(metrics) summary.npmi_values(tensor_result, 1) summary.npmi_annotations(tensor_annotations, 1) summary.npmi_metrics(tensor_metrics, 1) writer.close() print("Successfuly saved converted output to %s" % os.path.dirname(file_path))
def write_results(self, tensor): writer = tf2.summary.create_file_writer(self.get_temp_dir()) with writer.as_default(): summary.npmi_annotations(tensor, 1) writer.close()