SoftwareDefinedBuildings / XBOS

The eXtensible Building Operating System
BSD 2-Clause "Simplified" License
28 stars 18 forks source link

Unit of measure conversion works only in some cases #57

Closed marcopritoni closed 6 years ago

marcopritoni commented 6 years ago

I tried to get data stored in Wh (Green_Button_Meters) and convert it to kWh directly by MDAL.

The conversion only worked when I asked for all the uuids and not for a single one: The code below is an example:

import pandas as pd

from matplotlib import style import matplotlib %matplotlib inline style.use('ggplot')

from xbos import get_client from xbos.devices.thermostat import Thermostat from xbos.services.pundat import DataClient, timestamp, make_dataframe from xbos.services import mdal

c = get_client(entity="../../Documents/bosswave_key/marco.ent") client = mdal.MDALClient("xbos/mdal", client=c) METER = "f9ba6d7e-d730-31d7-95fb-d7ec0e4ab765"

BRICKq = """SELECT ?meter_uuid WHERE { ?meter rdf:type/rdfs:subClassOf* brick:Green_Button_Meter . ?meter bf:uuid ?meter_uuid . };"""

no conversion asked : original unit

query1 = { "Composition": [METER], "Selectors": [mdal.MEAN], "Variables": [ {"Name": "meter", "Definition": BRICKq,

"Units":

    }
],
"Time": {
    "T0": "2017-01-01 00:00:00 PST",
    "T1": "2017-01-10 00:00:00 PST",
    "WindowSize": '15min',
    "Aligned": True,
},

}

resp1 = client.do_query(query1,timeout=300) test1 = resp1["df"] test1.columns = ["noUnitConversion"]

conversion to kWh (ask for specific UUID)

query2 = { "Composition": [METER], "Selectors": [mdal.MEAN], "Variables": [ {"Name": "meter", "Definition": BRICKq, "Units": "kwh" } ], "Time": { "T0": "2017-01-01 00:00:00 PST", "T1": "2017-03-31 00:00:00 PST", "WindowSize": '15min', "Aligned": True, }, }

resp2 = client.do_query(query2,timeout=300) test2 = resp2["df"] test2.columns = ["singleUuidConversion_toKwh"]

conversion to kWh (ask for all data then post-process single meter)

query3 = { "Composition": ["meter"], "Selectors": [mdal.MEAN], "Variables": [ {"Name": "meter", "Definition": BRICKq, "Units": "kwh" } ], "Time": { "T0": "2017-01-01 00:00:00 PST", "T1": "2017-03-31 00:00:00 PST", "WindowSize": '15min', "Aligned": True, }, }

resp3 = client.do_query(query3,timeout=300) test3 = resp3["df"][[METER]] test3.columns = ["multiUuidConversion_toKwh"]

test = test1.join(test2).join(test3) test.plot(figsize=(18,5)) print(test.head())

Output: Saw [xbos/mdal] MDAL 1 seconds 424.285 ms ago

Saw [xbos/mdal] MDAL 6 seconds 653.132 ms ago noUnitConversion singleUuidConversion_toKwh \ 2017-01-01 00:00:00-08:00 856.0 856.0
2017-01-01 00:15:00-08:00 936.0 936.0
2017-01-01 00:30:00-08:00 968.0 968.0
2017-01-01 00:45:00-08:00 928.0 928.0
2017-01-01 01:00:00-08:00 944.0 944.0

                       multiUuidConversion_toKwh  

2017-01-01 00:00:00-08:00 0.856
2017-01-01 00:15:00-08:00 0.936
2017-01-01 00:30:00-08:00 0.968
2017-01-01 00:45:00-08:00 0.928
2017-01-01 01:00:00-08:00 0.944

image

image

gtfierro commented 6 years ago

Could you make a couple changes so this is easier for me to track down?

marcopritoni commented 6 years ago

Sure how's this one

https://gist.github.com/marcopritoni/6475915bdc501715aaab9ba23da00a1c

gtfierro commented 6 years ago

https://gist.github.com/marcopritoni/6475915bdc501715aaab9ba23da00a1c#file-xbos_units_test-py

gtfierro commented 6 years ago

So, this isn't actually a bug (but there is a problem that needs to be fixed). In the cases where you are fetching data by UUID (the first 2 cases), the request for unit conversion doesn't get applied because its only for the "meter" variable. There's nothing tying the UUID you specified to the requested unit of measure in the variable definition.

What needs fixing is there not being a way to specify unit conversion for UUIDs. Should be a straightforward extension of the existing interface, so I'll get working on that soon!

gtfierro commented 6 years ago

https://github.com/gtfierro/mdal/pull/7

I'll do some testing and then merge and deploy it

gtfierro commented 6 years ago

https://github.com/gtfierro/mdal/pull/8 is deployed and fixes the issue

To do unit conversion when using UUIDS, you'll want something like

query2 = {
    "Composition": ["meter"],
    "Selectors": [mdal.MEAN],
    "Variables": [
        {"Name": "meter",
         "UUIDS": [METER],
         "Units": "kwh"
        }
    ],
    "Time": {
        "T0": "2017-01-01 00:00:00 PST",
        "T1": "2017-01-10 00:00:00 PST",
        "WindowSize": '15min',
        "Aligned": True,
    },
}