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
|
def build_dataframe(self):
"""exports the object to pandas format"""
import pandas as pd
matrix = self.data
if matrix is None:
return
if isinstance(matrix, coo_matrix):
data = {'row': matrix.row, 'col': matrix.col, 'data' : matrix.data}
data_frame = pd.DataFrame(data=data).reindex(columns=['row', 'col', 'data'])
elif isinstance(matrix, np.ndarray):
data_frame = pd.DataFrame(data=matrix)
else:
raise NotImplementedError(type(matrix))
self.data_frame = data_frame
def write(self, mat, print_full=True):
"""writes to the F06"""
mat.write(np.compat.asbytes(str(self) + '\n'))
matrix = self.data
if self.data is None:
mat.write('skipping %s because data is None\n\n' % self.name)
return
if isinstance(matrix, coo_matrix):
if print_full:
for row, col, value in zip(matrix.row, matrix.col, matrix.data):
mat.write(np.compat.asbytes("(%i, %i) %s\n" % (row, col, value)))
else:
mat.write(str(matrix))
else:
mat.write(np.compat.asbytes('name=%r; shape=%s; form=%i; Type=%r\n' % (
self.name, str(self.data.shape).replace('L', ''),
self.form, self.shape_str)))
if print_full:
np.savetxt(mat, self.data, fmt='%.18e', delimiter=',')
#f06.write(str(matrix))
#print('WARNING: matrix type=%s does not support writing' % type(matrix))
mat.write(np.compat.asbytes('\n\n')) |
Partager