Commits

dan mackinlay committed c78b170

untested SQL persistence (actually, YAGNI)

Comments (0)

Files changed (3)

 from random import sample
 import itertools
 from pprint import pprint
+from settings import OUTPUT_DB_URL
 
 # we expect params to contain the following keys:
 # n_agents, dt, noise, radius, steps
     'n_branches': 100,
 }
 
-def swept_experiment(sweepables=None, base_params=DEFAULT_TRIAL_PARAMS, oversample=10, start_seed=10000, n_pairs=10):
+def stashed_swept_experiment(db_url = OUTPUT_DB_URL, *args, **kwargs):
+    import persistence
+    session = persistence.get_db_session(OUTPUT_DB_URL)
+    for params, stat_vals in swept_experiment_iterator(*args, **kwargs):
+        for stat_val in stat_vals:
+            persistence.persist_stat(session, "mi", stat_val, params)
+    return session
+
+def swept_experiment(*args, **kwargs):
+    return [e for e in swept_experiment_iterator(*args, **kwargs)]
+    
+def swept_experiment_iterator(sweepables=None, base_params=DEFAULT_TRIAL_PARAMS, oversample=10, start_seed=10000, n_pairs=10):
     sweepables = sweepables or {} #cast to empty dict
     swept_names = sweepables.keys()
     swept_iterables = [sweepables[name] for name in swept_names]
-    estimates = []
     seed = start_seed
     n_vals = 1
     for iterable in swept_iterables:
                 these_params,
                 seed=seed
             )
-            estimates.append((
+            yield (
                 trial[0].params,
                 analyse_branched_trial(
                     trial,
                     n_pairs=n_pairs
                 )
-            ))
-            seed = abs(hash(str(5))) #start seed in very different places next time
-    return estimates
+            )
+            seed = abs(hash(str(5))) #start seed in a different place next time around
 
 def branched_trial(base_params=DEFAULT_TRIAL_PARAMS, seed=1):
     """
     about, from continuous input.
     
     If no n_bins is given, we choose the maximimum statistically valid number
-    by the Cochrane criterion, and leave it be. This will give good results in
-    all except particularly pathological cases: Bins will be (marginwise)
+    by the Cochrane criterion, and leave it be. This will give OK results in
+    all except pathological cases: Bins will be (marginwise)
     approximately equal occupancy.
     """
     # kwargs.setdefault('method', 'qe')
+from sqlalchemy import create_engine, Table, Column, MetaData, BigInteger, SmallInteger, Integer, String, Float, Sequence
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import sessionmaker
+
+# http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#metadata-constraints
+# http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html
+# http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html
+# http://docs.sqlalchemy.org/en/rel_0_7/core/tutorial.html
+# http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/declarative.html
+
+Base = declarative_base()
+
+class SummaryStat(Base):
+    __tablename__='summary_stat'
+    id = Column(Integer, Sequence('summary_stat_id_seq'), primary_key=True, autoincrement=True)
+    stat_name = Column(String(256), index=True)
+    stat_val = Column(Float)
+    noise = Column(Float, index = True)
+    dt = Column(Float, index = True)
+    radius = Column(Float, index = True)
+    base_seed = Column(BigInteger)
+    seed = Column(BigInteger)
+    discard_steps = Column(Integer)
+    n_agents = Column(Integer)
+    n_pairs = Column(Integer)
+    keep_keys = ['noise', 'dt', 'radius', 'base_seed', 'seed', 'discard_steps', 'n_agents']
+
+def get_db_session(db_url):
+    engine = create_engine(db_url)
+    Session = sessionmaker(bind=engine)
+    session = Session()
+    Base.metadata.bind = engine
+    Base.metadata.create_all()
+    return session
+
+def persist_stat(session, stat_name, stat_val, params=None):
+    db_rec = SummaryStat(**dict([(key, params[key]) for key in SummaryStat.keep_keys]))
+    summary_stat_rec.stat_name = stat_name
+    summary_stat_rec.stat_val = stat_val
+    session.add(db_rec)
 from path import path
 
-BASE_PATH = path('../bubble_economy.Data')
-IMAGE_OUTPUT_PATH = BASE_PATH/'graphs'
-EXPERIMENT_OUTPUT_PATH = BASE_PATH/'experiments'
-# track argument pickles (OpenPBS only ATM)
-TEMP_ARGS_PATH = BASE_PATH/'args'
-#track jobs (OpenPBS only ATM, though other things should use it too)
-TEMP_JOBS_PATH = BASE_PATH/'jobs'
-#OpenPBS default args
-BASE_QSUB_ARGS = {}
+BASE_DATA_PATH = path('../bubble_economy.Data')
+WORKING_DATA_PATH = BASE_DATA_PATH/'working'
+OUTPUT_DATA_PATH = BASE_DATA_PATH/'output'
+IMAGE_OUTPUT_PATH = BASE_DATA_PATH/'images'
 
-DEFAULT_SUMMARY_STATS=('mean', 'std_dev', 'multiplicity')
-ALLOWED_SUMMARY_STATS=('mean', 'std_dev', 'multiplicity', 'quartiles', 'raw')
-
+OUTPUT_DB_URL = OUTPUT_DATA_PATH/'output.db'
 
 # override in local_settings.py:
 try: