Skip to content

Revise the jython script for timelapse movies #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 52 additions & 26 deletions script/CARE_generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @String(label="Input path (.tif or folder with .tifs)", required=false, value='/home/random/Development/imagej/project/CSBDeep/script') input
# @String(label="Output path (.tif or folder)", required=false, value='/home/random/Development/imagej/project/CSBDeep/script/out') output
# @String(label="Model file", required=false, value='/home/random/Development/imagej/project/CSBDeep/data/Tobias Boothe/models/phago_C2_no_transform_model.zip') modelFile
# @File(label="Input path (.tif or folder with .tifs)", required=false, value='/home/random/Development/imagej/project/CSBDeep/script', style="both") input
# @File(label="Output path (.tif or folder)", required=false, value='/home/random/Development/imagej/project/CSBDeep/script/out', style="directory") output
# @File(label="Model file", required=false, value='/home/random/Development/imagej/project/CSBDeep/data/Tobias Boothe/models/phago_C2_no_transform_model.zip', style="file") modelFile
# @String(label="Model file", required=false, value='phago_C2_no_transform_model') _modelName
# @Integer(label="Number of tiles", required=false, value=8) nTiles
# @Integer(label="Tile overlap", required=false, value=32) overlap
Expand All @@ -16,14 +16,13 @@
from java.io import File
import sys
from de.csbdresden.csbdeep.commands import GenericNetwork
from ij import IJ
from ij.plugin import Duplicator
import os

def getFileName(path):
fileparts = path.split("/")
return fileparts[len(fileparts)-1]

def runNetwork(inputPath, outputPath):
def runNetwork(outputPath, imp):
inputPath = imp.getOriginalFileInfo().fileName
print("input: " + inputPath + ", output: " + outputPath)
imp = io.open(inputPath)
mymod = (command.run(GenericNetwork, False,
"input", imp,
"nTiles", nTiles,
Expand All @@ -38,27 +37,54 @@ def runNetwork(inputPath, outputPath):
print(myoutput)
io.save(myoutput, outputPath)

if input.endswith(".tif"):
if output.endswith(".tif"):
runNetwork(input, output)

inputPath = input.getPath() # Extract input file path
outputPath = output.getPath() # Extract output file path

if inputPath.endswith(".tif"): # If processing a single .tif file

impSource = IJ.openImage(inputPath)

[width, height, nChannels, nSlices, nFrames] = impSource.getDimensions()
if nChannels > 1: # if more than 1 channel, exit
print("ERROR: please provide an image with a single channel")
sys.exit()

if outputPath.endswith(".tif"):
if nFrames > 1: # If there is only 1 specified .tif file but multiple frames
print("ERROR: To process an hyperstack with multiple frames, please provide a directory as output")
sys.exit()
runNetwork(outputPath, impSource)

else:
if not(output.endswith("/")):
output += "/"
runNetwork(input, output + getFileName(input))
else:
if output.endswith(".tif"):
print("Processing: " + input.getName())
print("Found "+str(impSource.getNFrames())+" frames to process")

for dt in xrange(nFrames): # For each frame
frame = dt+1 # Frames in the movie will begin at 1
# Duplicate the frame of interest
impSingleTp = Duplicator().run(impSource, 1, 1, 1, nSlices, frame, frame)
# Create a new output fileName
root, ext = os.path.splitext(input.getName())
outputFileName = root + "_frame" + str(frame) + ".tif"
# Run the network
print("Processing: " + outputFileName)
runNetwork(os.path.join(outputPath, outputFileName), impSingleTp)

elif input.isDirectory():
if outputPath.endswith(".tif"):
print("ERROR: please provide a directory as output, because your input is also a directory")
sys.exit()
if not(output.endswith("/")):
output += "/"
if not(input.endswith("/")):
input += "/"
if(output == input):
if(outputPath == inputPath):
print("ERROR: please provide an output directory that is not the same as the input directory")
sys.exit()
directory = File(input);
listOfFilesInFolder = directory.listFiles();

listOfFilesInFolder = input.listFiles();
for file in listOfFilesInFolder:
if file.toString().endswith(".tif"):
runNetwork(file.toString(), output + getFileName(file.toString()))
if file.getName().endswith(".tif"):
impSource = IJ.openImage(file.toString())
runNetwork(os.path.join(outputPath, file.getName()), impSource)

else: # input is not a directory but not a .tif file either
print("ERROR: please provide a .tif file or directory for input")
sys.exit()