def executeamd(): response = {'status':'success','data':{}} amd = request.json wspecs = amd['input']['weights'] wobj = amdparser.generateW(wspecs['uri'], wspecs['type'], uploaddir=UPLOAD_FOLDER) attribute = amd['input']['attribute'] y = amdparser.gety(attribute, uploaddir=UPLOAD_FOLDER) kwargs = amd['parameters'] args = [y, wobj] #A hack to handle mismatches between the AMD keywords and the PySAL keywords for k, v in kwargs.iteritems(): if k == 'transform': kwargs['transformation'] = v kwargs.pop(k, None) callpath, call = amdparser.parse_analysis(funcs, amd['analysis_type']) #Setup the call, the args and the kwargs call = pmdwrapper(call) for k, v in kwargs.iteritems(): try: kwargs[k] = ast.literal_eval(v) except: pass funcreturn = call(*args, **kwargs) funcreturn = recursedict(vars(funcreturn)) response['data'] = funcreturn return jsonify(response)
def executeamd(): response = {'status': 'success', 'data': {}} amd = request.json wspecs = amd['input']['weights'] wobj = amdparser.generateW(wspecs['uri'], wspecs['type'], uploaddir=UPLOAD_FOLDER) attribute = amd['input']['attribute'] y = amdparser.gety(attribute, uploaddir=UPLOAD_FOLDER) kwargs = amd['parameters'] args = [y, wobj] #A hack to handle mismatches between the AMD keywords and the PySAL keywords for k, v in kwargs.iteritems(): if k == 'transform': kwargs['transformation'] = v kwargs.pop(k, None) callpath, call = amdparser.parse_analysis(funcs, amd['analysis_type']) #Setup the call, the args and the kwargs call = pmdwrapper(call) for k, v in kwargs.iteritems(): try: kwargs[k] = ast.literal_eval(v) except: pass funcreturn = call(*args, **kwargs) funcreturn = recursedict(vars(funcreturn)) response['data'] = funcreturn return jsonify(response)
def post(module, method, module2=None): """ To make a POST using CURL to the flask dev server: Fisher-Jenks using the Hartigan Olympic time example curl -i -H "Content-Type: application/json" -X POST -d '{"args":["[12, 10.8, 11, 10.8, 10.8, 10.6, 10.8, 10.3, 10.3,10.3,10.4,10.5,10.2,10.0,9.9]"], "kwargs":{"k":5}}' http://localhost:5000/ap/esda/fisher_jenks/ or Sample Jenks Caspall using the same example - note that sample percentage is not passed. curl -i -H "Content-Type: application/json" -X POST -d '{"args":["[12, 10.8, 11, 10.8, 10.8, 10.6, 10.8, 10.3, 10.3,10.3,10.4,10.5,10.2,10.0,9.9]"], "kwargs":{"k":5}}' http://localhost:5000/ai/esda/jenks_caspall_sampled/ or Using the CherryPy server on port 8080 Queen from shapefile - NOTE: The file must be uploaded already curl -i -H "Content-Type: application/json" -X POST -d '{"args":[NAT.shp]}' http://localhost:8080/api/weights/queen_from_shapefile/ """ if not request.json: response = {'status': 'error', 'data': {}} response['data'] = 'Post datatype was not json' return jsonify(response), 400 else: response = {'status': 'success', 'data': {}} #Setup the call, the args and the kwargs if module2 == None: call = pmdwrapper(funcs[module][method]) else: call = pmdwrapper(funcs[module][module2][method]) #Parse the args keys = request.json.keys() req = request.json #Setup the python arg / kwarg containers args = [] kwargs = {} if 'args' in keys: for a in req['args']: try: args.append(ast.literal_eval(a)) except: args.append(a) if 'kwargs' in keys: for k, v in req['kwargs'].iteritems(): try: kwargs[k] = ast.literal_eval(v) except: kwargs[k] = v # or if they are uploaded shapefiles for i, a in enumerate(args): try: if a in UPLOADED_FILES: args[i] = UPLOAD_FOLDER + '/' + a shpname = a.split('.')[0] except: pass try: if a.split('_')[0] == 'cached': cacheid = a.split('_')[1] query = "SELECT Obj FROM WObj WHERE ID = {}".format( cacheid) cur = get_db().cursor().execute(query) result = cur.fetchone()[0] obj = cPickle.loads(str(result)) args[i] = obj cur.close() except: pass for k, v in kwargs.iteritems(): try: if v in UPLOADED_FILES: kwargs[k] = os.path.join(UPLOAD_FOLDER, v) except: pass try: if v.split('_')[0] == 'cached': cacheid = v.split('_')[1] query = "SELECT Obj FROM WObj WHERE ID = {}".format( cacheid) cur = get_db().cursor().execute(query) result = cur.fetchone()[0] obj = cPickle.loads(str(result)) kwargs[k] = obj cur.close() except: pass #This is a hack until I get the vector/list checking going on if module == 'esda': args[0] = np.array(args[0]) #Make the call and get the return items funcreturn = call(*args, **kwargs) #Write the W Object to the database if isinstance(funcreturn, ps.W): pdata = cPickle.dumps(funcreturn, cPickle.HIGHEST_PROTOCOL) cur = get_db().cursor() if method == 'queen_from_shapefile': m = 'Queen' else: m = 'Rook' indb = False #Query the db to see if the name / type is in the db query = "SELECT Type, Shapefile FROM WObj" cur = get_db().cursor().execute(query) result = cur.fetchall() for r in result: if r[0] == m and r[1] == shpname: indb = True break if indb == False: obj = (m, sqlite3.Binary(pdata), funcreturn._shpName) cur.execute("INSERT INTO WObj values (NULL, ?, ?, ?)", obj) get_db().commit() cur.close() response['data'] = { 'Shapefile': funcreturn._shpName, 'Weight Type': method } else: funcreturn = recursedict(vars(funcreturn)) response['data'] = funcreturn return jsonify(response)
def post(module,method, module2=None): """ To make a POST using CURL to the flask dev server: Fisher-Jenks using the Hartigan Olympic time example curl -i -H "Content-Type: application/json" -X POST -d '{"args":["[12, 10.8, 11, 10.8, 10.8, 10.6, 10.8, 10.3, 10.3,10.3,10.4,10.5,10.2,10.0,9.9]"], "kwargs":{"k":5}}' http://localhost:5000/ap/esda/fisher_jenks/ or Sample Jenks Caspall using the same example - note that sample percentage is not passed. curl -i -H "Content-Type: application/json" -X POST -d '{"args":["[12, 10.8, 11, 10.8, 10.8, 10.6, 10.8, 10.3, 10.3,10.3,10.4,10.5,10.2,10.0,9.9]"], "kwargs":{"k":5}}' http://localhost:5000/ai/esda/jenks_caspall_sampled/ or Using the CherryPy server on port 8080 Queen from shapefile - NOTE: The file must be uploaded already curl -i -H "Content-Type: application/json" -X POST -d '{"args":[NAT.shp]}' http://localhost:8080/api/weights/queen_from_shapefile/ """ if not request.json: response = {'status':'error','data':{}} response['data'] = 'Post datatype was not json' return jsonify(response), 400 else: response = {'status':'success','data':{}} #Setup the call, the args and the kwargs if module2 == None: call = pmdwrapper(funcs[module][method]) else: call = pmdwrapper(funcs[module][module2][method]) #Parse the args keys = request.json.keys() req = request.json #Setup the python arg / kwarg containers args = [] kwargs = {} if 'args' in keys: for a in req['args']: try: args.append(ast.literal_eval(a)) except: args.append(a) if 'kwargs' in keys: for k, v in req['kwargs'].iteritems(): try: kwargs[k] = ast.literal_eval(v) except: kwargs[k] = v # or if they are uploaded shapefiles for i, a in enumerate(args): try: if a in UPLOADED_FILES: args[i] = UPLOAD_FOLDER + '/' + a shpname = a.split('.')[0] except: pass try: if a.split('_')[0] == 'cached': cacheid = a.split('_')[1] query = "SELECT Obj FROM WObj WHERE ID = {}".format(cacheid) cur = get_db().cursor().execute(query) result = cur.fetchone()[0] obj = cPickle.loads(str(result)) args[i] = obj cur.close() except: pass for k, v in kwargs.iteritems(): try: if v in UPLOADED_FILES: kwargs[k] = os.path.join(UPLOAD_FOLDER, v) except: pass try: if v.split('_')[0] == 'cached': cacheid = v.split('_')[1] query = "SELECT Obj FROM WObj WHERE ID = {}".format(cacheid) cur = get_db().cursor().execute(query) result = cur.fetchone()[0] obj = cPickle.loads(str(result)) kwargs[k] = obj cur.close() except: pass #This is a hack until I get the vector/list checking going on if module == 'esda': args[0] = np.array(args[0]) #Make the call and get the return items funcreturn = call(*args, **kwargs) #Write the W Object to the database if isinstance(funcreturn, ps.W): pdata = cPickle.dumps(funcreturn, cPickle.HIGHEST_PROTOCOL) cur = get_db().cursor() if method == 'queen_from_shapefile': m = 'Queen' else: m = 'Rook' indb = False #Query the db to see if the name / type is in the db query = "SELECT Type, Shapefile FROM WObj" cur = get_db().cursor().execute(query) result = cur.fetchall() for r in result: if r[0] == m and r[1] == shpname: indb = True break if indb == False: obj = (m, sqlite3.Binary(pdata), funcreturn._shpName) cur.execute("INSERT INTO WObj values (NULL, ?, ?, ?)",obj) get_db().commit() cur.close() response['data'] = {'Shapefile':funcreturn._shpName, 'Weight Type':method} else: funcreturn = recursedict(vars(funcreturn)) response['data'] = funcreturn return jsonify(response)
def post(request, module, method, module2=None): """ To make a POST using CURL: Fisher-Jenks using the Hartigan Olympic time example curl -i -H "Content-Type: application/json" -X POST -d '{"args":["[12, 10.8, 11, 10.8, 10.8, 10.6, 10.8, 10.3, 10.3,10.3,10.4,10.5,10.2,10.0,9.9]"], "kwargs":{"k":5}}' http://localhost:8080/api/esda/mapclassify/Fisher_Jenks/ or Using the CherryPy server on port 8080 Queen from shapefile - NOTE: The file must be uploaded already curl -i -H "Content-Type: application/json" -X POST -d '{"args":[NAT.shp]}' http://localhost:8080/api/weights/queen_from_shapefile/ or curl -i -k -H "Content-Type: application/json" -u [email protected]:jay -X POST -d '{"args":{y":[12, 10.8, 11, 10.8, 10.8, 10.6, 10.8, 10.3, 10.3,10.3,10.4,10.5,10.2,10.0,9.9]}, "kwargs":{"k":5}}' https://webpool.csf.asu.edu/pysalrest/api/esda/mapclassify/Fisher_Jenks/ """ #TODO: This needs a refactor badly - handling python objs, etc. if not request.json: response = {'status': 'error', 'data': {}} response['data'] = 'Post datatype was not json' return jsonify(response), 400 else: response = {'status': 'success', 'data': {}} #Setup the call, the args and the kwargs if module2 == None: argorder = getargorder(pysalfunctions[module][method]) call = pmdwrapper(pysalfunctions[module][method]) else: argorder = getargorder(pysalfunctions[module][module2][method]) call = pmdwrapper(pysalfunctions[module][module2][method]) #Parse the args keys = request.json.keys() req = request.json #Setup the python arg / kwarg containers args = [] kwargs = {} if 'args' in keys: for a in argorder: if a in req['args'].keys(): #Try to treat as a url, assuming tokenized input value = req['args'][a] value = gettoken(value) if not value: value = req['args'][a] if not a: return { 'status': 'failure', 'message': 'Unable to get data from url: {}'.format(a) } #Try to cast the data to a python type try: pythonarg = ast.literal_eval(value) except: pythonarg = value args.append(pythonarg) #An unexpected argument was provided else: print "Arg {} not found in inputs. Trying anyway".format(a) if 'kwargs' in keys: for k, v in req['kwargs'].iteritems(): #Try to treat as a token value = v value = gettoken(v) if not value: value = v #Try to cast the response to a json object try: kwargs[k] = ast.literal_eval(v) except: kwargs[k] = v # or if they are uploaded shapefiles for i, a in enumerate(args): try: if a in UPLOADED_FILES: args[i] = UPLOAD_FOLDER + '/' + a shpname = a.split('.')[0] except: pass try: if a.split('_')[0] == 'cached': cacheid = a.split('_')[1] #Get the object out of the DB result = UserPyObj.query.filter_by(id=cacheid).first() obj = cPickle.loads(str(result.pyobj)) args[i] = obj except: pass if isinstance(a, list): arr = np.array(a) args[i] = np.array(a) for k, v in kwargs.iteritems(): try: if v in UPLOADED_FILES: kwargs[k] = os.path.join(UPLOAD_FOLDER, v) except: pass try: if v.split('_')[0] == 'cached': cacheid = v.split('_')[1] query = "SELECT Obj FROM WObj WHERE ID = {}".format( cacheid) cur = get_db().cursor().execute(query) result = cur.fetchone()[0] obj = cPickle.loads(str(result)) kwargs[k] = obj cur.close() except: pass #Explicit duck typing #Make the call and get the return items try: funcreturn = call(*args, **kwargs) except: try: for i, a in enumerate(args): if isinstance(a, list): args[i] = np.array(args[i]).reshape(-1, 1) if isinstance(a, np.ndarray): args[i] = a.reshape(-1, 1) funcreturn = call(*args, **kwargs) except: for i, a in enumerate(args): if isinstance(a, list): args[i] = np.array(args[i]).reshape(1, -1) funcreturn = call(*args, **kwargs) pdata = cPickle.dumps(funcreturn, cPickle.HIGHEST_PROTOCOL) datahashvalues = '{}_{}'.format(time.time(), funcreturn) datahash = hashlib.md5(datahashvalues).hexdigest() insertrow = UserPyObj(g.user.id, pdata, method, datahash=datahash) db.session.add(insertrow) db.session.commit() #Extract the response (attributes) from the function return and assign href for each try: attributes = inspect.getmembers( funcreturn, lambda a: not (inspect.isroutine(a))) publicattributes = [ a for a in attributes if not (a[0].startswith('_') and a[0].startswith('__')) ] except: pass #Build the response response['data'] = {} for a in publicattributes: response['data'][a[0]] = { 'type': '{}'.format(type(a[1])), 'href': 'mydata/{}/{}'.format(datahash, a[0]) } response['data']['full_results'] = { 'type': 'obj', 'href': 'mydata/{}/full'.format(datahash) } response['data']['raw'] = { 'type': 'python_object', 'href': 'mydata/{}/raw'.format(datahash) } return response