-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathio.py
More file actions
125 lines (92 loc) · 3.17 KB
/
Copy pathio.py
File metadata and controls
125 lines (92 loc) · 3.17 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import csv
import json
import yaml
import pickle
import importlib
import numpy as np
from compiam import utils
def write_csv(data, out_path, header=None):
"""Writing multi-dimensional data into a file (.csv)
:param data: the data to write
:param output_path: the path where the data is going to be stored
:returns: None
"""
data = np.array(data)
with open(out_path, "w") as f:
writer = csv.writer(f, delimiter=",")
if header:
if len(header) != len(data[0, :]):
raise ValueError("Header and row length mismatch")
writer.writerow(header)
writer.writerows(data)
def read_csv(file_path):
"""Reading a csv file (.csv)
:param file_path: path to the csv
:returns: numpy array containing the data from the read CSV
"""
output = np.genfromtxt(file_path, delimiter=",")
return output[~np.isnan(output)]
def save_object(obj, file_path):
"""Saves object to pickle file
:param obj: an object to save to pickle file
:param file_path: path to save the object
"""
with open(file_path, "wb") as outp: # Overwrites any existing file.
pickle.dump(obj, outp, pickle.HIGHEST_PROTOCOL)
def write_json(j, path):
"""Write json, <j>, to <path>
:param j: json
:type path: json
:param path: path to write to,
if the directory doesn't exist, one will be created
:type path: str
"""
utils.create_if_not_exists(path)
# Opening JSON file
with open(path, "w") as f:
json.dump(j, f)
def write_json(sections, output_path):
"""Writing json-based data into a file (.json)
:param data: the data to write
:param output_path: the path where the data is going to be stored
:returns: None
"""
output_path = output_path + ".json"
with open(output_path, "w") as fhandle:
json.dump(sections, fhandle)
fhandle.close()
def write_scalar_txt(data, output_path):
"""Writing scalar data into a file (.txt)
:param data: the data to write
:param output_path: the path where the data is going to be stored
:returns: None
"""
output_path = output_path + ".txt"
with open(output_path, "w") as f:
f.write("{}".format(data))
f.close()
def resolve_dottedname(dotted_name):
"""Resolve a dotted name to an actual object, similar to zope.dottedname.resolve
:param dotted_name: a dotted name
:returns: the object the dotted name refers to
"""
module_name, _, attribute_name = dotted_name.rpartition(".")
if not module_name:
raise ImportError(f"Invalid dotted name: '{dotted_name}'")
module = importlib.import_module(module_name)
return getattr(module, attribute_name)
def load_yaml(path):
"""Load yaml at <path> to dictionary, d
:param path: input file
:returns: loaded yaml information
"""
def constructor_dottedname(loader, node):
value = loader.construct_scalar(node)
return resolve_dottedname(value)
yaml.add_constructor("!dottedname", constructor_dottedname)
if not os.path.isfile(path):
return None
with open(path) as f:
d = yaml.load(f, Loader=yaml.FullLoader)
return d