def load_external_hit(url="http://65.215.1.20/faces/simp_sim3.py", assignments=1, input="1", title="A nice HIT", description="Please help with our AI task", reward="0.15", duration=900, autoapproval=259200, approvalrate=95, height=500, keywords="image, similarity", numapproved=48, comment=None): ell = locals() label = get_next_label() input2 = log_root + "/" + label + ".input" question2 = log_root + "/" + label + ".question" properties2 = log_root + "/" + label + ".properties" itmpl = tools.my_read(input_template) qtmpl = tools.my_read(question_template) ptmpl = tools.my_read(properties_template) st = string.Template(itmpl).substitute(ell) tools.my_write(input2, input_shortcutifier(st)) st = string.Template(qtmpl).substitute(ell) tools.my_write(question2, st) st = string.Template(ptmpl).substitute(ell) tools.my_write(properties2, st) return load_hit_helper(label, input2, question2, properties2, comment)
def load_external_hit(url="http://65.215.1.20/faces/simp_sim3.py",assignments=1,input="1",title="A nice HIT",description="Please help with our AI task",reward="0.15",duration=900,autoapproval=259200,approvalrate=95,height=500,keywords="image, similarity",numapproved=48,comment=None): ell = locals() label = get_next_label() input2 = log_root+"/"+label+".input" question2 = log_root+"/"+label+".question" properties2 = log_root+"/"+label+".properties" itmpl=tools.my_read(input_template) qtmpl=tools.my_read(question_template) ptmpl=tools.my_read(properties_template) st = string.Template(itmpl).substitute(ell) tools.my_write(input2,input_shortcutifier(st)) st = string.Template(qtmpl).substitute(ell) tools.my_write(question2,st) st = string.Template(ptmpl).substitute(ell) tools.my_write(properties2,st) return load_hit_helper(label,input2,question2,properties2, comment)
def get_agreement(filename): s = tools.my_read(filename).strip().splitlines() trips = {} for l in s: (a, b, c, e) = l.split() a = int(a) b = int(b) c = int(c) tb = min(b, c) tc = max(b, c) if b < c: y = 1 else: y = 0 if (a, tb, tc) not in trips: trips[(a, tb, tc)] = [0, 0] trips[(a, tb, tc)][y] = trips[(a, tb, tc)][y] + 1 v = trips.values() data = [] for (a, b) in v: if (a + b > 1): data.append((a * (a - 1.) + b * (b - 1.)) / ( (a + b) * (a + b - 1.))) # double count agreement and disagreement pairs r = scipy.stats.morestats.bayes_mvs(data, 0.95)[0] print "Mean", r[0], " 95% confidence interval", r[1] print "This corresponds to a signal of ", 0.5 + np.sqrt(r[0] / 2. - 1 / 4.) return r[0]
def read_out_files(fnames): if type(fnames)!=list: fnames = [fnames] res = [] for f in fnames: res += [map(int,i.split()[:3]) for i in tools.my_read(f).strip().splitlines()] return res
def create_info(dir): if dir[-1] != "/" and dir[-1] != "\\": dir += "/" assert not os.path.exists(dir + "info.csv") ids = tools.my_read(dir + "ids.txt").strip().splitlines() n = len(ids) print n, "ids." rs = [] atts = [i for i in os.listdir(dir) if "." not in i] print "Attributes:", atts for i in range(n): r = {"id": i, "image": ids[i]} for att in atts: if att[:4] != "att_": the_att = "att_" + att else: the_att = att if os.path.exists(dir + att + "/1/" + ids[i]): r[the_att] = 1 else: if os.path.exists(dir + att + "/0/" + ids[i]): r[the_att] = 0 else: r[the_att] = -100 rs.append(r) f = open(dir + "info.csv", "wb") c = csv.DictWriter(f, sorted(rs[0].keys())) header = {} for k in rs[0].keys(): header[k] = k c.writerow(header) c.writerows(rs) f.close()
def get_agreement(filename): s = tools.my_read(filename).strip().splitlines() trips = {} for l in s: (a,b,c,e) = l.split() a = int(a) b = int(b) c = int(c) tb = min(b,c) tc = max(b,c) if b<c: y = 1 else: y = 0 if (a,tb,tc) not in trips: trips[(a,tb,tc)]=[0,0] trips[(a,tb,tc)][y]=trips[(a,tb,tc)][y]+1 v = trips.values() data = [] for (a,b) in v: if(a+b>1): data.append( (a*(a-1.)+b*(b-1.))/((a+b)*(a+b-1.))) # double count agreement and disagreement pairs r =scipy.stats.morestats.bayes_mvs(data, 0.95)[0] print "Mean",r[0], " 95% confidence interval",r[1] print "This corresponds to a signal of ", 0.5+np.sqrt(r[0]/2.-1/4.) return r[0]
def create_info(dir): if dir[-1]!="/" and dir[-1]!="\\": dir+="/" assert not os.path.exists(dir+"info.csv") ids = tools.my_read(dir+"ids.txt").strip().splitlines() n = len(ids) print n,"ids." rs = [] atts = [i for i in os.listdir(dir) if "." not in i] print "Attributes:",atts for i in range(n): r = { "id": i, "image": ids[i] } for att in atts: if att[:4]!="att_": the_att="att_"+att else: the_att = att if os.path.exists(dir+att+"/1/"+ids[i]): r[the_att] = 1 else: if os.path.exists(dir+att+"/0/"+ids[i]): r[the_att] = 0 else: r[the_att] = -100 rs.append(r) f=open(dir+"info.csv","wb") c = csv.DictWriter(f,sorted(rs[0].keys())) header = {} for k in rs[0].keys(): header[k]=k c.writerow(header) c.writerows(rs) f.close()
def out2trips(filename): lst = [] lines = tools.my_read(filename).splitlines() for l in lines: (a,b,c,d) = l.split() if a!=b and a!=c: lst.append((int(a),int(b),int(c))) else: print "dumb trip" return lst
def py_to_server(label="test", pycontents=None): if pycontents == None: pycontents = tools.my_read(exp_root + "/to_server.py") label = str(label) url = "http://65.215.1.20/faces/add_exp.py?" url += urllib.urlencode([("name", label), ("contents", pycontents)]) f = urllib2.urlopen(url) r = f.read().strip() assert r == "<html>SUCCESS</html>" return "http://65.215.1.20/faces/exps/" + label + ".py"
def out2trips(filename): lst = [] lines = tools.my_read(filename).splitlines() for l in lines: (a, b, c, d) = l.split() if a != b and a != c: lst.append((int(a), int(b), int(c))) else: print "dumb trip" return lst
def read_out_files(fnames): if type(fnames) != list: fnames = [fnames] res = [] for f in fnames: res += [ map(int, i.split()[:3]) for i in tools.my_read(f).strip().splitlines() ] return res
def py_to_server(label="test",pycontents=None): if pycontents == None: pycontents = tools.my_read(exp_root+"/to_server.py") label=str(label) url = "http://65.215.1.20/faces/add_exp.py?" url += urllib.urlencode([("name",label),("contents",pycontents)]) f=urllib2.urlopen(url) r = f.read().strip() assert r=="<html>SUCCESS</html>" return "http://65.215.1.20/faces/exps/"+label+".py"
def read_model(model_file): d = tools.my_read(model_file).strip().splitlines() assert(d[9][-20:].strip()=="port vectors plus 1") num_vectors = int(d[9].split()[0])-1 thresh = float(d[10].split()[0]) #print num_vectors, thresh alphas = np.array([[float(t.split()[0]) for t in d[11:]]]) #print len(alphas) svs = np.array([[float(t.split(":")[1]) for t in r.strip().split()[1:-2]] for r in d[11:]]) w = np.dot(alphas,svs) return w, thresh
def test_omers_fit(): print "Testing Omer's neckties" adaptive_trips = read_out_files(glob.glob("c:/sim/turkexps/neckties/small/*.out")) print len(adaptive_trips),"adaptive trips" random_trips = read_out_files(glob.glob("c:/sim/turkexps/neckties/small/random/*.out")) print len(random_trips),"random trips" control_trips = read_out_files(glob.glob("c:/sim/turkexps/neckties/small/control/*.out")) print len(control_trips),"control trips" M = np.array([[float(b) for b in a.split()] for a in tools.my_read("c:/users/adum/simexp/matlab/neckties_fit_5.txt").strip().splitlines()]) S = np.dot(M,M.T) #grad_proj2(random_trips,control_trips,step_size=5,its=2,Sinit=S) print "n=",S.shape[0] show_nn(S) show_posts(S,random_trips) doppel_trips(S,random_trips) #also loads posts from file
def test_omers_fit(): print "Testing Omer's neckties" adaptive_trips = read_out_files( glob.glob("c:/sim/turkexps/neckties/small/*.out")) print len(adaptive_trips), "adaptive trips" random_trips = read_out_files( glob.glob("c:/sim/turkexps/neckties/small/random/*.out")) print len(random_trips), "random trips" control_trips = read_out_files( glob.glob("c:/sim/turkexps/neckties/small/control/*.out")) print len(control_trips), "control trips" M = np.array([[float(b) for b in a.split()] for a in tools.my_read( "c:/users/adum/simexp/matlab/neckties_fit_5.txt").strip().splitlines() ]) S = np.dot(M, M.T) #grad_proj2(random_trips,control_trips,step_size=5,its=2,Sinit=S) print "n=", S.shape[0] show_nn(S) show_posts(S, random_trips) doppel_trips(S, random_trips) #also loads posts from file
def create_data(dir,att,info_file,fit_file,data_file): x = tools.my_read(fit_file).strip().splitlines() x = [[float(zz) for zz in z.split()] for z in x] m = len(x) d = len(x[0]) f = open(info_file,"r") csvr = csv.DictReader(f) y = [int(r[att]) for r in csvr] f.close() st = "" for i in range(m): if y[i]!=-100: if y[i]==1: st+="+1" else: #translate 0 to -1 st+="-1" for j in range(d): st+=" "+str(j+1)+":"+str(x[i][j]) st+=" # "+str(i) st+="\n" st = st.strip() tools.my_write(data_file,st) return (x,y)
#print "or" #print "checktrips.py batch_id, where batch_id is the integer id of the batch" #print "or" #print "checktrips.py outfileloc" if len(sys.argv) == 2: label = sys.argv[1].strip() else: label = str(turk.get_last_label()) if "\\" in label or "." in label or "/" in label: label = label.replace("/", "\\") count = 0 fname = "" for filename in glob.glob(turk.log_root + "/*.out_file_loc"): if tools.my_read(filename).strip().replace("/", "\\") == label: fname = filename count += 1 if count > 1: print "Found more than one .out_file_loc file pointing to", label print "Exiting..." exit(0) if count == 0: print "Couldn't find any .out_file_loc file pointing to", label print "Maybe try absolute path" exit(0) label = fname[fname.rindex("\\") + 1:-13] print label trips = tools.remove_comments( tools.my_read(turk.log_root + "/" + label + ".trips").strip().split("\n"))
if len(sys.argv)!=4 and len(sys.argv)!=3: print "Usage: runtrips.py config_file trips_file output_file" print " or" print "Usage: runtrips.py config_file trips_file" print "in which case the output file is trips_file.out" exit(1) if len(sys.argv)==3: (a,b,c) = sys.argv[2].rpartition(".") if b=="": sys.argv.append(sys.argv[2]+".out") else: sys.argv.append(a+".out") print "outfile "+sys.argv[-1] config = tools.remove_comments(tools.my_read(sys.argv[1]).split("\n")) trips = tools.remove_comments(tools.my_read(sys.argv[2]).strip().split("\n")) db = config[0] payment = config[1] assert float(payment)<0.9 #make sure we don't pay more than 90 cents/hit! height = config[2] comment = config[3] #see if line 4 has an integer try: reps = int(config[4]) instructions = string.join(config[5:],"\n") except: reps = 1 instructions = string.join(config[4:],"\n")
s+='var datab = "'+db+'";\n' def list_to_str(lst): res = "[" for i in lst: if type(i)==list: res+=list_to_str(i) else: res+=str(i) res+="," if res[-1]==",": return res[:-1] + "]" return res+"]" tree = [] inf = tools.my_read(infile).splitlines() for line in inf: (a,b) = line.split(":") tree.append([a.split(),b.split()]) s+='var tree = '+list_to_str(tree)+";\n" s+='var ids = [' ids = tools.my_read(dataroot+"/"+db+"/ids.txt").splitlines() for i in ids: s+='"'+i+'",' s=s[:-1] s+='];\n' s+= '</script></head>\n' s+= tools.my_read(htmlfile) tools.my_write(outfile,s)
def im_filename(j): global filenames if filenames == None: filenames = tools.my_read(fdir + "/ids.txt").strip().splitlines() return fdir + "\\" + filenames[j]
@author: adum """ import tools, turk, csv from numpy import * import matplotlib.pyplot as plt import Tkinter from PIL import Image, ImageTk import random #trip_file = "c:/sim/turkexps/fonts/fonts1.out" trip_file = "c:/sim/fonts10000.out" ids = [] id_locs = tools.my_read("c:/matlab/fonts/ids.txt").strip().split("\n") txt = tools.my_read(trip_file).strip().split("\n") txt2 = [l.split() for l in txt] trips = [(int(l[0]), int(l[1]), int(l[2])) for l in txt2] for t in trips: for i in t: if i not in ids: ids.append(i) trips2 = [(ids.index(a), ids.index(b), ids.index(c)) for (a, b, c) in trips] n = len(ids) print n
# -*- coding: utf-8 -*- """ Created on Wed Oct 06 08:08:39 2010 @author: adum """ import tools, numpy import random coords = tools.my_read("c:/sim/tsp/fonts.txt").strip().splitlines() M = [] for x in coords: (a,b,c)=x.split() M.append((float(a),float(b),float(c))) print len(M) def draw_fonts(f,title): res="<table width=300 border=1>" res+="<tr><td><u>"+title+"</u></td></tr>" for i in f: res+='<tr><td><font face="'+i+'">'+i+'</font></td></tr>\n' res+="</table>\n" return res def font_dist(i,j): # if i==5: # return 0
def list_to_str(lst): res = "[" for i in lst: if type(i) == list: res += list_to_str(i) else: res += str(i) res += "," if res[-1] == ",": return res[:-1] + "]" return res + "]" tree = [] inf = tools.my_read(infile).splitlines() for line in inf: (a, b) = line.split(":") tree.append([a.split(), b.split()]) s += 'var tree = ' + list_to_str(tree) + ";\n" s += 'var ids = [' ids = tools.my_read(dataroot + "/" + db + "/ids.txt").splitlines() for i in ids: s += '"' + i + '",' s = s[:-1] s += '];\n' s += '</script></head>\n' s += tools.my_read(htmlfile) tools.my_write(outfile, s)
def im_filename(j): global filenames if filenames==None: filenames = tools.my_read(fdir+"/ids.txt").strip().splitlines() return fdir+"\\"+filenames[j]
] def remove_comments(s): if type(s) == list: return [remove_comments(i) for i in s] if s.find("#") != -1: return s[:s.index("#")].strip() return s.strip() if len(sys.argv) != 4: print "Usage: runtrips.py config_file trips_file output_file" exit(1) config = remove_comments(tools.my_read(sys.argv[1]).split("\n")) trips = remove_comments(tools.my_read(sys.argv[2]).strip().split("\n")) db = config[0] payment = config[1] assert float(payment) < 0.9 #make sure we don't pay more than 90 cents/hit! height = config[2] comment = config[3] instructions = string.join(config[4:], "\n") if "%s" not in instructions: print "Instructions should have %s in them!" inst_shortcut = turk.shortcut_to_server(instructions) trips = [i.split() for i in trips] #print trips
@author: adum """ import tools, turk, csv from numpy import * import matplotlib.pyplot as plt import Tkinter from PIL import Image, ImageTk import random #trip_file = "c:/sim/turkexps/fonts/fonts1.out" trip_file = "c:/sim/fonts10000.out" ids = [] id_locs = tools.my_read("c:/matlab/fonts/ids.txt").strip().split("\n") txt = tools.my_read(trip_file).strip().split("\n") txt2 = [l.split() for l in txt] trips = [(int(l[0]),int(l[1]),int(l[2])) for l in txt2] for t in trips: for i in t: if i not in ids: ids.append(i) trips2 = [ (ids.index(a),ids.index(b),ids.index(c)) for (a,b,c) in trips] n = len(ids) print n
#print "or" #print "checktrips.py batch_id, where batch_id is the integer id of the batch" if len(sys.argv)==2: label = sys.argv[1].strip() else: label = str(turk.get_last_label()) if "\\" in label or "." in label or "/" in label: label=label.replace("/","\\") count = 0 fname = "" for filename in glob.glob(turk.log_root+"/*.out_file_loc"): if tools.my_read(filename).strip().replace("/","\\")==label: fname = filename count += 1 if count>1: print "Found more than one .out_file_loc file pointing to",label print "Exiting..." exit(0) if count==0: print "Couldn't find any .out_file_loc file pointing to",label print "Maybe try absolute path" exit(0) label = fname[fname.rindex("\\")+1:-13] print label trips = tools.remove_comments(tools.my_read(turk.log_root+"/"+label+".trips").strip().split("\n"))