def create_ecc_image_collection(depends_on = ROOT + 'images/', creates = get_cert_paths('images',None,None)): conn = connect_to_db() colroot = get_col_root(steptag = 'images') colfiles = colroot + '.files' colchunks = colroot + '.chunks' db = conn[DATA_DB_NAME] if colfiles in db.collection_names(): db.drop_collection(colfiles) db.drop_collection(colchunks) fs = gridfs.GridFS(db,collection = colroot) p = os.path.join(depends_on,'faces','caricature') L = filter(lambda x : x.endswith('.png'), os.listdir(p)) for l in L: contents = open(os.path.join(p,l)).read() metadata = {'filename' : l, 'type': 'face', 'mode' : 'cartoon','subject':'human'} fs.put(contents,**metadata) p = os.path.join(depends_on,'faces','cartoon') L = filter(lambda x : x.endswith('.png'), os.listdir(p)) for l in L: contents = open(os.path.join(p,l)).read() metadata = {'filename' : l, 'type': 'face', 'mode' : 'cartoon','subject':'monkey'} fs.put(contents,**metadata) p = os.path.join(depends_on,'faces','human') L = filter(lambda x : x.endswith('.png'), os.listdir(p)) for l in L: contents = open(os.path.join(p,l)).read() metadata = {'filename' : l, 'type': 'face', 'mode' : 'photo','subject':'human'} fs.put(contents,**metadata) p = os.path.join(depends_on,'faces','monkey') L = filter(lambda x : x.endswith('.png'), os.listdir(p)) for l in L: contents = open(os.path.join(p,l)).read() metadata = {'filename' : l, 'type': 'face', 'mode' : 'photo','subject':'monkey'} fs.put(contents,**metadata) attdict = {} attdict['bodies'] = {'mode':'photo','subject':'body'} attdict['cars'] = {'mode':'photo','subject':'car'} attdict['cats'] = {'mode':'photo','subject':'cat'} attdict['cubes'] = {'mode':'generated','subject':'cube'} attdict['familar'] = {'mode':'photo','subject':'familiar'} attdict['round'] = {'mode':'photo','subject':'round'} attdict['smooths'] = {'mode':'generated','subject':'smooth'} attdict['spikes'] = {'mode':'generated','subject':'spike'} p = os.path.join(depends_on,'objects') L = filter(lambda x : os.path.isdir(os.path.join(p,x)),os.listdir(p)) for l in L: p1 = os.path.join(p,l) L1 = filter(lambda x : x.endswith('.png'), os.listdir(p1)) for l1 in L1: contents = open(os.path.join(p1,l1)).read() metadata = {'filename' : l1, 'type': 'object', 'mode':attdict[l]['mode'] , 'subject' : attdict[l]['subject']} fs.put(contents,**metadata) createCertificateDict(creates[0],{'root':colroot,'steptag':'images'})
def db_computation_handler(insteptags,func,inconfig_path,outconfig_path,filebuncher = None, args = None, kwargs = None,inquery = None, outquery = None): if filebuncher is None: filebuncher = lambda X : [[x] for x in X] conn = connect_to_db() db = conn[DATA_DB_NAME] if is_string_like(insteptags): insteptags = [insteptags] incertpaths = get_cert_paths(insteptags,inconfig_path,inquery) incertdicts = [cPickle.load(open(incertpath)) for incertpath in incertpaths] incolroots = [incertdict['root'] for incertdict in incertdicts] incolnames = [incolroot + '.files' for incolroot in incolroots] if inquery is None: inquery = {} if outquery is None: outquery = {} assert inquery <= outquery for incolname in incolnames: if incolname not in db.collection_names(): raise NoInputCollectionError(incolname) incols = [db[incolname] for incolname in incolnames] inrecs = zip(*[list(incol.find(inquery)) for incol in incols]) inrecs = [zip(insteptags,rec) for rec in inrecs] outsteptags = get_tags(func,args,kwargs) outcolroots = [get_col_root(steptag = steptag,config_path = outconfig_path,conn = conn) for steptag in outsteptags] recgroups = filebuncher(inrecs) in_fs = dict([(intag,gridfs.GridFS(db,collection = incolroot)) for (intag,incolroot) in zip(insteptags,incolroots)]) out_fs = dict([(outtag,gridfs.GridFS(db,collection = outcolroot)) for (outtag,outcolroot) in zip(outsteptags,outcolroots)]) if kwargs is None: kwargs = {} if args is None: args = () for recs in recgroups: in_fhs = [[(r[0],in_fs[r[0]].get(r[1]['_id'])) for r in rec] for rec in recs] results = func(in_fhs,outconfig_path,*args,**kwargs) if is_string_like(results): results = (results,) if isinstance(results,tuple): results = [results] assert len(results) == len(recs), 'something wrong in your function' assert all([len(result) == len(outsteptags) for result in results]) for (rec,result) in zip(recs,results): for (outtag,res) in zip(outsteptags,result): print(rec[0][1]['_id']) out_fs[outtag].delete(rec[0][1]['_id']) out_fs[outtag].put(res,**rec[0][1]) outcertpaths = get_cert_paths(outsteptags,outconfig_path,outquery) for (outcertpath,outsteptag,outcolroot) in zip(outcertpaths,outsteptags,outcolroots): createCertificateDict(outcertpath,{'config_path': outconfig_path, 'steptag': outsteptag, 'root' : outcolroot,'query':outquery})