-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyxport.py
executable file
·178 lines (142 loc) · 5.22 KB
/
pyxport.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PyXport module
verion: 1.0
author: Etienne MONIER (https://github.com/etienne-monier)
"""
import numpy as np
import matplotlib.pyplot as plt
import imageio
import os
import os.path
def _to_im_scale(im):
"""to_im_scale function.
This function returns
"""
return np.uint8(np.floor(255*im))
def plot2im(mat, loc, reference=None, cmap='viridis'):
"""plot2im function.
This function saves the plotted image when using matshow with mat.
The colormap is cmap.
The user can define a reference matrix. Its maximum and minimum
value will be chosen as the the colormap limits. Above (resp. below)
values will be thresholded to the colormap upper (resp. lower)
limit.
Arguments
---------
mat: numpy array
The data matrix.
loc: str
The saving location.
reference: numpy array
A reference matrix.
cmap: optional, str
The colormap. Default is viridis.
"""
# Check that matrix is 2D
if mat.ndim != 2:
raise ValueError('The mat parameer should be a 2D Numpy array.'
'The given array has shape {}.'.format(mat.shape))
# Check location. If the directory does not exist, create recursively.
if (os.path.dirname(loc) != "" and not os.path.isdir(
os.path.dirname(loc))):
os.makedirs(os.path.dirname(loc))
# Catch reference matrix if given.
if np.any(reference is None):
reference = mat
# Create the colormaped image matrix.
cmap = plt.cm.get_cmap(cmap)
norm = plt.Normalize(vmin=reference.min(), vmax=reference.max())
image = cmap(norm(mat))
# Save the image.
imageio.imwrite(loc, _to_im_scale(image))
def save_dat(data, loc, sep=' '):
"""Save data to .dat file for LaTeX pgfplotstable package usage.
This function accepts three types of data.
1. A Numpy array was given. Then, the .dat file will only contain
its values separated by newline.
2. A list/typle of one or two Numpy arrays was given. Then, the .dat
file will have the values of the different arrays separated by the
optional separator sep.
3. A dictionary of Numpy arrays was given. Then, the keys will be
given in the first line of the .dat file.
Note
----
In the case of multiple arrays input, the arrays should have the
save size. Otherwise, a ValueError will be raised.
Arguments
---------
data: 1D array, tuple or list of 1D arrays, dictionary of 1D arrays)
Arrays to save.
loc: str
Place to save the data.
sep: str
Data separator. Default is one space.
"""
# Search data type and prepare data.
dico_flag = False
# Is it a single array ?
if type(data).__module__ == np.__name__:
if data.ndim > 1:
raise ValueError(
'The data was a numpy array of dimension {} where the only'
'dimension allowed is 1. Give a tuple, list or dictionary'
'instead.'.format(data.ndim))
data_out = data[np.newaxis, :]
# Is it a list or tuple ?
elif type(data) is tuple or type(data) is list:
if len(set([type(a).__module__ == np.__name__ for a in data])) != 1:
raise ValueError(
'Some elements of the data list were not numpy data.')
if len(set([a.shape for a in data])) != 1:
raise ValueError(
'Elements of the data list have inconsistent shapes.')
# if len(data)>2:
# raise ValueError('A list/tuple of length superior to 2 was
# given.
# This is not accepted by pgfplotstable. Use a dictionary
# instead to add keywords.')
data_out = np.asarray(data)
# Is it a dico ?
elif type(data) is dict:
if len(set([type(a).__module__ == np.__name__ for a in list(
data.values())])) != 1:
raise ValueError(
'Some elements of the data list were not numpy data.')
if len(set([a.shape for a in list(data.values())])) != 1:
raise ValueError(
'Elements of the data list have inconsistent shapes.')
dico_flag = True
data_out = np.stack(tuple(data.values()))
# Not known data
else:
raise ValueError(
'The data is not a numpy array, nor a tuple, nor a list, nor'
'a dictionary. Instead, its type is {}'.format(type(data)))
# Create dir if absent
if (os.path.dirname(loc) != '' and not os.path.isdir(
os.path.dirname(loc))):
os.makedirs(os.path.dirname(loc))
# Catch data shape
N, L = data_out.shape
# Open file
file = open(loc, 'w')
# If a dictionary was given, write keys
if dico_flag:
for ind, key in enumerate(data.keys()):
if ind == 0:
file.write('{}'.format(key))
else:
file.write('{}{}'.format(sep, key))
file.write('\n')
# Then, the data is written
for l in range(L):
for n in range(N):
if n == 0:
file.write('{}'.format(data_out[n, l]))
else:
file.write('{}{}'.format(sep, data_out[n, l]))
file.write('\n')
# Close file
file.close()