Python pmc(all=False, metric="TSS")

Similar to R pmc, returns a dict with a list for: date, stress, lts, sts, sb, rr
This commit is contained in:
Alejandro Martinez
2017-12-17 11:28:16 -03:00
parent 33b86235e9
commit bbfc4e44fe
6 changed files with 147 additions and 3 deletions

View File

@@ -7,6 +7,7 @@
#include "Colors.h"
#include "RideCache.h"
#include "DataFilter.h"
#include "PMCData.h"
#include "Bindings.h"
@@ -687,6 +688,108 @@ Bindings::rideFileCacheMeanmax(RideFileCache* cache) const
return ans;
}
PyObject*
Bindings::pmc(bool all, QString metric) const
{
Context *context = python->contexts.value(threadid());
// return a dict with PMC data for all or the current season
// XXX uses the default half-life
if (context) {
// import datetime if necessary
if (PyDateTimeAPI == NULL) PyDateTime_IMPORT;
// get the currently selected date range
DateRange range(context->currentDateRange());
// convert the name to a symbol, if not found just leave as it is
const RideMetricFactory &factory = RideMetricFactory::instance();
for (int i=0; i<factory.metricCount(); i++) {
QString symbol = factory.metricName(i);
QString name = context->specialFields.internalName(factory.rideMetric(symbol)->name());
name.replace(" ","_");
if (name == metric) {
metric = symbol;
break;
}
}
// create the data
PMCData pmcData(context, Specification(), metric);
// how many entries ?
unsigned int size = all ? pmcData.days() : range.from.daysTo(range.to) + 1;
// returning a dict with
// date, stress, lts, sts, sb, rr
PyObject* ans = PyDict_New();
// DATE - 1 a day from start
PyObject* datelist = PyList_New(size);
QDate start = all ? pmcData.start() : range.from;
for(unsigned int k=0; k<size; k++) {
QDate d = start.addDays(k);
PyList_SET_ITEM(datelist, k, PyDate_FromDate(d.year(), d.month(), d.day()));
}
// add to the dict
PyDict_SetItemString(ans, "date", datelist);
// PMC DATA
PyObject* stress = PyList_New(size);
PyObject* lts = PyList_New(size);
PyObject* sts = PyList_New(size);
PyObject* sb = PyList_New(size);
PyObject* rr = PyList_New(size);
if (all) {
// just copy
for(unsigned int k=0; k<size; k++) {
PyList_SET_ITEM(stress, k, PyFloat_FromDouble(pmcData.stress()[k]));
PyList_SET_ITEM(lts, k, PyFloat_FromDouble(pmcData.lts()[k]));
PyList_SET_ITEM(sts, k, PyFloat_FromDouble(pmcData.sts()[k]));
PyList_SET_ITEM(sb, k, PyFloat_FromDouble(pmcData.sb()[k]));
PyList_SET_ITEM(rr, k, PyFloat_FromDouble(pmcData.rr()[k]));
}
} else {
unsigned int index=0;
QDate start = pmcData.start();
for(int k=0; k < pmcData.days(); k++) {
// day today
if (start.addDays(k) >= range.from && start.addDays(k) <= range.to) {
PyList_SET_ITEM(stress, index, PyFloat_FromDouble(pmcData.stress()[k]));
PyList_SET_ITEM(lts, index, PyFloat_FromDouble(pmcData.lts()[k]));
PyList_SET_ITEM(sts, index, PyFloat_FromDouble(pmcData.sts()[k]));
PyList_SET_ITEM(sb, index, PyFloat_FromDouble(pmcData.sb()[k]));
PyList_SET_ITEM(rr, index, PyFloat_FromDouble(pmcData.rr()[k]));
index++;
}
}
}
// add to the dict
PyDict_SetItemString(ans, "stress", stress);
PyDict_SetItemString(ans, "lts", lts);
PyDict_SetItemString(ans, "sts", sts);
PyDict_SetItemString(ans, "sb", sb);
PyDict_SetItemString(ans, "rr", rr);
// return it
return ans;
}
// nothing to return
return NULL;
}
int
Bindings::webpage(QString url) const
{

View File

@@ -37,6 +37,7 @@ class Bindings {
PyObject* activityMetrics(bool compare=false) const;
PyObject* seasonMetrics(bool all=false, QString filter=QString(), bool compare=false) const;
PythonDataSeries *metrics(QString metric, bool all=false, QString filter=QString()) const;
PyObject* pmc(bool all=false, QString metric=QString("TSS")) const;
// working with meanmax data
PyObject* activityMeanmax(bool compare=false) const;

View File

@@ -126,6 +126,7 @@ public:
PyObject* activityMetrics(bool compare=false) /TransferBack/;
PyObject* seasonMetrics(bool all=false, QString filter=QString(), bool compare=false) /TransferBack/;
PythonDataSeries *metrics(QString metric, bool all=false, QString filter=QString()) /TransferBack/;
PyObject* pmc(bool all=false, QString metric=QString("TSS")) /TransferBack/;
// working with meanmax data
PyObject* activityMeanmax(bool compare=false) /TransferBack/;

View File

@@ -65,8 +65,10 @@
#define sipName_type &sipStrings_goldencheetah[248]
#define sipNameNr_url 253
#define sipName_url &sipStrings_goldencheetah[253]
#define sipNameNr_all 257
#define sipName_all &sipStrings_goldencheetah[257]
#define sipNameNr_pmc 257
#define sipName_pmc &sipStrings_goldencheetah[257]
#define sipNameNr_all 261
#define sipName_all &sipStrings_goldencheetah[261]
#define sipMalloc sipAPI_goldencheetah->api_malloc
#define sipFree sipAPI_goldencheetah->api_free

View File

@@ -339,6 +339,41 @@ static PyObject *meth_Bindings_metrics(PyObject *sipSelf, PyObject *sipArgs, PyO
}
extern "C" {static PyObject *meth_Bindings_pmc(PyObject *, PyObject *, PyObject *);}
static PyObject *meth_Bindings_pmc(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds)
{
PyObject *sipParseErr = NULL;
{
bool a0 = 0;
::QString a1def = QString("TSS");
::QString* a1 = &a1def;
int a1State = 0;
::Bindings *sipCpp;
static const char *sipKwdList[] = {
sipName_all,
sipName_metric,
};
if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, sipKwdList, NULL, "B|bJ1", &sipSelf, sipType_Bindings, &sipCpp, &a0, sipType_QString,&a1, &a1State))
{
PyObject * sipRes;
sipRes = sipCpp->pmc(a0,*a1);
sipReleaseType(a1,sipType_QString,a1State);
return sipRes;
}
}
/* Raise an exception if the arguments couldn't be parsed. */
sipNoMethod(sipParseErr, sipName_Bindings, sipName_pmc, NULL);
return NULL;
}
extern "C" {static PyObject *meth_Bindings_activityMeanmax(PyObject *, PyObject *, PyObject *);}
static PyObject *meth_Bindings_activityMeanmax(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds)
{
@@ -512,6 +547,7 @@ static PyMethodDef methods_Bindings[] = {
{SIP_MLNAME_CAST(sipName_athlete), meth_Bindings_athlete, METH_VARARGS, NULL},
{SIP_MLNAME_CAST(sipName_build), meth_Bindings_build, METH_VARARGS, NULL},
{SIP_MLNAME_CAST(sipName_metrics), (PyCFunction)meth_Bindings_metrics, METH_VARARGS|METH_KEYWORDS, NULL},
{SIP_MLNAME_CAST(sipName_pmc), (PyCFunction)meth_Bindings_pmc, METH_VARARGS|METH_KEYWORDS, NULL},
{SIP_MLNAME_CAST(sipName_seasonMeanmax), (PyCFunction)meth_Bindings_seasonMeanmax, METH_VARARGS|METH_KEYWORDS, NULL},
{SIP_MLNAME_CAST(sipName_seasonMetrics), (PyCFunction)meth_Bindings_seasonMetrics, METH_VARARGS|METH_KEYWORDS, NULL},
{SIP_MLNAME_CAST(sipName_series), (PyCFunction)meth_Bindings_series, METH_VARARGS|METH_KEYWORDS, NULL},
@@ -537,7 +573,7 @@ sipClassTypeDef sipTypeDef_goldencheetah_Bindings = {
{
sipNameNr_Bindings,
{0, 0, 1},
14, methods_Bindings,
15, methods_Bindings,
0, 0,
0, 0,
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

View File

@@ -41,6 +41,7 @@ const char sipStrings_goldencheetah[] = {
'b', 'u', 'i', 'l', 'd', 0,
't', 'y', 'p', 'e', 0,
'u', 'r', 'l', 0,
'p', 'm', 'c', 0,
'a', 'l', 'l', 0,
};