This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
forked from ilayluz/learning-amg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
49 lines (37 loc) · 1.53 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import json
import shutil
import os
import glob
from functools import lru_cache
import numpy as np
from collections import Counter
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def most_frequent_splitting(splittings):
"""Given a list of numpy array, returns the most frequent one"""
list_of_tuples = [tuple(splitting) for splitting in splittings] # we need a list of immutable types
counter = Counter(list_of_tuples)
most_frequent_tuple = counter.most_common(1)[0][0]
return np.array(most_frequent_tuple)
def create_results_dir(run_name):
results_dir = 'results/' + run_name
os.makedirs(results_dir)
# make a copy of all Python files, for reproducibility
local_dir = os.path.dirname(__file__)
for py_file in glob.glob(local_dir + '/*.py'):
shutil.copy(py_file, results_dir)
def write_config_file(run_name, config, seed):
results_dir = 'results/' + run_name
config_dict = {'train_config': config.train_config.__dict__,
'data_config': config.data_config.__dict__,
'model_config': config.model_config.__dict__,
'run_config': config.run_config.__dict__,
'seed': seed}
with open(f'{results_dir}/config.json', 'w') as outfile:
json.dump(config_dict, outfile)
@lru_cache(maxsize=None)
def tril_indices(grid_size):
"""Cached version of np.tril_indices used for creating relaxation matrices"""
return np.tril_indices(grid_size)