-
-
Save GenevieveBuckley/38bcdab7dc2bc2a088ca7496671483b5 to your computer and use it in GitHub Desktop.
microscopium illumination correction script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from microscopium import io | |
from microscopium.preprocess import correct_multiimage_illumination | |
from microscopium.preprocess import find_background_illumination | |
def main(): | |
IMAGE_FILE_PATH = "/data/" | |
OUTPUT_FILE_PATH = "/output/" | |
## get valid filenames and build output filenames | |
filenames, names_illum = get_valid_file_names(IMAGE_FILE_PATH, OUTPUT_FILE_PATH) | |
for i, directory_files in enumerate(filenames): | |
run_illum(directory_files, names_illum[i]) | |
print("Directory processed: ", directory_files[0]) | |
def get_valid_file_names(input_filepath, output_filepath): | |
""" | |
Get full filenames relative to top level directory for each file in the BBBC trial, and | |
construct filenames with paths for saving output | |
:param filepath: path to the directory containing folders of images | |
:return (valid_filenames, illum_filenames): tuple with lists of filenames for reading and saving | |
images | |
""" | |
filename_reg = r'(^Week._.*)(_..._s._w.)(.*)(\.tif)$' | |
valid_filenames = [] | |
illum_filenames = [] | |
for root, directories, filenames in os.walk(input_filepath): | |
current_subdir = root | |
new_subdir = [] | |
new_subdir_illum = [] | |
for filename in os.listdir(current_subdir): | |
match = re.search(filename_reg, filename) | |
if match: | |
new_subdir.append(os.path.join(root, match.group(1) + match.group(2) + match.group(3) + match.group(4))) | |
new_subdir_illum.append("".join([output_filepath, root[51:]]) + "_" + match.group(1) + match.group(2) + match.group(3) + "_illum" + match.group(4)) | |
if len(new_subdir) != 0 and len(new_subdir_illum) != 0: | |
valid_filenames.append(new_subdir) | |
illum_filenames.append(new_subdir_illum) | |
return (valid_filenames, illum_filenames) | |
def run_illum(filenames, names_out): | |
""" | |
Find background illumination and correct all images corresponding to elements in filenames. | |
Save corrected images using names_out which includes a relative path from the top level directory. | |
:param filenames: list of valid filenames with relative paths from top level directory | |
:param names_out: list of valid filenames for saving output with relative paths from top level directory | |
""" | |
illum = find_background_illumination(filenames) | |
corrected_images = correct_multiimage_illumination(filenames, illum=illum) | |
for (image, name) in zip(corrected_images, names_out): | |
io.imsave(name, image) | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment