예제 #1
0
def get_imgs(path, imdir, job_order, f_job=None, img_save=None, csv_save=None):
    """Function to handle the acquired images, do renaming,
    max projections etc."""
    if f_job is None:
        f_job = 2
    if img_save is None:
        img_save = True
    if csv_save is None:
        csv_save = True
    img_paths = Directory(path).get_all_files('*' + job_order + '*.tif')
    new_paths = []
    metadata_d = {}
    for img_path in img_paths:
        img = File(img_path)
        img_array = img.read_image()
        well = img.get_name('U\d\d--V\d\d')
        job_order = img.get_name('E\d\d')
        job_ord_int = int(re.sub("\D", "", job_order))
        field = img.get_name('X\d\d--Y\d\d')
        z_slice = img.get_name('Z\d\d')
        channel = img.get_name('C\d\d')
        if job_ord_int == f_job:
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice + '--' +
                                                            channel +
                                                            '.ome.tif'
                                                            )
                                                     )
                                        )
        elif job_ord_int == f_job + 1 and channel == 'C00':
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice +
                                                            '--C01.ome.tif'
                                                            )
                                                     )
                                        )
            channel = 'C01'
        elif job_ord_int == f_job + 1 and channel == 'C01':
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice +
                                                            '--C02.ome.tif'
                                                            )
                                                     )
                                        )
            channel = 'C02'
        elif job_ord_int == f_job + 2:
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice +
                                                            '--C03.ome.tif'
                                                            )
                                                     )
                                        )
            channel = 'C03'
        else:
            new_name = img_path
        if not (len(img_array) == 16 or len(img_array) == 256):
            new_paths.append(new_name)
            metadata_d[well + '--' + field + '--' + channel] = img.meta_data()
        os.rename(img_path, new_name)
    max_projs = make_proj(new_paths)
    new_dir = os.path.normpath(os.path.join(imdir, 'maxprojs'))
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)
    if img_save:
        print('Saving images')
    if csv_save:
        print('Calculating histograms')
    for channel, proj in max_projs.iteritems():
        if img_save:
            ptime = time.time()
            p = os.path.normpath(os.path.join(new_dir, 'image--' + well + '--' +
                                 field + '--' + channel + '.ome.tif'))
            metadata = metadata_d[well + '--' + field + '--' + channel]
            File(p).save_image(proj, metadata)
            print('Save image:' + str(time.time()-ptime) + ' secs')
        if csv_save:
            ptime = time.time()
            if proj.dtype.name == 'uint8':
                max_int = 255
            if proj.dtype.name == 'uint16':
                max_int = 65535
            histo = histogram(proj, 0, max_int, 256)
            rows = defaultdict(list)
            for b, count in enumerate(histo):
                rows[b].append(count)
            p = os.path.normpath(os.path.join(new_dir, well + '--' + channel +
                                 '.ome.csv'))
            write_csv(p, rows, ['bin', 'count'])
            print('Save csv:' + str(time.time()-ptime) + ' secs')
    return