示例#1
0
def alignRec(record, template=template, bgfile='image_Ch1.nrrd', alignSet='', threshold=0.6):
    record = checkDir(record)
    record['last_host'] = host
    print 'Finalising alignment for: ' + record['name']
    # bgfile = record['original_nrrd'][('Ch' + str(record['background_channel']) + '_file')]
    record['aligned_bg'], r = cmtk.align(bgfile, template=template, settings=alignSet)
    record['aligned_avgslice_score'] = str(
        ci.rateOne(record['aligned_bg'], results=None, methord=slicescore.avgOverlapCoeff, template=template))
    record['aligned_slice_score'] = str(
        ci.rateOne(record['aligned_bg'], results=None, methord=slicescore.OverlapCoeff, template=template))
    record['aligned_score'] = str(
        np.mean([np.float128(record['aligned_avgslice_score']), np.float128(record['aligned_slice_score'])]))
    # Note: np.float128 array score converted to string as mongoDB only supports float(64/32 dependant on machine).
    record['aligned_bg'] = str(record['aligned_bg']).replace(tempfolder, '')
    print 'Result: ' + record['aligned_score']
    if record['aligned_score'] > threshold:
        record['alignment_stage'] = 6
        print 'Passed!'
    else:
        record['alignment_stage'] = 0
        print 'Failed!'
    if r > 0:
        print 'Error Code:' + str(r)
        record['alignment_stage'] = 0
    record['max_stage'] = 6
    return record
示例#2
0
def initialRec(record, template=template, init_threshold=0.3, bgfile='image_Ch1.nrrd', alignSet='',
               initialSet='--principal-axes'):
    start = datetime.datetime.now()
    record = checkDir(record)
    record['last_host'] = host
    print 'Staring initial alignment for: ' + record['name']
    # bgfile = record['original_nrrd'][('Ch' + str(record['background_channel']) + '_file')]
    record['temp_initial_nrrd'], r = cmtk.align(bgfile, cmtk.initial(bgfile, template=template, mode=initialSet)[0],
                                                imageOUT=tempfolder + record['name'] + '_initial.nrrd',
                                                template=template, settings=alignSet)
    record['temp_initial_score'] = str(ci.rateOne(record['temp_initial_nrrd'], results=None, template=template))
    # Note: np.float128 array score converted to string as mongoDB only supports float(64/32 dependant on machine).
    record['temp_initial_nrrd'] = str(record['temp_initial_nrrd']).replace(tempfolder, '')
    print 'Result: ' + record['temp_initial_score']
    totaltime = datetime.datetime.now() - start
    if record['temp_initial_score'] > init_threshold:
        record['alignment_stage'] = 3
    else:
        record['alignment_stage'] = 0
    if r > 0:
        record['alignment_stage'] = 1002
    else:
        if record['notes'] is None:
            record['notes'] = time.strftime(
                "%c") + ' Initial alignment performed by ' + host + ' in ' + str(totaltime) + ' scoring ' + record[
                                  'temp_initial_score'] + '/' + str(init_threshold)
        else:
            record['notes'] = record['notes'] + '\n' + time.strftime(
                "%c") + ' Initial alignment performed by ' + host + ' in ' + str(totaltime) + ' scoring ' + record[
                                  'temp_initial_score'] + '/' + str(init_threshold)
    record['max_stage'] = 3
    return record
示例#3
0
def affineRec(record, template=template, bgfile="image_Ch1.nrrd", affineSet="--dofs 6,9 --auto-multi-levels 4"):
    start = datetime.datetime.now()
    record = checkDir(record)
    print "Staring affine alignment for: " + record["name"]
    # bgfile = record['original_nrrd'][('Ch' + str(record['background_channel']) + '_file')]
    affine, r = cmtk.affine(bgfile, template=template, scale=affineSet)
    print affine
    totaltime = datetime.datetime.now() - start
    record["alignment_stage"] = 4
    if r > 0:
        record["alignment_stage"] = 1003
    else:
        if record["notes"] is None:
            record["notes"] = time.strftime("%c") + " Affine alignment performed by " + host + " in " + str(totaltime)
        else:
            record["notes"] = (
                record["notes"]
                + "\n"
                + time.strftime("%c")
                + " Affine alignment performed by "
                + host
                + " in "
                + str(totaltime)
            )
    if r == 99:
        record["alignment_stage"] = 2
    record["max_stage"] = 4
    record["last_host"] = host
    return record
示例#4
0
def alignRem(record, template=template, chfile='image_Ch1.nrrd', alignSet=''):
    record = checkDir(record)
    print 'Aligning signal, etc. for: ' + record['name']
    record['last_host'] = host
    # bgfile = record['original_nrrd'][('Ch' + str(record['background_channel']) + '_file')]
    # sgfile = record['original_nrrd'][('Ch' + str(record['signal_channel']) + '_file')]
    # a=0
    # for i in range(1,6):
    # if not i == record['background_channel']:
    #   if i == record['signal_channel']:
    if record['ac1_channel'] < 1:
        for i in range(1, 4):
            print 'Checking for AC1 in channel ' + str(i)
            if not (i == int(record['signal_channel']) or i == int(record['background_channel'])):
                record['ac1_channel'] = i
                print 'Set AC1 Channel to ' + str(i)
    r = 0
    sgchan = '_Ch' + str(record['signal_channel'])
    bgchan = '_Ch' + str(record['background_channel'])
    acchan = '_Ch' + str(record['ac1_channel'])
    chfile = chfile.replace(bgchan, acchan).replace(acchan, sgchan)
    if os.path.isfile(chfile):
        record['aligned_sg'], r = cmtk.align(chfile, xform=chfile.replace(sgchan + '.nrrd', bgchan + '_warp.xform'),
                                             template=template, settings=alignSet)
        record['alignment_stage'] = 7
        record['max_stage'] = 7
        record['aligned_sg'] = str(record['aligned_sg']).replace(tempfolder, '')
    else:
        print chfile + ' not found'
        r = 5
    chfile = chfile.replace(sgchan, acchan)
    if os.path.isfile(chfile):
        record['aligned_ac1'], r = cmtk.align(chfile, xform=chfile.replace(acchan + '.nrrd', bgchan + '_warp.xform'),
                                              template=template, settings=alignSet)
        record['max_stage'] = 7
        record['aligned_ac1'] = str(record['aligned_ac1']).replace(tempfolder, '')
    else:
        print chfile + ' not found'

    if r > 0:
        print 'Error code:' + str(r)
        record['alignment_stage'] = 0
    return record
示例#5
0
def warpRec(record, template=template, bgfile='image_Ch1.nrrd',
            warpSet='--grid-spacing 80 --exploration 30 --coarsest 4 --accuracy 0.2 --refine 4 --energy-weight 1e-1'):
    start = datetime.datetime.now()
    record = checkDir(record)
    print 'Staring warping alignment for: ' + record['name']
    # bgfile = record['original_nrrd'][('Ch' + str(record['background_channel']) + '_file')]
    warp, r = cmtk.warp(bgfile, template=template, settings=warpSet)
    totaltime = datetime.datetime.now() - start
    record['alignment_stage'] = 5
    if r > 0:
        record['alignment_stage'] = 1004
    else:
        if record['notes'] is None:
            record['notes'] = time.strftime(
                "%c") + ' Warp alignment performed by ' + host + ' in ' + str(totaltime)
        else:
            record['notes'] = record['notes'] + '\n' + time.strftime(
                "%c") + ' Warp alignment performed by ' + host + ' in ' + str(totaltime)
    if r == 99: record['alignment_stage'] = 2
    record['max_stage'] = 5
    record['last_host'] = host
    return record
示例#6
0
def convRec(record):
    try:
        if not 'loading_host' in record:
            record['loading_host'] = 'roberts-mbp'
        if not record['loading_host'] == host:
            print 'Warning: ' + host + ' is not the loading host (' + record['loading_host'] + ')'
        if str(record['original_path']).endswith(os.path.sep):
            file = record['original_path'] + record['name'] + record['original_ext']
        else:
            file = record['original_path'] + os.path.sep + record['name'] + record['original_ext']
        print 'Converting ' + file
        if (os.path.exists(file) or os.path.exists(file + '.gz')):
            record['last_host'] = host
            if (os.path.exists(file + '.gz') and (not os.path.exists(file))):
                check_call(['gzip', '-d', file + '.gz'])
            if '.gz' in file:
                check_call(['gzip', '-df', file])
                file = str(file).replace('.gz', '')
                if str(record['original_ext']) == '.gz':
                    record['name'] = str(os.path.splitext(os.path.basename(file))[0])
                    record['original_ext'] = str(os.path.splitext(os.path.basename(file))[1])
            print 'Opening file: ' + file + '...'
            tif = TiffFile(file)
            image = tif.asarray()
            print 'Converting file: ' + file + '...'
            record = checkDir(record)
            if tif.is_lsm:
                metadata = tif[0].cz_lsm_scan_information
                voxelZ = metadata['plane_spacing']
                voxelY = metadata['line_spacing']
                voxelX = metadata['sample_spacing']
                header = {}
                header['encoding'] = 'gzip'
                header['space directions'] = [[float(voxelX), 0.0, 0.0], [0.0, float(voxelY), 0.0],
                                              [0.0, 0.0, float(voxelZ)]]
                header['space units'] = ['"microns"', '"microns"', '"microns"']
                # header['keyvaluepairs'] = dict(metadata)
                # print header
            else:
                metadata = tif[0].image_description
                # metadata = json.loads(metadata.decode('utf-8'))
                # voxel = metadata['Voxel size']
                # TBD: resolve zoxel size!
                voxelZ = 0.5
                voxelY = 0.5
                voxelX = 0.5
                #temp fix for zebrafish:
                if (record['settings_id'] == 10):
                    voxelZ = 1
                    voxelY = 1
                    voxelX = 1
                header = {}
                header['encoding'] = 'gzip'
                header['space directions'] = [[float(voxelX), 0.0, 0.0], [0.0, float(voxelY), 0.0],
                                              [0.0, 0.0, float(voxelZ)]]
                header['space units'] = ['"px"', '"px"', '"px"']
                
                print(image.shape, image.dtype)
                if image.ndim > 4:
                    sh = np.array(image.shape)
                    rmdim = np.argmin(sh)
                    if sh[rmdim] > 1:
                        sh[rmdim] = np.max(sh)
                        rmdim = np.argmin(sh)
                    image = np.max(image, axis=rmdim)
                    print 'slimmed down to:'
                    print(image.shape, image.dtype)

                print metadata
                # TBD: add voxel size data
                # header['keyvaluepairs'] = dict(metadata)
            tif.close()
            check_call(['gzip', '-f', file])
            image = np.squeeze(image)
            sh = np.array(np.shape(image))
            ch = np.argmin(sh)
            iy = np.argmax(sh)
            sh[iy] = 0
            ix = np.argmax(sh)
            sh[ix] = 0
            iz = np.argmax(sh)
            sh = np.shape(image)
            # move channel to last axis:
            image = np.swapaxes(image, ch, -1)
            # move smalest (Z) axis to last axis before channel:
            image = np.swapaxes(image, iz, -2)
            # swap X & Y to match NRRD standard order for saving:
            image = np.swapaxes(image, 0, 1)
            print record['name'] + record['original_ext'] + ' - ' + str(np.shape(image))
            rt = 0
            mt = 0
            sg = 0
            bg = 0
            cur.execute(
                "SELECT system_template.orientation FROM system_template, system_setting, images_alignment WHERE images_alignment.id = %s AND images_alignment.settings_id = system_setting.id AND system_setting.template_id = system_template.id",
                [record['id']])
            tempOrien = cur.fetchone()[0]
            cur.execute(
                "SELECT system_setting.auto_balance_th FROM system_template, system_setting, images_alignment WHERE images_alignment.id = %s AND images_alignment.settings_id = system_setting.id AND system_setting.template_id = system_template.id",
                [record['id']])
            tempTresh = float(cur.fetchone()[0])
            if 'orig_orientation' not in record.keys():
                if sh[ch] == 2:
                    record['orig_orientation'] = comp_orien[tempOrien]
                else:
                    record['orig_orientation'] = 'right-posterior-inferior'  # needs to be set at load
            if record['orig_orientation'] == '':
                if sh[ch] == 2:
                    record['orig_orientation'] = comp_orien[tempOrien]
                else:
                    record['orig_orientation'] = 'right-posterior-inferior'  # needs to be set at load
            header['space'] = comp_orien[tempOrien]
            for c in xrange(0, sh[ch]):
                upd = {}
                chan, Nbound, hist = ab.AutoBalance(np.squeeze(image[:, :, :, c]), threshold=tempTresh, background=0)
                print 'Ch' + str(c + 1) + ' - ' + str(np.shape(chan))
                Sname = tempfolder + record['name'] + '_Ch' + str(c + 1) + '.nrrd'

                if not record['orig_orientation'] == comp_orien[tempOrien]:
                    chan = ro.reorientate(np.uint8(chan), curOr=record['orig_orientation'],
                                          targOr=comp_orien[tempOrien])
                else:
                    chan = np.uint8(chan)

                if not ((record['crop_xyz'] is None) or (record['crop_xyz'] is '')):
                    cut = map(int, str(record['crop_xyz']).replace('[', '').replace(']', '').split(','))
                    cut = np.array(cut)
                    if np.sum(cut) > 0:
                        print 'cropping: (' + str(cut) + ')...'
                        sh = np.shape(chan)
                        print sh
                        chan = chan[cut[0]:sh[0] - cut[1], cut[2]:sh[1] - cut[3], cut[4]:sh[2] - cut[5]]
                        print np.shape(chan)

                print 'saving...'
                nrrd.write(Sname, chan, options=header)
                upd.update(
                    {'image_id': record['id'], 'channel': + int(c + 1), 'file': str(Sname).replace(tempfolder, ''),
                     'pre_histogram': list(hist), 'new_min': int(Nbound['min']), 'new_max': int(Nbound['max'])})
                print str(upd)
                cur.execute(
                    "SELECT count(*) FROM images_original_nrrd WHERE image_id = %(image_id)s AND channel = %(channel)s",
                    upd)
                r = cur.fetchone()[0]
                if r > 0:
                    cur.execute(
                        "UPDATE images_original_nrrd SET file = %(file)s, pre_hist = %(pre_histogram)s, new_min = %(new_min)s, new_max = %(new_max)s, is_index = False WHERE image_id = %(image_id)s AND channel = %(channel)s",
                        upd)
                    cur.connection.commit()
                else:
                    cur.execute(
                        "INSERT INTO images_original_nrrd (image_id, channel, file, pre_hist, new_min, new_max, is_index) VALUES (%(image_id)s, %(channel)s, %(file)s, %(pre_histogram)s, %(new_min)s, %(new_max)s, False)",
                        upd)
                    cur.connection.commit()
                try:
                    os.chmod(Sname, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
                except:
                    pass
                ct = sum(chan[chan > 20])
                if ct > rt:
                    rt = ct
                    bg = c + 1
                if mt == 0:
                    mt = ct
                    sg = c + 1
                if ct < mt:
                    mt = ct
                    sg = c + 1
            print 'BG: ' + str(bg) + ', SG: ' + str(sg)
            if record['background_channel'] == 0:
                record['background_channel'] = bg
            if record['signal_channel'] == 0:
                record['signal_channel'] = sg
            record['alignment_stage'] = 2
            # record.update({'original_nrrd': upd})
            record['max_stage'] = 2
            # collection.save(record)

            print 'conversion complete.'
            del upd, hist, chan, Nbound, tif, image, sh, ch, iy, ix, iz, Sname, rt, bg, ct, mt, sg
        else:
            record['alignment_stage'] = 1001
            if record['notes'] is None:
                record['notes'] = time.strftime(
                    "%c") + ' Error finding file ' + file + ' on ' + host
            else:
                record['notes'] = record['notes'] + '\n' + time.strftime(
                    "%c") + ' Error finding file ' + file + ' on ' + host
    except Exception, e:
        print str(e)
        record['alignment_stage'] = 0
        if record['notes'] is None:
            record['notes'] = time.strftime(
                "%c") + ' Error with handling uploaded file by ' + host
        else:
            record['notes'] = record['notes'] + '\n' + time.strftime(
                "%c") + ' Error with handling uploaded file by ' + host