Exemplo n.º 1
0
def query_settings(question, default=1):
    """Ask a question via raw_input() and return an int.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be an int number (the default) or None (meaning
        an answer is required of the user).

    The "answer" return value is an int.
    """
    cur.execute('SELECT id, name FROM system_setting')
    records = cur.fetchall()
    ids = []
    for record in records:
      print str(record[0]) + ' - ' + str(record[1])
      ids.append(int(record[0]))
    valid = ids
    prompt = str(ids)
    if not default == None:
      prompt = prompt + ' default=' + str(default)

    while True:
        sys.stdout.write(question + '\n' + prompt + '\n')
        choice = raw_input()
        if default is not None and choice == '':
            if default in valid:
              return int(default)
        elif int(choice) in valid:
            return int(choice)
        else:
            sys.stdout.write("Please respond with the number of the reqired settings " + str(ids) + "\n")
Exemplo n.º 2
0
def initial(name, template=template, init_threshold=0.3, bgfile='image_Ch1.nrrd', alignSet='',
            initialSet='--principal-axes'):
    cur.execute("SELECT * FROM images_alignment WHERE alignment_stage = 2 AND name like %s", [name])
    records = cur.fetchall()
    key = []
    for desc in cur.description:
        key.append(desc[0])
    for line in records:
        record = dict(zip(key, line))
        # clear old failed alignments:
        cur.execute("UPDATE images_alignment SET alignment_stage = 2 WHERE last_host = %s AND alignment_stage = 1002",
                    [str(host)])
        cur.connection.commit()
        # remove image from stack before processing:
        cur.execute("UPDATE images_alignment SET alignment_stage = 1002, last_host = %s WHERE id = %s ",
                    [str(host), str(record['id'])])
        cur.connection.commit()
        record = initialRec(record, template, init_threshold, bgfile, alignSet, initialSet)
        u = str(record['id']) + ' -> '
        for k, v in record.items():
            if not (k == 'id' or v == None or v == 'None'):
                cur.execute("UPDATE images_alignment SET " + str(k) + "=%s WHERE id = %s ", [v, record['id']])
                u = u + str(k) + '=' + str(v) + ', '
        print u
        cur.connection.commit()
        gc.collect()
Exemplo n.º 3
0
def warp(name, template=template, bgfile='image_Ch1.nrrd',
         warpSet='--grid-spacing 80 --exploration 30 --coarsest 4 --accuracy 0.2 --refine 4 --energy-weight 1e-1'):
    cur.execute("SELECT * FROM images_alignment WHERE alignment_stage = 4 AND name like %s", [name])
    records = cur.fetchall()
    key = []
    for desc in cur.description:
        key.append(desc[0])
    for line in records:
        record = dict(zip(key, line))
        # clear old failed alignments:
        cur.execute("UPDATE images_alignment SET alignment_stage = 4 WHERE last_host = %s AND alignment_stage = 1004",
                    [str(host)])
        cur.connection.commit()
        # remove image from stack before processing:
        cur.execute("UPDATE images_alignment SET alignment_stage = 1004, last_host = %s WHERE id = %s ",
                    [str(host), str(record['id'])])
        cur.connection.commit()
        record = warpRec(record, template, bgfile, warpSet)
        u = str(record['id']) + ' -> '
        for k, v in record.items():
            if not (k == 'id' or v == None or v == 'None'):
                cur.execute("UPDATE images_alignment SET " + str(k) + "=%s WHERE id = %s ", [v, record['id']])
                u = u + str(k) + '=' + str(v) + ', '
        print u
        cur.connection.commit()
        gc.collect()
Exemplo n.º 4
0
def affine(name, template=template, bgfile="image_Ch1.nrrd", affineSet="--dofs 6,9 --auto-multi-levels 4"):
    cur.execute("SELECT * FROM images_alignment WHERE alignment_stage = 3 AND name like %s", [name])
    records = cur.fetchall()
    key = []
    for desc in cur.description:
        key.append(desc[0])
    for line in records:
        record = dict(zip(key, line))
        # clear old failed alignments:
        cur.execute(
            "UPDATE images_alignment SET alignment_stage = 3 WHERE last_host = %s AND alignment_stage = 1003",
            [str(host)],
        )
        cur.connection.commit()
        # remove image from stack before processing:
        cur.execute(
            "UPDATE images_alignment SET alignment_stage = 1003, last_host = %s WHERE id = %s ",
            [str(host), str(record["id"])],
        )
        cur.connection.commit()
        record = affineRec(record, template, bgfile, affineSet)
        u = str(record["id"]) + " -> "
        for k, v in record.items():
            if not (k == "id" or v == None or v == "None"):
                cur.execute("UPDATE images_alignment SET " + str(k) + "=%s WHERE id = %s ", [v, record["id"]])
                u = u + str(k) + "=" + str(v) + ", "
        print u
        cur.connection.commit()
        gc.collect()
Exemplo n.º 5
0
def merge(name):
    cur.execute("SELECT * FROM images_alignment WHERE alignment_stage = 10 AND name like %s", [name])
    records = cur.fetchall()
    key = []
    for desc in cur.description:
        key.append(desc[0])
    for line in records:
        record = dict(zip(key, line))
        cur.execute(
            "UPDATE images_alignment SET alignment_stage = 1010, last_host = %s WHERE id = %s ",
            [str(host), str(record["id"])],
        )
        cur.connection.commit()
        r = mergeRec(record)
        u = ""
        for k, v in record.items():
            if not (k == "id" or v == None or v == "None"):
                if not u == "":
                    u = u + ", "
                if type(v) == type(""):
                    u = u + str(k) + " = '" + str(v) + "'"
                else:
                    u = u + str(k) + " = " + str(v)
        print u
        cur.execute("UPDATE images_alignment SET " + u + " WHERE id = %s ", [str(record["id"])])
        cur.connection.commit()
Exemplo n.º 6
0
def convert(name):
    cur.execute("SELECT * FROM images_alignment WHERE alignment_stage = 1 AND name like %s", [name])
    records = cur.fetchall()
    key = []
    for desc in cur.description:
        key.append(desc[0])
    for line in records:
        record = dict(zip(key, line))
        # remove image from stack before processing:
        cur.execute("UPDATE images_alignment SET alignment_stage = 1001, last_host = %s WHERE id = %s ",
                    [str(host), str(record['id'])])
        cur.connection.commit()
        r = convRec(record)
        u = str(record['id']) + ' -> '
        for k, v in record.items():
            if not (k == 'id' or v == None or v == 'None'):
                cur.execute("UPDATE images_alignment SET " + str(k) + "=%s WHERE id = %s ", [v, record['id']])
                u = u + str(k) + '=' + str(v) + ', '
        print u
        cur.connection.commit()
        gc.collect()
Exemplo n.º 7
0
        )
        cur.connection.commit()
        r = mergeRec(record)
        u = ""
        for k, v in record.items():
            if not (k == "id" or v == None or v == "None"):
                if not u == "":
                    u = u + ", "
                if type(v) == type(""):
                    u = u + str(k) + " = '" + str(v) + "'"
                else:
                    u = u + str(k) + " = " + str(v)
        print u
        cur.execute("UPDATE images_alignment SET " + u + " WHERE id = %s ", [str(record["id"])])
        cur.connection.commit()


if __name__ == "__main__":
    if active and "10" in run_stage:
        cur.execute("SELECT name FROM images_alignment WHERE alignment_stage = 10 ORDER BY id")
        records = cur.fetchall()
        total = len(records)
        count = 0
        for line in records:
            count += 1
            print "Converting: " + str(count) + " of " + str(total)
            merge(line[0])
        print "done"
    else:
        print "inactive or stage 10 not selected"
Exemplo n.º 8
0
                    [str(host), str(record['id'])])
        cur.connection.commit()
        record = alignRem(record, template, bgfile, alignSet)
        u = str(record['id']) + ' -> '
        for k, v in record.items():
            if not (k == 'id' or v == None or v == 'None'):
                cur.execute("UPDATE images_alignment SET " + str(k) + "=%s WHERE id = %s ", [v, record['id']])
                u = u + str(k) + '=' + str(v) + ', '
        print u
        cur.connection.commit()
        gc.collect()


if __name__ == "__main__":
    if active and '5' in run_stage:
        cur.execute(
            "SELECT images_alignment.name, system_template.file, images_original_nrrd.file, system_setting.cmtk_align_var, system_setting.final_pass_level FROM images_alignment, system_template, system_setting, images_original_nrrd WHERE alignment_stage = 5 AND images_original_nrrd.channel = images_alignment.background_channel AND images_original_nrrd.image_id = images_alignment.id AND images_alignment.settings_id = system_setting.id AND system_setting.template_id = system_template.id ORDER BY images_alignment.id")
        records = cur.fetchall()
        total = len(records)
        if total == 0:
            cur.execute(
                "UPDATE images_alignment SET alignment_stage = 5 FROM (SELECT id FROM images_alignment WHERE alignment_stage = 2005 ORDER BY id LIMIT 2) s WHERE s.id = images_alignment.id")
            cur.connection.commit()
            gc.collect()
        count = 0
        print records
        for line in records:
            count += 1
            print 'Warp alignment: ' + str(count) + ' of ' + str(total)
            align(line[0], template=(templatedir + line[1]), bgfile=(tempfolder + line[2]), alignSet=line[3],
                  passLevel=line[4])
        print 'done'
Exemplo n.º 9
0
import os, sys, nrrd, cmtk, gc, stat, shutil
import numpy as np
import warpScoring.slicescore as slicescore
import warpScoring.CheckImages as ci
from cmtk import cur, tempfolder, active, run_stage, cmtkdir, template, checkDir, host, templatedir
from NRRDtools.labelObjects import labelObj, cutObj, cropObj


if __name__ == "__main__":
  if active and '0' in run_stage:
    cur.execute("SELECT images_mask_original.id, images_mask_original.intensity_threshold, images_mask_original.min_object_size, images_original_nrrd.file FROM images_mask_original, images_original_nrrd WHERE images_original_nrrd.id = images_mask_original.image_id AND images_mask_original.complete = False ORDER BY images_mask_original.id")
    records = cur.fetchall()
    total = len(records)
    count = 0
    print records
    for line in records:
      count +=1
      print 'Create original image mask: ' + str(count) + ' of ' + str(total)
      outfile = str(line[3]).replace('.nrrd','-objMask.nrrd').replace('.nrrd', str(line[0]) + '.nrrd')
      modfile = str(line[3]).replace('.nrrd','-modFile.nrrd').replace('.nrrd', str(line[0]) + '.nrrd')
      if not os.path.isfile(tempfolder + modfile):
          shutil.copyfile(tempfolder + str(line[3]), tempfolder + modfile)
      objs = labelObj(tempfolder + str(line[3]), tempfolder + outfile, t=line[1], ms=line[2])
      cur.execute("UPDATE images_mask_original SET complete=True, cut_complete=False, crop_complete=False, detected_objects=%s WHERE id = %s ", [objs.tolist(), str(line[0])])
      cur.connection.commit()
      gc.collect()
      try:
        os.chmod(tempfolder + outfile, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
      except:
        pass
    print 'done'
Exemplo n.º 10
0
def loadFile(file, folder, settings_id=1, orientation=comp_orien['LPS'], overwrite=False):
  r=0
  if os.path.exists(file) and ('.tif' in file or '.lsm' in file):
    name = str(os.path.splitext(os.path.basename(file))[0])
    cur.execute("SELECT count(*) FROM images_alignment WHERE name like %s", [name])
    r = int(cur.fetchone()[0])
    if r < 1:
      cur.execute("INSERT INTO images_alignment (settings_id, name, max_stage, alignment_stage, last_host, loading_host, original_ext, original_path, orig_orientation) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", [str(settings_id), name, str(0), str(1), host, host, str(os.path.splitext(os.path.basename(file))[1]), str(folder), orientation])
      cur.connection.commit()
    else:
      cur.execute("SELECT * FROM images_alignment WHERE name like '" + str(os.path.splitext(os.path.basename(file))[0]) + "'")
      print cur.fetchone()
      if overwrite:
        cur.execute("UPDATE images_alignment SET orig_orientation = %s, settings_id = %s, max_stage = 0, alignment_stage = 1, last_host = %s, loading_host = %s, original_ext = %s, original_path = %s WHERE name = %s", [orientation, str(settings_id), host, host, str(os.path.splitext(os.path.basename(file))[1]), str(folder), name])
        cur.connection.commit()
      else:
        if query_yes_no("Image already exists in processing stack do you want to update original file details?"):
          cur.execute("UPDATE images_alignment SET orig_orientation = %s, settings_id = %s, max_stage = 0, alignment_stage = 1, last_host = %s, loading_host = %s, original_ext = %s, original_path = %s WHERE name = %s", [orientation, str(settings_id), host, host, str(os.path.splitext(os.path.basename(file))[1]), str(folder), name])
          cur.connection.commit()
    cur.execute("SELECT count(*) FROM images_alignment WHERE name like %s", [name])
    r = int(cur.fetchone()[0])
  gc.collect()
  if r > 0:
    return True
  else:
    return False
Exemplo n.º 11
0
                    [str(host), str(record['id'])])
        cur.connection.commit()
        record = warpRec(record, template, bgfile, warpSet)
        u = str(record['id']) + ' -> '
        for k, v in record.items():
            if not (k == 'id' or v == None or v == 'None'):
                cur.execute("UPDATE images_alignment SET " + str(k) + "=%s WHERE id = %s ", [v, record['id']])
                u = u + str(k) + '=' + str(v) + ', '
        print u
        cur.connection.commit()
        gc.collect()


if __name__ == "__main__":
    if active and '4' in run_stage:
        cur.execute(
            "SELECT images_alignment.name, system_template.file, images_original_nrrd.file, system_setting.cmtk_warp_var FROM images_alignment, system_template, system_setting, images_original_nrrd WHERE alignment_stage = 4 AND images_original_nrrd.channel = images_alignment.background_channel AND images_original_nrrd.image_id = images_alignment.id AND images_alignment.settings_id = system_setting.id AND system_setting.template_id = system_template.id ORDER BY images_alignment.id")
        records = cur.fetchall()
        total = len(records)
        if total == 0:
            cur.execute(
                "UPDATE images_alignment SET alignment_stage = 4 FROM (SELECT id FROM images_alignment WHERE alignment_stage = 2004 ORDER BY id LIMIT 2) s WHERE s.id = images_alignment.id")
            cur.connection.commit()
            gc.collect()
        count = 0
        print records
        for line in records:
            count += 1
            print 'Warp alignment: ' + str(count) + ' of ' + str(total)
            warp(line[0], template=(templatedir + line[1]), bgfile=(tempfolder + line[2]), warpSet=line[3])
        # clear old failed alignments:
        cur.execute("UPDATE images_alignment SET alignment_stage = 4 WHERE last_host = %s AND alignment_stage = 1004",
Exemplo n.º 12
0
def pushLive(id, name, sgfile):
    if os.path.exists(sgfile):
        if (id > 9999):
            first = str(id)[:-4]
            last = str(id)[-4:]
        else:
            first = "000"
            last = str(id)
        while len(first) < 3:
            first = "0" + first
        while len(last) < 4:
            last = "0" + last
        first = "a" + first
        if not os.path.exists(tempfolder + "../../IMAGE_DATA/VFB/i/" + first):
            os.makedirs(tempfolder + "../../IMAGE_DATA/VFB/i/" + first)
        if not os.path.exists(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last):
            os.makedirs(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last)
        print 'Linking ' + sgfile + ' to ' + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd"
        if os.path.exists(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd"):
            os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd")
        shutil.copyfile(sgfile, tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd")
        data, head = nrrd.read(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd")
        if head['sizes'][2] == 185:
            print "Inflating from " + str(head['sizes'][2]) + " slices to 270..."
            dataNew = np.zeros([head['sizes'][0],head['sizes'][1],270],dtype=np.uint8)
            dataNew[:,:,25:210]=data;
            head['encoding'] = 'gzip'
            if head.has_key('space direction'):
                head.pop("space directions", None)
            data = dataNew
        im = np.transpose(np.max(data,axis=2))
        if (data[0][0][0] < 1):
            data[0][0][0] = np.uint8(100)
        filesize = np.subtract(data.shape, 1)
        if (data[filesize[0]][filesize[1]][filesize[2]] < 1):
            data[filesize[0]][filesize[1]][filesize[2]] = np.uint8(100)
        nrrd.write(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd", data, options=head)
        print "Creating Thumbnail"
        im = Image.fromarray(im)
        if np.shape(im)[0] < np.shape(im)[1]:
            im = im.resize((120,60), Image.ANTIALIAS)
        elif np.shape(im)[0] > np.shape(im)[1]:
            im = im.resize((60,120), Image.ANTIALIAS)
        else:
            im = im.resize((60,60), Image.ANTIALIAS)
        im.save(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/thumbnail.png","PNG")
        print "Converting to Tiff"
        subprocess.call(Fiji + " -macro nrrd2tif.ijm " + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.nrrd" + " -batch", shell=True)
        
        if os.path.isfile(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz"):
            os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz")
            print "Clearing old woolz data..."
        
        print "Creating wlz: " + "/VFB/i/" + first + "/" + last + "/volume.wlz"
        print "Using voxel sizes: Z:" + str(head['space directions'][2][2]) + ", X:" + str(head['space directions'][0][0]) + ", Y:" + str(head['space directions'][1][1])
        
        subprocess.call("nice " + wlzDir + "WlzExtFFConvert -f tif -F wlz " + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.tif |" + wlzDir + "WlzThreshold -v50 |" + wlzDir + "WlzSetVoxelSize -z" + str(head['space directions'][2][2]) + " -x" + str(head['space directions'][0][0]) + " -y" + str(head['space directions'][1][1]) + " >" + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz" , shell=True)
        
        cur.execute("UPDATE images_alignment SET alignment_stage = 21, notes = %s WHERE id = %s", [str("VFB_"+first+last),str(id)])
        cur.connection.commit()
        print "Clearing up..."
        os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.tif")
    else:
        print "Skipping " + name
    gc.collect()
Exemplo n.º 13
0
        if os.path.isfile(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz"):
            os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz")
            print "Clearing old woolz data..."
        
        print "Creating wlz: " + "/VFB/i/" + first + "/" + last + "/volume.wlz"
        print "Using voxel sizes: Z:" + str(head['space directions'][2][2]) + ", X:" + str(head['space directions'][0][0]) + ", Y:" + str(head['space directions'][1][1])
        
        subprocess.call("nice " + wlzDir + "WlzExtFFConvert -f tif -F wlz " + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.tif |" + wlzDir + "WlzThreshold -v50 |" + wlzDir + "WlzSetVoxelSize -z" + str(head['space directions'][2][2]) + " -x" + str(head['space directions'][0][0]) + " -y" + str(head['space directions'][1][1]) + " >" + tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.wlz" , shell=True)
        
        cur.execute("UPDATE images_alignment SET alignment_stage = 21, notes = %s WHERE id = %s", [str("VFB_"+first+last),str(id)])
        cur.connection.commit()
        print "Clearing up..."
        os.remove(tempfolder + "../../IMAGE_DATA/VFB/i/" + first + "/" + last + "/volume.tif")
    else:
        print "Skipping " + name
    gc.collect()

if __name__ == "__main__":
  if active and '5' in run_stage:
    cur.execute("SELECT id, name, aligned_sg FROM images_alignment WHERE alignment_stage = 11 ORDER BY images_alignment.id")
    records = cur.fetchall()
    for line in records:
        try:
            pushLive(line[0], line[1], sgfile=(tempfolder + line[2]))
        except:
          cur.execute("UPDATE images_alignment SET alignment_stage = 21, notes = 'FAILED' WHERE id = %s", [str(line[0])])
          cur.connection.commit()
    print 'done'
  else:
    print 'inactive or stage 4 not selected'
Exemplo n.º 14
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
Exemplo n.º 15
0
                    [str(host), str(record['id'])])
        cur.connection.commit()
        r = convRec(record)
        u = str(record['id']) + ' -> '
        for k, v in record.items():
            if not (k == 'id' or v == None or v == 'None'):
                cur.execute("UPDATE images_alignment SET " + str(k) + "=%s WHERE id = %s ", [v, record['id']])
                u = u + str(k) + '=' + str(v) + ', '
        print u
        cur.connection.commit()
        gc.collect()


if __name__ == "__main__":
    if active and '1' in run_stage:
        cur.execute("SELECT name FROM images_alignment WHERE alignment_stage = 1 ORDER BY id")
        records = cur.fetchall()
        total = len(records)
        if total == 0:
            # add any paused records to the active list
            cur.execute(
                "UPDATE images_alignment SET alignment_stage = 1 FROM (SELECT id FROM images_alignment WHERE alignment_stage = 2001 ORDER BY id LIMIT 2) s WHERE s.id = images_alignment.id")
            # clear old failed alignments:
            cur.execute(
                "UPDATE images_alignment SET alignment_stage = 1 WHERE last_host = %s AND alignment_stage = 1001",
                [str(host)])
            cur.connection.commit()
            gc.collect()
        count = 0
        for line in records:
            count += 1