From f1e8003fd621a40e2386c310a2189bb9a13b0c9d Mon Sep 17 00:00:00 2001 From: cjd24 Date: Mon, 23 Apr 2018 08:43:10 +0100 Subject: BIG FILTHY COMMIT OF SCARY STUFF --- src/conversion/csv/excel.ts | 1 + src/conversion/czi/czi.ts | 26 ++++++++++++++++++++------ src/conversion/czi/pyramidWorker.ts | 19 ++++++++++++++++++- 3 files changed, 39 insertions(+), 7 deletions(-) (limited to 'src/conversion') diff --git a/src/conversion/csv/excel.ts b/src/conversion/csv/excel.ts index 73563a6..3f5d30b 100644 --- a/src/conversion/csv/excel.ts +++ b/src/conversion/csv/excel.ts @@ -4,6 +4,7 @@ import * as fs from 'fs'; import { SupportedViews, writeJSONToFile } from '../types/helpers'; import * as csv from 'csv-parse'; import { extension } from '../../mimetype' + const writeFile = require('util').promisify(fs.writeFile); const readFile = require('util').promisify(fs.readFile); const createFolder = require('util').promisify(fs.mkdir); 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 => { // 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 = async(): Promise => 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 = async(): Promise => return errorOccurred; }; -const initialExtractAndConvert: (absFilePath: string) => Promise = async (absFilePath: string): Promise => { +const initialExtractAndConvert: (absFilePath: string, space: string) => Promise = async (absFilePath: string, space: string): Promise => { - 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 = async (): Promise => { +export const main: (original: string, space:string) => Promise = async (original: string, space:string): Promise => { 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 = async (): Promise => { 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 { + 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 { console.error("Has the calling function checked that the image has completed processing in WholeCZIHierarchy??"); -- cgit v1.2.1