Skip to content

Commit

Permalink
Merge a0d13f5 into 7d21655
Browse files Browse the repository at this point in the history
  • Loading branch information
fscottfoti committed May 19, 2014
2 parents 7d21655 + a0d13f5 commit efb6db3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 53 deletions.
88 changes: 39 additions & 49 deletions urbansim/server/urbansimd.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import cPickle
import decimal
import json
import math
import os
import string
import sys
import time
from decimal import Decimal

import numpy
import pandas as pd
import yaml
import simplejson
from bottle import Bottle, route, run, response, hook, request, post
import pandas as pd
from bottle import route, run, response, hook, request

from cStringIO import StringIO
from urbansim.utils import misc, yamlio

from urbansim.urbansim import modelcompile
from urbansim.utils import misc
import inspect
import models


def jsonp(request, dictionary):
Expand Down Expand Up @@ -58,37 +55,33 @@ def enable_cors():
@route('/configs')
def list_configs():
def resp():
print os.listdir(misc.configs_dir())
files = [f for f in os.listdir(misc.configs_dir())
if f[-5:] == '.json']
if f[-5:] == '.yaml']

def not_modelset(f):
c = open(os.path.join(misc.configs_dir(), f)).read()
c = json.loads(c)
return 'model' in c and c['model'] != 'modelset'
return filter(not_modelset, files)
return files
return wrap_request(request, response, resp())


@route('/config/<configname>', method="OPTIONS")
def ans_opt(configname):
return {}


@route('/config/<configname>', method="GET")
def read_config(configname):
def resp():
c = open(os.path.join(misc.configs_dir(), configname)).read()
return simplejson.loads(c)
return yaml.load(c)
return wrap_request(request, response, resp())


@route('/config/<configname>', method="OPTIONS")
def ans_opt(configname):
return {}


@route('/config/<configname>', method="PUT")
def write_config(configname):
json = request.json

def resp():
s = simplejson.dumps(json, indent=4)
print s
s = yamlio.ordered_yaml(json)
return open(os.path.join(misc.configs_dir(), configname), "w").write(s)
return wrap_request(request, response, resp())

Expand Down Expand Up @@ -378,37 +371,34 @@ def resp(name):
return wrap_request(request, response, resp(name))


@route('/compilemodel')
def compilemodel():
def resp(req, modelname, mode):
print "Request: %s\n" % req
req = simplejson.loads(req)
returnobj = modelcompile.gen_model(req, modelname, mode)
print returnobj[1]
return returnobj[1]
modelname = request.query.get('modelname', 'autorun')
mode = request.query.get('mode', 'estimate')
req = request.query.get('json', '')
return wrap_request(request, response, resp(req, modelname, mode))
@route('/list_models')
def list_models():
def resp():
print "Request: %s\n" % request.query.json
functions = inspect.getmembers(models, predicate=inspect.isfunction)
functions = filter(lambda x: not x[0].startswith('_'), functions)
functions = [x[0] for x in functions]
return functions
return wrap_request(request, response, resp())


@route('/execmodel')
def execmodel():
def resp(modelname, mode):
print "Request: %s\n" % request.query.json
req = simplejson.loads(request.query.json)
returnobj = modelcompile.run_model(req, DSET, configname=modelname, mode=mode)
return returnobj
modelname = request.query.get('modelname', 'autorun')
mode = request.query.get('mode', 'estimate')
return wrap_request(request, response, resp(modelname, mode))
@route('/exec_model/<modelname>')
def exec_model(modelname):
def resp(modelname):
backup = sys.stdout
sys.stdout = StringIO()
getattr(models, modelname)(DSET)
s = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = backup
return {"log": s}
return wrap_request(request, response, resp(modelname))


def pandas_statement(table, where, sort, orderdesc, groupby, metric,
limit, page):
if where:
where = "[DSET.fetch('%s').apply(lambda x: bool(%s),axis=1)]" % (
table, where)
where = ".query('%s')" % where
else:
where = ""
if sort and orderdesc:
Expand Down
6 changes: 3 additions & 3 deletions urbansim/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ def configs_dir():


def charts_dir():
return mkifnotexists("charts")
return mkifnotexists("web/charts")


def maps_dir():
return mkifnotexists("maps")
return mkifnotexists("web/maps")


def runs_dir():
return mkifnotexists("runs")


def reports_dir():
return mkifnotexists("reports")
return mkifnotexists("web/reports")


def coef_dir():
Expand Down
2 changes: 1 addition & 1 deletion urbansim/utils/yamlio.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def ordered_yaml(cfg):
'alts_fit_filters', 'alts_predict_filters',
'interaction_predict_filters',
'choice_column', 'sample_size', 'estimation_sample_size',
'model_expression', 'ytransform']
'model_expression', 'ytransform', 'coefficients', 'fitted']

s = []
for key in order:
Expand Down

0 comments on commit efb6db3

Please sign in to comment.