# -*- coding: utf-8 -*-
import numpy as np
import os
import pandas as pd
[docs]def export_data(data, export_type='vtk', fileName='mesostructure.vti'):
'''
The function exports data in the 3D array to the given export type (ex. vtk, npy, npz, csv, txt etc.).
Parameters
----------
data: 3D array of size NXNXN, type int
Micro/Mesostructure/Inclusion in voxel format.
export_type: str.
Export type (vtk/csv/txt/npy/npz)
fileName: str.
File location and file name with proper extension (./.../fileName.csv for export_type='csv')
'''
if not isinstance(data, np.ndarray):
raise Exception('given data must be ndarray type')
if export_type == 'vtk':
ext = os.path.splitext(fileName)
if ext[1] != '.vti':
raise Exception('File name extension should be .vti for vtk export type')
write_vti_format(fileName, data)
elif export_type == 'csv':
ext = os.path.splitext(fileName)
if ext[1] != '.csv':
raise Exception('File name extension should be .csv for csv export type')
data_shape = np.shape(data)
[x, y, z] = np.meshgrid(np.arange(0, data_shape[0]), np.arange(0, data_shape[1]), np.arange(0, data_shape[2]))
data_array = np.array([x.ravel(), y.ravel(), z.ravel(), data.ravel()])
data_array = np.transpose(data_array)
data_frame = pd.DataFrame(data_array, columns=['x', 'y', 'z', 'values'])
data_frame.to_csv(fileName, index=False)
elif export_type == 'txt':
ext = os.path.splitext(fileName)
if ext[1] != '.txt':
raise Exception('File name extension should be .txt for txt export type')
data_shape = np.shape(data)
[x, y, z] = np.meshgrid(np.arange(0, data_shape[0]), np.arange(0, data_shape[1]), np.arange(0, data_shape[2]))
data_array = np.array([x.ravel(), y.ravel(), z.ravel(), data.ravel()])
data_array = np.transpose(data_array)
data_frame = pd.DataFrame(data_array, columns=['x', 'y', 'z', 'values'])
data_frame.to_csv(fileName, index=False)
elif export_type == 'npy':
ext = os.path.splitext(fileName)
if ext[1] != '.npy':
raise Exception('File name extension should be .npy for npy export type')
np.save(fileName, data)
elif export_type == 'npz':
ext = os.path.splitext(fileName)
if ext[1] != '.npz':
raise Exception('File name extension should be .npz for npz export type')
np.savez(fileName, data)
[docs]def vti_tail(f):
f.writelines(' ' * 2 + '</Piece>\n')
f.writelines(' ' * 1 + '</ImageData>\n')
f.writelines('</VTKFile>')
[docs]def visualize_sections(matrix, slices=2):
'''
Visualize 2D sections of 3D matrix. Given number of sections (slices) are generated in each direction (xy,xz,yz)
'''
import matplotlib.pyplot as plt
step = np.array(np.shape(matrix)).astype(float) / (float(slices + 1))
for i in range(slices):
plt.imshow(matrix[int(step[0] * (i + 1)), :, :])
plt.title('yz section')
plt.show()
for i in range(slices):
plt.imshow(matrix[:, int(step[1] * (i + 1)), :])
plt.title('xz section')
plt.show()
for i in range(slices):
plt.imshow(matrix[:, :, int(step[2] * (i + 1))])
plt.title('xy section')
plt.show()
plt.show()