use of com.jmatio.types.MLArray in project tika by apache.
the class MatParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
//Set MIME type as Matlab
metadata.set(Metadata.CONTENT_TYPE, MATLAB_MIME_TYPE);
TemporaryResources tmp = TikaInputStream.isTikaInputStream(stream) ? null : new TemporaryResources();
try {
// Use TIS so we can spool a temp file for parsing.
TikaInputStream tis = TikaInputStream.get(stream, tmp);
//Extract information from header file
//input .mat file
MatFileReader mfr = new MatFileReader(tis.getFile());
//.mat header information
MatFileHeader hdr = mfr.getMatFileHeader();
// Example header: "MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Sun Mar 2 23:41:57 2014"
// Break header information into its parts
String[] parts = hdr.getDescription().split(",");
if (parts[2].contains("Created")) {
int lastIndex1 = parts[2].lastIndexOf("Created on:");
String dateCreated = parts[2].substring(lastIndex1 + "Created on:".length()).trim();
metadata.set("createdOn", dateCreated);
}
if (parts[1].contains("Platform")) {
int lastIndex2 = parts[1].lastIndexOf("Platform:");
String platform = parts[1].substring(lastIndex2 + "Platform:".length()).trim();
metadata.set("platform", platform);
}
if (parts[0].contains("MATLAB")) {
metadata.set("fileType", parts[0]);
}
// Get endian indicator from header file
// Retrieve endian bytes and convert to string
String endianBytes = new String(hdr.getEndianIndicator(), UTF_8);
// Convert bytes to characters to string
String endianCode = String.valueOf(endianBytes.toCharArray());
metadata.set("endian", endianCode);
//Text output
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
xhtml.newline();
//Loop through each variable
for (Map.Entry<String, MLArray> entry : mfr.getContent().entrySet()) {
String varName = entry.getKey();
MLArray varData = entry.getValue();
xhtml.element("p", varName + ":" + String.valueOf(varData));
// If the variable is a structure, extract variable info from structure
if (varData.isStruct()) {
MLStructure mlStructure = (MLStructure) mfr.getMLArray(varName);
xhtml.startElement("ul");
xhtml.newline();
for (MLArray element : mlStructure.getAllFields()) {
xhtml.startElement("li");
xhtml.characters(String.valueOf(element));
// If there is an embedded structure, extract variable info.
if (element.isStruct()) {
xhtml.startElement("ul");
// Should this actually be a recursive call?
xhtml.element("li", element.contentToString());
xhtml.endElement("ul");
}
xhtml.endElement("li");
}
xhtml.endElement("ul");
}
}
xhtml.endDocument();
} catch (IOException e) {
throw new TikaException("Error parsing Matlab file with MatParser", e);
} finally {
if (tmp != null) {
tmp.dispose();
}
}
}
use of com.jmatio.types.MLArray in project vcell by virtualcell.
the class FrapDataUtils method saveImageDatasetAsExternalMatlabData.
// export the frap data to matlab file. The matlab file contains timestamps(1*Tn) , mask(numImgX * numImgY),
// ImageDataSet(1*Tn) each cell of (1*Tn) point to a 2d image(numImgX * numImgY)
public static void saveImageDatasetAsExternalMatlabData(FRAPData frapData, LocalWorkspace localWorkspace, String matlabFileName, int startingIndexForRecovery, CartesianMesh cartesianMesh) throws IOException {
ImageDataset imageDataset = frapData.getImageDataset();
if (imageDataset.getSizeC() > 1) {
throw new RuntimeException("FRAPData.saveImageDatasetAsExternalMatlabData(): multiple channels not yet supported");
}
int numX = cartesianMesh.getSizeX();
int numY = cartesianMesh.getSizeY();
// prepare variable to write into matlab file, listOfVars is the outmost structure to write to Matlab file.
ArrayList<MLArray> listOfVars = new ArrayList<MLArray>();
double[] timeArray = imageDataset.getImageTimeStamps();
ROI cellROI = frapData.getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name());
short[] shortCellMask = cellROI.getPixelsXYZ();
// add image data set to Matlab cell, each cell points to a numX*numY array
MLCell imageCell = new MLCell("ImageDataSet", new int[] { timeArray.length, 1 });
for (int tIndex = 0; tIndex < imageDataset.getSizeT(); tIndex++) {
// images according to zIndex at specific time points(tIndex)
short[] originalData = imageDataset.getPixelsZ(0, tIndex);
double[] doubleImgData = new double[originalData.length];
for (int i = 0; i < originalData.length; i++) {
doubleImgData[i] = 0x0000ffff & originalData[i];
}
MLDouble mlDoublImgData = new MLDouble("ImageDataAtTime_" + tIndex, doubleImgData, numX);
imageCell.set(mlDoublImgData, tIndex, 0);
}
listOfVars.add(imageCell);
// create mask in a Matlab 2D double(numX*numY)
double[] doubleCellMask = new double[shortCellMask.length];
for (int i = 0; i < shortCellMask.length; i++) {
doubleCellMask[i] = 0x0000ffff & shortCellMask[i];
}
MLDouble cellMask = new MLDouble("CellMask", doubleCellMask, numX);
listOfVars.add(cellMask);
// create times in a Matlab 2D double(1*numTimePoints)
MLDouble times = new MLDouble("ExperimentalTimeStamps", new double[][] { timeArray });
listOfVars.add(times);
MatFileWriter writer = new MatFileWriter();
writer.write(matlabFileName, listOfVars);
}
Aggregations