예제 #1
0
파일: represent.py 프로젝트: LouisK130/oii
def bin_zip(hit, hdr_path, adc_path, roi_path, outfile):
    """hit should be the hit on the mvco resolver for pid=bin_pid on the 'pid' resolver.
    outfile must be a filelike object open for writing, not a pathname. StringIO will work"""
    props = read_hdr(LocalFileSource(hdr_path))
    context = props[CONTEXT]
    del props[CONTEXT]
    props = [(k,props[k]) for k,_ in HDR_SCHEMA if k in props]

    raw_targets = list(read_adc(LocalFileSource(adc_path), 1, -1, hit.schema_version))
    raw_targets = add_bin_pid(raw_targets, hit.bin_pid)
    stitched_targets = deepcopy(raw_targets)
    stitched_targets = list_stitched_targets(stitched_targets)
    stitched_targets = add_bin_pid(stitched_targets, hit.bin_pid)
    target_pids = ['%s_%05d' % (hit.bin_pid, target['targetNumber']) for target in stitched_targets]
    template = dict(hit=hit,context=context,properties=props,targets=stitched_targets,target_pids=target_pids)

    with tempfile.SpooledTemporaryFile() as temp:
        z = ZipFile(temp,'w',ZIP_DEFLATED)
        csv_out = '\n'.join(bin2csv(stitched_targets, hit.schema_version))
        z.writestr(hit.bin_lid + '.csv', csv_out)
        # xml as well, including header info
        z.writestr(hit.bin_lid + '.xml', bin2xml(template))
        pairs = list(find_pairs(raw_targets))
        with open(roi_path,'rb') as roi_file:
            for target in stitched_targets:
                im = None
                for (a,b) in pairs:
                    if a[TARGET_NUMBER] == target[TARGET_NUMBER]:
                        images = list(read_rois((a,b),roi_file=roi_file)) # read the images
                        (im, _) = stitch((a,b), images) # stitch them
                if im is None:
                    im = list(read_rois([target],roi_file=roi_file))[0] # read the image
                # need LID here
                target_lid = re.sub(r'.*/','',target[PID]) # FIXME resolver should do this
                z.writestr(target_lid + '.png', im2bytes(im))
        z.close()
        temp.seek(0)
        shutil.copyfileobj(temp, outfile)

    return stitched_targets
예제 #2
0
파일: webapi.py 프로젝트: LouisK130/oii
def get_roi_image_from_files(schema_version, adc_path, roi_path, bin_pid, target_no, fast_stitching=False, mask=False, stitch_version=1):
    to_stitch = app.config[STITCH]
    if to_stitch:
        offset=max(1,target_no-1)
        limit=3 # read three targets, in case we need to stitch
    else:
        offset=target_no
        limit=1 # just read one
    targets = list(read_targets(adc_path, offset, limit, schema_version)) 
    if len(targets) == 0: # no targets? return Not Found
        return None
    # open the ROI file as we may need to read more than one
    with open(roi_path,'rb') as roi_file:
        if to_stitch:
            pairs = list(find_pairs(targets)) # look for stitched pairs
        else:
            pairs = targets
        roi_image = None
        if len(pairs) >= 1: # found one?
            (a,b) = pairs[0] # split pair
            if b[TARGET_NUMBER] == target_no: # second of a pair?
                return None
            images = list(read_rois((a,b),roi_file=roi_file)) # read the images
            if mask:
                roi_image = stitching.mask((a,b))
            elif fast_stitching:
                roi_image = stitch_raw((a,b), images, background=180)
            elif stitch_version == 1:
                (roi_image, mask) = stitch((a,b), images) # stitch them
            elif stitch_version == 2:
                (roi_image, mask) = joe_stitch((a,b), images)
        else:
            # now check that the target number is correct
            for target in targets:
                if target[TARGET_NUMBER] == target_no:
                    images = list(read_rois([target],roi_file=roi_file)) # read the image
                    roi_image = images[0]
        return roi_image