def run_stats(graph_path: RichPath, output_path: RichPath): number_graphs, number_annotations, number_variables = 0, 0, 0 annotation_table = Counter() data_generator = chain( *(g.read_as_jsonl() for g in graph_path.iterate_filtered_files_in_dir('*.jsonl.gz'))) for data in data_generator: number_graphs += 1 if len(data['supernodes']) > 0 else 0 number_variables += len(data['supernodes']) number_annotations += sum( 1 for supernode in data['supernodes'].values() if supernode['annotation'] not in {None, 'None', 'Nothing', 'Any'}) annotation_table.update((supernode['annotation'] for supernode in data['supernodes'].values() if supernode['annotation'] not in {None, 'None', 'Nothing', 'Any'})) with open(output_path.to_local_path().path, "a") as f: f.write("Statistics for file: " + graph_path.to_local_path().path + "\n") f.write("Number of graphs: %d\n" % (number_graphs)) f.write("Number of variables: %d\n" % (number_variables)) f.write("Number of annotations: %d\n" % (number_annotations)) f.write("Number of different annotations: %d\n" % (len(list(annotation_table)))) f.write("\nFrequency distribution of annotations type:\n\n") for annotation, value in annotation_table.most_common(): f.write("%s\t%d\n" % (annotation, value))
def restore_model(cls, path: RichPath, device=None) -> 'BaseComponent': """Restore model to a given device.""" model_path = path.to_local_path().path with gzip.open(model_path, 'rb') as f: model = torch.load(f, map_location=device) # type: BaseComponent if device is not None: model.to(device) return model