aboutsummaryrefslogtreecommitdiff
path: root/src/conversion/czi
diff options
context:
space:
mode:
authorcjd242018-04-23 08:43:10 +0100
committercjd242018-04-23 08:43:10 +0100
commitf1e8003fd621a40e2386c310a2189bb9a13b0c9d (patch)
treea0323aa44d4e9ee87bc9b5fb98e769c4af79265d /src/conversion/czi
parentAdd tabular functionality (except cols) (diff)
downloadmedicine-f1e8003fd621a40e2386c310a2189bb9a13b0c9d.tar.gz
medicine-f1e8003fd621a40e2386c310a2189bb9a13b0c9d.zip

BIG FILTHY COMMIT OF SCARY STUFF

Diffstat (limited to 'src/conversion/czi')
-rw-r--r--src/conversion/czi/czi.ts26
-rw-r--r--src/conversion/czi/pyramidWorker.ts19
2 files changed, 38 insertions, 7 deletions
diff --git a/src/conversion/czi/czi.ts b/src/conversion/czi/czi.ts
index 429f60d..33bc738 100644
--- a/src/conversion/czi/czi.ts
+++ b/src/conversion/czi/czi.ts
@@ -16,7 +16,6 @@ const readdir = require('util').promisify(fs.readdir);
const exists = require('util').promisify(fs.exists);
// Various constants for placing files and defining tiles
-const testFile: string = '/cs/scratch/cjd24/0701.czi'
let extractDirectory: string = "";
let outputImageData: string = "";
let outputImageDirectory: string = "";
@@ -378,6 +377,9 @@ const cleanSupportedViewObject: (obj: parsexml.Node[]) => object = (
const cleanSupportedViews: (view: SupportedViews) => void = (
view: SupportedViews
): void => {
+ if (!view.scalable_image) {
+ throw new Error("SupportedView.scalable_image does not exist where it should.");
+ }
for (const channel of view.scalable_image.channels) {
channel.metadata = cleanSupportedViewObject(
channel.metadata as parsexml.Node[]
@@ -398,6 +400,9 @@ const supportedViews: SupportedViews = {
metadata: {}
}
};
+if (!supportedViews.scalable_image) {
+ throw new Error("SupportedView.scalable_image does not exist where it should.");
+}
supportedViews.scalable_image.channels.pop();
/* tslint:disable:curly cyclomatic-complexity */
@@ -412,6 +417,9 @@ const parseExtractedXML: (xmlFile: string) => void = async(
// Parse the XML :(
// THE FOLLOWING 2 FOR LOOPS ARE DISGUSTING AND I HATE IT,
// AND I WISH I DIDNT WRITE IT BUT DON'T SEE ANY OTHER WAY TO DO IT LOL :'(
+ if (!supportedViews.scalable_image) {
+ throw new Error("SupportedView.scalable_image does not exist where it should.");
+ }
for (const child of metaXML.root.children) if (child.name === 'Metadata') {
for (const entry of child.children) if (entry.name === 'Information') {
for (const properties of entry.children) if (properties.name === 'Image') {
@@ -738,6 +746,9 @@ const createSupportedViewsObject: () => void = async(): Promise<void> => {
// Cleanup and create "smart" supported views object without whole metadata
cleanSupportedViews(supportedViews);
+ if (!supportedViews.scalable_image) {
+ throw new Error("SupportedView.scalable_image does not exist where it should.");
+ }
supportedViews.scalable_image.metadata = {
tile_size: tileSize,
max_zoom: maxZoom,
@@ -756,6 +767,9 @@ const buildCustomPyramids: () => Promise<boolean> = async(): Promise<boolean> =>
finalCZIJson.zoom_level_count = Math.log2(maxZoom) + 1;
// For all channels in the base image
+ if (!supportedViews.scalable_image) {
+ throw new Error("SupportedView.scalable_image does not exist where it should.");
+ }
for (const cval of supportedViews.scalable_image.channels) {
errorOccurred = false;
@@ -789,9 +803,9 @@ const buildCustomPyramids: () => Promise<boolean> = async(): Promise<boolean> =>
return errorOccurred;
};
-const initialExtractAndConvert: (absFilePath: string) => Promise<void> = async (absFilePath: string): Promise<void> => {
+const initialExtractAndConvert: (absFilePath: string, space: string) => Promise<void> = async (absFilePath: string, space: string): Promise<void> => {
- outputImageData = `${absFilePath.split('.')[0]}-processed`;
+ outputImageData = space;
extractDirectory = outputImageData + "/extract/";
outputImageDirectory = outputImageData + "/data/";
@@ -841,12 +855,12 @@ process.on("warning", (err: any) => console.warn(err))
* Main function used to call the rest of the relevant code to crunch a CZI
* extraction.
*/
-const main: () => Promise<void> = async (): Promise<void> => {
+export const main: (original: string, space:string) => Promise<void> = async (original: string, space:string): Promise<void> => {
console.log('\n');
try {
console.log('>> Extracting from CZI and Converting to PNG');
- await initialExtractAndConvert(testFile);
+ await initialExtractAndConvert(original, space);
console.log('\n>> Checking/Creating output directories...');
checkForOutputDirectories([outputImageDirectory, `${outputImageData}/tmp/`]);
@@ -866,7 +880,7 @@ const main: () => Promise<void> = async (): Promise<void> => {
console.error(Err);
}
};
-main();
+// main();
// /* tslint:disable */
// async function main2() {
diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts
index 5e6911c..6b12409 100644
--- a/src/conversion/czi/pyramidWorker.ts
+++ b/src/conversion/czi/pyramidWorker.ts
@@ -1,8 +1,11 @@
-import { CZIHeightMap, CZITile, CZITileRequest } from '../types/customPyramidIndex';
+import { WholeCZIHierarchy, CZIHeightMap, CZITile, CZITileRequest } from '../types/customPyramidIndex';
import { TileBounds, execpaths } from '../types/helpers';
import * as sharp from 'sharp';
import { isTileRelated } from './tileExtraction';
import { uuid } from '../../uuid'
+import * as fs from 'fs';
+import { RequestError } from '../../errors'
+const readFile = require('util').promisify(fs.readFile);
const findRelatedTiles: Function = function(plane: CZIHeightMap, desired: TileBounds): CZITile[][] {
@@ -32,6 +35,20 @@ const findRelatedTiles: Function = function(plane: CZIHeightMap, desired: TileBo
return relatedTiles;
};
+export const getTile: Function = async function(imageDir: string, cVal: string, x:number = 0, y:number = 0, w:number = 1024, h:number = 1024, zoom: number = 1): Promise<Buffer> {
+ let layout: WholeCZIHierarchy = JSON.parse(await readFile(imageDir + "/layout.json", 'utf8'));
+
+ let bound: TileBounds = new TileBounds(x, x + w, y, y + h);
+ let map: CZIHeightMap;
+ try {
+ map = layout.c_values[Number(cVal)].height_map[Math.floor(Math.log2(zoom))]
+ } catch {
+ throw new RequestError(400, "bad_zoom_index")
+ }
+
+ return readFile((await getFinalTile(imageDir, map, cVal, bound)).file_path);
+}
+
const getFinalTile: Function = async function(imageDir: string, imageTier: CZIHeightMap, cVal: string, desiredRegion: TileBounds): Promise<CZITileRequest> {
console.error("Has the calling function checked that the image has completed processing in WholeCZIHierarchy??");