Commit f0812128 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

In Metric2D, the configuration input aggregate_z can be a mathtematical...

In Metric2D, the configuration input aggregate_z can be a mathtematical expression involving several database fields.
parent 318f06f0
......@@ -17,7 +17,8 @@ Configure the 2-dim metrics
on the database field ``people.id``.
A ``metric`` is the result of an *aggregation function* applied on the values
of a given database field.
of a given database field or on the values of a mathematical expression
involving several database fields.
A 2-dim metric is a table displaying one metric when records are
aggregated by values for a given database fields along the *x* and *y* axis.
......@@ -70,12 +71,21 @@ Filter conditions
Aggregate
---------
The group fields and the metric are defined in the ``Aggregate`` tab. The
entries ``Group (x)``, ``Group(y)`` and ``Aggregate (z)`` are equal to the
address of database field, virtual field, key of the ``history.data``
dictionary or the pseudo field ``year``.
The group fields and the metric are defined in the ``Aggregate`` tab:
The entry ``metric (z)`` is the name of the aggregation function. Possible
values are: ``count``, ``max``, ``mean``, ``median``, ``min``, ``size``,
``std``, ``sum``, *etc*. In fact, all the descriptive stats methods of the
DataFrame_ class.
\ No newline at end of file
``Group (x)`` and ``Group(y)``
are equal to the address of database field, virtual field, key
of the ``history.data`` dictionary or the pseudo field ``year``.
``Aggregate (z)``
is equal to the address of database field, virtual field, key
of the ``history.data`` dictionary or the pseudo field ``year``.
It can also be equal to a *mathematical expression* involving several
database fields.
``metric (z)``
is equal to the name of the aggregation function. Possible
values are: ``count``, ``max``, ``mean``, ``median``, ``min``, ``size``,
``std``, ``sum``, *etc*. In fact, all the descriptive stats methods of
the DataFrame_ class.
\ No newline at end of file
......@@ -15,6 +15,10 @@ from plugin_dbui import get_id, Store
from StringIO import StringIO
REG_DBFIELD = re.compile("\w+\.\w+(?:\.\w+)?", re.UNICODE)
REG_SINGLE_DBFIELD = re.compile("^ *\w+\.\w+(\.\w+)? *$", re.UNICODE)
def do_title(config, selector):
"""Build the report title.
......@@ -938,6 +942,27 @@ class Metric2D(BaseReport):
metric = {}
metric[address_z] = aggregate
# database field expression along the z axis
expression = None
if not REG_SINGLE_DBFIELD.match(address_z):
expression = address_z
ref_addresses = list(addresses)
del maps[2]
del addresses[2]
i = 0
for m in REG_DBFIELD.finditer(address_z):
address = m.group()
map = split_dbfield(address)
tmp = "a%i" % i
i += 1
addresses.append(tmp)
maps.append(map)
expression = expression.replace(address, tmp)
# build the data frame
data = self._do_data(maps)
if not data:
......@@ -946,6 +971,11 @@ class Metric2D(BaseReport):
df = pd.DataFrame(data, columns=addresses)
# compute the expression and remove temporarely column
if expression:
df[address_z] = df.eval(expression)
df = df[ref_addresses]
# remove duplicate entries
if aggregate in ('count', 'size'):
df = df.drop_duplicates()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment