diff options
| author | cjd24 | 2018-04-26 03:13:52 +0100 |
|---|---|---|
| committer | cjd24 | 2018-04-26 03:13:52 +0100 |
| commit | 60576c0b58907d48cffcb79ec023e7afaa5a509b (patch) | |
| tree | 5a28023c784dd4271e699db50bdc2f021d59f9d8 /src/conversion/czi | |
| parent | small fixes of fs.unlink and paths passed to c (diff) | |
| download | medicine-60576c0b58907d48cffcb79ec023e7afaa5a509b.tar.gz medicine-60576c0b58907d48cffcb79ec023e7afaa5a509b.zip | |
Fixing the threading issue with vips, should be ready for server deployment
Diffstat (limited to 'src/conversion/czi')
| -rw-r--r-- | src/conversion/czi/czi.ts | 118 | ||||
| -rw-r--r-- | src/conversion/czi/pyramidWorker.ts | 12 |
2 files changed, 77 insertions, 53 deletions
diff --git a/src/conversion/czi/czi.ts b/src/conversion/czi/czi.ts index 448d81c..53cfb85 100644 --- a/src/conversion/czi/czi.ts +++ b/src/conversion/czi/czi.ts @@ -13,9 +13,10 @@ import { uuid } from '../../uuid' import { queue as jobQueue, Promiser as Job } from '../../ppq' import { profiler } from '../../profiler'; import { exec } from '../types/exec' -sharp.concurrency(2); let log: Logger; +const THREAD_LIMIT: number = 2; +sharp.concurrency(THREAD_LIMIT); const readFile = require('util').promisify(fs.readFile); const readdir = require('util').promisify(fs.readdir); const exists = require('util').promisify(fs.exists); @@ -538,51 +539,64 @@ const zoomTier: ( quadrents += `${outputImageDirectory}${previousPlane[ys + 1][xs + 1].file} `; } - let outputFileName: string =`${outputImageData}/tmp/${uuid.generate()}.png`; - await jobQueue.enqueue(4, exec, null, `${execpaths} vips arrayjoin "${quadrents}" ${outputFileName} --across ${across}`); + let outputFileName: string =`${outputImageData}/tmp/${uuid.generate()}.png`; - // Rescale and push to file - if (quadrentCount !== 3) { - - let extRight: number = 0, extBottom: number = 0; - if (moreToRight) { - extBottom = tileSize; - } else if (moreToBottom){ - extRight = tileSize; - } - - let fullTileQuadRef: SharpInstance = sharp(outputFileName) - .background({r: 0, g: 0, b: 0, alpha: 1}) - .extend({ - top: 0, left: 0, - bottom: extBottom, right: extRight - }); - let fullTileQuad: Buffer = await jobQueue.enqueue(5, fullTileQuadRef.toBuffer as Job<Buffer>, fullTileQuadRef); - - let rescaledTileQuadRef: SharpInstance = sharp(fullTileQuad) - .resize(tileSize, tileSize); - - await jobQueue.enqueue(5, rescaledTileQuadRef.toFile as Job<sharp.OutputInfo>, rescaledTileQuadRef, - `${outputImageDirectory}img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`); - } else { - let fullTileQuadRef: SharpInstance = sharp(outputFileName) - .resize(tileSize, tileSize) - - await jobQueue.enqueue(5, fullTileQuadRef.toFile as Job<sharp.OutputInfo>, fullTileQuadRef, - `${outputImageDirectory}img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`); - } - fs.unlink(`${outputFileName}`, (err) => { - log.warn(fileName + ' Error when deleting redundant resource: \n' + err.stack) - }); + try { + await new Promise((res) => { + shell.exec(`${execpaths} vips arrayjoin "${quadrents}" ${outputFileName} --across ${across} --vips-concurrency=${THREAD_LIMIT}`); + res(); + }); + } catch (err) { + if (err) { + log.fatal(err); + } + } - finalCZIJson.total_files++; - return { - x_offset: previousPlane[ys][xs].x_offset, - y_offset: previousPlane[ys][xs].y_offset, - width: previousPlane[ys][xs].width * 2, - height: previousPlane[ys][xs].height * 2, - file: `img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png` - }; + // Rescale and push to file + if (quadrentCount !== 3) { + + let extRight: number = 0, extBottom: number = 0; + if (moreToRight) { + extBottom = tileSize; + } else if (moreToBottom){ + extRight = tileSize; + } + + let fullTileQuadRef: SharpInstance = sharp(outputFileName) + .background({r: 0, g: 0, b: 0, alpha: 1}) + .extend({ + top: 0, left: 0, + bottom: extBottom, right: extRight + }); + let fullTileQuad: Buffer = await jobQueue.enqueue(5, fullTileQuadRef.toBuffer as Job<Buffer>, fullTileQuadRef); + + let rescaledTileQuadRef: SharpInstance = sharp(fullTileQuad) + .resize(tileSize, tileSize); + + await jobQueue.enqueue(5, rescaledTileQuadRef.toFile as Job<sharp.OutputInfo>, rescaledTileQuadRef, + `${outputImageDirectory}img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`); + } else { + let fullTileQuadRef: SharpInstance = sharp(outputFileName) + .resize(tileSize, tileSize) + + await jobQueue.enqueue(5, fullTileQuadRef.toFile as Job<sharp.OutputInfo>, fullTileQuadRef, + `${outputImageDirectory}img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`); + } + + fs.unlink(`${outputFileName}`, (err) => { + if (err) { + log.warn(fileName + ' Error when deleting redundant resource: \n' + err.stack) + } + }); + + finalCZIJson.total_files++; + return { + x_offset: previousPlane[ys][xs].x_offset, + y_offset: previousPlane[ys][xs].y_offset, + width: previousPlane[ys][xs].width * 2, + height: previousPlane[ys][xs].height * 2, + file: `img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png` + }; } const cziRow: Promise<CZITile>[] = []; @@ -730,7 +744,7 @@ const extrapolateDimension: ( writeJSONToFile(`${outputImageData}/intermediate-stage-map.json`, retHeightMap); } - log.notify(`${fileName} > Completed Extrapolation for dimension, \'C\': ${cVal}\n\n`); + log.notify(`${fileName} > Completed Extrapolation for dimension, \'C\': ${cVal}`); return retHeightMap; }; @@ -814,7 +828,7 @@ const buildCustomPyramids: () => Promise<boolean> = async(): Promise<boolean> => // On error, write the error to console and set an error true. if (err) { console.error(err.message); - console.log(err); + console.log(err.stack); } successfulBuild = false; } @@ -838,8 +852,8 @@ const initialExtractAndConvert: (absFilePath: string, space: string) => Promise< checkForOutputDirectories([outputImageData, extractDirectory]); log.silly(`${fileName} > This will complete at roughly 2GB/min`); - exec(`${execpaths} CZICrunch "${absFilePath}" "${extractDirectory}"`); - exec(`${execpaths} python3 ./ext/bin/convertJxrs.py "${extractDirectory}"`); + await exec(`${execpaths} CZICrunch "${absFilePath}" "${extractDirectory}"`); + await exec(`${execpaths} python3 ./ext/bin/convertJxrs.py "${extractDirectory}"`); // let totalFiles: number = 0, counter: number = 0; // console.log("=========== BEGIN JXR CONVERSION ===========") @@ -892,16 +906,16 @@ log.notice("CZI Convertor Received new file: " + fileName); outputImageDirectory = outputImageData + "/data/"; if (!(await exists(`${outputImageData}/supported_views.json`))) { - log.info(`${fileName} > Begin Extracting and Converting to PNG`); + log.information(`${fileName} > Begin Extracting and Converting to PNG`); await initialExtractAndConvert(original, space); - log.info(`${fileName} > Checking/Creating output directories...`); + log.information(`${fileName} > Checking/Creating output directories...`); checkForOutputDirectories([outputImageDirectory, `${outputImageData}/tmp/`]); - log.info(`${fileName} > Creating Supported Views and writing files...`); + log.information(`${fileName} > Creating Supported Views and writing files...`); await createSupportedViewsObject(false); } else { - log.alert("It appears as though this CZI has already been extracted, loading files..."); + log.notify("It appears as though this CZI has already been extracted, loading files..."); await createSupportedViewsObject(true); supportedViews = require(`${outputImageData}/supported_views.json`); } diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts index c52aac2..9851c1f 100644 --- a/src/conversion/czi/pyramidWorker.ts +++ b/src/conversion/czi/pyramidWorker.ts @@ -9,6 +9,7 @@ import { RequestError } from '../../errors' import { logger, Logger } from '../../logger' import { exec } from '../types/exec' import { queue as jobQueue, Promiser as Job } from '../../ppq' +const shell = require('shelljs'); const readFile = require('util').promisify(fs.readFile); const log: Logger = logger.for({component: "CZI Live Server"}); @@ -78,7 +79,16 @@ const getFinalTile: Function = async function(imageDir: string, imageTier: CZIHe let id: string = uuid.generate(); let intermediateFileName: string = `${imageDir}tmp/${id}.png` let outputFileName: string = `${imageDir}tmp/${id}-out.png` - await jobQueue.enqueue(2, exec, null, `${execpaths} vips arrayjoin "${involvedTiles}" ${intermediateFileName} --across ${imageTier.plane[0].length}`); + try { + await new Promise((res) => { + shell.exec(`${execpaths} vips arrayjoin "${involvedTiles}" ${intermediateFileName} --across ${imageTier.plane[0].length} --vips-concurrency=2`); + res(); + }); + } catch (err) { + if (err) { + log.fatal(err); + } + } desiredRegion.scaleDown(imageTier.zoom_level); |
