Exemple #1
0
def get_csvs(path, fbs, wells):
    """Function to find the correct csv files and get their base names."""
    search = Directory(path)
    csvs = sorted(search.get_all_files('*.ome.csv'))
    for csv_path in csvs:
        csv_file = File(csv_path)
        # Get the filebase from the csv path.
        fbase = csv_file.cut_path('C\d\d.+$')
        #  Get the well from the csv path.
        well_name = csv_file.get_name('U\d\d--V\d\d')
        fbs.append(fbase)
        wells.append(well_name)
    return {'wells':wells, 'bases':fbs}
Exemple #2
0
def make_proj(img_list):
    """Function to make a dict of max projections from a list of paths
    to images. Each channel will make one max projection"""
    channels = []
    print('Making max projections')
    ptime = time.time()
    sorted_images = defaultdict(list)
    max_imgs = {}
    for path in img_list:
        img = File(path)
        channel = img.get_name('C\d\d')
        sorted_images[channel].append(img.read_image())
        max_imgs[channel] = np.maximum.reduce(sorted_images[channel])
    print('Max proj:' + str(time.time()-ptime) + ' secs')
    return max_imgs
Exemple #3
0
def get_gain(line,
             imaging_dir,
             last_field,
             end_63x,
             sock,
             stop_com,
             r_script,
             initialgains_file,
             gain_dict
             ):
    # empty lists for keeping csv file base path names
    # and corresponding well names
    filebases = []
    fin_wells = []
    # Parse reply, check well (UV), field (XY).
    # Get well path.
    # Get all image paths in well.
    # Make a max proj per channel and well.
    # Save meta data and image max proj.
    if 'image' in line:
        root = parse_reply(line, imaging_dir)
        img = File(root)
        img_name = img.get_name('image--.*.tif')
        field_name = img.get_name('X\d\d--Y\d\d')
        channel = img.get_name('C\d\d')
        field_path = img.get_dir()
        well_path = Directory(field_path).get_dir()
        if (field_name == last_field and channel == 'C31'):
            if end_63x:
                sock.send(stop_com)
            ptime = time.time()
            get_imgs(well_path,
                     well_path,
                     'E02',
                     img_save=False
                     )
            print(str(time.time()-ptime) + ' secs')
            # get all CSVs and wells
            csv_result = get_csvs(well_path,
                                  filebases,
                                  fin_wells,
                                  )
            filebases = csv_result['bases']
            fin_wells = csv_result['wells']

    # For all experiment wells imaged so far, run R script
    if filebases:
        gain_dict = run_r(filebases,
                          fin_wells,
                          r_script,
                          imaging_dir,
                          initialgains_file,
                          gain_dict
                          )
    return gain_dict
Exemple #4
0
def send_com(com_list,
             end_com_list,
             sock,
             start_com,
             cstart,
             stop_cam_com,
             stop_com,
             imaging_dir,
             last_field,
             end_63x,
             r_script,
             initialgains_file,
             saved_gains,
             template,
             job_list,
             pattern,
             first_job,
             coords,
             stage1=None,
             stage3=None,
             stage4=None
             ):
    for com, end_com in zip(com_list, end_com_list):
        # Send CAM list for the gain job to the server (stage1).
        # Send gain change command to server in the four channels (stage3/4).
        # Send CAM list for the experiment jobs to server (stage3/4).
        # Reset gain_dict for each iteration.
        gain_dict = defaultdict(list)
        com = '/cli:1 /app:matrix /cmd:deletelist\n' + com
        print(com)
        sock.send(com)
        time.sleep(3)
        # Start scan.
        print(start_com)
        sock.send(start_com)
        time.sleep(7)
        # Start CAM scan.
        print(cstart)
        sock.send(cstart)
        time.sleep(3)
        stage5 = True
        while stage5:
            print('Waiting for images...')
            reply = sock.recv_timeout(120, ['image--'])
            for line in reply.splitlines():
                if stage1:
                    print('Stage1')
                    gain_dict = get_gain(line,
                                         imaging_dir,
                                         last_field,
                                         end_63x,
                                         sock,
                                         stop_com,
                                         r_script,
                                         initialgains_file,
                                         gain_dict
                                         )
                    #print(gain_dict) #testing
                    if not saved_gains:
                        saved_gains = gain_dict
                    if saved_gains:
                        #print(saved_gains) #testing
                        saved_gains.update(gain_dict)
                        header = ['well', 'green', 'blue', 'yellow', 'red']
                        csv_name = 'output_gains.csv'
                        write_csv(os.path.normpath(os.path.join(imaging_dir,
                                                                csv_name
                                                                )
                                                   ),
                                  saved_gains,
                                  header
                                  )
                        com_result = gen_com(gain_dict,
                                             template,
                                             job_list,
                                             pattern,
                                             first_job,
                                             end_63x,
                                             coords=coords
                                             )
                        late_com_list = com_result['com']
                        late_end_com_list = com_result['end_com']
                else:
                    if stage3:
                        print('Stage3')
                        img_saving = False
                    if stage4:
                        print('Stage4')
                        img_saving = True
                    if 'image' in line:
                        error = True
                        count = 0
                        while error and count < 2:
                            try:
                                root = parse_reply(line, imaging_dir)
                                img = File(root)
                                img_name = img.get_name('image--.*.tif')
                                print(img_name)
                                job_order = img.get_name('E\d\d')
                                field_path = img.get_dir()
                                get_imgs(field_path,
                                         imaging_dir,
                                         job_order,
                                         f_job=first_job,
                                         img_save=img_saving,
                                         csv_save=False
                                         )
                                error = False
                            except TypeError as e:
                                error = True
                                count += 1
                                time.sleep(1)
                                print('No images yet... but maybe later?' , e)
                if all(test in line for test in end_com):
                    stage5 = False
        # Stop scan
        #print(stop_cam_com)
        #sock.send(stop_cam_com)
        #time.sleep(5)
        print(stop_com)
        sock.send(stop_com)
        time.sleep(6) # Wait for it to come to complete stop.
        if gain_dict and stage1:
            send_com(late_com_list,
                     late_end_com_list,
                     sock,
                     start_com,
                     cstart,
                     stop_cam_com,
                     stop_com,
                     imaging_dir,
                     last_field,
                     end_63x,
                     r_script,
                     initialgains_file,
                     saved_gains,
                     template,
                     job_list,
                     pattern,
                     first_job,
                     coords,
                     stage1=False,
                     stage3=stage3,
                     stage4=stage4
                     )
Exemple #5
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