Importing multiple folders to Virtual Stack using Python

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Importing multiple folders to Virtual Stack using Python

otills
Hi, I am attempting to modify the demo Python script 'Create a virtual stack from the TIF files present in a folder and its subfolders' to import tiff sequences from different directories, but a particular XY position, as a virtual stack. I have successfully managed to get a filtered (filtered to tif files acquired at a particular XY position - Pos2) list of files when testing outside of ImageJ.

However, I get the error "AttributeError: 'NoneType' object has no attribute 'addSlice'" when trying to run the script below. I am a novice Python programmer and so pointers and guidance are really welcome.

I would also like to incorporate ordering the files by date created using - os.path.getctime(os.path.join(root,filename)), but I haven't managed this yet either..?

Thanks in advance,

Oli

#Create a virtual stack from the TIF files present in a folder and its subfolders, recursively
# Walk recursively through an user-selected directory
# and add all found filenames that end with ".tif"
# to a VirtualStack, which is then shown.
#
# It is assumed that all images are of the same type
# and have the same dimensions.
 
import os
import glob
import fnmatch
import re

from ij.io import DirectoryChooser
from ij import IJ, ImagePlus, VirtualStack

pattern = '*Pos2*.tif'
 
def run():
  srcDir = DirectoryChooser("Choose!").getDirectory()
  if not srcDir:
    # user canceled dialog
    return
  # Assumes all files have the same size
  vs = None
  for root, directories, filenames in os.walk(srcDir):
    for filename in filenames:
      # Skip non-TIFF files
      if not filename.endswith(".tif"):
        continue
      # Combine fname with path
      path = os.path.join(root, filename)
      # Upon finding the first image, initialize the VirtualStack
      #if vs is None
      if vs is None and fnmatch.fnmatch(path,pattern):
        imp = IJ.openImage(path)
        vs = VirtualStack(imp.width, imp.height, None, srcDir)
      # Add a slice, relative to the srcDir
      vs.addSlice(path[len(srcDir):])
  #
  ImagePlus("Stack from subdirectories", vs).show()
 
run()
Reply | Threaded
Open this post in threaded view
|

Re: Importing multiple folders to Virtual Stack using Python

otills
I have cracked the date filtering, but still no luck getting the ImageJ import part of the script to work..

for root, directories, filenames in os.walk(rootPath):
        #print(fullPath)
        for f in filenames:
                fi = os.path.join(root,f)
                if fnmatch.fnmatch(fi,pattern):
                        t = os.path.getctime(fi)
                        file_date_tuple = (fi, t)
                        file_date_tuple_list.append(file_date_tuple)
        file_date_tuple_list.sort(key=lambda x: x[1])
Reply | Threaded
Open this post in threaded view
|

Re: Importing multiple folders to Virtual Stack using Python

Albert Cardona-2
See:
"Create a virtual stack from the TIF files present in a folder and its
subfolders, recursively"

http://imagej.net/Jython_Scripting#Create_a_virtual_stack_from_the_TIF_files_present_in_a_folder_and_its_subfolders.2C_recursively

Hope it helps.

Albert

2016-04-19 2:08 GMT-04:00 otills <[hidden email]>:

> I have cracked the date filtering, but still no luck getting the ImageJ
> import part of the script to work..
>
> for root, directories, filenames in os.walk(rootPath):
>         #print(fullPath)
>         for f in filenames:
>                 fi = os.path.join(root,f)
>                 if fnmatch.fnmatch(fi,pattern):
>                         t = os.path.getctime(fi)
>                         file_date_tuple = (fi, t)
>                         file_date_tuple_list.append(file_date_tuple)
>         file_date_tuple_list.sort(key=lambda x: x[1])
>
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/Importing-multiple-folders-to-Virtual-Stack-using-Python-tp5016162p5016172.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>



--
http://albert.rierol.net
http://www.janelia.org/lab/cardona-lab
http://www.ini.uzh.ch/~acardona/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Importing multiple folders to Virtual Stack using Python

otills
Thanks Albert,

This is the script I have been trying to modify for my purpose, but I seem unable to modify it to get sorting by date created and filtering based on search terms (filter by position label) additional to *.tif.

I will keep trying..

Oli
Reply | Threaded
Open this post in threaded view
|

Re: Importing multiple folders to Virtual Stack using Python

John Hayes
In reply to this post by otills
Hi Oli,

That error message means "vs" is not initialized. Apparently, that also
means in your preceding "if" statement that tests if vs is None that the
second condition is failing.

HTH,
John
On Apr 18, 2016 8:38 AM, "otills" <[hidden email]> wrote:

> Hi, I am attempting to modify the demo Python script 'Create a virtual
> stack
> from the TIF files present in a folder and its subfolders' to import tiff
> sequences from different directories, but a particular XY position, as a
> virtual stack. I have successfully managed to get a filtered (filtered to
> tif files acquired at a particular XY position - Pos2) list of files when
> testing outside of ImageJ.
>
> However, I get the error "AttributeError: 'NoneType' object has no
> attribute
> 'addSlice'" when trying to run the script below. I am a novice Python
> programmer and so pointers and guidance are really welcome.
>
> I would also like to incorporate ordering the files by date created using -
> os.path.getctime(os.path.join(root,filename)), but I haven't managed this
> yet either..?
>
> Thanks in advance,
>
> Oli
>
> #Create a virtual stack from the TIF files present in a folder and its
> subfolders, recursively
> # Walk recursively through an user-selected directory
> # and add all found filenames that end with ".tif"
> # to a VirtualStack, which is then shown.
> #
> # It is assumed that all images are of the same type
> # and have the same dimensions.
>
> import os
> import glob
> import fnmatch
> import re
>
> from ij.io import DirectoryChooser
> from ij import IJ, ImagePlus, VirtualStack
>
> pattern = '*Pos2*.tif'
>
> def run():
>   srcDir = DirectoryChooser("Choose!").getDirectory()
>   if not srcDir:
>     # user canceled dialog
>     return
>   # Assumes all files have the same size
>   vs = None
>   for root, directories, filenames in os.walk(srcDir):
>     for filename in filenames:
>       # Skip non-TIFF files
>       if not filename.endswith(".tif"):
>         continue
>       # Combine fname with path
>       path = os.path.join(root, filename)
>       # Upon finding the first image, initialize the VirtualStack
>       #if vs is None
>       if vs is None and fnmatch.fnmatch(path,pattern):
>         imp = IJ.openImage(path)
>         vs = VirtualStack(imp.width, imp.height, None, srcDir)
>       # Add a slice, relative to the srcDir
>       vs.addSlice(path[len(srcDir):])
>   #
>   ImagePlus("Stack from subdirectories", vs).show()
>
> run()
>
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/Importing-multiple-folders-to-Virtual-Stack-using-Python-tp5016162.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Importing multiple folders to Virtual Stack using Python

otills
Thanks for the helpful advice.

Albert - the script you flagged is the very useful script which I had been trying to modify.

I have something that works now, by - generating a list of paths for all images on a drive, filtering the paths (based on folder labels in a path), ordering the images by date created and finally importing all images for a particular position as a virtual stack.

Script is copied below:

#Create a virtual stack from the TIF files present in a folder and its subfolders, recursively
# Walk recursively through an user-selected directory
# and add all found filenames that end with ".tif"
# to a VirtualStack, which is then shown.
#
# It is assumed that all images are of the same type
# and have the same dimensions.
 
import os
import glob 
import fnmatch 
import re 

from ij.io import DirectoryChooser
from ij import IJ, ImagePlus, VirtualStack

pattern = "*B2*.tif"
file_date_tuple_list = []
files = []
dateCreated = []
#list(files)
#list(dateCreated)
def run():
  srcDir = DirectoryChooser("Choose!").getDirectory()
  if not srcDir:
    # user canceled dialog
    return
  # Assumes all files have the same size
  vs = None
  for root, directories, filenames in os.walk(srcDir):
    for filename in filenames:
    	# Combine root and filename to generate path for each file
		path = os.path.join(root, filename)
		# Get date created for later file sorting
		t = os.path.getctime(path)
		# Combine path and date
		file_date_tuple = (path, t)
		# And append to the list
		file_date_tuple_list.append(file_date_tuple)
  # Once files have been identified and added to the tuple_list sort the list by date created.
  file_date_tuple_list.sort(key=lambda x: x[1])
  f,dC = zip(*file_date_tuple_list)		
  # Now filter them and prepare for loading into Virtual Image Stack
  for filtered in f:
  #	print filtered
    if fnmatch.fnmatch(filtered,pattern):
  		imp = IJ.openImage(filtered)
  		# Upon finding the first image, initialize the VirtualStack
		if vs is None:
			vs = VirtualStack(imp.width, imp.height, None, srcDir)
		# Add a slice, relative to the srcDir
		vs.addSlice(filtered[len(srcDir):])
  #print f	
  ImagePlus("Stack from subdirectories", vs).show()
  # Save the file list (sorted but not filtered) to enable faster loading of positions from the same imageset.
  #try:
  outFile = open("/Volumes/Data/Users/otills/Desktop/B2outputFile.txt", "w")
  for s in f:
	outFile.write(s + "\n")
run()

Searching the drive to generate the path list obviously takes quite some time and so I would like to save the path list that is generated (this is done in the script below) and then use this for loading subsequent positions, by just filtering this list and loading the files.

I am attempting to use the script below to do acheive this, however I cannot figure out how to get ImageJ to import these files. I end up with a blank stack with the text 'File not found....), but the image paths are correct.

How should I be using these pathnames to create a Virtual Stack?

Thanks,

Oli
import os
import glob 
import fnmatch 
import re 

from ij.io import DirectoryChooser
from ij import IJ, ImagePlus, VirtualStack

def run():
 # Load list of image files.
 indexFile = open("/Volumes/Data/Users/otills/Desktop/outputFile.txt").read().splitlines()
 vs = None
 # Create a regular expression object for searching (change as required..)
 pattern = re.compile('.*B2.*\.tif')
 # Filter the image file list
 filteredIndexFiles = filter(pattern.search,indexFile)
 # Loop over the filtered image file list and load each file into a virtual stack.
 for f in filteredIndexFiles:
  	if vs is None:
  		imp = IJ.openImage(f)
		vs = VirtualStack(imp.width, imp.height, None,f)
	vs.addSlice(f[len(filteredIndexFiles):])	
 ImagePlus("Stack from subdirectories", vs).show()
  
run()