def align(name, template=template, bgfile='image_Ch1.nrrd', alignSet='', passLevel=0.6): cur.execute("SELECT * FROM images_alignment WHERE alignment_stage = 5 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 = 5 WHERE last_host = %s AND alignment_stage = 1005", [str(host)]) cur.connection.commit() # remove image from stack before processing: cur.execute("UPDATE images_alignment SET alignment_stage = 1005, last_host = %s WHERE id = %s ", [str(host), str(record['id'])]) cur.connection.commit() record = alignRec(record, template, bgfile, alignSet, passLevel) 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() cur.execute("SELECT * FROM images_alignment WHERE alignment_stage = 6 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 = 6 WHERE last_host = %s AND alignment_stage = 1006", [str(host)]) cur.connection.commit() # remove image from stack before processing: cur.execute("UPDATE images_alignment SET alignment_stage = 1006, last_host = %s WHERE id = %s ", [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()
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()
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")
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()
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()
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()
) 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"
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'