-1

I have a code that already works but I need to use it to analyse many files in the same folder. How can I re-write it to do this? All the files have similar names (e.g. "pos001", "pos002", "pos003").

This is the code at the moment:

pos001 = mpimg.imread('pos001.tif')
coord_pos001 = np.genfromtxt('treat_pos001_fluo__spots.csv', delimiter=",")

Here I label the tif file "pos001" to differentiate separate objects in the same image:

label_im = label(pos001)
regions = regionprops(label_im)

Here I select only the object of interest by setting its pixel values == 1 and all the others == 0 (I'm interested in many objects, I show only one here):

cell1 = np.where(label_im != 1, 0, label_im)

Here I convert the x,y coordinates of the spots in the csv file to a 515x512 image where each spot has value 1:

x = coord_pos001[:,2]
y = coord_pos001[:,1]

coords = np.column_stack((x, y))

img = Image.new("RGB", (512,512), "white")
draw = ImageDraw.Draw(img)

dotSize = 1
for (x,y) in coords:
    draw.rectangle([x,y,x+dotSize-1,y+dotSize-1], fill="black")

im_invert = ImageOps.invert(img)
bin_img = im_invert.convert('1')

Here I set the values of the spots of the csv file equal to 1:

bin_img = np.where(bin_img == 255, 1, bin_img)

I convert the arrays from 2d to 1d:

bin_img = bin_img.astype(np.int64)
cell1 = cell1.flatten()
bin_img = bin_img.flatten()

I multiply the arrays to get an array where only the spots overlapping the labelled object have value = 1:

spots_cell1 = []
for num1, num2 in zip(cell1, bin_img):
    spots_cell1.append(num1 * num2)

I count the spots belonging to that object:

spots_cell1 = sum(float(num) == 1 for num in spots_cell1)
print(spots_cell1)

I hope it's clear. Thank you in advance!

  • 1
    check os.listdir() – Yuri Jul 06 '21 at 14:07
  • Does this answer your question? [Modify python script to run on every file in a directory](https://stackoverflow.com/questions/3241804/modify-python-script-to-run-on-every-file-in-a-directory) – Pranav Hosangadi Jul 06 '21 at 16:32
  • @PranavHosangadi I think it's helpful but not completely. As you can see in my code I have two files that need to be analysed in couple (pos001.tif and pos001.csv and so on for all the positions). The tif files are in a different folder from the csv ones. – Carmen Grandi Jul 07 '21 at 06:54
  • You can list all the tif files, and iterate over each file name. Then use those file names to create the file names / paths to the csv files. – Pranav Hosangadi Jul 07 '21 at 14:34

1 Answers1

0

You can define a function that takes the .tif file path and the .csv file path and processes the two

def process(tif_file, csv_file):
    pos = mpimg.imread(tif_file)
    coord = np.genfromtxt(csv_file, delimiter=",")
    # Do other processing with pos and coord

To process a single pair of files, you'd do:

process('pos001.tif', 'treat_pos001_fluo__spots.csv')

To list all the files in your tif file directory, you'd use the example in this answer:

import os

tif_file_directory = "/home/username/path/to/tif/files"
csv_file_directory = "/home/username/path/to/csv/files"
all_tif_files = os.listdir(tif_file_directory)

for file in all_tif_files:
    if file.endswith(".tif"):                          # Make sure this is a tif file
        fname = file.rstrip(".tif")                    # Get just the file name without the .tif extension
        tif_file = f"{tif_file_directory}/{fname}.tif" # Full path to tif file
        csv_file = f"{csv_file_directory}/treat_{fname}_fluo__spots.csv" # Full path to csv file

        # Just to keep track of what is processed, print them
        print(f"Processing {tif_file} and {csv_file}")
        process(tif_file, csv_file)

The f"...{variable}..." construct is called an f-string. More information here: https://realpython.com/python-f-strings/

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • Thanks, this is really helpful! It now works without giving any error, but I can only analyse one position (the last one). I don't understand why it doesn't work for all the others. If I do print(all_tif_files) all the positions are in there. – Carmen Grandi Jul 08 '21 at 13:20
  • Step through your code and see why it does that. [What is a debugger and how can it help me diagnose problems?](//stackoverflow.com/q/25385173/843953) The only thing I can think of is that your call to `process()` is _outside_ the loop instead of inside it. – Pranav Hosangadi Jul 08 '21 at 14:21