Skip to content

Commit

Permalink
Merge pull request #34 from fact-project/ontime_from_db
Browse files Browse the repository at this point in the history
Ontime from db
  • Loading branch information
maxnoe authored Feb 15, 2017
2 parents 85e63c2 + 69f5204 commit 731b2dc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
4 changes: 2 additions & 2 deletions fact/factdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .database import factdata_db
from .utils import get_ontime_by_source, get_ontime_by_source_and_runtype
from .database import factdata_db, connect_database
from .utils import get_ontime_by_source, get_ontime_by_source_and_runtype, get_correct_ontime
from .models import *
67 changes: 62 additions & 5 deletions fact/factdb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,67 @@
import peewee
from .models import RunInfo, Source, RunType
from ..credentials import create_factdb_engine
from ..time import night_integer


SECOND = peewee.SQL('SECOND')


def read_into_dataframe(query, engine=None):
''' read the result of a peewee query object into a pandas DataFrame '''

sql, params = query.sql()
df = pd.read_sql_query(sql, engine or create_factdb_engine(), params=params)

return df


def get_correct_ontime(start=None, end=None, engine=None):
'''
The database field fOnTime underestimates the real ontime by about 5 seconds
because of how the number is calculated from the FTM auxfiles.
A better estimate can be obtained by taking (fRunStop - fRunStart) * fEffectiveOn.
Parameters
----------
start : int or datetime.date
First night to select, either in fact int format or as date
end : int or datetime.date
Last night to select, either in fact int format or as date
engine: sqlalchemy.Engine
The engine connected to the database.
If None, fact.credentials.create_factdb_engine will be used to create one.
Source: D. Neise, A. Biland. Also see github.com/dneise/about_fact_ontime
'''

duration = (peewee.fn.TIMESTAMPDIFF(SECOND, RunInfo.frunstart, RunInfo.frunstop))

query = RunInfo.select(
RunInfo.fnight.alias('night'),
RunInfo.frunid.alias('run_id'),
RunInfo.frunstart.alias('start'),
RunInfo.frunstart.alias('stop'),
duration.alias('duration'),
RunInfo.feffectiveon.alias('relative_ontime'),
)

if start is not None:
start = night_integer(start) if not isinstance(start, int) else start
query = query.where(RunInfo.fnight >= start)

if end is not None:
end = night_integer(end) if not isinstance(end, int) else end
query = query.where(RunInfo.fnight <= end)

df = read_into_dataframe(query, engine=engine)
df['ontime'] = df['duration'] * df['relative_ontime']

return df


def get_ontime_by_source_and_runtype(engine=None):
query, params = (
query = (
RunInfo
.select(
peewee.fn.SUM(RunInfo.fontime).alias('ontime'),
Expand All @@ -16,9 +73,8 @@ def get_ontime_by_source_and_runtype(engine=None):
.switch(RunInfo)
.join(RunType, on=RunType.fruntypekey == RunInfo.fruntypekey)
.group_by(Source.fsourcename, RunType.fruntypename)
.sql()
)
df = pd.read_sql(query, engine or create_factdb_engine(), params=params)
df = read_into_dataframe(query, engine or create_factdb_engine())
df.set_index(['source', 'runtype'], inplace=True)

return df
Expand All @@ -38,8 +94,9 @@ def get_ontime_by_source(runtype=None, engine=None):
if runtype is not None:
query = query.where(RunType.fruntypename == runtype)

query, params = query.group_by(Source.fsourcename).sql()
df = pd.read_sql(query, engine or create_factdb_engine(), params=params)
query = query.group_by(Source.fsourcename)

df = read_into_dataframe(query, engine or create_factdb_engine())
df.set_index('source', inplace=True)

return df
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='pyfact',
version='0.8.5',
version='0.8.6',
description='A module containing useful methods for working with fact',
url='http://github.com/fact-project/pyfact',
author='Maximilian Noethe, Dominik Neise',
Expand Down

0 comments on commit 731b2dc

Please sign in to comment.