repository_name
stringlengths
5
67
func_path_in_repository
stringlengths
4
234
func_name
stringlengths
0
314
whole_func_string
stringlengths
52
3.87M
language
stringclasses
6 values
func_code_string
stringlengths
39
1.84M
func_code_tokens
listlengths
15
672k
func_documentation_string
stringlengths
1
47.2k
func_documentation_tokens
listlengths
1
3.92k
split_name
stringclasses
1 value
func_code_url
stringlengths
85
339
eventable/vobject
vobject/vcard.py
serializeFields
def serializeFields(obj, order=None): """ Turn an object's fields into a ';' and ',' seperated string. If order is None, obj should be a list, backslash escape each field and return a ';' separated string. """ fields = [] if order is None: fields = [backslashEscape(val) for val in obj] else: for field in order: escapedValueList = [backslashEscape(val) for val in toList(getattr(obj, field))] fields.append(','.join(escapedValueList)) return ';'.join(fields)
python
def serializeFields(obj, order=None): fields = [] if order is None: fields = [backslashEscape(val) for val in obj] else: for field in order: escapedValueList = [backslashEscape(val) for val in toList(getattr(obj, field))] fields.append(','.join(escapedValueList)) return ';'.join(fields)
[ "def", "serializeFields", "(", "obj", ",", "order", "=", "None", ")", ":", "fields", "=", "[", "]", "if", "order", "is", "None", ":", "fields", "=", "[", "backslashEscape", "(", "val", ")", "for", "val", "in", "obj", "]", "else", ":", "for", "field", "in", "order", ":", "escapedValueList", "=", "[", "backslashEscape", "(", "val", ")", "for", "val", "in", "toList", "(", "getattr", "(", "obj", ",", "field", ")", ")", "]", "fields", ".", "append", "(", "','", ".", "join", "(", "escapedValueList", ")", ")", "return", "';'", ".", "join", "(", "fields", ")" ]
Turn an object's fields into a ';' and ',' seperated string. If order is None, obj should be a list, backslash escape each field and return a ';' separated string.
[ "Turn", "an", "object", "s", "fields", "into", "a", ";", "and", "seperated", "string", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L264-L279
eventable/vobject
vobject/vcard.py
Address.toString
def toString(val, join_char='\n'): """ Turn a string or array value into a string. """ if type(val) in (list, tuple): return join_char.join(val) return val
python
def toString(val, join_char='\n'): if type(val) in (list, tuple): return join_char.join(val) return val
[ "def", "toString", "(", "val", ",", "join_char", "=", "'\\n'", ")", ":", "if", "type", "(", "val", ")", "in", "(", "list", ",", "tuple", ")", ":", "return", "join_char", ".", "join", "(", "val", ")", "return", "val" ]
Turn a string or array value into a string.
[ "Turn", "a", "string", "or", "array", "value", "into", "a", "string", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L75-L81
eventable/vobject
vobject/vcard.py
VCardTextBehavior.encode
def encode(cls, line): """ Backslash escape line.value. """ if not line.encoded: encoding = getattr(line, 'encoding_param', None) if encoding and encoding.upper() == cls.base64string: if isinstance(line.value, bytes): line.value = codecs.encode(line.value, "base64").decode("utf-8").replace('\n', '') else: line.value = codecs.encode(line.value.encode(encoding), "base64").decode("utf-8") else: line.value = backslashEscape(line.value) line.encoded = True
python
def encode(cls, line): if not line.encoded: encoding = getattr(line, 'encoding_param', None) if encoding and encoding.upper() == cls.base64string: if isinstance(line.value, bytes): line.value = codecs.encode(line.value, "base64").decode("utf-8").replace('\n', '') else: line.value = codecs.encode(line.value.encode(encoding), "base64").decode("utf-8") else: line.value = backslashEscape(line.value) line.encoded = True
[ "def", "encode", "(", "cls", ",", "line", ")", ":", "if", "not", "line", ".", "encoded", ":", "encoding", "=", "getattr", "(", "line", ",", "'encoding_param'", ",", "None", ")", "if", "encoding", "and", "encoding", ".", "upper", "(", ")", "==", "cls", ".", "base64string", ":", "if", "isinstance", "(", "line", ".", "value", ",", "bytes", ")", ":", "line", ".", "value", "=", "codecs", ".", "encode", "(", "line", ".", "value", ",", "\"base64\"", ")", ".", "decode", "(", "\"utf-8\"", ")", ".", "replace", "(", "'\\n'", ",", "''", ")", "else", ":", "line", ".", "value", "=", "codecs", ".", "encode", "(", "line", ".", "value", ".", "encode", "(", "encoding", ")", ",", "\"base64\"", ")", ".", "decode", "(", "\"utf-8\"", ")", "else", ":", "line", ".", "value", "=", "backslashEscape", "(", "line", ".", "value", ")", "line", ".", "encoded", "=", "True" ]
Backslash escape line.value.
[ "Backslash", "escape", "line", ".", "value", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L149-L162
eventable/vobject
vobject/vcard.py
VCard3_0.generateImplicitParameters
def generateImplicitParameters(cls, obj): """ Create PRODID, VERSION, and VTIMEZONEs if needed. VTIMEZONEs will need to exist whenever TZID parameters exist or when datetimes with tzinfo exist. """ if not hasattr(obj, 'version'): obj.add(ContentLine('VERSION', [], cls.versionString))
python
def generateImplicitParameters(cls, obj): if not hasattr(obj, 'version'): obj.add(ContentLine('VERSION', [], cls.versionString))
[ "def", "generateImplicitParameters", "(", "cls", ",", "obj", ")", ":", "if", "not", "hasattr", "(", "obj", ",", "'version'", ")", ":", "obj", ".", "add", "(", "ContentLine", "(", "'VERSION'", ",", "[", "]", ",", "cls", ".", "versionString", ")", ")" ]
Create PRODID, VERSION, and VTIMEZONEs if needed. VTIMEZONEs will need to exist whenever TZID parameters exist or when datetimes with tzinfo exist.
[ "Create", "PRODID", "VERSION", "and", "VTIMEZONEs", "if", "needed", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L193-L201
eventable/vobject
vobject/vcard.py
Photo.serialize
def serialize(cls, obj, buf, lineLength, validate): """ Apple's Address Book is *really* weird with images, it expects base64 data to have very specific whitespace. It seems Address Book can handle PHOTO if it's not wrapped, so don't wrap it. """ if wacky_apple_photo_serialize: lineLength = REALLY_LARGE VCardTextBehavior.serialize(obj, buf, lineLength, validate)
python
def serialize(cls, obj, buf, lineLength, validate): if wacky_apple_photo_serialize: lineLength = REALLY_LARGE VCardTextBehavior.serialize(obj, buf, lineLength, validate)
[ "def", "serialize", "(", "cls", ",", "obj", ",", "buf", ",", "lineLength", ",", "validate", ")", ":", "if", "wacky_apple_photo_serialize", ":", "lineLength", "=", "REALLY_LARGE", "VCardTextBehavior", ".", "serialize", "(", "obj", ",", "buf", ",", "lineLength", ",", "validate", ")" ]
Apple's Address Book is *really* weird with images, it expects base64 data to have very specific whitespace. It seems Address Book can handle PHOTO if it's not wrapped, so don't wrap it.
[ "Apple", "s", "Address", "Book", "is", "*", "really", "*", "weird", "with", "images", "it", "expects", "base64", "data", "to", "have", "very", "specific", "whitespace", ".", "It", "seems", "Address", "Book", "can", "handle", "PHOTO", "if", "it", "s", "not", "wrapped", "so", "don", "t", "wrap", "it", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L229-L237
eventable/vobject
vobject/vcard.py
NameBehavior.transformToNative
def transformToNative(obj): """ Turn obj.value into a Name. """ if obj.isNative: return obj obj.isNative = True obj.value = Name(**dict(zip(NAME_ORDER, splitFields(obj.value)))) return obj
python
def transformToNative(obj): if obj.isNative: return obj obj.isNative = True obj.value = Name(**dict(zip(NAME_ORDER, splitFields(obj.value)))) return obj
[ "def", "transformToNative", "(", "obj", ")", ":", "if", "obj", ".", "isNative", ":", "return", "obj", "obj", ".", "isNative", "=", "True", "obj", ".", "value", "=", "Name", "(", "*", "*", "dict", "(", "zip", "(", "NAME_ORDER", ",", "splitFields", "(", "obj", ".", "value", ")", ")", ")", ")", "return", "obj" ]
Turn obj.value into a Name.
[ "Turn", "obj", ".", "value", "into", "a", "Name", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L294-L302
eventable/vobject
vobject/vcard.py
NameBehavior.transformFromNative
def transformFromNative(obj): """ Replace the Name in obj.value with a string. """ obj.isNative = False obj.value = serializeFields(obj.value, NAME_ORDER) return obj
python
def transformFromNative(obj): obj.isNative = False obj.value = serializeFields(obj.value, NAME_ORDER) return obj
[ "def", "transformFromNative", "(", "obj", ")", ":", "obj", ".", "isNative", "=", "False", "obj", ".", "value", "=", "serializeFields", "(", "obj", ".", "value", ",", "NAME_ORDER", ")", "return", "obj" ]
Replace the Name in obj.value with a string.
[ "Replace", "the", "Name", "in", "obj", ".", "value", "with", "a", "string", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L305-L311
eventable/vobject
vobject/vcard.py
AddressBehavior.transformToNative
def transformToNative(obj): """ Turn obj.value into an Address. """ if obj.isNative: return obj obj.isNative = True obj.value = Address(**dict(zip(ADDRESS_ORDER, splitFields(obj.value)))) return obj
python
def transformToNative(obj): if obj.isNative: return obj obj.isNative = True obj.value = Address(**dict(zip(ADDRESS_ORDER, splitFields(obj.value)))) return obj
[ "def", "transformToNative", "(", "obj", ")", ":", "if", "obj", ".", "isNative", ":", "return", "obj", "obj", ".", "isNative", "=", "True", "obj", ".", "value", "=", "Address", "(", "*", "*", "dict", "(", "zip", "(", "ADDRESS_ORDER", ",", "splitFields", "(", "obj", ".", "value", ")", ")", ")", ")", "return", "obj" ]
Turn obj.value into an Address.
[ "Turn", "obj", ".", "value", "into", "an", "Address", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L322-L330
eventable/vobject
vobject/vcard.py
OrgBehavior.transformToNative
def transformToNative(obj): """ Turn obj.value into a list. """ if obj.isNative: return obj obj.isNative = True obj.value = splitFields(obj.value) return obj
python
def transformToNative(obj): if obj.isNative: return obj obj.isNative = True obj.value = splitFields(obj.value) return obj
[ "def", "transformToNative", "(", "obj", ")", ":", "if", "obj", ".", "isNative", ":", "return", "obj", "obj", ".", "isNative", "=", "True", "obj", ".", "value", "=", "splitFields", "(", "obj", ".", "value", ")", "return", "obj" ]
Turn obj.value into a list.
[ "Turn", "obj", ".", "value", "into", "a", "list", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/vcard.py#L350-L358
eventable/vobject
docs/build/lib/vobject/vcard.py
VCardTextBehavior.decode
def decode(cls, line): """ Remove backslash escaping from line.valueDecode line, either to remove backslash espacing, or to decode base64 encoding. The content line should contain a ENCODING=b for base64 encoding, but Apple Addressbook seems to export a singleton parameter of 'BASE64', which does not match the 3.0 vCard spec. If we encouter that, then we transform the parameter to ENCODING=b """ if line.encoded: if 'BASE64' in line.singletonparams: line.singletonparams.remove('BASE64') line.encoding_param = cls.base64string encoding = getattr(line, 'encoding_param', None) if encoding: line.value = codecs.decode(line.value.encode("utf-8"), "base64") else: line.value = stringToTextValues(line.value)[0] line.encoded=False
python
def decode(cls, line): if line.encoded: if 'BASE64' in line.singletonparams: line.singletonparams.remove('BASE64') line.encoding_param = cls.base64string encoding = getattr(line, 'encoding_param', None) if encoding: line.value = codecs.decode(line.value.encode("utf-8"), "base64") else: line.value = stringToTextValues(line.value)[0] line.encoded=False
[ "def", "decode", "(", "cls", ",", "line", ")", ":", "if", "line", ".", "encoded", ":", "if", "'BASE64'", "in", "line", ".", "singletonparams", ":", "line", ".", "singletonparams", ".", "remove", "(", "'BASE64'", ")", "line", ".", "encoding_param", "=", "cls", ".", "base64string", "encoding", "=", "getattr", "(", "line", ",", "'encoding_param'", ",", "None", ")", "if", "encoding", ":", "line", ".", "value", "=", "codecs", ".", "decode", "(", "line", ".", "value", ".", "encode", "(", "\"utf-8\"", ")", ",", "\"base64\"", ")", "else", ":", "line", ".", "value", "=", "stringToTextValues", "(", "line", ".", "value", ")", "[", "0", "]", "line", ".", "encoded", "=", "False" ]
Remove backslash escaping from line.valueDecode line, either to remove backslash espacing, or to decode base64 encoding. The content line should contain a ENCODING=b for base64 encoding, but Apple Addressbook seems to export a singleton parameter of 'BASE64', which does not match the 3.0 vCard spec. If we encouter that, then we transform the parameter to ENCODING=b
[ "Remove", "backslash", "escaping", "from", "line", ".", "valueDecode", "line", "either", "to", "remove", "backslash", "espacing", "or", "to", "decode", "base64", "encoding", ".", "The", "content", "line", "should", "contain", "a", "ENCODING", "=", "b", "for", "base64", "encoding", "but", "Apple", "Addressbook", "seems", "to", "export", "a", "singleton", "parameter", "of", "BASE64", "which", "does", "not", "match", "the", "3", ".", "0", "vCard", "spec", ".", "If", "we", "encouter", "that", "then", "we", "transform", "the", "parameter", "to", "ENCODING", "=", "b" ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/docs/build/lib/vobject/vcard.py#L124-L142
eventable/vobject
vobject/behavior.py
Behavior.validate
def validate(cls, obj, raiseException=False, complainUnrecognized=False): """Check if the object satisfies this behavior's requirements. @param obj: The L{ContentLine<base.ContentLine>} or L{Component<base.Component>} to be validated. @param raiseException: If True, raise a L{base.ValidateError} on validation failure. Otherwise return a boolean. @param complainUnrecognized: If True, fail to validate if an uncrecognized parameter or child is found. Otherwise log the lack of recognition. """ if not cls.allowGroup and obj.group is not None: err = "{0} has a group, but this object doesn't support groups".format(obj) raise base.VObjectError(err) if isinstance(obj, base.ContentLine): return cls.lineValidate(obj, raiseException, complainUnrecognized) elif isinstance(obj, base.Component): count = {} for child in obj.getChildren(): if not child.validate(raiseException, complainUnrecognized): return False name = child.name.upper() count[name] = count.get(name, 0) + 1 for key, val in cls.knownChildren.items(): if count.get(key, 0) < val[0]: if raiseException: m = "{0} components must contain at least {1} {2}" raise base.ValidateError(m .format(cls.name, val[0], key)) return False if val[1] and count.get(key, 0) > val[1]: if raiseException: m = "{0} components cannot contain more than {1} {2}" raise base.ValidateError(m.format(cls.name, val[1], key)) return False return True else: err = "{0} is not a Component or Contentline".format(obj) raise base.VObjectError(err)
python
def validate(cls, obj, raiseException=False, complainUnrecognized=False): if not cls.allowGroup and obj.group is not None: err = "{0} has a group, but this object doesn't support groups".format(obj) raise base.VObjectError(err) if isinstance(obj, base.ContentLine): return cls.lineValidate(obj, raiseException, complainUnrecognized) elif isinstance(obj, base.Component): count = {} for child in obj.getChildren(): if not child.validate(raiseException, complainUnrecognized): return False name = child.name.upper() count[name] = count.get(name, 0) + 1 for key, val in cls.knownChildren.items(): if count.get(key, 0) < val[0]: if raiseException: m = "{0} components must contain at least {1} {2}" raise base.ValidateError(m .format(cls.name, val[0], key)) return False if val[1] and count.get(key, 0) > val[1]: if raiseException: m = "{0} components cannot contain more than {1} {2}" raise base.ValidateError(m.format(cls.name, val[1], key)) return False return True else: err = "{0} is not a Component or Contentline".format(obj) raise base.VObjectError(err)
[ "def", "validate", "(", "cls", ",", "obj", ",", "raiseException", "=", "False", ",", "complainUnrecognized", "=", "False", ")", ":", "if", "not", "cls", ".", "allowGroup", "and", "obj", ".", "group", "is", "not", "None", ":", "err", "=", "\"{0} has a group, but this object doesn't support groups\"", ".", "format", "(", "obj", ")", "raise", "base", ".", "VObjectError", "(", "err", ")", "if", "isinstance", "(", "obj", ",", "base", ".", "ContentLine", ")", ":", "return", "cls", ".", "lineValidate", "(", "obj", ",", "raiseException", ",", "complainUnrecognized", ")", "elif", "isinstance", "(", "obj", ",", "base", ".", "Component", ")", ":", "count", "=", "{", "}", "for", "child", "in", "obj", ".", "getChildren", "(", ")", ":", "if", "not", "child", ".", "validate", "(", "raiseException", ",", "complainUnrecognized", ")", ":", "return", "False", "name", "=", "child", ".", "name", ".", "upper", "(", ")", "count", "[", "name", "]", "=", "count", ".", "get", "(", "name", ",", "0", ")", "+", "1", "for", "key", ",", "val", "in", "cls", ".", "knownChildren", ".", "items", "(", ")", ":", "if", "count", ".", "get", "(", "key", ",", "0", ")", "<", "val", "[", "0", "]", ":", "if", "raiseException", ":", "m", "=", "\"{0} components must contain at least {1} {2}\"", "raise", "base", ".", "ValidateError", "(", "m", ".", "format", "(", "cls", ".", "name", ",", "val", "[", "0", "]", ",", "key", ")", ")", "return", "False", "if", "val", "[", "1", "]", "and", "count", ".", "get", "(", "key", ",", "0", ")", ">", "val", "[", "1", "]", ":", "if", "raiseException", ":", "m", "=", "\"{0} components cannot contain more than {1} {2}\"", "raise", "base", ".", "ValidateError", "(", "m", ".", "format", "(", "cls", ".", "name", ",", "val", "[", "1", "]", ",", "key", ")", ")", "return", "False", "return", "True", "else", ":", "err", "=", "\"{0} is not a Component or Contentline\"", ".", "format", "(", "obj", ")", "raise", "base", ".", "VObjectError", "(", "err", ")" ]
Check if the object satisfies this behavior's requirements. @param obj: The L{ContentLine<base.ContentLine>} or L{Component<base.Component>} to be validated. @param raiseException: If True, raise a L{base.ValidateError} on validation failure. Otherwise return a boolean. @param complainUnrecognized: If True, fail to validate if an uncrecognized parameter or child is found. Otherwise log the lack of recognition.
[ "Check", "if", "the", "object", "satisfies", "this", "behavior", "s", "requirements", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/behavior.py#L63-L103
eventable/vobject
vobject/behavior.py
Behavior.serialize
def serialize(cls, obj, buf, lineLength, validate=True): """ Set implicit parameters, do encoding, return unicode string. If validate is True, raise VObjectError if the line doesn't validate after implicit parameters are generated. Default is to call base.defaultSerialize. """ cls.generateImplicitParameters(obj) if validate: cls.validate(obj, raiseException=True) if obj.isNative: transformed = obj.transformFromNative() undoTransform = True else: transformed = obj undoTransform = False out = base.defaultSerialize(transformed, buf, lineLength) if undoTransform: obj.transformToNative() return out
python
def serialize(cls, obj, buf, lineLength, validate=True): cls.generateImplicitParameters(obj) if validate: cls.validate(obj, raiseException=True) if obj.isNative: transformed = obj.transformFromNative() undoTransform = True else: transformed = obj undoTransform = False out = base.defaultSerialize(transformed, buf, lineLength) if undoTransform: obj.transformToNative() return out
[ "def", "serialize", "(", "cls", ",", "obj", ",", "buf", ",", "lineLength", ",", "validate", "=", "True", ")", ":", "cls", ".", "generateImplicitParameters", "(", "obj", ")", "if", "validate", ":", "cls", ".", "validate", "(", "obj", ",", "raiseException", "=", "True", ")", "if", "obj", ".", "isNative", ":", "transformed", "=", "obj", ".", "transformFromNative", "(", ")", "undoTransform", "=", "True", "else", ":", "transformed", "=", "obj", "undoTransform", "=", "False", "out", "=", "base", ".", "defaultSerialize", "(", "transformed", ",", "buf", ",", "lineLength", ")", "if", "undoTransform", ":", "obj", ".", "transformToNative", "(", ")", "return", "out" ]
Set implicit parameters, do encoding, return unicode string. If validate is True, raise VObjectError if the line doesn't validate after implicit parameters are generated. Default is to call base.defaultSerialize.
[ "Set", "implicit", "parameters", "do", "encoding", "return", "unicode", "string", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/behavior.py#L144-L169
eventable/vobject
vobject/win32tz.py
list_timezones
def list_timezones(): """Return a list of all time zones known to the system.""" l = [] for i in xrange(parentsize): l.append(_winreg.EnumKey(tzparent, i)) return l
python
def list_timezones(): l = [] for i in xrange(parentsize): l.append(_winreg.EnumKey(tzparent, i)) return l
[ "def", "list_timezones", "(", ")", ":", "l", "=", "[", "]", "for", "i", "in", "xrange", "(", "parentsize", ")", ":", "l", ".", "append", "(", "_winreg", ".", "EnumKey", "(", "tzparent", ",", "i", ")", ")", "return", "l" ]
Return a list of all time zones known to the system.
[ "Return", "a", "list", "of", "all", "time", "zones", "known", "to", "the", "system", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/win32tz.py#L15-L20
eventable/vobject
vobject/win32tz.py
pickNthWeekday
def pickNthWeekday(year, month, dayofweek, hour, minute, whichweek): """dayofweek == 0 means Sunday, whichweek > 4 means last instance""" first = datetime.datetime(year=year, month=month, hour=hour, minute=minute, day=1) weekdayone = first.replace(day=((dayofweek - first.isoweekday()) % 7 + 1)) for n in xrange(whichweek - 1, -1, -1): dt = weekdayone + n * WEEKS if dt.month == month: return dt
python
def pickNthWeekday(year, month, dayofweek, hour, minute, whichweek): first = datetime.datetime(year=year, month=month, hour=hour, minute=minute, day=1) weekdayone = first.replace(day=((dayofweek - first.isoweekday()) % 7 + 1)) for n in xrange(whichweek - 1, -1, -1): dt = weekdayone + n * WEEKS if dt.month == month: return dt
[ "def", "pickNthWeekday", "(", "year", ",", "month", ",", "dayofweek", ",", "hour", ",", "minute", ",", "whichweek", ")", ":", "first", "=", "datetime", ".", "datetime", "(", "year", "=", "year", ",", "month", "=", "month", ",", "hour", "=", "hour", ",", "minute", "=", "minute", ",", "day", "=", "1", ")", "weekdayone", "=", "first", ".", "replace", "(", "day", "=", "(", "(", "dayofweek", "-", "first", ".", "isoweekday", "(", ")", ")", "%", "7", "+", "1", ")", ")", "for", "n", "in", "xrange", "(", "whichweek", "-", "1", ",", "-", "1", ",", "-", "1", ")", ":", "dt", "=", "weekdayone", "+", "n", "*", "WEEKS", "if", "dt", ".", "month", "==", "month", ":", "return", "dt" ]
dayofweek == 0 means Sunday, whichweek > 4 means last instance
[ "dayofweek", "==", "0", "means", "Sunday", "whichweek", ">", "4", "means", "last", "instance" ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/win32tz.py#L77-L85
eventable/vobject
vobject/win32tz.py
valuesToDict
def valuesToDict(key): """Convert a registry key's values to a dictionary.""" d = {} size = _winreg.QueryInfoKey(key)[1] for i in xrange(size): d[_winreg.EnumValue(key, i)[0]] = _winreg.EnumValue(key, i)[1] return d
python
def valuesToDict(key): d = {} size = _winreg.QueryInfoKey(key)[1] for i in xrange(size): d[_winreg.EnumValue(key, i)[0]] = _winreg.EnumValue(key, i)[1] return d
[ "def", "valuesToDict", "(", "key", ")", ":", "d", "=", "{", "}", "size", "=", "_winreg", ".", "QueryInfoKey", "(", "key", ")", "[", "1", "]", "for", "i", "in", "xrange", "(", "size", ")", ":", "d", "[", "_winreg", ".", "EnumValue", "(", "key", ",", "i", ")", "[", "0", "]", "]", "=", "_winreg", ".", "EnumValue", "(", "key", ",", "i", ")", "[", "1", "]", "return", "d" ]
Convert a registry key's values to a dictionary.
[ "Convert", "a", "registry", "key", "s", "values", "to", "a", "dictionary", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/win32tz.py#L148-L154
eventable/vobject
docs/build/lib/vobject/ics_diff.py
diff
def diff(left, right): """ Take two VCALENDAR components, compare VEVENTs and VTODOs in them, return a list of object pairs containing just UID and the bits that didn't match, using None for objects that weren't present in one version or the other. When there are multiple ContentLines in one VEVENT, for instance many DESCRIPTION lines, such lines original order is assumed to be meaningful. Order is also preserved when comparing (the unlikely case of) multiple parameters of the same type in a ContentLine """ def processComponentLists(leftList, rightList): output = [] rightIndex = 0 rightListSize = len(rightList) for comp in leftList: if rightIndex >= rightListSize: output.append((comp, None)) else: leftKey = getSortKey(comp) rightComp = rightList[rightIndex] rightKey = getSortKey(rightComp) while leftKey > rightKey: output.append((None, rightComp)) rightIndex += 1 if rightIndex >= rightListSize: output.append((comp, None)) break else: rightComp = rightList[rightIndex] rightKey = getSortKey(rightComp) if leftKey < rightKey: output.append((comp, None)) elif leftKey == rightKey: rightIndex += 1 matchResult = processComponentPair(comp, rightComp) if matchResult is not None: output.append(matchResult) return output def newComponent(name, body): if body is None: return None else: c = Component(name) c.behavior = getBehavior(name) c.isNative = True return c def processComponentPair(leftComp, rightComp): """ Return None if a match, or a pair of components including UIDs and any differing children. """ leftChildKeys = leftComp.contents.keys() rightChildKeys = rightComp.contents.keys() differentContentLines = [] differentComponents = {} for key in leftChildKeys: rightList = rightComp.contents.get(key, []) if isinstance(leftComp.contents[key][0], Component): compDifference = processComponentLists(leftComp.contents[key], rightList) if len(compDifference) > 0: differentComponents[key] = compDifference elif leftComp.contents[key] != rightList: differentContentLines.append((leftComp.contents[key], rightList)) for key in rightChildKeys: if key not in leftChildKeys: if isinstance(rightComp.contents[key][0], Component): differentComponents[key] = ([], rightComp.contents[key]) else: differentContentLines.append(([], rightComp.contents[key])) if len(differentContentLines) == 0 and len(differentComponents) == 0: return None else: left = newFromBehavior(leftComp.name) right = newFromBehavior(leftComp.name) # add a UID, if one existed, despite the fact that they'll always be # the same uid = leftComp.getChildValue('uid') if uid is not None: left.add( 'uid').value = uid right.add('uid').value = uid for name, childPairList in differentComponents.items(): leftComponents, rightComponents = zip(*childPairList) if len(leftComponents) > 0: # filter out None left.contents[name] = filter(None, leftComponents) if len(rightComponents) > 0: # filter out None right.contents[name] = filter(None, rightComponents) for leftChildLine, rightChildLine in differentContentLines: nonEmpty = leftChildLine or rightChildLine name = nonEmpty[0].name if leftChildLine is not None: left.contents[name] = leftChildLine if rightChildLine is not None: right.contents[name] = rightChildLine return left, right vevents = processComponentLists(sortByUID(getattr(left, 'vevent_list', [])), sortByUID(getattr(right, 'vevent_list', []))) vtodos = processComponentLists(sortByUID(getattr(left, 'vtodo_list', [])), sortByUID(getattr(right, 'vtodo_list', []))) return vevents + vtodos
python
def diff(left, right): def processComponentLists(leftList, rightList): output = [] rightIndex = 0 rightListSize = len(rightList) for comp in leftList: if rightIndex >= rightListSize: output.append((comp, None)) else: leftKey = getSortKey(comp) rightComp = rightList[rightIndex] rightKey = getSortKey(rightComp) while leftKey > rightKey: output.append((None, rightComp)) rightIndex += 1 if rightIndex >= rightListSize: output.append((comp, None)) break else: rightComp = rightList[rightIndex] rightKey = getSortKey(rightComp) if leftKey < rightKey: output.append((comp, None)) elif leftKey == rightKey: rightIndex += 1 matchResult = processComponentPair(comp, rightComp) if matchResult is not None: output.append(matchResult) return output def newComponent(name, body): if body is None: return None else: c = Component(name) c.behavior = getBehavior(name) c.isNative = True return c def processComponentPair(leftComp, rightComp): leftChildKeys = leftComp.contents.keys() rightChildKeys = rightComp.contents.keys() differentContentLines = [] differentComponents = {} for key in leftChildKeys: rightList = rightComp.contents.get(key, []) if isinstance(leftComp.contents[key][0], Component): compDifference = processComponentLists(leftComp.contents[key], rightList) if len(compDifference) > 0: differentComponents[key] = compDifference elif leftComp.contents[key] != rightList: differentContentLines.append((leftComp.contents[key], rightList)) for key in rightChildKeys: if key not in leftChildKeys: if isinstance(rightComp.contents[key][0], Component): differentComponents[key] = ([], rightComp.contents[key]) else: differentContentLines.append(([], rightComp.contents[key])) if len(differentContentLines) == 0 and len(differentComponents) == 0: return None else: left = newFromBehavior(leftComp.name) right = newFromBehavior(leftComp.name) uid = leftComp.getChildValue('uid') if uid is not None: left.add( 'uid').value = uid right.add('uid').value = uid for name, childPairList in differentComponents.items(): leftComponents, rightComponents = zip(*childPairList) if len(leftComponents) > 0: left.contents[name] = filter(None, leftComponents) if len(rightComponents) > 0: right.contents[name] = filter(None, rightComponents) for leftChildLine, rightChildLine in differentContentLines: nonEmpty = leftChildLine or rightChildLine name = nonEmpty[0].name if leftChildLine is not None: left.contents[name] = leftChildLine if rightChildLine is not None: right.contents[name] = rightChildLine return left, right vevents = processComponentLists(sortByUID(getattr(left, 'vevent_list', [])), sortByUID(getattr(right, 'vevent_list', []))) vtodos = processComponentLists(sortByUID(getattr(left, 'vtodo_list', [])), sortByUID(getattr(right, 'vtodo_list', []))) return vevents + vtodos
[ "def", "diff", "(", "left", ",", "right", ")", ":", "def", "processComponentLists", "(", "leftList", ",", "rightList", ")", ":", "output", "=", "[", "]", "rightIndex", "=", "0", "rightListSize", "=", "len", "(", "rightList", ")", "for", "comp", "in", "leftList", ":", "if", "rightIndex", ">=", "rightListSize", ":", "output", ".", "append", "(", "(", "comp", ",", "None", ")", ")", "else", ":", "leftKey", "=", "getSortKey", "(", "comp", ")", "rightComp", "=", "rightList", "[", "rightIndex", "]", "rightKey", "=", "getSortKey", "(", "rightComp", ")", "while", "leftKey", ">", "rightKey", ":", "output", ".", "append", "(", "(", "None", ",", "rightComp", ")", ")", "rightIndex", "+=", "1", "if", "rightIndex", ">=", "rightListSize", ":", "output", ".", "append", "(", "(", "comp", ",", "None", ")", ")", "break", "else", ":", "rightComp", "=", "rightList", "[", "rightIndex", "]", "rightKey", "=", "getSortKey", "(", "rightComp", ")", "if", "leftKey", "<", "rightKey", ":", "output", ".", "append", "(", "(", "comp", ",", "None", ")", ")", "elif", "leftKey", "==", "rightKey", ":", "rightIndex", "+=", "1", "matchResult", "=", "processComponentPair", "(", "comp", ",", "rightComp", ")", "if", "matchResult", "is", "not", "None", ":", "output", ".", "append", "(", "matchResult", ")", "return", "output", "def", "newComponent", "(", "name", ",", "body", ")", ":", "if", "body", "is", "None", ":", "return", "None", "else", ":", "c", "=", "Component", "(", "name", ")", "c", ".", "behavior", "=", "getBehavior", "(", "name", ")", "c", ".", "isNative", "=", "True", "return", "c", "def", "processComponentPair", "(", "leftComp", ",", "rightComp", ")", ":", "\"\"\"\n Return None if a match, or a pair of components including UIDs and\n any differing children.\n\n \"\"\"", "leftChildKeys", "=", "leftComp", ".", "contents", ".", "keys", "(", ")", "rightChildKeys", "=", "rightComp", ".", "contents", ".", "keys", "(", ")", "differentContentLines", "=", "[", "]", "differentComponents", "=", "{", "}", "for", "key", "in", "leftChildKeys", ":", "rightList", "=", "rightComp", ".", "contents", ".", "get", "(", "key", ",", "[", "]", ")", "if", "isinstance", "(", "leftComp", ".", "contents", "[", "key", "]", "[", "0", "]", ",", "Component", ")", ":", "compDifference", "=", "processComponentLists", "(", "leftComp", ".", "contents", "[", "key", "]", ",", "rightList", ")", "if", "len", "(", "compDifference", ")", ">", "0", ":", "differentComponents", "[", "key", "]", "=", "compDifference", "elif", "leftComp", ".", "contents", "[", "key", "]", "!=", "rightList", ":", "differentContentLines", ".", "append", "(", "(", "leftComp", ".", "contents", "[", "key", "]", ",", "rightList", ")", ")", "for", "key", "in", "rightChildKeys", ":", "if", "key", "not", "in", "leftChildKeys", ":", "if", "isinstance", "(", "rightComp", ".", "contents", "[", "key", "]", "[", "0", "]", ",", "Component", ")", ":", "differentComponents", "[", "key", "]", "=", "(", "[", "]", ",", "rightComp", ".", "contents", "[", "key", "]", ")", "else", ":", "differentContentLines", ".", "append", "(", "(", "[", "]", ",", "rightComp", ".", "contents", "[", "key", "]", ")", ")", "if", "len", "(", "differentContentLines", ")", "==", "0", "and", "len", "(", "differentComponents", ")", "==", "0", ":", "return", "None", "else", ":", "left", "=", "newFromBehavior", "(", "leftComp", ".", "name", ")", "right", "=", "newFromBehavior", "(", "leftComp", ".", "name", ")", "# add a UID, if one existed, despite the fact that they'll always be", "# the same", "uid", "=", "leftComp", ".", "getChildValue", "(", "'uid'", ")", "if", "uid", "is", "not", "None", ":", "left", ".", "add", "(", "'uid'", ")", ".", "value", "=", "uid", "right", ".", "add", "(", "'uid'", ")", ".", "value", "=", "uid", "for", "name", ",", "childPairList", "in", "differentComponents", ".", "items", "(", ")", ":", "leftComponents", ",", "rightComponents", "=", "zip", "(", "*", "childPairList", ")", "if", "len", "(", "leftComponents", ")", ">", "0", ":", "# filter out None", "left", ".", "contents", "[", "name", "]", "=", "filter", "(", "None", ",", "leftComponents", ")", "if", "len", "(", "rightComponents", ")", ">", "0", ":", "# filter out None", "right", ".", "contents", "[", "name", "]", "=", "filter", "(", "None", ",", "rightComponents", ")", "for", "leftChildLine", ",", "rightChildLine", "in", "differentContentLines", ":", "nonEmpty", "=", "leftChildLine", "or", "rightChildLine", "name", "=", "nonEmpty", "[", "0", "]", ".", "name", "if", "leftChildLine", "is", "not", "None", ":", "left", ".", "contents", "[", "name", "]", "=", "leftChildLine", "if", "rightChildLine", "is", "not", "None", ":", "right", ".", "contents", "[", "name", "]", "=", "rightChildLine", "return", "left", ",", "right", "vevents", "=", "processComponentLists", "(", "sortByUID", "(", "getattr", "(", "left", ",", "'vevent_list'", ",", "[", "]", ")", ")", ",", "sortByUID", "(", "getattr", "(", "right", ",", "'vevent_list'", ",", "[", "]", ")", ")", ")", "vtodos", "=", "processComponentLists", "(", "sortByUID", "(", "getattr", "(", "left", ",", "'vtodo_list'", ",", "[", "]", ")", ")", ",", "sortByUID", "(", "getattr", "(", "right", ",", "'vtodo_list'", ",", "[", "]", ")", ")", ")", "return", "vevents", "+", "vtodos" ]
Take two VCALENDAR components, compare VEVENTs and VTODOs in them, return a list of object pairs containing just UID and the bits that didn't match, using None for objects that weren't present in one version or the other. When there are multiple ContentLines in one VEVENT, for instance many DESCRIPTION lines, such lines original order is assumed to be meaningful. Order is also preserved when comparing (the unlikely case of) multiple parameters of the same type in a ContentLine
[ "Take", "two", "VCALENDAR", "components", "compare", "VEVENTs", "and", "VTODOs", "in", "them", "return", "a", "list", "of", "object", "pairs", "containing", "just", "UID", "and", "the", "bits", "that", "didn", "t", "match", "using", "None", "for", "objects", "that", "weren", "t", "present", "in", "one", "version", "or", "the", "other", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/docs/build/lib/vobject/ics_diff.py#L47-L171
eventable/vobject
vobject/icalendar.py
stringToDateTime
def stringToDateTime(s, tzinfo=None): """ Returns datetime.datetime object. """ try: year = int(s[0:4]) month = int(s[4:6]) day = int(s[6:8]) hour = int(s[9:11]) minute = int(s[11:13]) second = int(s[13:15]) if len(s) > 15: if s[15] == 'Z': tzinfo = getTzid('UTC') except: raise ParseError("'{0!s}' is not a valid DATE-TIME".format(s)) year = year and year or 2000 if tzinfo is not None and hasattr(tzinfo,'localize'): # PyTZ case return tzinfo.localize(datetime.datetime(year, month, day, hour, minute, second)) return datetime.datetime(year, month, day, hour, minute, second, 0, tzinfo)
python
def stringToDateTime(s, tzinfo=None): try: year = int(s[0:4]) month = int(s[4:6]) day = int(s[6:8]) hour = int(s[9:11]) minute = int(s[11:13]) second = int(s[13:15]) if len(s) > 15: if s[15] == 'Z': tzinfo = getTzid('UTC') except: raise ParseError("'{0!s}' is not a valid DATE-TIME".format(s)) year = year and year or 2000 if tzinfo is not None and hasattr(tzinfo,'localize'): return tzinfo.localize(datetime.datetime(year, month, day, hour, minute, second)) return datetime.datetime(year, month, day, hour, minute, second, 0, tzinfo)
[ "def", "stringToDateTime", "(", "s", ",", "tzinfo", "=", "None", ")", ":", "try", ":", "year", "=", "int", "(", "s", "[", "0", ":", "4", "]", ")", "month", "=", "int", "(", "s", "[", "4", ":", "6", "]", ")", "day", "=", "int", "(", "s", "[", "6", ":", "8", "]", ")", "hour", "=", "int", "(", "s", "[", "9", ":", "11", "]", ")", "minute", "=", "int", "(", "s", "[", "11", ":", "13", "]", ")", "second", "=", "int", "(", "s", "[", "13", ":", "15", "]", ")", "if", "len", "(", "s", ")", ">", "15", ":", "if", "s", "[", "15", "]", "==", "'Z'", ":", "tzinfo", "=", "getTzid", "(", "'UTC'", ")", "except", ":", "raise", "ParseError", "(", "\"'{0!s}' is not a valid DATE-TIME\"", ".", "format", "(", "s", ")", ")", "year", "=", "year", "and", "year", "or", "2000", "if", "tzinfo", "is", "not", "None", "and", "hasattr", "(", "tzinfo", ",", "'localize'", ")", ":", "# PyTZ case", "return", "tzinfo", ".", "localize", "(", "datetime", ".", "datetime", "(", "year", ",", "month", ",", "day", ",", "hour", ",", "minute", ",", "second", ")", ")", "return", "datetime", ".", "datetime", "(", "year", ",", "month", ",", "day", ",", "hour", ",", "minute", ",", "second", ",", "0", ",", "tzinfo", ")" ]
Returns datetime.datetime object.
[ "Returns", "datetime", ".", "datetime", "object", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/icalendar.py#L1726-L1745
eventable/vobject
vobject/icalendar.py
VCalendar2_0.serialize
def serialize(cls, obj, buf, lineLength, validate=True): """ Set implicit parameters, do encoding, return unicode string. If validate is True, raise VObjectError if the line doesn't validate after implicit parameters are generated. Default is to call base.defaultSerialize. """ cls.generateImplicitParameters(obj) if validate: cls.validate(obj, raiseException=True) if obj.isNative: transformed = obj.transformFromNative() undoTransform = True else: transformed = obj undoTransform = False out = None outbuf = buf or six.StringIO() if obj.group is None: groupString = '' else: groupString = obj.group + '.' if obj.useBegin: foldOneLine(outbuf, "{0}BEGIN:{1}".format(groupString, obj.name), lineLength) try: first_props = [s for s in cls.sortFirst if s in obj.contents \ and not isinstance(obj.contents[s][0], Component)] first_components = [s for s in cls.sortFirst if s in obj.contents \ and isinstance(obj.contents[s][0], Component)] except Exception: first_props = first_components = [] # first_components = [] prop_keys = sorted(list(k for k in obj.contents.keys() if k not in first_props \ and not isinstance(obj.contents[k][0], Component))) comp_keys = sorted(list(k for k in obj.contents.keys() if k not in first_components \ and isinstance(obj.contents[k][0], Component))) sorted_keys = first_props + prop_keys + first_components + comp_keys children = [o for k in sorted_keys for o in obj.contents[k]] for child in children: # validate is recursive, we only need to validate once child.serialize(outbuf, lineLength, validate=False) if obj.useBegin: foldOneLine(outbuf, "{0}END:{1}".format(groupString, obj.name), lineLength) out = buf or outbuf.getvalue() if undoTransform: obj.transformToNative() return out
python
def serialize(cls, obj, buf, lineLength, validate=True): cls.generateImplicitParameters(obj) if validate: cls.validate(obj, raiseException=True) if obj.isNative: transformed = obj.transformFromNative() undoTransform = True else: transformed = obj undoTransform = False out = None outbuf = buf or six.StringIO() if obj.group is None: groupString = '' else: groupString = obj.group + '.' if obj.useBegin: foldOneLine(outbuf, "{0}BEGIN:{1}".format(groupString, obj.name), lineLength) try: first_props = [s for s in cls.sortFirst if s in obj.contents \ and not isinstance(obj.contents[s][0], Component)] first_components = [s for s in cls.sortFirst if s in obj.contents \ and isinstance(obj.contents[s][0], Component)] except Exception: first_props = first_components = [] prop_keys = sorted(list(k for k in obj.contents.keys() if k not in first_props \ and not isinstance(obj.contents[k][0], Component))) comp_keys = sorted(list(k for k in obj.contents.keys() if k not in first_components \ and isinstance(obj.contents[k][0], Component))) sorted_keys = first_props + prop_keys + first_components + comp_keys children = [o for k in sorted_keys for o in obj.contents[k]] for child in children: child.serialize(outbuf, lineLength, validate=False) if obj.useBegin: foldOneLine(outbuf, "{0}END:{1}".format(groupString, obj.name), lineLength) out = buf or outbuf.getvalue() if undoTransform: obj.transformToNative() return out
[ "def", "serialize", "(", "cls", ",", "obj", ",", "buf", ",", "lineLength", ",", "validate", "=", "True", ")", ":", "cls", ".", "generateImplicitParameters", "(", "obj", ")", "if", "validate", ":", "cls", ".", "validate", "(", "obj", ",", "raiseException", "=", "True", ")", "if", "obj", ".", "isNative", ":", "transformed", "=", "obj", ".", "transformFromNative", "(", ")", "undoTransform", "=", "True", "else", ":", "transformed", "=", "obj", "undoTransform", "=", "False", "out", "=", "None", "outbuf", "=", "buf", "or", "six", ".", "StringIO", "(", ")", "if", "obj", ".", "group", "is", "None", ":", "groupString", "=", "''", "else", ":", "groupString", "=", "obj", ".", "group", "+", "'.'", "if", "obj", ".", "useBegin", ":", "foldOneLine", "(", "outbuf", ",", "\"{0}BEGIN:{1}\"", ".", "format", "(", "groupString", ",", "obj", ".", "name", ")", ",", "lineLength", ")", "try", ":", "first_props", "=", "[", "s", "for", "s", "in", "cls", ".", "sortFirst", "if", "s", "in", "obj", ".", "contents", "and", "not", "isinstance", "(", "obj", ".", "contents", "[", "s", "]", "[", "0", "]", ",", "Component", ")", "]", "first_components", "=", "[", "s", "for", "s", "in", "cls", ".", "sortFirst", "if", "s", "in", "obj", ".", "contents", "and", "isinstance", "(", "obj", ".", "contents", "[", "s", "]", "[", "0", "]", ",", "Component", ")", "]", "except", "Exception", ":", "first_props", "=", "first_components", "=", "[", "]", "# first_components = []", "prop_keys", "=", "sorted", "(", "list", "(", "k", "for", "k", "in", "obj", ".", "contents", ".", "keys", "(", ")", "if", "k", "not", "in", "first_props", "and", "not", "isinstance", "(", "obj", ".", "contents", "[", "k", "]", "[", "0", "]", ",", "Component", ")", ")", ")", "comp_keys", "=", "sorted", "(", "list", "(", "k", "for", "k", "in", "obj", ".", "contents", ".", "keys", "(", ")", "if", "k", "not", "in", "first_components", "and", "isinstance", "(", "obj", ".", "contents", "[", "k", "]", "[", "0", "]", ",", "Component", ")", ")", ")", "sorted_keys", "=", "first_props", "+", "prop_keys", "+", "first_components", "+", "comp_keys", "children", "=", "[", "o", "for", "k", "in", "sorted_keys", "for", "o", "in", "obj", ".", "contents", "[", "k", "]", "]", "for", "child", "in", "children", ":", "# validate is recursive, we only need to validate once", "child", ".", "serialize", "(", "outbuf", ",", "lineLength", ",", "validate", "=", "False", ")", "if", "obj", ".", "useBegin", ":", "foldOneLine", "(", "outbuf", ",", "\"{0}END:{1}\"", ".", "format", "(", "groupString", ",", "obj", ".", "name", ")", ",", "lineLength", ")", "out", "=", "buf", "or", "outbuf", ".", "getvalue", "(", ")", "if", "undoTransform", ":", "obj", ".", "transformToNative", "(", ")", "return", "out" ]
Set implicit parameters, do encoding, return unicode string. If validate is True, raise VObjectError if the line doesn't validate after implicit parameters are generated. Default is to call base.defaultSerialize.
[ "Set", "implicit", "parameters", "do", "encoding", "return", "unicode", "string", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/icalendar.py#L988-L1044
eventable/vobject
vobject/ics_diff.py
deleteExtraneous
def deleteExtraneous(component, ignore_dtstamp=False): """ Recursively walk the component's children, deleting extraneous details like X-VOBJ-ORIGINAL-TZID. """ for comp in component.components(): deleteExtraneous(comp, ignore_dtstamp) for line in component.lines(): if 'X-VOBJ-ORIGINAL-TZID' in line.params: del line.params['X-VOBJ-ORIGINAL-TZID'] if ignore_dtstamp and hasattr(component, 'dtstamp_list'): del component.dtstamp_list
python
def deleteExtraneous(component, ignore_dtstamp=False): for comp in component.components(): deleteExtraneous(comp, ignore_dtstamp) for line in component.lines(): if 'X-VOBJ-ORIGINAL-TZID' in line.params: del line.params['X-VOBJ-ORIGINAL-TZID'] if ignore_dtstamp and hasattr(component, 'dtstamp_list'): del component.dtstamp_list
[ "def", "deleteExtraneous", "(", "component", ",", "ignore_dtstamp", "=", "False", ")", ":", "for", "comp", "in", "component", ".", "components", "(", ")", ":", "deleteExtraneous", "(", "comp", ",", "ignore_dtstamp", ")", "for", "line", "in", "component", ".", "lines", "(", ")", ":", "if", "'X-VOBJ-ORIGINAL-TZID'", "in", "line", ".", "params", ":", "del", "line", ".", "params", "[", "'X-VOBJ-ORIGINAL-TZID'", "]", "if", "ignore_dtstamp", "and", "hasattr", "(", "component", ",", "'dtstamp_list'", ")", ":", "del", "component", ".", "dtstamp_list" ]
Recursively walk the component's children, deleting extraneous details like X-VOBJ-ORIGINAL-TZID.
[ "Recursively", "walk", "the", "component", "s", "children", "deleting", "extraneous", "details", "like", "X", "-", "VOBJ", "-", "ORIGINAL", "-", "TZID", "." ]
train
https://github.com/eventable/vobject/blob/498555a553155ea9b26aace93332ae79365ecb31/vobject/ics_diff.py#L37-L48
SoftwareDefinedBuildings/XBOS
apps/hole_filling/pelican/backfill.py
fillPelicanHole
def fillPelicanHole(site, username, password, tstat_name, start_time, end_time): """Fill a hole in a Pelican thermostat's data stream. Arguments: site -- The thermostat's Pelican site name username -- The Pelican username for the site password -- The Pelican password for the site tstat_name -- The name of the thermostat, as identified by Pelican start_time -- The start of the data hole in UTC, e.g. "2018-01-29 15:00:00" end_time -- The end of the data hole in UTC, e.g. "2018-01-29 16:00:00" Returns: A Pandas dataframe with historical Pelican data that falls between the specified start and end times. Note that this function assumes the Pelican thermostat's local time zone is US/Pacific. It will properly handle PST vs. PDT. """ start = datetime.strptime(start_time, _INPUT_TIME_FORMAT).replace(tzinfo=pytz.utc).astimezone(_pelican_time) end = datetime.strptime(end_time, _INPUT_TIME_FORMAT).replace(tzinfo=pytz.utc).astimezone(_pelican_time) heat_needs_fan = _lookupHeatNeedsFan(site, username, password, tstat_name) if heat_needs_fan is None: return None # Pelican's API only allows a query covering a time range of up to 1 month # So we may need run multiple requests for historical data history_blocks = [] while start < end: block_start = start block_end = min(start + timedelta(days=30), end) blocks = _lookupHistoricalData(site, username, password, tstat_name, block_start, block_end) if blocks is None: return None history_blocks.extend(blocks) start += timedelta(days=30, minutes=1) output_rows = [] for block in history_blocks: runStatus = block.find("runStatus").text if runStatus.startswith("Heat"): fanState = (heatNeedsFan == "Yes") else: fanState = (runStatus != "Off") api_time = datetime.strptime(block.find("timestamp").text, "%Y-%m-%dT%H:%M").replace(tzinfo=_pelican_time) # Need to convert seconds to nanoseconds timestamp = int(api_time.timestamp() * 10**9) output_rows.append({ "temperature": float(block.find("temperature").text), "relative_humidity": float(block.find("humidity").text), "heating_setpoint": float(block.find("heatSetting").text), "cooling_setpoint": float(block.find("coolSetting").text), # Driver explicitly uses "Schedule" field, but we don't have this in history "override": block.find("setBy").text != "Schedule", "fan": fanState, "mode": _mode_name_mappings[block.find("system").text], "state": _state_mappings.get(runStatus, 0), "time": timestamp, }) df = pd.DataFrame(output_rows) df.drop_duplicates(subset="time", keep="first", inplace=True) return df
python
def fillPelicanHole(site, username, password, tstat_name, start_time, end_time): start = datetime.strptime(start_time, _INPUT_TIME_FORMAT).replace(tzinfo=pytz.utc).astimezone(_pelican_time) end = datetime.strptime(end_time, _INPUT_TIME_FORMAT).replace(tzinfo=pytz.utc).astimezone(_pelican_time) heat_needs_fan = _lookupHeatNeedsFan(site, username, password, tstat_name) if heat_needs_fan is None: return None history_blocks = [] while start < end: block_start = start block_end = min(start + timedelta(days=30), end) blocks = _lookupHistoricalData(site, username, password, tstat_name, block_start, block_end) if blocks is None: return None history_blocks.extend(blocks) start += timedelta(days=30, minutes=1) output_rows = [] for block in history_blocks: runStatus = block.find("runStatus").text if runStatus.startswith("Heat"): fanState = (heatNeedsFan == "Yes") else: fanState = (runStatus != "Off") api_time = datetime.strptime(block.find("timestamp").text, "%Y-%m-%dT%H:%M").replace(tzinfo=_pelican_time) timestamp = int(api_time.timestamp() * 10**9) output_rows.append({ "temperature": float(block.find("temperature").text), "relative_humidity": float(block.find("humidity").text), "heating_setpoint": float(block.find("heatSetting").text), "cooling_setpoint": float(block.find("coolSetting").text), "override": block.find("setBy").text != "Schedule", "fan": fanState, "mode": _mode_name_mappings[block.find("system").text], "state": _state_mappings.get(runStatus, 0), "time": timestamp, }) df = pd.DataFrame(output_rows) df.drop_duplicates(subset="time", keep="first", inplace=True) return df
[ "def", "fillPelicanHole", "(", "site", ",", "username", ",", "password", ",", "tstat_name", ",", "start_time", ",", "end_time", ")", ":", "start", "=", "datetime", ".", "strptime", "(", "start_time", ",", "_INPUT_TIME_FORMAT", ")", ".", "replace", "(", "tzinfo", "=", "pytz", ".", "utc", ")", ".", "astimezone", "(", "_pelican_time", ")", "end", "=", "datetime", ".", "strptime", "(", "end_time", ",", "_INPUT_TIME_FORMAT", ")", ".", "replace", "(", "tzinfo", "=", "pytz", ".", "utc", ")", ".", "astimezone", "(", "_pelican_time", ")", "heat_needs_fan", "=", "_lookupHeatNeedsFan", "(", "site", ",", "username", ",", "password", ",", "tstat_name", ")", "if", "heat_needs_fan", "is", "None", ":", "return", "None", "# Pelican's API only allows a query covering a time range of up to 1 month", "# So we may need run multiple requests for historical data", "history_blocks", "=", "[", "]", "while", "start", "<", "end", ":", "block_start", "=", "start", "block_end", "=", "min", "(", "start", "+", "timedelta", "(", "days", "=", "30", ")", ",", "end", ")", "blocks", "=", "_lookupHistoricalData", "(", "site", ",", "username", ",", "password", ",", "tstat_name", ",", "block_start", ",", "block_end", ")", "if", "blocks", "is", "None", ":", "return", "None", "history_blocks", ".", "extend", "(", "blocks", ")", "start", "+=", "timedelta", "(", "days", "=", "30", ",", "minutes", "=", "1", ")", "output_rows", "=", "[", "]", "for", "block", "in", "history_blocks", ":", "runStatus", "=", "block", ".", "find", "(", "\"runStatus\"", ")", ".", "text", "if", "runStatus", ".", "startswith", "(", "\"Heat\"", ")", ":", "fanState", "=", "(", "heatNeedsFan", "==", "\"Yes\"", ")", "else", ":", "fanState", "=", "(", "runStatus", "!=", "\"Off\"", ")", "api_time", "=", "datetime", ".", "strptime", "(", "block", ".", "find", "(", "\"timestamp\"", ")", ".", "text", ",", "\"%Y-%m-%dT%H:%M\"", ")", ".", "replace", "(", "tzinfo", "=", "_pelican_time", ")", "# Need to convert seconds to nanoseconds", "timestamp", "=", "int", "(", "api_time", ".", "timestamp", "(", ")", "*", "10", "**", "9", ")", "output_rows", ".", "append", "(", "{", "\"temperature\"", ":", "float", "(", "block", ".", "find", "(", "\"temperature\"", ")", ".", "text", ")", ",", "\"relative_humidity\"", ":", "float", "(", "block", ".", "find", "(", "\"humidity\"", ")", ".", "text", ")", ",", "\"heating_setpoint\"", ":", "float", "(", "block", ".", "find", "(", "\"heatSetting\"", ")", ".", "text", ")", ",", "\"cooling_setpoint\"", ":", "float", "(", "block", ".", "find", "(", "\"coolSetting\"", ")", ".", "text", ")", ",", "# Driver explicitly uses \"Schedule\" field, but we don't have this in history", "\"override\"", ":", "block", ".", "find", "(", "\"setBy\"", ")", ".", "text", "!=", "\"Schedule\"", ",", "\"fan\"", ":", "fanState", ",", "\"mode\"", ":", "_mode_name_mappings", "[", "block", ".", "find", "(", "\"system\"", ")", ".", "text", "]", ",", "\"state\"", ":", "_state_mappings", ".", "get", "(", "runStatus", ",", "0", ")", ",", "\"time\"", ":", "timestamp", ",", "}", ")", "df", "=", "pd", ".", "DataFrame", "(", "output_rows", ")", "df", ".", "drop_duplicates", "(", "subset", "=", "\"time\"", ",", "keep", "=", "\"first\"", ",", "inplace", "=", "True", ")", "return", "df" ]
Fill a hole in a Pelican thermostat's data stream. Arguments: site -- The thermostat's Pelican site name username -- The Pelican username for the site password -- The Pelican password for the site tstat_name -- The name of the thermostat, as identified by Pelican start_time -- The start of the data hole in UTC, e.g. "2018-01-29 15:00:00" end_time -- The end of the data hole in UTC, e.g. "2018-01-29 16:00:00" Returns: A Pandas dataframe with historical Pelican data that falls between the specified start and end times. Note that this function assumes the Pelican thermostat's local time zone is US/Pacific. It will properly handle PST vs. PDT.
[ "Fill", "a", "hole", "in", "a", "Pelican", "thermostat", "s", "data", "stream", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/hole_filling/pelican/backfill.py#L73-L137
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py
Preprocess_Data.add_degree_days
def add_degree_days(self, col='OAT', hdh_cpoint=65, cdh_cpoint=65): """ Adds Heating & Cooling Degree Hours. Parameters ---------- col : str Column name which contains the outdoor air temperature. hdh_cpoint : int Heating degree hours. Defaults to 65. cdh_cpoint : int Cooling degree hours. Defaults to 65. """ if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data # Calculate hdh data['hdh'] = data[col] over_hdh = data.loc[:, col] > hdh_cpoint data.loc[over_hdh, 'hdh'] = 0 data.loc[~over_hdh, 'hdh'] = hdh_cpoint - data.loc[~over_hdh, col] # Calculate cdh data['cdh'] = data[col] under_cdh = data.loc[:, col] < cdh_cpoint data.loc[under_cdh, 'cdh'] = 0 data.loc[~under_cdh, 'cdh'] = data.loc[~under_cdh, col] - cdh_cpoint self.preprocessed_data = data
python
def add_degree_days(self, col='OAT', hdh_cpoint=65, cdh_cpoint=65): if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data data['hdh'] = data[col] over_hdh = data.loc[:, col] > hdh_cpoint data.loc[over_hdh, 'hdh'] = 0 data.loc[~over_hdh, 'hdh'] = hdh_cpoint - data.loc[~over_hdh, col] data['cdh'] = data[col] under_cdh = data.loc[:, col] < cdh_cpoint data.loc[under_cdh, 'cdh'] = 0 data.loc[~under_cdh, 'cdh'] = data.loc[~under_cdh, col] - cdh_cpoint self.preprocessed_data = data
[ "def", "add_degree_days", "(", "self", ",", "col", "=", "'OAT'", ",", "hdh_cpoint", "=", "65", ",", "cdh_cpoint", "=", "65", ")", ":", "if", "self", ".", "preprocessed_data", ".", "empty", ":", "data", "=", "self", ".", "original_data", "else", ":", "data", "=", "self", ".", "preprocessed_data", "# Calculate hdh", "data", "[", "'hdh'", "]", "=", "data", "[", "col", "]", "over_hdh", "=", "data", ".", "loc", "[", ":", ",", "col", "]", ">", "hdh_cpoint", "data", ".", "loc", "[", "over_hdh", ",", "'hdh'", "]", "=", "0", "data", ".", "loc", "[", "~", "over_hdh", ",", "'hdh'", "]", "=", "hdh_cpoint", "-", "data", ".", "loc", "[", "~", "over_hdh", ",", "col", "]", "# Calculate cdh", "data", "[", "'cdh'", "]", "=", "data", "[", "col", "]", "under_cdh", "=", "data", ".", "loc", "[", ":", ",", "col", "]", "<", "cdh_cpoint", "data", ".", "loc", "[", "under_cdh", ",", "'cdh'", "]", "=", "0", "data", ".", "loc", "[", "~", "under_cdh", ",", "'cdh'", "]", "=", "data", ".", "loc", "[", "~", "under_cdh", ",", "col", "]", "-", "cdh_cpoint", "self", ".", "preprocessed_data", "=", "data" ]
Adds Heating & Cooling Degree Hours. Parameters ---------- col : str Column name which contains the outdoor air temperature. hdh_cpoint : int Heating degree hours. Defaults to 65. cdh_cpoint : int Cooling degree hours. Defaults to 65.
[ "Adds", "Heating", "&", "Cooling", "Degree", "Hours", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py#L34-L65
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py
Preprocess_Data.add_col_features
def add_col_features(self, col=None, degree=None): """ Exponentiate columns of dataframe. Basically this function squares/cubes a column. e.g. df[col^2] = pow(df[col], degree) where degree=2. Parameters ---------- col : list(str) Column to exponentiate. degree : list(str) Exponentiation degree. """ if not col and not degree: return else: if isinstance(col, list) and isinstance(degree, list): if len(col) != len(degree): print('col len: ', len(col)) print('degree len: ', len(degree)) raise ValueError('col and degree should have equal length.') else: if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data for i in range(len(col)): data.loc[:,col[i]+str(degree[i])] = pow(data.loc[:,col[i]],degree[i]) / pow(10,degree[i]-1) self.preprocessed_data = data else: raise TypeError('col and degree should be lists.')
python
def add_col_features(self, col=None, degree=None): if not col and not degree: return else: if isinstance(col, list) and isinstance(degree, list): if len(col) != len(degree): print('col len: ', len(col)) print('degree len: ', len(degree)) raise ValueError('col and degree should have equal length.') else: if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data for i in range(len(col)): data.loc[:,col[i]+str(degree[i])] = pow(data.loc[:,col[i]],degree[i]) / pow(10,degree[i]-1) self.preprocessed_data = data else: raise TypeError('col and degree should be lists.')
[ "def", "add_col_features", "(", "self", ",", "col", "=", "None", ",", "degree", "=", "None", ")", ":", "if", "not", "col", "and", "not", "degree", ":", "return", "else", ":", "if", "isinstance", "(", "col", ",", "list", ")", "and", "isinstance", "(", "degree", ",", "list", ")", ":", "if", "len", "(", "col", ")", "!=", "len", "(", "degree", ")", ":", "print", "(", "'col len: '", ",", "len", "(", "col", ")", ")", "print", "(", "'degree len: '", ",", "len", "(", "degree", ")", ")", "raise", "ValueError", "(", "'col and degree should have equal length.'", ")", "else", ":", "if", "self", ".", "preprocessed_data", ".", "empty", ":", "data", "=", "self", ".", "original_data", "else", ":", "data", "=", "self", ".", "preprocessed_data", "for", "i", "in", "range", "(", "len", "(", "col", ")", ")", ":", "data", ".", "loc", "[", ":", ",", "col", "[", "i", "]", "+", "str", "(", "degree", "[", "i", "]", ")", "]", "=", "pow", "(", "data", ".", "loc", "[", ":", ",", "col", "[", "i", "]", "]", ",", "degree", "[", "i", "]", ")", "/", "pow", "(", "10", ",", "degree", "[", "i", "]", "-", "1", ")", "self", ".", "preprocessed_data", "=", "data", "else", ":", "raise", "TypeError", "(", "'col and degree should be lists.'", ")" ]
Exponentiate columns of dataframe. Basically this function squares/cubes a column. e.g. df[col^2] = pow(df[col], degree) where degree=2. Parameters ---------- col : list(str) Column to exponentiate. degree : list(str) Exponentiation degree.
[ "Exponentiate", "columns", "of", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py#L68-L103
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py
Preprocess_Data.standardize
def standardize(self): """ Standardize data. """ if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data scaler = preprocessing.StandardScaler() data = pd.DataFrame(scaler.fit_transform(data), columns=data.columns, index=data.index) self.preprocessed_data = data
python
def standardize(self): if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data scaler = preprocessing.StandardScaler() data = pd.DataFrame(scaler.fit_transform(data), columns=data.columns, index=data.index) self.preprocessed_data = data
[ "def", "standardize", "(", "self", ")", ":", "if", "self", ".", "preprocessed_data", ".", "empty", ":", "data", "=", "self", ".", "original_data", "else", ":", "data", "=", "self", ".", "preprocessed_data", "scaler", "=", "preprocessing", ".", "StandardScaler", "(", ")", "data", "=", "pd", ".", "DataFrame", "(", "scaler", ".", "fit_transform", "(", "data", ")", ",", "columns", "=", "data", ".", "columns", ",", "index", "=", "data", ".", "index", ")", "self", ".", "preprocessed_data", "=", "data" ]
Standardize data.
[ "Standardize", "data", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py#L106-L116
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py
Preprocess_Data.normalize
def normalize(self): """ Normalize data. """ if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data data = pd.DataFrame(preprocessing.normalize(data), columns=data.columns, index=data.index) self.preprocessed_data = data
python
def normalize(self): if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data data = pd.DataFrame(preprocessing.normalize(data), columns=data.columns, index=data.index) self.preprocessed_data = data
[ "def", "normalize", "(", "self", ")", ":", "if", "self", ".", "preprocessed_data", ".", "empty", ":", "data", "=", "self", ".", "original_data", "else", ":", "data", "=", "self", ".", "preprocessed_data", "data", "=", "pd", ".", "DataFrame", "(", "preprocessing", ".", "normalize", "(", "data", ")", ",", "columns", "=", "data", ".", "columns", ",", "index", "=", "data", ".", "index", ")", "self", ".", "preprocessed_data", "=", "data" ]
Normalize data.
[ "Normalize", "data", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Preprocess_Data.py#L119-L128
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Preprocess_Data.py
Preprocess_Data.add_time_features
def add_time_features(self, year=False, month=False, week=True, tod=True, dow=True): """ Add time features to dataframe. Parameters ---------- year : bool Year. month : bool Month. week : bool Week. tod : bool Time of Day. dow : bool Day of Week. """ var_to_expand = [] if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data if year: data["year"] = data.index.year var_to_expand.append("year") if month: data["month"] = data.index.month var_to_expand.append("month") if week: data["week"] = data.index.week var_to_expand.append("week") if tod: data["tod"] = data.index.hour var_to_expand.append("tod") if dow: data["dow"] = data.index.weekday var_to_expand.append("dow") # One-hot encode the time features for var in var_to_expand: add_var = pd.get_dummies(data[var], prefix=var, drop_first=True) # Add all the columns to the model data data = data.join(add_var) # Drop the original column that was expanded data.drop(columns=[var], inplace=True) self.preprocessed_data = data
python
def add_time_features(self, year=False, month=False, week=True, tod=True, dow=True): var_to_expand = [] if self.preprocessed_data.empty: data = self.original_data else: data = self.preprocessed_data if year: data["year"] = data.index.year var_to_expand.append("year") if month: data["month"] = data.index.month var_to_expand.append("month") if week: data["week"] = data.index.week var_to_expand.append("week") if tod: data["tod"] = data.index.hour var_to_expand.append("tod") if dow: data["dow"] = data.index.weekday var_to_expand.append("dow") for var in var_to_expand: add_var = pd.get_dummies(data[var], prefix=var, drop_first=True) data = data.join(add_var) data.drop(columns=[var], inplace=True) self.preprocessed_data = data
[ "def", "add_time_features", "(", "self", ",", "year", "=", "False", ",", "month", "=", "False", ",", "week", "=", "True", ",", "tod", "=", "True", ",", "dow", "=", "True", ")", ":", "var_to_expand", "=", "[", "]", "if", "self", ".", "preprocessed_data", ".", "empty", ":", "data", "=", "self", ".", "original_data", "else", ":", "data", "=", "self", ".", "preprocessed_data", "if", "year", ":", "data", "[", "\"year\"", "]", "=", "data", ".", "index", ".", "year", "var_to_expand", ".", "append", "(", "\"year\"", ")", "if", "month", ":", "data", "[", "\"month\"", "]", "=", "data", ".", "index", ".", "month", "var_to_expand", ".", "append", "(", "\"month\"", ")", "if", "week", ":", "data", "[", "\"week\"", "]", "=", "data", ".", "index", ".", "week", "var_to_expand", ".", "append", "(", "\"week\"", ")", "if", "tod", ":", "data", "[", "\"tod\"", "]", "=", "data", ".", "index", ".", "hour", "var_to_expand", ".", "append", "(", "\"tod\"", ")", "if", "dow", ":", "data", "[", "\"dow\"", "]", "=", "data", ".", "index", ".", "weekday", "var_to_expand", ".", "append", "(", "\"dow\"", ")", "# One-hot encode the time features", "for", "var", "in", "var_to_expand", ":", "add_var", "=", "pd", ".", "get_dummies", "(", "data", "[", "var", "]", ",", "prefix", "=", "var", ",", "drop_first", "=", "True", ")", "# Add all the columns to the model data", "data", "=", "data", ".", "join", "(", "add_var", ")", "# Drop the original column that was expanded", "data", ".", "drop", "(", "columns", "=", "[", "var", "]", ",", "inplace", "=", "True", ")", "self", ".", "preprocessed_data", "=", "data" ]
Add time features to dataframe. Parameters ---------- year : bool Year. month : bool Month. week : bool Week. tod : bool Time of Day. dow : bool Day of Week.
[ "Add", "time", "features", "to", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Preprocess_Data.py#L135-L187
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Model_Data.py
Model_Data.split_data
def split_data(self): """ Split data according to baseline and projection time period values. """ try: # Extract data ranging in time_period1 time_period1 = (slice(self.baseline_period[0], self.baseline_period[1])) self.baseline_in = self.original_data.loc[time_period1, self.input_col] self.baseline_out = self.original_data.loc[time_period1, self.output_col] if self.exclude_time_period: for i in range(0, len(self.exclude_time_period), 2): # Drop data ranging in exclude_time_period1 exclude_time_period1 = (slice(self.exclude_time_period[i], self.exclude_time_period[i+1])) self.baseline_in.drop(self.baseline_in.loc[exclude_time_period1].index, axis=0, inplace=True) self.baseline_out.drop(self.baseline_out.loc[exclude_time_period1].index, axis=0, inplace=True) except Exception as e: raise e # CHECK: Can optimize this part # Error checking to ensure time_period values are valid if self.projection_period: for i in range(0, len(self.projection_period), 2): period = (slice(self.projection_period[i], self.projection_period[i+1])) try: self.original_data.loc[period, self.input_col] self.original_data.loc[period, self.output_col] except Exception as e: raise e
python
def split_data(self): try: time_period1 = (slice(self.baseline_period[0], self.baseline_period[1])) self.baseline_in = self.original_data.loc[time_period1, self.input_col] self.baseline_out = self.original_data.loc[time_period1, self.output_col] if self.exclude_time_period: for i in range(0, len(self.exclude_time_period), 2): exclude_time_period1 = (slice(self.exclude_time_period[i], self.exclude_time_period[i+1])) self.baseline_in.drop(self.baseline_in.loc[exclude_time_period1].index, axis=0, inplace=True) self.baseline_out.drop(self.baseline_out.loc[exclude_time_period1].index, axis=0, inplace=True) except Exception as e: raise e if self.projection_period: for i in range(0, len(self.projection_period), 2): period = (slice(self.projection_period[i], self.projection_period[i+1])) try: self.original_data.loc[period, self.input_col] self.original_data.loc[period, self.output_col] except Exception as e: raise e
[ "def", "split_data", "(", "self", ")", ":", "try", ":", "# Extract data ranging in time_period1", "time_period1", "=", "(", "slice", "(", "self", ".", "baseline_period", "[", "0", "]", ",", "self", ".", "baseline_period", "[", "1", "]", ")", ")", "self", ".", "baseline_in", "=", "self", ".", "original_data", ".", "loc", "[", "time_period1", ",", "self", ".", "input_col", "]", "self", ".", "baseline_out", "=", "self", ".", "original_data", ".", "loc", "[", "time_period1", ",", "self", ".", "output_col", "]", "if", "self", ".", "exclude_time_period", ":", "for", "i", "in", "range", "(", "0", ",", "len", "(", "self", ".", "exclude_time_period", ")", ",", "2", ")", ":", "# Drop data ranging in exclude_time_period1", "exclude_time_period1", "=", "(", "slice", "(", "self", ".", "exclude_time_period", "[", "i", "]", ",", "self", ".", "exclude_time_period", "[", "i", "+", "1", "]", ")", ")", "self", ".", "baseline_in", ".", "drop", "(", "self", ".", "baseline_in", ".", "loc", "[", "exclude_time_period1", "]", ".", "index", ",", "axis", "=", "0", ",", "inplace", "=", "True", ")", "self", ".", "baseline_out", ".", "drop", "(", "self", ".", "baseline_out", ".", "loc", "[", "exclude_time_period1", "]", ".", "index", ",", "axis", "=", "0", ",", "inplace", "=", "True", ")", "except", "Exception", "as", "e", ":", "raise", "e", "# CHECK: Can optimize this part", "# Error checking to ensure time_period values are valid", "if", "self", ".", "projection_period", ":", "for", "i", "in", "range", "(", "0", ",", "len", "(", "self", ".", "projection_period", ")", ",", "2", ")", ":", "period", "=", "(", "slice", "(", "self", ".", "projection_period", "[", "i", "]", ",", "self", ".", "projection_period", "[", "i", "+", "1", "]", ")", ")", "try", ":", "self", ".", "original_data", ".", "loc", "[", "period", ",", "self", ".", "input_col", "]", "self", ".", "original_data", ".", "loc", "[", "period", ",", "self", ".", "output_col", "]", "except", "Exception", "as", "e", ":", "raise", "e" ]
Split data according to baseline and projection time period values.
[ "Split", "data", "according", "to", "baseline", "and", "projection", "time", "period", "values", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Model_Data.py#L125-L152
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Model_Data.py
Model_Data.linear_regression
def linear_regression(self): """ Linear Regression. This function runs linear regression and stores the, 1. Model 2. Model name 3. Mean score of cross validation 4. Metrics """ model = LinearRegression() scores = [] kfold = KFold(n_splits=self.cv, shuffle=True, random_state=42) for i, (train, test) in enumerate(kfold.split(self.baseline_in, self.baseline_out)): model.fit(self.baseline_in.iloc[train], self.baseline_out.iloc[train]) scores.append(model.score(self.baseline_in.iloc[test], self.baseline_out.iloc[test])) mean_score = sum(scores) / len(scores) self.models.append(model) self.model_names.append('Linear Regression') self.max_scores.append(mean_score) self.metrics['Linear Regression'] = {} self.metrics['Linear Regression']['R2'] = mean_score self.metrics['Linear Regression']['Adj R2'] = self.adj_r2(mean_score, self.baseline_in.shape[0], self.baseline_in.shape[1])
python
def linear_regression(self): model = LinearRegression() scores = [] kfold = KFold(n_splits=self.cv, shuffle=True, random_state=42) for i, (train, test) in enumerate(kfold.split(self.baseline_in, self.baseline_out)): model.fit(self.baseline_in.iloc[train], self.baseline_out.iloc[train]) scores.append(model.score(self.baseline_in.iloc[test], self.baseline_out.iloc[test])) mean_score = sum(scores) / len(scores) self.models.append(model) self.model_names.append('Linear Regression') self.max_scores.append(mean_score) self.metrics['Linear Regression'] = {} self.metrics['Linear Regression']['R2'] = mean_score self.metrics['Linear Regression']['Adj R2'] = self.adj_r2(mean_score, self.baseline_in.shape[0], self.baseline_in.shape[1])
[ "def", "linear_regression", "(", "self", ")", ":", "model", "=", "LinearRegression", "(", ")", "scores", "=", "[", "]", "kfold", "=", "KFold", "(", "n_splits", "=", "self", ".", "cv", ",", "shuffle", "=", "True", ",", "random_state", "=", "42", ")", "for", "i", ",", "(", "train", ",", "test", ")", "in", "enumerate", "(", "kfold", ".", "split", "(", "self", ".", "baseline_in", ",", "self", ".", "baseline_out", ")", ")", ":", "model", ".", "fit", "(", "self", ".", "baseline_in", ".", "iloc", "[", "train", "]", ",", "self", ".", "baseline_out", ".", "iloc", "[", "train", "]", ")", "scores", ".", "append", "(", "model", ".", "score", "(", "self", ".", "baseline_in", ".", "iloc", "[", "test", "]", ",", "self", ".", "baseline_out", ".", "iloc", "[", "test", "]", ")", ")", "mean_score", "=", "sum", "(", "scores", ")", "/", "len", "(", "scores", ")", "self", ".", "models", ".", "append", "(", "model", ")", "self", ".", "model_names", ".", "append", "(", "'Linear Regression'", ")", "self", ".", "max_scores", ".", "append", "(", "mean_score", ")", "self", ".", "metrics", "[", "'Linear Regression'", "]", "=", "{", "}", "self", ".", "metrics", "[", "'Linear Regression'", "]", "[", "'R2'", "]", "=", "mean_score", "self", ".", "metrics", "[", "'Linear Regression'", "]", "[", "'Adj R2'", "]", "=", "self", ".", "adj_r2", "(", "mean_score", ",", "self", ".", "baseline_in", ".", "shape", "[", "0", "]", ",", "self", ".", "baseline_in", ".", "shape", "[", "1", "]", ")" ]
Linear Regression. This function runs linear regression and stores the, 1. Model 2. Model name 3. Mean score of cross validation 4. Metrics
[ "Linear", "Regression", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Model_Data.py#L176-L203
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Model_Data.py
Model_Data.lasso_regression
def lasso_regression(self): """ Lasso Regression. This function runs lasso regression and stores the, 1. Model 2. Model name 3. Max score 4. Metrics """ score_list = [] max_score = float('-inf') best_alpha = None for alpha in self.alphas: # model = Lasso(normalize=True, alpha=alpha, max_iter=5000) model = Lasso(alpha=alpha, max_iter=5000) model.fit(self.baseline_in, self.baseline_out.values.ravel()) scores = [] kfold = KFold(n_splits=self.cv, shuffle=True, random_state=42) for i, (train, test) in enumerate(kfold.split(self.baseline_in, self.baseline_out)): model.fit(self.baseline_in.iloc[train], self.baseline_out.iloc[train]) scores.append(model.score(self.baseline_in.iloc[test], self.baseline_out.iloc[test])) mean_score = np.mean(scores) score_list.append(mean_score) if mean_score > max_score: max_score = mean_score best_alpha = alpha # self.models.append(Lasso(normalize=True, alpha=best_alpha, max_iter=5000)) self.models.append(Lasso(alpha=best_alpha, max_iter=5000)) self.model_names.append('Lasso Regression') self.max_scores.append(max_score) self.metrics['Lasso Regression'] = {} self.metrics['Lasso Regression']['R2'] = max_score self.metrics['Lasso Regression']['Adj R2'] = self.adj_r2(max_score, self.baseline_in.shape[0], self.baseline_in.shape[1])
python
def lasso_regression(self): score_list = [] max_score = float('-inf') best_alpha = None for alpha in self.alphas: model = Lasso(alpha=alpha, max_iter=5000) model.fit(self.baseline_in, self.baseline_out.values.ravel()) scores = [] kfold = KFold(n_splits=self.cv, shuffle=True, random_state=42) for i, (train, test) in enumerate(kfold.split(self.baseline_in, self.baseline_out)): model.fit(self.baseline_in.iloc[train], self.baseline_out.iloc[train]) scores.append(model.score(self.baseline_in.iloc[test], self.baseline_out.iloc[test])) mean_score = np.mean(scores) score_list.append(mean_score) if mean_score > max_score: max_score = mean_score best_alpha = alpha self.models.append(Lasso(alpha=best_alpha, max_iter=5000)) self.model_names.append('Lasso Regression') self.max_scores.append(max_score) self.metrics['Lasso Regression'] = {} self.metrics['Lasso Regression']['R2'] = max_score self.metrics['Lasso Regression']['Adj R2'] = self.adj_r2(max_score, self.baseline_in.shape[0], self.baseline_in.shape[1])
[ "def", "lasso_regression", "(", "self", ")", ":", "score_list", "=", "[", "]", "max_score", "=", "float", "(", "'-inf'", ")", "best_alpha", "=", "None", "for", "alpha", "in", "self", ".", "alphas", ":", "# model = Lasso(normalize=True, alpha=alpha, max_iter=5000)", "model", "=", "Lasso", "(", "alpha", "=", "alpha", ",", "max_iter", "=", "5000", ")", "model", ".", "fit", "(", "self", ".", "baseline_in", ",", "self", ".", "baseline_out", ".", "values", ".", "ravel", "(", ")", ")", "scores", "=", "[", "]", "kfold", "=", "KFold", "(", "n_splits", "=", "self", ".", "cv", ",", "shuffle", "=", "True", ",", "random_state", "=", "42", ")", "for", "i", ",", "(", "train", ",", "test", ")", "in", "enumerate", "(", "kfold", ".", "split", "(", "self", ".", "baseline_in", ",", "self", ".", "baseline_out", ")", ")", ":", "model", ".", "fit", "(", "self", ".", "baseline_in", ".", "iloc", "[", "train", "]", ",", "self", ".", "baseline_out", ".", "iloc", "[", "train", "]", ")", "scores", ".", "append", "(", "model", ".", "score", "(", "self", ".", "baseline_in", ".", "iloc", "[", "test", "]", ",", "self", ".", "baseline_out", ".", "iloc", "[", "test", "]", ")", ")", "mean_score", "=", "np", ".", "mean", "(", "scores", ")", "score_list", ".", "append", "(", "mean_score", ")", "if", "mean_score", ">", "max_score", ":", "max_score", "=", "mean_score", "best_alpha", "=", "alpha", "# self.models.append(Lasso(normalize=True, alpha=best_alpha, max_iter=5000))", "self", ".", "models", ".", "append", "(", "Lasso", "(", "alpha", "=", "best_alpha", ",", "max_iter", "=", "5000", ")", ")", "self", ".", "model_names", ".", "append", "(", "'Lasso Regression'", ")", "self", ".", "max_scores", ".", "append", "(", "max_score", ")", "self", ".", "metrics", "[", "'Lasso Regression'", "]", "=", "{", "}", "self", ".", "metrics", "[", "'Lasso Regression'", "]", "[", "'R2'", "]", "=", "max_score", "self", ".", "metrics", "[", "'Lasso Regression'", "]", "[", "'Adj R2'", "]", "=", "self", ".", "adj_r2", "(", "max_score", ",", "self", ".", "baseline_in", ".", "shape", "[", "0", "]", ",", "self", ".", "baseline_in", ".", "shape", "[", "1", "]", ")" ]
Lasso Regression. This function runs lasso regression and stores the, 1. Model 2. Model name 3. Max score 4. Metrics
[ "Lasso", "Regression", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Model_Data.py#L206-L246
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Model_Data.py
Model_Data.random_forest
def random_forest(self): """ Random Forest. This function runs random forest and stores the, 1. Model 2. Model name 3. Max score 4. Metrics """ model = RandomForestRegressor(random_state=42) scores = [] kfold = KFold(n_splits=self.cv, shuffle=True, random_state=42) for i, (train, test) in enumerate(kfold.split(self.baseline_in, self.baseline_out)): model.fit(self.baseline_in.iloc[train], self.baseline_out.iloc[train]) scores.append(model.score(self.baseline_in.iloc[test], self.baseline_out.iloc[test])) mean_score = np.mean(scores) self.models.append(model) self.model_names.append('Random Forest Regressor') self.max_scores.append(mean_score) self.metrics['Random Forest Regressor'] = {} self.metrics['Random Forest Regressor']['R2'] = mean_score self.metrics['Random Forest Regressor']['Adj R2'] = self.adj_r2(mean_score, self.baseline_in.shape[0], self.baseline_in.shape[1])
python
def random_forest(self): model = RandomForestRegressor(random_state=42) scores = [] kfold = KFold(n_splits=self.cv, shuffle=True, random_state=42) for i, (train, test) in enumerate(kfold.split(self.baseline_in, self.baseline_out)): model.fit(self.baseline_in.iloc[train], self.baseline_out.iloc[train]) scores.append(model.score(self.baseline_in.iloc[test], self.baseline_out.iloc[test])) mean_score = np.mean(scores) self.models.append(model) self.model_names.append('Random Forest Regressor') self.max_scores.append(mean_score) self.metrics['Random Forest Regressor'] = {} self.metrics['Random Forest Regressor']['R2'] = mean_score self.metrics['Random Forest Regressor']['Adj R2'] = self.adj_r2(mean_score, self.baseline_in.shape[0], self.baseline_in.shape[1])
[ "def", "random_forest", "(", "self", ")", ":", "model", "=", "RandomForestRegressor", "(", "random_state", "=", "42", ")", "scores", "=", "[", "]", "kfold", "=", "KFold", "(", "n_splits", "=", "self", ".", "cv", ",", "shuffle", "=", "True", ",", "random_state", "=", "42", ")", "for", "i", ",", "(", "train", ",", "test", ")", "in", "enumerate", "(", "kfold", ".", "split", "(", "self", ".", "baseline_in", ",", "self", ".", "baseline_out", ")", ")", ":", "model", ".", "fit", "(", "self", ".", "baseline_in", ".", "iloc", "[", "train", "]", ",", "self", ".", "baseline_out", ".", "iloc", "[", "train", "]", ")", "scores", ".", "append", "(", "model", ".", "score", "(", "self", ".", "baseline_in", ".", "iloc", "[", "test", "]", ",", "self", ".", "baseline_out", ".", "iloc", "[", "test", "]", ")", ")", "mean_score", "=", "np", ".", "mean", "(", "scores", ")", "self", ".", "models", ".", "append", "(", "model", ")", "self", ".", "model_names", ".", "append", "(", "'Random Forest Regressor'", ")", "self", ".", "max_scores", ".", "append", "(", "mean_score", ")", "self", ".", "metrics", "[", "'Random Forest Regressor'", "]", "=", "{", "}", "self", ".", "metrics", "[", "'Random Forest Regressor'", "]", "[", "'R2'", "]", "=", "mean_score", "self", ".", "metrics", "[", "'Random Forest Regressor'", "]", "[", "'Adj R2'", "]", "=", "self", ".", "adj_r2", "(", "mean_score", ",", "self", ".", "baseline_in", ".", "shape", "[", "0", "]", ",", "self", ".", "baseline_in", ".", "shape", "[", "1", "]", ")" ]
Random Forest. This function runs random forest and stores the, 1. Model 2. Model name 3. Max score 4. Metrics
[ "Random", "Forest", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Model_Data.py#L338-L364
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Model_Data.py
Model_Data.run_models
def run_models(self): """ Run all models. Returns ------- model Best model dict Metrics of the models """ self.linear_regression() self.lasso_regression() self.ridge_regression() self.elastic_net_regression() self.random_forest() self.ann() # Index of the model with max score best_model_index = self.max_scores.index(max(self.max_scores)) # Store name of the optimal model self.best_model_name = self.model_names[best_model_index] # Store optimal model self.best_model = self.models[best_model_index] return self.metrics
python
def run_models(self): self.linear_regression() self.lasso_regression() self.ridge_regression() self.elastic_net_regression() self.random_forest() self.ann() best_model_index = self.max_scores.index(max(self.max_scores)) self.best_model_name = self.model_names[best_model_index] self.best_model = self.models[best_model_index] return self.metrics
[ "def", "run_models", "(", "self", ")", ":", "self", ".", "linear_regression", "(", ")", "self", ".", "lasso_regression", "(", ")", "self", ".", "ridge_regression", "(", ")", "self", ".", "elastic_net_regression", "(", ")", "self", ".", "random_forest", "(", ")", "self", ".", "ann", "(", ")", "# Index of the model with max score", "best_model_index", "=", "self", ".", "max_scores", ".", "index", "(", "max", "(", "self", ".", "max_scores", ")", ")", "# Store name of the optimal model", "self", ".", "best_model_name", "=", "self", ".", "model_names", "[", "best_model_index", "]", "# Store optimal model", "self", ".", "best_model", "=", "self", ".", "models", "[", "best_model_index", "]", "return", "self", ".", "metrics" ]
Run all models. Returns ------- model Best model dict Metrics of the models
[ "Run", "all", "models", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Model_Data.py#L396-L424
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Model_Data.py
Model_Data.custom_model
def custom_model(self, func): """ Run custom model provided by user. To Do, 1. Define custom function's parameters, its data types, and return types Parameters ---------- func : function Custom function Returns ------- dict Custom function's metrics """ y_pred = func(self.baseline_in, self.baseline_out) self.custom_metrics = {} self.custom_metrics['r2'] = r2_score(self.baseline_out, y_pred) self.custom_metrics['mse'] = mean_squared_error(self.baseline_out, y_pred) self.custom_metrics['rmse'] = math.sqrt(self.custom_metrics['mse']) self.custom_metrics['adj_r2'] = self.adj_r2(self.custom_metrics['r2'], self.baseline_in.shape[0], self.baseline_in.shape[1]) return self.custom_metrics
python
def custom_model(self, func): y_pred = func(self.baseline_in, self.baseline_out) self.custom_metrics = {} self.custom_metrics['r2'] = r2_score(self.baseline_out, y_pred) self.custom_metrics['mse'] = mean_squared_error(self.baseline_out, y_pred) self.custom_metrics['rmse'] = math.sqrt(self.custom_metrics['mse']) self.custom_metrics['adj_r2'] = self.adj_r2(self.custom_metrics['r2'], self.baseline_in.shape[0], self.baseline_in.shape[1]) return self.custom_metrics
[ "def", "custom_model", "(", "self", ",", "func", ")", ":", "y_pred", "=", "func", "(", "self", ".", "baseline_in", ",", "self", ".", "baseline_out", ")", "self", ".", "custom_metrics", "=", "{", "}", "self", ".", "custom_metrics", "[", "'r2'", "]", "=", "r2_score", "(", "self", ".", "baseline_out", ",", "y_pred", ")", "self", ".", "custom_metrics", "[", "'mse'", "]", "=", "mean_squared_error", "(", "self", ".", "baseline_out", ",", "y_pred", ")", "self", ".", "custom_metrics", "[", "'rmse'", "]", "=", "math", ".", "sqrt", "(", "self", ".", "custom_metrics", "[", "'mse'", "]", ")", "self", ".", "custom_metrics", "[", "'adj_r2'", "]", "=", "self", ".", "adj_r2", "(", "self", ".", "custom_metrics", "[", "'r2'", "]", ",", "self", ".", "baseline_in", ".", "shape", "[", "0", "]", ",", "self", ".", "baseline_in", ".", "shape", "[", "1", "]", ")", "return", "self", ".", "custom_metrics" ]
Run custom model provided by user. To Do, 1. Define custom function's parameters, its data types, and return types Parameters ---------- func : function Custom function Returns ------- dict Custom function's metrics
[ "Run", "custom", "model", "provided", "by", "user", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Model_Data.py#L427-L453
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Model_Data.py
Model_Data.best_model_fit
def best_model_fit(self): """ Fit data to optimal model and return its metrics. Returns ------- dict Best model's metrics """ self.best_model.fit(self.baseline_in, self.baseline_out) self.y_true = self.baseline_out # Pandas Series self.y_pred = self.best_model.predict(self.baseline_in) # numpy.ndarray # Set all negative values to zero since energy > 0 self.y_pred[self.y_pred < 0] = 0 # n and k values for adj r2 score self.n_test = self.baseline_in.shape[0] # Number of points in data sample self.k_test = self.baseline_in.shape[1] # Number of variables in model, excluding the constant # Store best model's metrics self.best_metrics['name'] = self.best_model_name self.best_metrics['r2'] = r2_score(self.y_true, self.y_pred) self.best_metrics['mse'] = mean_squared_error(self.y_true, self.y_pred) self.best_metrics['rmse'] = math.sqrt(self.best_metrics['mse']) self.best_metrics['adj_r2'] = self.adj_r2(self.best_metrics['r2'], self.n_test, self.k_test) # Normalized Mean Bias Error numerator = sum(self.y_true - self.y_pred) denominator = (self.n_test - self.k_test) * (sum(self.y_true) / len(self.y_true)) self.best_metrics['nmbe'] = numerator / denominator # MAPE can't have 0 values in baseline_out -> divide by zero error self.baseline_out_copy = self.baseline_out[self.baseline_out != 0] self.baseline_in_copy = self.baseline_in[self.baseline_in.index.isin(self.baseline_out_copy.index)] self.y_true_copy = self.baseline_out_copy # Pandas Series self.y_pred_copy = self.best_model.predict(self.baseline_in_copy) # numpy.ndarray self.best_metrics['mape'] = np.mean(np.abs((self.y_true_copy - self.y_pred_copy) / self.y_true_copy)) * 100 return self.best_metrics
python
def best_model_fit(self): self.best_model.fit(self.baseline_in, self.baseline_out) self.y_true = self.baseline_out self.y_pred = self.best_model.predict(self.baseline_in) self.y_pred[self.y_pred < 0] = 0 self.n_test = self.baseline_in.shape[0] self.k_test = self.baseline_in.shape[1] self.best_metrics['name'] = self.best_model_name self.best_metrics['r2'] = r2_score(self.y_true, self.y_pred) self.best_metrics['mse'] = mean_squared_error(self.y_true, self.y_pred) self.best_metrics['rmse'] = math.sqrt(self.best_metrics['mse']) self.best_metrics['adj_r2'] = self.adj_r2(self.best_metrics['r2'], self.n_test, self.k_test) numerator = sum(self.y_true - self.y_pred) denominator = (self.n_test - self.k_test) * (sum(self.y_true) / len(self.y_true)) self.best_metrics['nmbe'] = numerator / denominator self.baseline_out_copy = self.baseline_out[self.baseline_out != 0] self.baseline_in_copy = self.baseline_in[self.baseline_in.index.isin(self.baseline_out_copy.index)] self.y_true_copy = self.baseline_out_copy self.y_pred_copy = self.best_model.predict(self.baseline_in_copy) self.best_metrics['mape'] = np.mean(np.abs((self.y_true_copy - self.y_pred_copy) / self.y_true_copy)) * 100 return self.best_metrics
[ "def", "best_model_fit", "(", "self", ")", ":", "self", ".", "best_model", ".", "fit", "(", "self", ".", "baseline_in", ",", "self", ".", "baseline_out", ")", "self", ".", "y_true", "=", "self", ".", "baseline_out", "# Pandas Series", "self", ".", "y_pred", "=", "self", ".", "best_model", ".", "predict", "(", "self", ".", "baseline_in", ")", "# numpy.ndarray", "# Set all negative values to zero since energy > 0", "self", ".", "y_pred", "[", "self", ".", "y_pred", "<", "0", "]", "=", "0", "# n and k values for adj r2 score", "self", ".", "n_test", "=", "self", ".", "baseline_in", ".", "shape", "[", "0", "]", "# Number of points in data sample", "self", ".", "k_test", "=", "self", ".", "baseline_in", ".", "shape", "[", "1", "]", "# Number of variables in model, excluding the constant", "# Store best model's metrics", "self", ".", "best_metrics", "[", "'name'", "]", "=", "self", ".", "best_model_name", "self", ".", "best_metrics", "[", "'r2'", "]", "=", "r2_score", "(", "self", ".", "y_true", ",", "self", ".", "y_pred", ")", "self", ".", "best_metrics", "[", "'mse'", "]", "=", "mean_squared_error", "(", "self", ".", "y_true", ",", "self", ".", "y_pred", ")", "self", ".", "best_metrics", "[", "'rmse'", "]", "=", "math", ".", "sqrt", "(", "self", ".", "best_metrics", "[", "'mse'", "]", ")", "self", ".", "best_metrics", "[", "'adj_r2'", "]", "=", "self", ".", "adj_r2", "(", "self", ".", "best_metrics", "[", "'r2'", "]", ",", "self", ".", "n_test", ",", "self", ".", "k_test", ")", "# Normalized Mean Bias Error", "numerator", "=", "sum", "(", "self", ".", "y_true", "-", "self", ".", "y_pred", ")", "denominator", "=", "(", "self", ".", "n_test", "-", "self", ".", "k_test", ")", "*", "(", "sum", "(", "self", ".", "y_true", ")", "/", "len", "(", "self", ".", "y_true", ")", ")", "self", ".", "best_metrics", "[", "'nmbe'", "]", "=", "numerator", "/", "denominator", "# MAPE can't have 0 values in baseline_out -> divide by zero error", "self", ".", "baseline_out_copy", "=", "self", ".", "baseline_out", "[", "self", ".", "baseline_out", "!=", "0", "]", "self", ".", "baseline_in_copy", "=", "self", ".", "baseline_in", "[", "self", ".", "baseline_in", ".", "index", ".", "isin", "(", "self", ".", "baseline_out_copy", ".", "index", ")", "]", "self", ".", "y_true_copy", "=", "self", ".", "baseline_out_copy", "# Pandas Series", "self", ".", "y_pred_copy", "=", "self", ".", "best_model", ".", "predict", "(", "self", ".", "baseline_in_copy", ")", "# numpy.ndarray", "self", ".", "best_metrics", "[", "'mape'", "]", "=", "np", ".", "mean", "(", "np", ".", "abs", "(", "(", "self", ".", "y_true_copy", "-", "self", ".", "y_pred_copy", ")", "/", "self", ".", "y_true_copy", ")", ")", "*", "100", "return", "self", ".", "best_metrics" ]
Fit data to optimal model and return its metrics. Returns ------- dict Best model's metrics
[ "Fit", "data", "to", "optimal", "model", "and", "return", "its", "metrics", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Model_Data.py#L456-L497
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Plot_Data.py
Plot_Data.correlation_plot
def correlation_plot(self, data): """ Create heatmap of Pearson's correlation coefficient. Parameters ---------- data : pd.DataFrame() Data to display. Returns ------- matplotlib.figure Heatmap. """ # CHECK: Add saved filename in result.json fig = plt.figure(Plot_Data.count) corr = data.corr() ax = sns.heatmap(corr) Plot_Data.count += 1 return fig
python
def correlation_plot(self, data): fig = plt.figure(Plot_Data.count) corr = data.corr() ax = sns.heatmap(corr) Plot_Data.count += 1 return fig
[ "def", "correlation_plot", "(", "self", ",", "data", ")", ":", "# CHECK: Add saved filename in result.json", "fig", "=", "plt", ".", "figure", "(", "Plot_Data", ".", "count", ")", "corr", "=", "data", ".", "corr", "(", ")", "ax", "=", "sns", ".", "heatmap", "(", "corr", ")", "Plot_Data", ".", "count", "+=", "1", "return", "fig" ]
Create heatmap of Pearson's correlation coefficient. Parameters ---------- data : pd.DataFrame() Data to display. Returns ------- matplotlib.figure Heatmap.
[ "Create", "heatmap", "of", "Pearson", "s", "correlation", "coefficient", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Plot_Data.py#L42-L63
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Plot_Data.py
Plot_Data.baseline_projection_plot
def baseline_projection_plot(self, y_true, y_pred, baseline_period, projection_period, model_name, adj_r2, data, input_col, output_col, model, site): """ Create baseline and projection plots. Parameters ---------- y_true : pd.Series() Actual y values. y_pred : np.ndarray Predicted y values. baseline_period : list(str) Baseline period. projection_period : list(str) Projection periods. model_name : str Optimal model's name. adj_r2 : float Adjusted R2 score of optimal model. data : pd.Dataframe() Data containing real values. input_col : list(str) Predictor column(s). output_col : str Target column. model : func Optimal model. Returns ------- matplotlib.figure Baseline plot """ # Baseline and projection plots fig = plt.figure(Plot_Data.count) # Number of plots to display if projection_period: nrows = len(baseline_period) + len(projection_period) / 2 else: nrows = len(baseline_period) / 2 # Plot 1 - Baseline base_df = pd.DataFrame() base_df['y_true'] = y_true base_df['y_pred'] = y_pred ax1 = fig.add_subplot(nrows, 1, 1) base_df.plot(ax=ax1, figsize=self.figsize, title='Baseline Period ({}-{}). \nBest Model: {}. \nBaseline Adj R2: {}. \nSite: {}.'.format(baseline_period[0], baseline_period[1], model_name, adj_r2, site)) if projection_period: # Display projection plots num_plot = 2 for i in range(0, len(projection_period), 2): ax = fig.add_subplot(nrows, 1, num_plot) period = (slice(projection_period[i], projection_period[i+1])) project_df = pd.DataFrame() try: project_df['y_true'] = data.loc[period, output_col] project_df['y_pred'] = model.predict(data.loc[period, input_col]) # Set all negative values to zero since energy > 0 project_df['y_pred'][project_df['y_pred'] < 0] = 0 project_df.plot(ax=ax, figsize=self.figsize, title='Projection Period ({}-{})'.format(projection_period[i], projection_period[i+1])) num_plot += 1 fig.tight_layout() Plot_Data.count += 1 return fig, project_df['y_true'], project_df['y_pred'] except: raise TypeError("If projecting into the future, please specify project_ind_col that has data available \ in the future time period requested.") return fig, None, None
python
def baseline_projection_plot(self, y_true, y_pred, baseline_period, projection_period, model_name, adj_r2, data, input_col, output_col, model, site): fig = plt.figure(Plot_Data.count) if projection_period: nrows = len(baseline_period) + len(projection_period) / 2 else: nrows = len(baseline_period) / 2 base_df = pd.DataFrame() base_df['y_true'] = y_true base_df['y_pred'] = y_pred ax1 = fig.add_subplot(nrows, 1, 1) base_df.plot(ax=ax1, figsize=self.figsize, title='Baseline Period ({}-{}). \nBest Model: {}. \nBaseline Adj R2: {}. \nSite: {}.'.format(baseline_period[0], baseline_period[1], model_name, adj_r2, site)) if projection_period: num_plot = 2 for i in range(0, len(projection_period), 2): ax = fig.add_subplot(nrows, 1, num_plot) period = (slice(projection_period[i], projection_period[i+1])) project_df = pd.DataFrame() try: project_df['y_true'] = data.loc[period, output_col] project_df['y_pred'] = model.predict(data.loc[period, input_col]) project_df['y_pred'][project_df['y_pred'] < 0] = 0 project_df.plot(ax=ax, figsize=self.figsize, title='Projection Period ({}-{})'.format(projection_period[i], projection_period[i+1])) num_plot += 1 fig.tight_layout() Plot_Data.count += 1 return fig, project_df['y_true'], project_df['y_pred'] except: raise TypeError("If projecting into the future, please specify project_ind_col that has data available \ in the future time period requested.") return fig, None, None
[ "def", "baseline_projection_plot", "(", "self", ",", "y_true", ",", "y_pred", ",", "baseline_period", ",", "projection_period", ",", "model_name", ",", "adj_r2", ",", "data", ",", "input_col", ",", "output_col", ",", "model", ",", "site", ")", ":", "# Baseline and projection plots", "fig", "=", "plt", ".", "figure", "(", "Plot_Data", ".", "count", ")", "# Number of plots to display", "if", "projection_period", ":", "nrows", "=", "len", "(", "baseline_period", ")", "+", "len", "(", "projection_period", ")", "/", "2", "else", ":", "nrows", "=", "len", "(", "baseline_period", ")", "/", "2", "# Plot 1 - Baseline", "base_df", "=", "pd", ".", "DataFrame", "(", ")", "base_df", "[", "'y_true'", "]", "=", "y_true", "base_df", "[", "'y_pred'", "]", "=", "y_pred", "ax1", "=", "fig", ".", "add_subplot", "(", "nrows", ",", "1", ",", "1", ")", "base_df", ".", "plot", "(", "ax", "=", "ax1", ",", "figsize", "=", "self", ".", "figsize", ",", "title", "=", "'Baseline Period ({}-{}). \\nBest Model: {}. \\nBaseline Adj R2: {}. \\nSite: {}.'", ".", "format", "(", "baseline_period", "[", "0", "]", ",", "baseline_period", "[", "1", "]", ",", "model_name", ",", "adj_r2", ",", "site", ")", ")", "if", "projection_period", ":", "# Display projection plots", "num_plot", "=", "2", "for", "i", "in", "range", "(", "0", ",", "len", "(", "projection_period", ")", ",", "2", ")", ":", "ax", "=", "fig", ".", "add_subplot", "(", "nrows", ",", "1", ",", "num_plot", ")", "period", "=", "(", "slice", "(", "projection_period", "[", "i", "]", ",", "projection_period", "[", "i", "+", "1", "]", ")", ")", "project_df", "=", "pd", ".", "DataFrame", "(", ")", "try", ":", "project_df", "[", "'y_true'", "]", "=", "data", ".", "loc", "[", "period", ",", "output_col", "]", "project_df", "[", "'y_pred'", "]", "=", "model", ".", "predict", "(", "data", ".", "loc", "[", "period", ",", "input_col", "]", ")", "# Set all negative values to zero since energy > 0", "project_df", "[", "'y_pred'", "]", "[", "project_df", "[", "'y_pred'", "]", "<", "0", "]", "=", "0", "project_df", ".", "plot", "(", "ax", "=", "ax", ",", "figsize", "=", "self", ".", "figsize", ",", "title", "=", "'Projection Period ({}-{})'", ".", "format", "(", "projection_period", "[", "i", "]", ",", "projection_period", "[", "i", "+", "1", "]", ")", ")", "num_plot", "+=", "1", "fig", ".", "tight_layout", "(", ")", "Plot_Data", ".", "count", "+=", "1", "return", "fig", ",", "project_df", "[", "'y_true'", "]", ",", "project_df", "[", "'y_pred'", "]", "except", ":", "raise", "TypeError", "(", "\"If projecting into the future, please specify project_ind_col that has data available \\\n in the future time period requested.\"", ")", "return", "fig", ",", "None", ",", "None" ]
Create baseline and projection plots. Parameters ---------- y_true : pd.Series() Actual y values. y_pred : np.ndarray Predicted y values. baseline_period : list(str) Baseline period. projection_period : list(str) Projection periods. model_name : str Optimal model's name. adj_r2 : float Adjusted R2 score of optimal model. data : pd.Dataframe() Data containing real values. input_col : list(str) Predictor column(s). output_col : str Target column. model : func Optimal model. Returns ------- matplotlib.figure Baseline plot
[ "Create", "baseline", "and", "projection", "plots", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Plot_Data.py#L66-L147
SoftwareDefinedBuildings/XBOS
apps/system_identification/rtu_energy.py
get_thermostat_meter_data
def get_thermostat_meter_data(zone): """ This method subscribes to the output of the meter for the given zone. It returns a handler to call when you want to stop subscribing data, which returns a list of the data readins over that time period """ meter_uri = zone2meter.get(zone, "None") data = [] def cb(msg): for po in msg.payload_objects: if po.type_dotted == (2,0,9,1): m = msgpack.unpackb(po.content) data.append(m['current_demand']) handle = c.subscribe(meter_uri+"/signal/meter", cb) def stop(): c.unsubscribe(handle) return data return stop
python
def get_thermostat_meter_data(zone): meter_uri = zone2meter.get(zone, "None") data = [] def cb(msg): for po in msg.payload_objects: if po.type_dotted == (2,0,9,1): m = msgpack.unpackb(po.content) data.append(m['current_demand']) handle = c.subscribe(meter_uri+"/signal/meter", cb) def stop(): c.unsubscribe(handle) return data return stop
[ "def", "get_thermostat_meter_data", "(", "zone", ")", ":", "meter_uri", "=", "zone2meter", ".", "get", "(", "zone", ",", "\"None\"", ")", "data", "=", "[", "]", "def", "cb", "(", "msg", ")", ":", "for", "po", "in", "msg", ".", "payload_objects", ":", "if", "po", ".", "type_dotted", "==", "(", "2", ",", "0", ",", "9", ",", "1", ")", ":", "m", "=", "msgpack", ".", "unpackb", "(", "po", ".", "content", ")", "data", ".", "append", "(", "m", "[", "'current_demand'", "]", ")", "handle", "=", "c", ".", "subscribe", "(", "meter_uri", "+", "\"/signal/meter\"", ",", "cb", ")", "def", "stop", "(", ")", ":", "c", ".", "unsubscribe", "(", "handle", ")", "return", "data", "return", "stop" ]
This method subscribes to the output of the meter for the given zone. It returns a handler to call when you want to stop subscribing data, which returns a list of the data readins over that time period
[ "This", "method", "subscribes", "to", "the", "output", "of", "the", "meter", "for", "the", "given", "zone", ".", "It", "returns", "a", "handler", "to", "call", "when", "you", "want", "to", "stop", "subscribing", "data", "which", "returns", "a", "list", "of", "the", "data", "readins", "over", "that", "time", "period" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/system_identification/rtu_energy.py#L53-L71
SoftwareDefinedBuildings/XBOS
apps/system_identification/rtu_energy.py
call_heat
def call_heat(tstat): """ Adjusts the temperature setpoints in order to call for heating. Returns a handler to call when you want to reset the thermostat """ current_hsp, current_csp = tstat.heating_setpoint, tstat.cooling_setpoint current_temp = tstat.temperature tstat.write({ 'heating_setpoint': current_temp+10, 'cooling_setpoint': current_temp+20, 'mode': HEAT, }) def restore(): tstat.write({ 'heating_setpoint': current_hsp, 'cooling_setpoint': current_csp, 'mode': AUTO, }) return restore
python
def call_heat(tstat): current_hsp, current_csp = tstat.heating_setpoint, tstat.cooling_setpoint current_temp = tstat.temperature tstat.write({ 'heating_setpoint': current_temp+10, 'cooling_setpoint': current_temp+20, 'mode': HEAT, }) def restore(): tstat.write({ 'heating_setpoint': current_hsp, 'cooling_setpoint': current_csp, 'mode': AUTO, }) return restore
[ "def", "call_heat", "(", "tstat", ")", ":", "current_hsp", ",", "current_csp", "=", "tstat", ".", "heating_setpoint", ",", "tstat", ".", "cooling_setpoint", "current_temp", "=", "tstat", ".", "temperature", "tstat", ".", "write", "(", "{", "'heating_setpoint'", ":", "current_temp", "+", "10", ",", "'cooling_setpoint'", ":", "current_temp", "+", "20", ",", "'mode'", ":", "HEAT", ",", "}", ")", "def", "restore", "(", ")", ":", "tstat", ".", "write", "(", "{", "'heating_setpoint'", ":", "current_hsp", ",", "'cooling_setpoint'", ":", "current_csp", ",", "'mode'", ":", "AUTO", ",", "}", ")", "return", "restore" ]
Adjusts the temperature setpoints in order to call for heating. Returns a handler to call when you want to reset the thermostat
[ "Adjusts", "the", "temperature", "setpoints", "in", "order", "to", "call", "for", "heating", ".", "Returns", "a", "handler", "to", "call", "when", "you", "want", "to", "reset", "the", "thermostat" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/system_identification/rtu_energy.py#L73-L92
SoftwareDefinedBuildings/XBOS
apps/system_identification/rtu_energy.py
call_cool
def call_cool(tstat): """ Adjusts the temperature setpoints in order to call for cooling. Returns a handler to call when you want to reset the thermostat """ current_hsp, current_csp = tstat.heating_setpoint, tstat.cooling_setpoint current_temp = tstat.temperature tstat.write({ 'heating_setpoint': current_temp-20, 'cooling_setpoint': current_temp-10, 'mode': COOL, }) def restore(): tstat.write({ 'heating_setpoint': current_hsp, 'cooling_setpoint': current_csp, 'mode': AUTO, }) return restore
python
def call_cool(tstat): current_hsp, current_csp = tstat.heating_setpoint, tstat.cooling_setpoint current_temp = tstat.temperature tstat.write({ 'heating_setpoint': current_temp-20, 'cooling_setpoint': current_temp-10, 'mode': COOL, }) def restore(): tstat.write({ 'heating_setpoint': current_hsp, 'cooling_setpoint': current_csp, 'mode': AUTO, }) return restore
[ "def", "call_cool", "(", "tstat", ")", ":", "current_hsp", ",", "current_csp", "=", "tstat", ".", "heating_setpoint", ",", "tstat", ".", "cooling_setpoint", "current_temp", "=", "tstat", ".", "temperature", "tstat", ".", "write", "(", "{", "'heating_setpoint'", ":", "current_temp", "-", "20", ",", "'cooling_setpoint'", ":", "current_temp", "-", "10", ",", "'mode'", ":", "COOL", ",", "}", ")", "def", "restore", "(", ")", ":", "tstat", ".", "write", "(", "{", "'heating_setpoint'", ":", "current_hsp", ",", "'cooling_setpoint'", ":", "current_csp", ",", "'mode'", ":", "AUTO", ",", "}", ")", "return", "restore" ]
Adjusts the temperature setpoints in order to call for cooling. Returns a handler to call when you want to reset the thermostat
[ "Adjusts", "the", "temperature", "setpoints", "in", "order", "to", "call", "for", "cooling", ".", "Returns", "a", "handler", "to", "call", "when", "you", "want", "to", "reset", "the", "thermostat" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/system_identification/rtu_energy.py#L94-L113
SoftwareDefinedBuildings/XBOS
apps/system_identification/rtu_energy.py
call_fan
def call_fan(tstat): """ Toggles the fan """ old_fan = tstat.fan tstat.write({ 'fan': not old_fan, }) def restore(): tstat.write({ 'fan': old_fan, }) return restore
python
def call_fan(tstat): old_fan = tstat.fan tstat.write({ 'fan': not old_fan, }) def restore(): tstat.write({ 'fan': old_fan, }) return restore
[ "def", "call_fan", "(", "tstat", ")", ":", "old_fan", "=", "tstat", ".", "fan", "tstat", ".", "write", "(", "{", "'fan'", ":", "not", "old_fan", ",", "}", ")", "def", "restore", "(", ")", ":", "tstat", ".", "write", "(", "{", "'fan'", ":", "old_fan", ",", "}", ")", "return", "restore" ]
Toggles the fan
[ "Toggles", "the", "fan" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/system_identification/rtu_energy.py#L115-L129
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_Data.import_csv
def import_csv(self, file_name='*', folder_name='.', head_row=0, index_col=0, convert_col=True, concat_files=False): """ Imports csv file(s) and stores the result in data. Note ---- 1. If folder exists out of current directory, folder_name should contain correct regex 2. Assuming there's no file called "\*.csv" Parameters ---------- file_name : str CSV file to be imported. Defaults to '\*', i.e. all csv files in the folder. folder_name : str Folder where file resides. Defaults to '.', i.e. current directory. head_row : int Skips all rows from 0 to head_row-1 index_col : int Skips all columns from 0 to index_col-1 convert_col : bool Convert columns to numeric type concat_files : bool Appends data from files to result dataframe """ # Import a specific or all csv files in folder if isinstance(file_name, str) and isinstance(folder_name, str): try: self.data = self._load_csv(file_name, folder_name, head_row, index_col, convert_col, concat_files) except Exception as e: raise e # Import multiple csv files in a particular folder. elif isinstance(file_name, list) and isinstance(folder_name, str): for i, file in enumerate(file_name): if isinstance(head_row, list): _head_row = head_row[i] else: _head_row = head_row if isinstance(index_col, list): _index_col = index_col[i] else: _index_col = index_col try: data_tmp = self._load_csv(file, folder_name, _head_row, _index_col, convert_col, concat_files) if concat_files: self.data = self.data.append(data_tmp, ignore_index=False, verify_integrity=False) else: self.data = self.data.join(data_tmp, how="outer") except Exception as e: raise e else: # Current implementation can't accept, # 1. file_name of type str and folder_name of type list(str) # 2. file_name and folder_name both of type list(str) raise NotImplementedError("Filename and Folder name can't both be of type list.")
python
def import_csv(self, file_name='*', folder_name='.', head_row=0, index_col=0, convert_col=True, concat_files=False): if isinstance(file_name, str) and isinstance(folder_name, str): try: self.data = self._load_csv(file_name, folder_name, head_row, index_col, convert_col, concat_files) except Exception as e: raise e elif isinstance(file_name, list) and isinstance(folder_name, str): for i, file in enumerate(file_name): if isinstance(head_row, list): _head_row = head_row[i] else: _head_row = head_row if isinstance(index_col, list): _index_col = index_col[i] else: _index_col = index_col try: data_tmp = self._load_csv(file, folder_name, _head_row, _index_col, convert_col, concat_files) if concat_files: self.data = self.data.append(data_tmp, ignore_index=False, verify_integrity=False) else: self.data = self.data.join(data_tmp, how="outer") except Exception as e: raise e else: raise NotImplementedError("Filename and Folder name can't both be of type list.")
[ "def", "import_csv", "(", "self", ",", "file_name", "=", "'*'", ",", "folder_name", "=", "'.'", ",", "head_row", "=", "0", ",", "index_col", "=", "0", ",", "convert_col", "=", "True", ",", "concat_files", "=", "False", ")", ":", "# Import a specific or all csv files in folder", "if", "isinstance", "(", "file_name", ",", "str", ")", "and", "isinstance", "(", "folder_name", ",", "str", ")", ":", "try", ":", "self", ".", "data", "=", "self", ".", "_load_csv", "(", "file_name", ",", "folder_name", ",", "head_row", ",", "index_col", ",", "convert_col", ",", "concat_files", ")", "except", "Exception", "as", "e", ":", "raise", "e", "# Import multiple csv files in a particular folder.", "elif", "isinstance", "(", "file_name", ",", "list", ")", "and", "isinstance", "(", "folder_name", ",", "str", ")", ":", "for", "i", ",", "file", "in", "enumerate", "(", "file_name", ")", ":", "if", "isinstance", "(", "head_row", ",", "list", ")", ":", "_head_row", "=", "head_row", "[", "i", "]", "else", ":", "_head_row", "=", "head_row", "if", "isinstance", "(", "index_col", ",", "list", ")", ":", "_index_col", "=", "index_col", "[", "i", "]", "else", ":", "_index_col", "=", "index_col", "try", ":", "data_tmp", "=", "self", ".", "_load_csv", "(", "file", ",", "folder_name", ",", "_head_row", ",", "_index_col", ",", "convert_col", ",", "concat_files", ")", "if", "concat_files", ":", "self", ".", "data", "=", "self", ".", "data", ".", "append", "(", "data_tmp", ",", "ignore_index", "=", "False", ",", "verify_integrity", "=", "False", ")", "else", ":", "self", ".", "data", "=", "self", ".", "data", ".", "join", "(", "data_tmp", ",", "how", "=", "\"outer\"", ")", "except", "Exception", "as", "e", ":", "raise", "e", "else", ":", "# Current implementation can't accept,", "# 1. file_name of type str and folder_name of type list(str)", "# 2. file_name and folder_name both of type list(str)", "raise", "NotImplementedError", "(", "\"Filename and Folder name can't both be of type list.\"", ")" ]
Imports csv file(s) and stores the result in data. Note ---- 1. If folder exists out of current directory, folder_name should contain correct regex 2. Assuming there's no file called "\*.csv" Parameters ---------- file_name : str CSV file to be imported. Defaults to '\*', i.e. all csv files in the folder. folder_name : str Folder where file resides. Defaults to '.', i.e. current directory. head_row : int Skips all rows from 0 to head_row-1 index_col : int Skips all columns from 0 to index_col-1 convert_col : bool Convert columns to numeric type concat_files : bool Appends data from files to result dataframe
[ "Imports", "csv", "file", "(", "s", ")", "and", "stores", "the", "result", "in", "data", ".", "Note", "----", "1", ".", "If", "folder", "exists", "out", "of", "current", "directory", "folder_name", "should", "contain", "correct", "regex", "2", ".", "Assuming", "there", "s", "no", "file", "called", "\\", "*", ".", "csv" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L44-L103
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_Data._load_csv
def _load_csv(self, file_name, folder_name, head_row, index_col, convert_col, concat_files): """ Load single csv file. Parameters ---------- file_name : str CSV file to be imported. Defaults to '*' - all csv files in the folder. folder_name : str Folder where file resides. Defaults to '.' - current directory. head_row : int Skips all rows from 0 to head_row-1 index_col : int Skips all columns from 0 to index_col-1 convert_col : bool Convert columns to numeric type concat_files : bool Appends data from files to result dataframe Returns ------- pd.DataFrame() Dataframe containing csv data """ # Denotes all csv files if file_name == "*": if not os.path.isdir(folder_name): raise OSError('Folder does not exist.') else: file_name_list = sorted(glob.glob(folder_name + '*.csv')) if not file_name_list: raise OSError('Either the folder does not contain any csv files or invalid folder provided.') else: # Call previous function again with parameters changed (file_name=file_name_list, folder_name=None) # Done to reduce redundancy of code self.import_csv(file_name=file_name_list, head_row=head_row, index_col=index_col, convert_col=convert_col, concat_files=concat_files) return self.data else: if not os.path.isdir(folder_name): raise OSError('Folder does not exist.') else: path = os.path.join(folder_name, file_name) if head_row > 0: data = pd.read_csv(path, index_col=index_col, skiprows=[i for i in range(head_row-1)]) else: data = pd.read_csv(path, index_col=index_col) # Convert time into datetime format try: # Special case format 1/4/14 21:30 data.index = pd.to_datetime(data.index, format='%m/%d/%y %H:%M') except: data.index = pd.to_datetime(data.index, dayfirst=False, infer_datetime_format=True) # Convert all columns to numeric type if convert_col: # Check columns in dataframe to see if they are numeric for col in data.columns: # If particular column is not numeric, then convert to numeric type if data[col].dtype != np.number: data[col] = pd.to_numeric(data[col], errors="coerce") return data
python
def _load_csv(self, file_name, folder_name, head_row, index_col, convert_col, concat_files): if file_name == "*": if not os.path.isdir(folder_name): raise OSError('Folder does not exist.') else: file_name_list = sorted(glob.glob(folder_name + '*.csv')) if not file_name_list: raise OSError('Either the folder does not contain any csv files or invalid folder provided.') else: self.import_csv(file_name=file_name_list, head_row=head_row, index_col=index_col, convert_col=convert_col, concat_files=concat_files) return self.data else: if not os.path.isdir(folder_name): raise OSError('Folder does not exist.') else: path = os.path.join(folder_name, file_name) if head_row > 0: data = pd.read_csv(path, index_col=index_col, skiprows=[i for i in range(head_row-1)]) else: data = pd.read_csv(path, index_col=index_col) try: data.index = pd.to_datetime(data.index, format='%m/%d/%y %H:%M') except: data.index = pd.to_datetime(data.index, dayfirst=False, infer_datetime_format=True) if convert_col: for col in data.columns: if data[col].dtype != np.number: data[col] = pd.to_numeric(data[col], errors="coerce") return data
[ "def", "_load_csv", "(", "self", ",", "file_name", ",", "folder_name", ",", "head_row", ",", "index_col", ",", "convert_col", ",", "concat_files", ")", ":", "# Denotes all csv files", "if", "file_name", "==", "\"*\"", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "folder_name", ")", ":", "raise", "OSError", "(", "'Folder does not exist.'", ")", "else", ":", "file_name_list", "=", "sorted", "(", "glob", ".", "glob", "(", "folder_name", "+", "'*.csv'", ")", ")", "if", "not", "file_name_list", ":", "raise", "OSError", "(", "'Either the folder does not contain any csv files or invalid folder provided.'", ")", "else", ":", "# Call previous function again with parameters changed (file_name=file_name_list, folder_name=None)", "# Done to reduce redundancy of code", "self", ".", "import_csv", "(", "file_name", "=", "file_name_list", ",", "head_row", "=", "head_row", ",", "index_col", "=", "index_col", ",", "convert_col", "=", "convert_col", ",", "concat_files", "=", "concat_files", ")", "return", "self", ".", "data", "else", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "folder_name", ")", ":", "raise", "OSError", "(", "'Folder does not exist.'", ")", "else", ":", "path", "=", "os", ".", "path", ".", "join", "(", "folder_name", ",", "file_name", ")", "if", "head_row", ">", "0", ":", "data", "=", "pd", ".", "read_csv", "(", "path", ",", "index_col", "=", "index_col", ",", "skiprows", "=", "[", "i", "for", "i", "in", "range", "(", "head_row", "-", "1", ")", "]", ")", "else", ":", "data", "=", "pd", ".", "read_csv", "(", "path", ",", "index_col", "=", "index_col", ")", "# Convert time into datetime format", "try", ":", "# Special case format 1/4/14 21:30", "data", ".", "index", "=", "pd", ".", "to_datetime", "(", "data", ".", "index", ",", "format", "=", "'%m/%d/%y %H:%M'", ")", "except", ":", "data", ".", "index", "=", "pd", ".", "to_datetime", "(", "data", ".", "index", ",", "dayfirst", "=", "False", ",", "infer_datetime_format", "=", "True", ")", "# Convert all columns to numeric type", "if", "convert_col", ":", "# Check columns in dataframe to see if they are numeric", "for", "col", "in", "data", ".", "columns", ":", "# If particular column is not numeric, then convert to numeric type", "if", "data", "[", "col", "]", ".", "dtype", "!=", "np", ".", "number", ":", "data", "[", "col", "]", "=", "pd", ".", "to_numeric", "(", "data", "[", "col", "]", ",", "errors", "=", "\"coerce\"", ")", "return", "data" ]
Load single csv file. Parameters ---------- file_name : str CSV file to be imported. Defaults to '*' - all csv files in the folder. folder_name : str Folder where file resides. Defaults to '.' - current directory. head_row : int Skips all rows from 0 to head_row-1 index_col : int Skips all columns from 0 to index_col-1 convert_col : bool Convert columns to numeric type concat_files : bool Appends data from files to result dataframe Returns ------- pd.DataFrame() Dataframe containing csv data
[ "Load", "single", "csv", "file", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L106-L174
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_MDAL.convert_to_utc
def convert_to_utc(time): """ Convert time to UTC Parameters ---------- time : str Time to convert. Has to be of the format '2016-01-01T00:00:00-08:00'. Returns ------- str UTC timestamp. """ # time is already in UTC if 'Z' in time: return time else: time_formatted = time[:-3] + time[-2:] dt = datetime.strptime(time_formatted, '%Y-%m-%dT%H:%M:%S%z') dt = dt.astimezone(timezone('UTC')) return dt.strftime('%Y-%m-%dT%H:%M:%SZ')
python
def convert_to_utc(time): if 'Z' in time: return time else: time_formatted = time[:-3] + time[-2:] dt = datetime.strptime(time_formatted, '%Y-%m-%dT%H:%M:%S%z') dt = dt.astimezone(timezone('UTC')) return dt.strftime('%Y-%m-%dT%H:%M:%SZ')
[ "def", "convert_to_utc", "(", "time", ")", ":", "# time is already in UTC", "if", "'Z'", "in", "time", ":", "return", "time", "else", ":", "time_formatted", "=", "time", "[", ":", "-", "3", "]", "+", "time", "[", "-", "2", ":", "]", "dt", "=", "datetime", ".", "strptime", "(", "time_formatted", ",", "'%Y-%m-%dT%H:%M:%S%z'", ")", "dt", "=", "dt", ".", "astimezone", "(", "timezone", "(", "'UTC'", ")", ")", "return", "dt", ".", "strftime", "(", "'%Y-%m-%dT%H:%M:%SZ'", ")" ]
Convert time to UTC Parameters ---------- time : str Time to convert. Has to be of the format '2016-01-01T00:00:00-08:00'. Returns ------- str UTC timestamp.
[ "Convert", "time", "to", "UTC" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L190-L212
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_MDAL.get_meter
def get_meter(self, site, start, end, point_type='Green_Button_Meter', var="meter", agg='MEAN', window='24h', aligned=True, return_names=True): """ Get meter data from MDAL. Parameters ---------- site : str Building name. start : str Start date - 'YYYY-MM-DDTHH:MM:SSZ' end : str End date - 'YYYY-MM-DDTHH:MM:SSZ' point_type : str Type of data, i.e. Green_Button_Meter, Building_Electric_Meter... var : str Variable - "meter", "weather"... agg : str Aggregation - MEAN, SUM, RAW... window : str Size of the moving window. aligned : bool ??? return_names : bool ??? Returns ------- (df, mapping, context) ??? """ # Convert time to UTC start = self.convert_to_utc(start) end = self.convert_to_utc(end) request = self.compose_MDAL_dic(point_type=point_type, site=site, start=start, end=end, var=var, agg=agg, window=window, aligned=aligned) resp = self.m.query(request) if return_names: resp = self.replace_uuid_w_names(resp) return resp
python
def get_meter(self, site, start, end, point_type='Green_Button_Meter', var="meter", agg='MEAN', window='24h', aligned=True, return_names=True): start = self.convert_to_utc(start) end = self.convert_to_utc(end) request = self.compose_MDAL_dic(point_type=point_type, site=site, start=start, end=end, var=var, agg=agg, window=window, aligned=aligned) resp = self.m.query(request) if return_names: resp = self.replace_uuid_w_names(resp) return resp
[ "def", "get_meter", "(", "self", ",", "site", ",", "start", ",", "end", ",", "point_type", "=", "'Green_Button_Meter'", ",", "var", "=", "\"meter\"", ",", "agg", "=", "'MEAN'", ",", "window", "=", "'24h'", ",", "aligned", "=", "True", ",", "return_names", "=", "True", ")", ":", "# Convert time to UTC", "start", "=", "self", ".", "convert_to_utc", "(", "start", ")", "end", "=", "self", ".", "convert_to_utc", "(", "end", ")", "request", "=", "self", ".", "compose_MDAL_dic", "(", "point_type", "=", "point_type", ",", "site", "=", "site", ",", "start", "=", "start", ",", "end", "=", "end", ",", "var", "=", "var", ",", "agg", "=", "agg", ",", "window", "=", "window", ",", "aligned", "=", "aligned", ")", "resp", "=", "self", ".", "m", ".", "query", "(", "request", ")", "if", "return_names", ":", "resp", "=", "self", ".", "replace_uuid_w_names", "(", "resp", ")", "return", "resp" ]
Get meter data from MDAL. Parameters ---------- site : str Building name. start : str Start date - 'YYYY-MM-DDTHH:MM:SSZ' end : str End date - 'YYYY-MM-DDTHH:MM:SSZ' point_type : str Type of data, i.e. Green_Button_Meter, Building_Electric_Meter... var : str Variable - "meter", "weather"... agg : str Aggregation - MEAN, SUM, RAW... window : str Size of the moving window. aligned : bool ??? return_names : bool ??? Returns ------- (df, mapping, context) ???
[ "Get", "meter", "data", "from", "MDAL", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L215-L258
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_MDAL.get_tstat
def get_tstat(self, site, start, end, var="tstat_temp", agg='MEAN', window='24h', aligned=True, return_names=True): """ Get thermostat data from MDAL. Parameters ---------- site : str Building name. start : str Start date - 'YYYY-MM-DDTHH:MM:SSZ' end : str End date - 'YYYY-MM-DDTHH:MM:SSZ' var : str Variable - "meter", "weather"... agg : str Aggregation - MEAN, SUM, RAW... window : str Size of the moving window. aligned : bool ??? return_names : bool ??? Returns ------- (df, mapping, context) ??? """ # Convert time to UTC start = self.convert_to_utc(start) end = self.convert_to_utc(end) point_map = { "tstat_state" : "Thermostat_Status", "tstat_hsp" : "Supply_Air_Temperature_Heating_Setpoint", "tstat_csp" : "Supply_Air_Temperature_Cooling_Setpoint", "tstat_temp": "Temperature_Sensor" } if isinstance(var,list): point_type = [point_map[point_type] for point_type in var] # list of all the point names using BRICK classes else: point_type = point_map[var] # single value using BRICK classes request = self.compose_MDAL_dic(point_type=point_type, site=site, start=start, end=end, var=var, agg=agg, window=window, aligned=aligned) resp = self.m.query(request) if return_names: resp = self.replace_uuid_w_names(resp) return resp
python
def get_tstat(self, site, start, end, var="tstat_temp", agg='MEAN', window='24h', aligned=True, return_names=True): start = self.convert_to_utc(start) end = self.convert_to_utc(end) point_map = { "tstat_state" : "Thermostat_Status", "tstat_hsp" : "Supply_Air_Temperature_Heating_Setpoint", "tstat_csp" : "Supply_Air_Temperature_Cooling_Setpoint", "tstat_temp": "Temperature_Sensor" } if isinstance(var,list): point_type = [point_map[point_type] for point_type in var] else: point_type = point_map[var] request = self.compose_MDAL_dic(point_type=point_type, site=site, start=start, end=end, var=var, agg=agg, window=window, aligned=aligned) resp = self.m.query(request) if return_names: resp = self.replace_uuid_w_names(resp) return resp
[ "def", "get_tstat", "(", "self", ",", "site", ",", "start", ",", "end", ",", "var", "=", "\"tstat_temp\"", ",", "agg", "=", "'MEAN'", ",", "window", "=", "'24h'", ",", "aligned", "=", "True", ",", "return_names", "=", "True", ")", ":", "# Convert time to UTC", "start", "=", "self", ".", "convert_to_utc", "(", "start", ")", "end", "=", "self", ".", "convert_to_utc", "(", "end", ")", "point_map", "=", "{", "\"tstat_state\"", ":", "\"Thermostat_Status\"", ",", "\"tstat_hsp\"", ":", "\"Supply_Air_Temperature_Heating_Setpoint\"", ",", "\"tstat_csp\"", ":", "\"Supply_Air_Temperature_Cooling_Setpoint\"", ",", "\"tstat_temp\"", ":", "\"Temperature_Sensor\"", "}", "if", "isinstance", "(", "var", ",", "list", ")", ":", "point_type", "=", "[", "point_map", "[", "point_type", "]", "for", "point_type", "in", "var", "]", "# list of all the point names using BRICK classes", "else", ":", "point_type", "=", "point_map", "[", "var", "]", "# single value using BRICK classes", "request", "=", "self", ".", "compose_MDAL_dic", "(", "point_type", "=", "point_type", ",", "site", "=", "site", ",", "start", "=", "start", ",", "end", "=", "end", ",", "var", "=", "var", ",", "agg", "=", "agg", ",", "window", "=", "window", ",", "aligned", "=", "aligned", ")", "resp", "=", "self", ".", "m", ".", "query", "(", "request", ")", "if", "return_names", ":", "resp", "=", "self", ".", "replace_uuid_w_names", "(", "resp", ")", "return", "resp" ]
Get thermostat data from MDAL. Parameters ---------- site : str Building name. start : str Start date - 'YYYY-MM-DDTHH:MM:SSZ' end : str End date - 'YYYY-MM-DDTHH:MM:SSZ' var : str Variable - "meter", "weather"... agg : str Aggregation - MEAN, SUM, RAW... window : str Size of the moving window. aligned : bool ??? return_names : bool ??? Returns ------- (df, mapping, context) ???
[ "Get", "thermostat", "data", "from", "MDAL", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L307-L359
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_MDAL.compose_MDAL_dic
def compose_MDAL_dic(self, site, point_type, start, end, var, agg, window, aligned, points=None, return_names=False): """ Create dictionary for MDAL request. Parameters ---------- site : str Building name. start : str Start date - 'YYYY-MM-DDTHH:MM:SSZ' end : str End date - 'YYYY-MM-DDTHH:MM:SSZ' point_type : str Type of data, i.e. Green_Button_Meter, Building_Electric_Meter... var : str Variable - "meter", "weather"... agg : str Aggregation - MEAN, SUM, RAW... window : str Size of the moving window. aligned : bool ??? return_names : bool ??? Returns ------- (df, mapping, context) ??? """ # Convert time to UTC start = self.convert_to_utc(start) end = self.convert_to_utc(end) request = {} # Add Time Details - single set for one or multiple series request['Time'] = { 'Start': start, 'End': end, 'Window': window, 'Aligned': aligned } # Define Variables request["Variables"] = {} request['Composition'] = [] request['Aggregation'] = {} if isinstance(point_type, str): # if point_type is a string -> single type of point requested request["Variables"][var] = self.compose_BRICK_query(point_type=point_type,site=site) # pass one point type at the time request['Composition'] = [var] request['Aggregation'][var] = [agg] elif isinstance(point_type, list): # loop through all the point_types and create one section of the brick query at the time for idx, point in enumerate(point_type): request["Variables"][var[idx]] = self.compose_BRICK_query(point_type=point,site=site) # pass one point type at the time request['Composition'].append(var[idx]) if isinstance(agg, str): # if agg is a string -> single type of aggregation requested request['Aggregation'][var[idx]] = [agg] elif isinstance(agg, list): # if agg is a list -> expected one agg per point request['Aggregation'][var[idx]] = [agg[idx]] return request
python
def compose_MDAL_dic(self, site, point_type, start, end, var, agg, window, aligned, points=None, return_names=False): start = self.convert_to_utc(start) end = self.convert_to_utc(end) request = {} request['Time'] = { 'Start': start, 'End': end, 'Window': window, 'Aligned': aligned } request["Variables"] = {} request['Composition'] = [] request['Aggregation'] = {} if isinstance(point_type, str): request["Variables"][var] = self.compose_BRICK_query(point_type=point_type,site=site) request['Composition'] = [var] request['Aggregation'][var] = [agg] elif isinstance(point_type, list): for idx, point in enumerate(point_type): request["Variables"][var[idx]] = self.compose_BRICK_query(point_type=point,site=site) request['Composition'].append(var[idx]) if isinstance(agg, str): request['Aggregation'][var[idx]] = [agg] elif isinstance(agg, list): request['Aggregation'][var[idx]] = [agg[idx]] return request
[ "def", "compose_MDAL_dic", "(", "self", ",", "site", ",", "point_type", ",", "start", ",", "end", ",", "var", ",", "agg", ",", "window", ",", "aligned", ",", "points", "=", "None", ",", "return_names", "=", "False", ")", ":", "# Convert time to UTC", "start", "=", "self", ".", "convert_to_utc", "(", "start", ")", "end", "=", "self", ".", "convert_to_utc", "(", "end", ")", "request", "=", "{", "}", "# Add Time Details - single set for one or multiple series", "request", "[", "'Time'", "]", "=", "{", "'Start'", ":", "start", ",", "'End'", ":", "end", ",", "'Window'", ":", "window", ",", "'Aligned'", ":", "aligned", "}", "# Define Variables ", "request", "[", "\"Variables\"", "]", "=", "{", "}", "request", "[", "'Composition'", "]", "=", "[", "]", "request", "[", "'Aggregation'", "]", "=", "{", "}", "if", "isinstance", "(", "point_type", ",", "str", ")", ":", "# if point_type is a string -> single type of point requested", "request", "[", "\"Variables\"", "]", "[", "var", "]", "=", "self", ".", "compose_BRICK_query", "(", "point_type", "=", "point_type", ",", "site", "=", "site", ")", "# pass one point type at the time", "request", "[", "'Composition'", "]", "=", "[", "var", "]", "request", "[", "'Aggregation'", "]", "[", "var", "]", "=", "[", "agg", "]", "elif", "isinstance", "(", "point_type", ",", "list", ")", ":", "# loop through all the point_types and create one section of the brick query at the time", "for", "idx", ",", "point", "in", "enumerate", "(", "point_type", ")", ":", "request", "[", "\"Variables\"", "]", "[", "var", "[", "idx", "]", "]", "=", "self", ".", "compose_BRICK_query", "(", "point_type", "=", "point", ",", "site", "=", "site", ")", "# pass one point type at the time", "request", "[", "'Composition'", "]", ".", "append", "(", "var", "[", "idx", "]", ")", "if", "isinstance", "(", "agg", ",", "str", ")", ":", "# if agg is a string -> single type of aggregation requested", "request", "[", "'Aggregation'", "]", "[", "var", "[", "idx", "]", "]", "=", "[", "agg", "]", "elif", "isinstance", "(", "agg", ",", "list", ")", ":", "# if agg is a list -> expected one agg per point", "request", "[", "'Aggregation'", "]", "[", "var", "[", "idx", "]", "]", "=", "[", "agg", "[", "idx", "]", "]", "return", "request" ]
Create dictionary for MDAL request. Parameters ---------- site : str Building name. start : str Start date - 'YYYY-MM-DDTHH:MM:SSZ' end : str End date - 'YYYY-MM-DDTHH:MM:SSZ' point_type : str Type of data, i.e. Green_Button_Meter, Building_Electric_Meter... var : str Variable - "meter", "weather"... agg : str Aggregation - MEAN, SUM, RAW... window : str Size of the moving window. aligned : bool ??? return_names : bool ??? Returns ------- (df, mapping, context) ???
[ "Create", "dictionary", "for", "MDAL", "request", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L362-L428
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_MDAL.get_point_name
def get_point_name(self, context): """ Get point name. Parameters ---------- context : ??? ??? Returns ------- ??? ??? """ metadata_table = self.parse_context(context) return metadata_table.apply(self.strip_point_name, axis=1)
python
def get_point_name(self, context): metadata_table = self.parse_context(context) return metadata_table.apply(self.strip_point_name, axis=1)
[ "def", "get_point_name", "(", "self", ",", "context", ")", ":", "metadata_table", "=", "self", ".", "parse_context", "(", "context", ")", "return", "metadata_table", ".", "apply", "(", "self", ".", "strip_point_name", ",", "axis", "=", "1", ")" ]
Get point name. Parameters ---------- context : ??? ??? Returns ------- ??? ???
[ "Get", "point", "name", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L510-L526
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Import_Data.py
Import_MDAL.replace_uuid_w_names
def replace_uuid_w_names(self, resp): """ Replace the uuid's with names. Parameters ---------- resp : ??? ??? Returns ------- ??? ??? """ col_mapper = self.get_point_name(resp.context)["?point"].to_dict() resp.df.rename(columns=col_mapper, inplace=True) return resp
python
def replace_uuid_w_names(self, resp): col_mapper = self.get_point_name(resp.context)["?point"].to_dict() resp.df.rename(columns=col_mapper, inplace=True) return resp
[ "def", "replace_uuid_w_names", "(", "self", ",", "resp", ")", ":", "col_mapper", "=", "self", ".", "get_point_name", "(", "resp", ".", "context", ")", "[", "\"?point\"", "]", ".", "to_dict", "(", ")", "resp", ".", "df", ".", "rename", "(", "columns", "=", "col_mapper", ",", "inplace", "=", "True", ")", "return", "resp" ]
Replace the uuid's with names. Parameters ---------- resp : ??? ??? Returns ------- ??? ???
[ "Replace", "the", "uuid", "s", "with", "names", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Import_Data.py#L529-L546
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Import_Data.py
Import_XBOS.get_weather_power_tstat
def get_weather_power_tstat(self, site, start, end, data_type=['weather', 'power']): """ Get weather and power data. Parameters ---------- site : str Site name. start : str Start date. end : str End date. data_type : str Type of data needed (all, weather, power, temperature, hsp, csp) """ m = dataclient.MDALClient("corbusier.cs.berkeley.edu:8088") request = { "Variables": { "greenbutton": { "Definition": """SELECT ?meter ?meter_uuid FROM %s WHERE { ?meter rdf:type brick:Green_Button_Meter . ?meter bf:uuid ?meter_uuid };""" % site, }, "weather": { "Definition": """SELECT ?t ?t_uuid FROM %s WHERE { ?t rdf:type/rdfs:subClassOf* brick:Weather_Temperature_Sensor . ?t bf:uuid ?t_uuid };""" % site, }, "tstat_state": { "Definition": """SELECT ?t ?t_uuid ?tstat FROM %s WHERE { ?t rdf:type/rdfs:subClassOf* brick:Thermostat_Status . ?t bf:uuid ?t_uuid ?t bf:isPointOf ?tstat . ?tstat rdf:type brick:Thermostat };""" % site, }, "tstat_hsp": { "Definition": """SELECT ?t ?t_uuid ?tstat FROM %s WHERE { ?t rdf:type/rdfs:subClassOf* brick:Supply_Air_Temperature_Heating_Setpoint . ?t bf:uuid ?t_uuid . ?t bf:isPointOf ?tstat . ?tstat rdf:type brick:Thermostat };""" % site, }, "tstat_csp": { "Definition": """SELECT ?t ?t_uuid ?tstat FROM %s WHERE { ?t rdf:type/rdfs:subClassOf* brick:Supply_Air_Temperature_Cooling_Setpoint . ?t bf:uuid ?t_uuid . ?t bf:isPointOf ?tstat . ?tstat rdf:type brick:Thermostat };""" % site, }, "tstat_temp": { "Definition": """SELECT ?t ?t_uuid ?tstat FROM %s WHERE { ?t rdf:type/rdfs:subClassOf* brick:Temperature_Sensor . ?t bf:uuid ?t_uuid . ?t bf:isPointOf ?tstat . ?tstat rdf:type brick:Thermostat };""" % site, }, }, } # outside air temp request['Composition'] = ['weather'] request['Aggregation'] = {'weather': ['MEAN']} request['Time'] = { 'Start': start, 'End': end, 'Window': '15m', 'Aligned': True } resp_weather = m.query(request) self.weather_data = resp_weather.df # power request['Composition'] = ['greenbutton'] request['Aggregation'] = {'greenbutton': ['MEAN']} resp_power = m.query(request) self.power_data = resp_power.df # tstat temperature request['Composition'] = ['tstat_temp', 'tstat_hsp', 'tstat_csp'] request['Aggregation'] = {'tstat_temp': ['MEAN']} resp_temp = m.query(request) self.temp_data = resp_temp # tstat heat setpoint request['Composition'] = ['tstat_hsp'] request['Aggregation'] = {'tstat_hsp': ['MAX']} resp_hsp = m.query(request) self.hsp_data = resp_hsp # tstat cool setpoint request['Composition'] = ['tstat_csp'] request['Aggregation'] = {'tstat_csp': ['MAX']} resp_csp = m.query(request) self.csp_data = resp_csp mapping = { 'weather': resp_weather, 'power': resp_power, 'temperature': resp_temp, 'hsp': resp_hsp, 'csp': resp_csp } first = True for dat in data_type: if first: try: self.data = mapping[dat].df first = False except: raise SystemError('Undefined data_type (Make sure all characters are lowercase)') else: try: self.data = self.data.join(mapping[dat].df) except: raise SystemError('Undefined data_type (Make sure all characters are lowercase)') return mapping
python
def get_weather_power_tstat(self, site, start, end, data_type=['weather', 'power']): m = dataclient.MDALClient("corbusier.cs.berkeley.edu:8088") request = { "Variables": { "greenbutton": { "Definition": % site, }, "weather": { "Definition": % site, }, "tstat_state": { "Definition": % site, }, "tstat_hsp": { "Definition": % site, }, "tstat_csp": { "Definition": % site, }, "tstat_temp": { "Definition": % site, }, }, } request['Composition'] = ['weather'] request['Aggregation'] = {'weather': ['MEAN']} request['Time'] = { 'Start': start, 'End': end, 'Window': '15m', 'Aligned': True } resp_weather = m.query(request) self.weather_data = resp_weather.df request['Composition'] = ['greenbutton'] request['Aggregation'] = {'greenbutton': ['MEAN']} resp_power = m.query(request) self.power_data = resp_power.df request['Composition'] = ['tstat_temp', 'tstat_hsp', 'tstat_csp'] request['Aggregation'] = {'tstat_temp': ['MEAN']} resp_temp = m.query(request) self.temp_data = resp_temp request['Composition'] = ['tstat_hsp'] request['Aggregation'] = {'tstat_hsp': ['MAX']} resp_hsp = m.query(request) self.hsp_data = resp_hsp request['Composition'] = ['tstat_csp'] request['Aggregation'] = {'tstat_csp': ['MAX']} resp_csp = m.query(request) self.csp_data = resp_csp mapping = { 'weather': resp_weather, 'power': resp_power, 'temperature': resp_temp, 'hsp': resp_hsp, 'csp': resp_csp } first = True for dat in data_type: if first: try: self.data = mapping[dat].df first = False except: raise SystemError('Undefined data_type (Make sure all characters are lowercase)') else: try: self.data = self.data.join(mapping[dat].df) except: raise SystemError('Undefined data_type (Make sure all characters are lowercase)') return mapping
[ "def", "get_weather_power_tstat", "(", "self", ",", "site", ",", "start", ",", "end", ",", "data_type", "=", "[", "'weather'", ",", "'power'", "]", ")", ":", "m", "=", "dataclient", ".", "MDALClient", "(", "\"corbusier.cs.berkeley.edu:8088\"", ")", "request", "=", "{", "\"Variables\"", ":", "{", "\"greenbutton\"", ":", "{", "\"Definition\"", ":", "\"\"\"SELECT ?meter ?meter_uuid FROM %s WHERE {\n ?meter rdf:type brick:Green_Button_Meter .\n ?meter bf:uuid ?meter_uuid\n };\"\"\"", "%", "site", ",", "}", ",", "\"weather\"", ":", "{", "\"Definition\"", ":", "\"\"\"SELECT ?t ?t_uuid FROM %s WHERE {\n ?t rdf:type/rdfs:subClassOf* brick:Weather_Temperature_Sensor .\n ?t bf:uuid ?t_uuid\n };\"\"\"", "%", "site", ",", "}", ",", "\"tstat_state\"", ":", "{", "\"Definition\"", ":", "\"\"\"SELECT ?t ?t_uuid ?tstat FROM %s WHERE {\n ?t rdf:type/rdfs:subClassOf* brick:Thermostat_Status .\n ?t bf:uuid ?t_uuid\n ?t bf:isPointOf ?tstat .\n ?tstat rdf:type brick:Thermostat\n };\"\"\"", "%", "site", ",", "}", ",", "\"tstat_hsp\"", ":", "{", "\"Definition\"", ":", "\"\"\"SELECT ?t ?t_uuid ?tstat FROM %s WHERE {\n ?t rdf:type/rdfs:subClassOf* brick:Supply_Air_Temperature_Heating_Setpoint .\n ?t bf:uuid ?t_uuid .\n ?t bf:isPointOf ?tstat .\n ?tstat rdf:type brick:Thermostat\n };\"\"\"", "%", "site", ",", "}", ",", "\"tstat_csp\"", ":", "{", "\"Definition\"", ":", "\"\"\"SELECT ?t ?t_uuid ?tstat FROM %s WHERE {\n ?t rdf:type/rdfs:subClassOf* brick:Supply_Air_Temperature_Cooling_Setpoint .\n ?t bf:uuid ?t_uuid .\n ?t bf:isPointOf ?tstat .\n ?tstat rdf:type brick:Thermostat\n };\"\"\"", "%", "site", ",", "}", ",", "\"tstat_temp\"", ":", "{", "\"Definition\"", ":", "\"\"\"SELECT ?t ?t_uuid ?tstat FROM %s WHERE {\n ?t rdf:type/rdfs:subClassOf* brick:Temperature_Sensor .\n ?t bf:uuid ?t_uuid .\n ?t bf:isPointOf ?tstat .\n ?tstat rdf:type brick:Thermostat\n };\"\"\"", "%", "site", ",", "}", ",", "}", ",", "}", "# outside air temp", "request", "[", "'Composition'", "]", "=", "[", "'weather'", "]", "request", "[", "'Aggregation'", "]", "=", "{", "'weather'", ":", "[", "'MEAN'", "]", "}", "request", "[", "'Time'", "]", "=", "{", "'Start'", ":", "start", ",", "'End'", ":", "end", ",", "'Window'", ":", "'15m'", ",", "'Aligned'", ":", "True", "}", "resp_weather", "=", "m", ".", "query", "(", "request", ")", "self", ".", "weather_data", "=", "resp_weather", ".", "df", "# power", "request", "[", "'Composition'", "]", "=", "[", "'greenbutton'", "]", "request", "[", "'Aggregation'", "]", "=", "{", "'greenbutton'", ":", "[", "'MEAN'", "]", "}", "resp_power", "=", "m", ".", "query", "(", "request", ")", "self", ".", "power_data", "=", "resp_power", ".", "df", "# tstat temperature", "request", "[", "'Composition'", "]", "=", "[", "'tstat_temp'", ",", "'tstat_hsp'", ",", "'tstat_csp'", "]", "request", "[", "'Aggregation'", "]", "=", "{", "'tstat_temp'", ":", "[", "'MEAN'", "]", "}", "resp_temp", "=", "m", ".", "query", "(", "request", ")", "self", ".", "temp_data", "=", "resp_temp", "# tstat heat setpoint", "request", "[", "'Composition'", "]", "=", "[", "'tstat_hsp'", "]", "request", "[", "'Aggregation'", "]", "=", "{", "'tstat_hsp'", ":", "[", "'MAX'", "]", "}", "resp_hsp", "=", "m", ".", "query", "(", "request", ")", "self", ".", "hsp_data", "=", "resp_hsp", "# tstat cool setpoint", "request", "[", "'Composition'", "]", "=", "[", "'tstat_csp'", "]", "request", "[", "'Aggregation'", "]", "=", "{", "'tstat_csp'", ":", "[", "'MAX'", "]", "}", "resp_csp", "=", "m", ".", "query", "(", "request", ")", "self", ".", "csp_data", "=", "resp_csp", "mapping", "=", "{", "'weather'", ":", "resp_weather", ",", "'power'", ":", "resp_power", ",", "'temperature'", ":", "resp_temp", ",", "'hsp'", ":", "resp_hsp", ",", "'csp'", ":", "resp_csp", "}", "first", "=", "True", "for", "dat", "in", "data_type", ":", "if", "first", ":", "try", ":", "self", ".", "data", "=", "mapping", "[", "dat", "]", ".", "df", "first", "=", "False", "except", ":", "raise", "SystemError", "(", "'Undefined data_type (Make sure all characters are lowercase)'", ")", "else", ":", "try", ":", "self", ".", "data", "=", "self", ".", "data", ".", "join", "(", "mapping", "[", "dat", "]", ".", "df", ")", "except", ":", "raise", "SystemError", "(", "'Undefined data_type (Make sure all characters are lowercase)'", ")", "return", "mapping" ]
Get weather and power data. Parameters ---------- site : str Site name. start : str Start date. end : str End date. data_type : str Type of data needed (all, weather, power, temperature, hsp, csp)
[ "Get", "weather", "and", "power", "data", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Import_Data.py#L193-L318
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.drop_columns
def drop_columns(self, col): """ Drop columns in dataframe. Parameters ---------- col : str Column to drop. """ try: self.cleaned_data.drop(col, axis=1, inplace=True) except Exception as e: raise e
python
def drop_columns(self, col): try: self.cleaned_data.drop(col, axis=1, inplace=True) except Exception as e: raise e
[ "def", "drop_columns", "(", "self", ",", "col", ")", ":", "try", ":", "self", ".", "cleaned_data", ".", "drop", "(", "col", ",", "axis", "=", "1", ",", "inplace", "=", "True", ")", "except", "Exception", "as", "e", ":", "raise", "e" ]
Drop columns in dataframe. Parameters ---------- col : str Column to drop.
[ "Drop", "columns", "in", "dataframe", ".", "Parameters", "----------", "col", ":", "str", "Column", "to", "drop", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L46-L58
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.rename_columns
def rename_columns(self, col): """ Rename columns of dataframe. Parameters ---------- col : list(str) List of columns to rename. """ try: self.cleaned_data.columns = col except Exception as e: raise e
python
def rename_columns(self, col): try: self.cleaned_data.columns = col except Exception as e: raise e
[ "def", "rename_columns", "(", "self", ",", "col", ")", ":", "try", ":", "self", ".", "cleaned_data", ".", "columns", "=", "col", "except", "Exception", "as", "e", ":", "raise", "e" ]
Rename columns of dataframe. Parameters ---------- col : list(str) List of columns to rename.
[ "Rename", "columns", "of", "dataframe", ".", "Parameters", "----------", "col", ":", "list", "(", "str", ")", "List", "of", "columns", "to", "rename", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L61-L73
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.resample_data
def resample_data(self, data, freq, resampler='mean'): """ Resample dataframe. Note ---- 1. Figure out how to apply different functions to different columns .apply() 2. This theoretically work in upsampling too, check docs http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html Parameters ---------- data : pd.DataFrame() Dataframe to resample freq : str Resampling frequency i.e. d, h, 15T... resampler : str Resampling type i.e. mean, max. Returns ------- pd.DataFrame() Dataframe containing resampled data """ if resampler == 'mean': data = data.resample(freq).mean() elif resampler == 'max': data = data.resample(freq).max() else: raise ValueError('Resampler can be \'mean\' or \'max\' only.') return data
python
def resample_data(self, data, freq, resampler='mean'): if resampler == 'mean': data = data.resample(freq).mean() elif resampler == 'max': data = data.resample(freq).max() else: raise ValueError('Resampler can be \'mean\' or \'max\' only.') return data
[ "def", "resample_data", "(", "self", ",", "data", ",", "freq", ",", "resampler", "=", "'mean'", ")", ":", "if", "resampler", "==", "'mean'", ":", "data", "=", "data", ".", "resample", "(", "freq", ")", ".", "mean", "(", ")", "elif", "resampler", "==", "'max'", ":", "data", "=", "data", ".", "resample", "(", "freq", ")", ".", "max", "(", ")", "else", ":", "raise", "ValueError", "(", "'Resampler can be \\'mean\\' or \\'max\\' only.'", ")", "return", "data" ]
Resample dataframe. Note ---- 1. Figure out how to apply different functions to different columns .apply() 2. This theoretically work in upsampling too, check docs http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html Parameters ---------- data : pd.DataFrame() Dataframe to resample freq : str Resampling frequency i.e. d, h, 15T... resampler : str Resampling type i.e. mean, max. Returns ------- pd.DataFrame() Dataframe containing resampled data
[ "Resample", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L76-L108
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.interpolate_data
def interpolate_data(self, data, limit, method): """ Interpolate dataframe. Parameters ---------- data : pd.DataFrame() Dataframe to interpolate limit : int Interpolation limit. method : str Interpolation method. Returns ------- pd.DataFrame() Dataframe containing interpolated data """ data = data.interpolate(how="index", limit=limit, method=method) return data
python
def interpolate_data(self, data, limit, method): data = data.interpolate(how="index", limit=limit, method=method) return data
[ "def", "interpolate_data", "(", "self", ",", "data", ",", "limit", ",", "method", ")", ":", "data", "=", "data", ".", "interpolate", "(", "how", "=", "\"index\"", ",", "limit", "=", "limit", ",", "method", "=", "method", ")", "return", "data" ]
Interpolate dataframe. Parameters ---------- data : pd.DataFrame() Dataframe to interpolate limit : int Interpolation limit. method : str Interpolation method. Returns ------- pd.DataFrame() Dataframe containing interpolated data
[ "Interpolate", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L111-L130
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_na
def remove_na(self, data, remove_na_how): """ Remove NAs from dataframe. Parameters ---------- data : pd.DataFrame() Dataframe to remove NAs from. remove_na_how : str Specificies how to remove NA i.e. all, any... Returns ------- pd.DataFrame() Dataframe with NAs removed. """ data = data.dropna(how=remove_na_how) return data
python
def remove_na(self, data, remove_na_how): data = data.dropna(how=remove_na_how) return data
[ "def", "remove_na", "(", "self", ",", "data", ",", "remove_na_how", ")", ":", "data", "=", "data", ".", "dropna", "(", "how", "=", "remove_na_how", ")", "return", "data" ]
Remove NAs from dataframe. Parameters ---------- data : pd.DataFrame() Dataframe to remove NAs from. remove_na_how : str Specificies how to remove NA i.e. all, any... Returns ------- pd.DataFrame() Dataframe with NAs removed.
[ "Remove", "NAs", "from", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L133-L150
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_outlier
def remove_outlier(self, data, sd_val): """ Remove outliers from dataframe. Note ---- 1. This function excludes all lines with NA in all columns. Parameters ---------- data : pd.DataFrame() Dataframe to remove outliers from. sd_val : int Standard Deviation Value (specifices how many SDs away is a point considered an outlier) Returns ------- pd.DataFrame() Dataframe with outliers removed. """ data = data.dropna() data = data[(np.abs(stats.zscore(data)) < float(sd_val)).all(axis=1)] return data
python
def remove_outlier(self, data, sd_val): data = data.dropna() data = data[(np.abs(stats.zscore(data)) < float(sd_val)).all(axis=1)] return data
[ "def", "remove_outlier", "(", "self", ",", "data", ",", "sd_val", ")", ":", "data", "=", "data", ".", "dropna", "(", ")", "data", "=", "data", "[", "(", "np", ".", "abs", "(", "stats", ".", "zscore", "(", "data", ")", ")", "<", "float", "(", "sd_val", ")", ")", ".", "all", "(", "axis", "=", "1", ")", "]", "return", "data" ]
Remove outliers from dataframe. Note ---- 1. This function excludes all lines with NA in all columns. Parameters ---------- data : pd.DataFrame() Dataframe to remove outliers from. sd_val : int Standard Deviation Value (specifices how many SDs away is a point considered an outlier) Returns ------- pd.DataFrame() Dataframe with outliers removed.
[ "Remove", "outliers", "from", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L153-L175
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_out_of_bounds
def remove_out_of_bounds(self, data, low_bound, high_bound): """ Remove out of bound datapoints from dataframe. This function removes all points < low_bound and > high_bound. To Do, 1. Add a different boundary for each column. Parameters ---------- data : pd.DataFrame() Dataframe to remove bounds from. low_bound : int Low bound of the data. high_bound : int High bound of the data. Returns ------- pd.DataFrame() Dataframe with out of bounds removed. """ data = data.dropna() data = data[(data > low_bound).all(axis=1) & (data < high_bound).all(axis=1)] return data
python
def remove_out_of_bounds(self, data, low_bound, high_bound): data = data.dropna() data = data[(data > low_bound).all(axis=1) & (data < high_bound).all(axis=1)] return data
[ "def", "remove_out_of_bounds", "(", "self", ",", "data", ",", "low_bound", ",", "high_bound", ")", ":", "data", "=", "data", ".", "dropna", "(", ")", "data", "=", "data", "[", "(", "data", ">", "low_bound", ")", ".", "all", "(", "axis", "=", "1", ")", "&", "(", "data", "<", "high_bound", ")", ".", "all", "(", "axis", "=", "1", ")", "]", "return", "data" ]
Remove out of bound datapoints from dataframe. This function removes all points < low_bound and > high_bound. To Do, 1. Add a different boundary for each column. Parameters ---------- data : pd.DataFrame() Dataframe to remove bounds from. low_bound : int Low bound of the data. high_bound : int High bound of the data. Returns ------- pd.DataFrame() Dataframe with out of bounds removed.
[ "Remove", "out", "of", "bound", "datapoints", "from", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L178-L203
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data._set_TS_index
def _set_TS_index(self, data): """ Convert index to datetime and all other columns to numeric Parameters ---------- data : pd.DataFrame() Input dataframe. Returns ------- pd.DataFrame() Modified dataframe. """ # Set index data.index = pd.to_datetime(data.index, error= "ignore") # Format types to numeric for col in data.columns: data[col] = pd.to_numeric(data[col], errors="coerce") return data
python
def _set_TS_index(self, data): data.index = pd.to_datetime(data.index, error= "ignore") for col in data.columns: data[col] = pd.to_numeric(data[col], errors="coerce") return data
[ "def", "_set_TS_index", "(", "self", ",", "data", ")", ":", "# Set index", "data", ".", "index", "=", "pd", ".", "to_datetime", "(", "data", ".", "index", ",", "error", "=", "\"ignore\"", ")", "# Format types to numeric", "for", "col", "in", "data", ".", "columns", ":", "data", "[", "col", "]", "=", "pd", ".", "to_numeric", "(", "data", "[", "col", "]", ",", "errors", "=", "\"coerce\"", ")", "return", "data" ]
Convert index to datetime and all other columns to numeric Parameters ---------- data : pd.DataFrame() Input dataframe. Returns ------- pd.DataFrame() Modified dataframe.
[ "Convert", "index", "to", "datetime", "and", "all", "other", "columns", "to", "numeric" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L283-L305
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data._utc_to_local
def _utc_to_local(self, data, local_zone="America/Los_Angeles"): """ Adjust index of dataframe according to timezone that is requested by user. Parameters ---------- data : pd.DataFrame() Pandas dataframe of json timeseries response from server. local_zone : str pytz.timezone string of specified local timezone to change index to. Returns ------- pd.DataFrame() Pandas dataframe with timestamp index adjusted for local timezone. """ # Accounts for localtime shift data.index = data.index.tz_localize(pytz.utc).tz_convert(local_zone) # Gets rid of extra offset information so can compare with csv data data.index = data.index.tz_localize(None) return data
python
def _utc_to_local(self, data, local_zone="America/Los_Angeles"): data.index = data.index.tz_localize(pytz.utc).tz_convert(local_zone) data.index = data.index.tz_localize(None) return data
[ "def", "_utc_to_local", "(", "self", ",", "data", ",", "local_zone", "=", "\"America/Los_Angeles\"", ")", ":", "# Accounts for localtime shift", "data", ".", "index", "=", "data", ".", "index", ".", "tz_localize", "(", "pytz", ".", "utc", ")", ".", "tz_convert", "(", "local_zone", ")", "# Gets rid of extra offset information so can compare with csv data", "data", ".", "index", "=", "data", ".", "index", ".", "tz_localize", "(", "None", ")", "return", "data" ]
Adjust index of dataframe according to timezone that is requested by user. Parameters ---------- data : pd.DataFrame() Pandas dataframe of json timeseries response from server. local_zone : str pytz.timezone string of specified local timezone to change index to. Returns ------- pd.DataFrame() Pandas dataframe with timestamp index adjusted for local timezone.
[ "Adjust", "index", "of", "dataframe", "according", "to", "timezone", "that", "is", "requested", "by", "user", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L308-L331
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data._local_to_utc
def _local_to_utc(self, timestamp, local_zone="America/Los_Angeles"): """ Convert local timestamp to UTC. Parameters ---------- timestamp : pd.DataFrame() Input Pandas dataframe whose index needs to be changed. local_zone : str Name of local zone. Defaults to PST. Returns ------- pd.DataFrame() Dataframe with UTC timestamps. """ timestamp_new = pd.to_datetime(timestamp, infer_datetime_format=True, errors='coerce') timestamp_new = timestamp_new.tz_localize(local_zone).tz_convert(pytz.utc) timestamp_new = timestamp_new.strftime('%Y-%m-%d %H:%M:%S') return timestamp_new
python
def _local_to_utc(self, timestamp, local_zone="America/Los_Angeles"): timestamp_new = pd.to_datetime(timestamp, infer_datetime_format=True, errors='coerce') timestamp_new = timestamp_new.tz_localize(local_zone).tz_convert(pytz.utc) timestamp_new = timestamp_new.strftime('%Y-%m-%d %H:%M:%S') return timestamp_new
[ "def", "_local_to_utc", "(", "self", ",", "timestamp", ",", "local_zone", "=", "\"America/Los_Angeles\"", ")", ":", "timestamp_new", "=", "pd", ".", "to_datetime", "(", "timestamp", ",", "infer_datetime_format", "=", "True", ",", "errors", "=", "'coerce'", ")", "timestamp_new", "=", "timestamp_new", ".", "tz_localize", "(", "local_zone", ")", ".", "tz_convert", "(", "pytz", ".", "utc", ")", "timestamp_new", "=", "timestamp_new", ".", "strftime", "(", "'%Y-%m-%d %H:%M:%S'", ")", "return", "timestamp_new" ]
Convert local timestamp to UTC. Parameters ---------- timestamp : pd.DataFrame() Input Pandas dataframe whose index needs to be changed. local_zone : str Name of local zone. Defaults to PST. Returns ------- pd.DataFrame() Dataframe with UTC timestamps.
[ "Convert", "local", "timestamp", "to", "UTC", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L334-L354
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_start_NaN
def remove_start_NaN(self, data, var=None): """ Remove start NaN. CHECK: Note issue with multi-column df. Parameters ---------- data : pd.DataFrame() Input dataframe. var : list(str) List that specifies specific columns of dataframe. Returns ------- pd.DataFrame() Dataframe starting from its first valid index. """ # Limit to one or some variables if var: start_ok_data = data[var].first_valid_index() else: start_ok_data = data.first_valid_index() data = data.loc[start_ok_data:, :] return data
python
def remove_start_NaN(self, data, var=None): if var: start_ok_data = data[var].first_valid_index() else: start_ok_data = data.first_valid_index() data = data.loc[start_ok_data:, :] return data
[ "def", "remove_start_NaN", "(", "self", ",", "data", ",", "var", "=", "None", ")", ":", "# Limit to one or some variables", "if", "var", ":", "start_ok_data", "=", "data", "[", "var", "]", ".", "first_valid_index", "(", ")", "else", ":", "start_ok_data", "=", "data", ".", "first_valid_index", "(", ")", "data", "=", "data", ".", "loc", "[", "start_ok_data", ":", ",", ":", "]", "return", "data" ]
Remove start NaN. CHECK: Note issue with multi-column df. Parameters ---------- data : pd.DataFrame() Input dataframe. var : list(str) List that specifies specific columns of dataframe. Returns ------- pd.DataFrame() Dataframe starting from its first valid index.
[ "Remove", "start", "NaN", ".", "CHECK", ":", "Note", "issue", "with", "multi", "-", "column", "df", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "var", ":", "list", "(", "str", ")", "List", "that", "specifies", "specific", "columns", "of", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L357-L383
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_end_NaN
def remove_end_NaN(self, data, var=None): """ Remove end NaN. CHECK: Note issue with multi-column df. Parameters ---------- data : pd.DataFrame() Input dataframe. var : list(str) List that specifies specific columns of dataframe. Returns ------- pd.DataFrame() Dataframe starting from its last valid index. """ # Limit to one or some variables if var: end_ok_data = data[var].last_valid_index() else: end_ok_data = data.last_valid_index() data = data.loc[:end_ok_data, :] return data
python
def remove_end_NaN(self, data, var=None): if var: end_ok_data = data[var].last_valid_index() else: end_ok_data = data.last_valid_index() data = data.loc[:end_ok_data, :] return data
[ "def", "remove_end_NaN", "(", "self", ",", "data", ",", "var", "=", "None", ")", ":", "# Limit to one or some variables", "if", "var", ":", "end_ok_data", "=", "data", "[", "var", "]", ".", "last_valid_index", "(", ")", "else", ":", "end_ok_data", "=", "data", ".", "last_valid_index", "(", ")", "data", "=", "data", ".", "loc", "[", ":", "end_ok_data", ",", ":", "]", "return", "data" ]
Remove end NaN. CHECK: Note issue with multi-column df. Parameters ---------- data : pd.DataFrame() Input dataframe. var : list(str) List that specifies specific columns of dataframe. Returns ------- pd.DataFrame() Dataframe starting from its last valid index.
[ "Remove", "end", "NaN", ".", "CHECK", ":", "Note", "issue", "with", "multi", "-", "column", "df", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "var", ":", "list", "(", "str", ")", "List", "that", "specifies", "specific", "columns", "of", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L386-L412
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data._find_missing
def _find_missing(self, data, return_bool=False): """ ??? Parameters ---------- data : pd.DataFrame() Input dataframe. return_bool : bool ??? Returns ------- pd.DataFrame() ??? """ # This returns the full table with True where the condition is true if return_bool == False: data = self._find_missing_return_frame(data) return data # This returns a bool selector if any of the column is True elif return_bool == "any": bool_sel = self._find_missing_return_frame(data).any(axis=0) return bool_sel # This returns a bool selector if all of the column are True elif return_bool == "all": bool_sel = self._find_missing_return_frame(data).all(axis=0) return bool_sel else: print("error in multi_col_how input")
python
def _find_missing(self, data, return_bool=False): if return_bool == False: data = self._find_missing_return_frame(data) return data elif return_bool == "any": bool_sel = self._find_missing_return_frame(data).any(axis=0) return bool_sel elif return_bool == "all": bool_sel = self._find_missing_return_frame(data).all(axis=0) return bool_sel else: print("error in multi_col_how input")
[ "def", "_find_missing", "(", "self", ",", "data", ",", "return_bool", "=", "False", ")", ":", "# This returns the full table with True where the condition is true", "if", "return_bool", "==", "False", ":", "data", "=", "self", ".", "_find_missing_return_frame", "(", "data", ")", "return", "data", "# This returns a bool selector if any of the column is True", "elif", "return_bool", "==", "\"any\"", ":", "bool_sel", "=", "self", ".", "_find_missing_return_frame", "(", "data", ")", ".", "any", "(", "axis", "=", "0", ")", "return", "bool_sel", "# This returns a bool selector if all of the column are True", "elif", "return_bool", "==", "\"all\"", ":", "bool_sel", "=", "self", ".", "_find_missing_return_frame", "(", "data", ")", ".", "all", "(", "axis", "=", "0", ")", "return", "bool_sel", "else", ":", "print", "(", "\"error in multi_col_how input\"", ")" ]
??? Parameters ---------- data : pd.DataFrame() Input dataframe. return_bool : bool ??? Returns ------- pd.DataFrame() ???
[ "???" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L432-L465
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.display_missing
def display_missing(self, data, return_bool="any"): """ ??? Parameters ---------- data : pd.DataFrame() Input dataframe. return_bool : bool ??? Returns ------- pd.DataFrame() ??? """ if return_bool == "any": bool_sel = self._find_missing(data, return_bool="any") elif return_bool == "all": bool_sel = self._find_missing(data, return_bool="all") return data[bool_sel]
python
def display_missing(self, data, return_bool="any"): if return_bool == "any": bool_sel = self._find_missing(data, return_bool="any") elif return_bool == "all": bool_sel = self._find_missing(data, return_bool="all") return data[bool_sel]
[ "def", "display_missing", "(", "self", ",", "data", ",", "return_bool", "=", "\"any\"", ")", ":", "if", "return_bool", "==", "\"any\"", ":", "bool_sel", "=", "self", ".", "_find_missing", "(", "data", ",", "return_bool", "=", "\"any\"", ")", "elif", "return_bool", "==", "\"all\"", ":", "bool_sel", "=", "self", ".", "_find_missing", "(", "data", ",", "return_bool", "=", "\"all\"", ")", "return", "data", "[", "bool_sel", "]" ]
??? Parameters ---------- data : pd.DataFrame() Input dataframe. return_bool : bool ??? Returns ------- pd.DataFrame() ???
[ "???", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "return_bool", ":", "bool", "???" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L468-L491
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.count_missing
def count_missing(self, data, output="number"): """ ??? Parameters ---------- data : pd.DataFrame() Input dataframe. output : str Sting indicating the output of function (number or percent) Returns ------- int/float Count of missing data (int or float) """ count = self._find_missing(data,return_bool=False).sum() if output == "number": return count elif output == "percent": return ((count / (data.shape[0])) * 100)
python
def count_missing(self, data, output="number"): count = self._find_missing(data,return_bool=False).sum() if output == "number": return count elif output == "percent": return ((count / (data.shape[0])) * 100)
[ "def", "count_missing", "(", "self", ",", "data", ",", "output", "=", "\"number\"", ")", ":", "count", "=", "self", ".", "_find_missing", "(", "data", ",", "return_bool", "=", "False", ")", ".", "sum", "(", ")", "if", "output", "==", "\"number\"", ":", "return", "count", "elif", "output", "==", "\"percent\"", ":", "return", "(", "(", "count", "/", "(", "data", ".", "shape", "[", "0", "]", ")", ")", "*", "100", ")" ]
??? Parameters ---------- data : pd.DataFrame() Input dataframe. output : str Sting indicating the output of function (number or percent) Returns ------- int/float Count of missing data (int or float)
[ "???", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "output", ":", "str", "Sting", "indicating", "the", "output", "of", "function", "(", "number", "or", "percent", ")" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L494-L516
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_missing
def remove_missing(self, data, return_bool="any"): """ ??? Parameters ---------- data : pd.DataFrame() Input dataframe. return_bool : bool ??? Returns ------- pd.DataFrame() ??? """ if return_bool == "any": bool_sel = self._find_missing(data,return_bool="any") elif return_bool == "all": bool_sel = self._find_missing(data,return_bool="all") return data[~bool_sel]
python
def remove_missing(self, data, return_bool="any"): if return_bool == "any": bool_sel = self._find_missing(data,return_bool="any") elif return_bool == "all": bool_sel = self._find_missing(data,return_bool="all") return data[~bool_sel]
[ "def", "remove_missing", "(", "self", ",", "data", ",", "return_bool", "=", "\"any\"", ")", ":", "if", "return_bool", "==", "\"any\"", ":", "bool_sel", "=", "self", ".", "_find_missing", "(", "data", ",", "return_bool", "=", "\"any\"", ")", "elif", "return_bool", "==", "\"all\"", ":", "bool_sel", "=", "self", ".", "_find_missing", "(", "data", ",", "return_bool", "=", "\"all\"", ")", "return", "data", "[", "~", "bool_sel", "]" ]
??? Parameters ---------- data : pd.DataFrame() Input dataframe. return_bool : bool ??? Returns ------- pd.DataFrame() ???
[ "???", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "return_bool", ":", "bool", "???" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L519-L541
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data._find_outOfBound
def _find_outOfBound(self, data, lowBound, highBound): """ Mask for selecting data that is out of bounds. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. Returns ------- ??? """ data = ((data < lowBound) | (data > highBound)) return data
python
def _find_outOfBound(self, data, lowBound, highBound): data = ((data < lowBound) | (data > highBound)) return data
[ "def", "_find_outOfBound", "(", "self", ",", "data", ",", "lowBound", ",", "highBound", ")", ":", "data", "=", "(", "(", "data", "<", "lowBound", ")", "|", "(", "data", ">", "highBound", ")", ")", "return", "data" ]
Mask for selecting data that is out of bounds. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. Returns ------- ???
[ "Mask", "for", "selecting", "data", "that", "is", "out", "of", "bounds", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "lowBound", ":", "float", "Lower", "bound", "for", "dataframe", ".", "highBound", ":", "float", "Higher", "bound", "for", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L544-L563
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.display_outOfBound
def display_outOfBound(self, data, lowBound, highBound): """ Select data that is out of bounds. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. Returns ------- pd.DataFrame() Dataframe containing data that is out of bounds. """ data = data[self._find_outOfBound(data, lowBound, highBound).any(axis=1)] return data
python
def display_outOfBound(self, data, lowBound, highBound): data = data[self._find_outOfBound(data, lowBound, highBound).any(axis=1)] return data
[ "def", "display_outOfBound", "(", "self", ",", "data", ",", "lowBound", ",", "highBound", ")", ":", "data", "=", "data", "[", "self", ".", "_find_outOfBound", "(", "data", ",", "lowBound", ",", "highBound", ")", ".", "any", "(", "axis", "=", "1", ")", "]", "return", "data" ]
Select data that is out of bounds. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. Returns ------- pd.DataFrame() Dataframe containing data that is out of bounds.
[ "Select", "data", "that", "is", "out", "of", "bounds", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "lowBound", ":", "float", "Lower", "bound", "for", "dataframe", ".", "highBound", ":", "float", "Higher", "bound", "for", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L566-L586
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.count_outOfBound
def count_outOfBound(self, data, lowBound, highBound, output): """ Count the number of out of bounds data. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. output : str Sting indicating the output of function (number or percent) Returns ------- int/float Count of out of bounds data (int or float) """ count = self._find_outOfBound(data, lowBound, highBound).sum() if output == "number": return count elif output == "percent": return count / (data.shape[0]) * 1.0 * 100
python
def count_outOfBound(self, data, lowBound, highBound, output): count = self._find_outOfBound(data, lowBound, highBound).sum() if output == "number": return count elif output == "percent": return count / (data.shape[0]) * 1.0 * 100
[ "def", "count_outOfBound", "(", "self", ",", "data", ",", "lowBound", ",", "highBound", ",", "output", ")", ":", "count", "=", "self", ".", "_find_outOfBound", "(", "data", ",", "lowBound", ",", "highBound", ")", ".", "sum", "(", ")", "if", "output", "==", "\"number\"", ":", "return", "count", "elif", "output", "==", "\"percent\"", ":", "return", "count", "/", "(", "data", ".", "shape", "[", "0", "]", ")", "*", "1.0", "*", "100" ]
Count the number of out of bounds data. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. output : str Sting indicating the output of function (number or percent) Returns ------- int/float Count of out of bounds data (int or float)
[ "Count", "the", "number", "of", "out", "of", "bounds", "data", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "lowBound", ":", "float", "Lower", "bound", "for", "dataframe", ".", "highBound", ":", "float", "Higher", "bound", "for", "dataframe", ".", "output", ":", "str", "Sting", "indicating", "the", "output", "of", "function", "(", "number", "or", "percent", ")" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L589-L615
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_outOfBound
def remove_outOfBound(self, data, lowBound, highBound): """ Remove out of bounds data from input dataframe. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. Returns ------- pd.DataFrame() Dataframe with no out of bounds data. """ data = data[~self._find_outOfBound(data, lowBound, highBound).any(axis=1)] return data
python
def remove_outOfBound(self, data, lowBound, highBound): data = data[~self._find_outOfBound(data, lowBound, highBound).any(axis=1)] return data
[ "def", "remove_outOfBound", "(", "self", ",", "data", ",", "lowBound", ",", "highBound", ")", ":", "data", "=", "data", "[", "~", "self", ".", "_find_outOfBound", "(", "data", ",", "lowBound", ",", "highBound", ")", ".", "any", "(", "axis", "=", "1", ")", "]", "return", "data" ]
Remove out of bounds data from input dataframe. Parameters ---------- data : pd.DataFrame() Input dataframe. lowBound : float Lower bound for dataframe. highBound : float Higher bound for dataframe. Returns ------- pd.DataFrame() Dataframe with no out of bounds data.
[ "Remove", "out", "of", "bounds", "data", "from", "input", "dataframe", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "lowBound", ":", "float", "Lower", "bound", "for", "dataframe", ".", "highBound", ":", "float", "Higher", "bound", "for", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L618-L638
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data._calc_outliers_bounds
def _calc_outliers_bounds(self, data, method, coeff, window): """ Calculate the lower and higher bound for outlier detection. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. window : int Size of the moving window. Returns ------- (float, float) Lower and higher bound for detecting outliers. """ if method == "std": lowBound = (data.mean(axis=0) - coeff * data.std(axis=0)).values[0] highBound = (data.mean(axis=0) + coeff * data.std(axis=0)).values[0] elif method == "rstd": rl_mean=data.rolling(window=window).mean(how=any) rl_std = data.rolling(window=window).std(how=any).fillna(method='bfill').fillna(method='ffill') lowBound = rl_mean - coeff * rl_std highBound = rl_mean + coeff * rl_std elif method == "rmedian": rl_med = data.rolling(window=window, center=True).median().fillna( method='bfill').fillna(method='ffill') lowBound = rl_med - coeff highBound = rl_med + coeff # Coeff is multip for std and IQR or threshold for rolling median elif method == "iqr": Q1 = data.quantile(.25) # Coeff is multip for std or % of quartile Q3 = data.quantile(.75) IQR = Q3 - Q1 lowBound = Q1 - coeff * IQR highBound = Q3 + coeff * IQR elif method == "qtl": lowBound = data.quantile(.005) highBound = data.quantile(.995) else: print ("Method chosen does not exist") lowBound = None highBound = None return lowBound, highBound
python
def _calc_outliers_bounds(self, data, method, coeff, window): if method == "std": lowBound = (data.mean(axis=0) - coeff * data.std(axis=0)).values[0] highBound = (data.mean(axis=0) + coeff * data.std(axis=0)).values[0] elif method == "rstd": rl_mean=data.rolling(window=window).mean(how=any) rl_std = data.rolling(window=window).std(how=any).fillna(method='bfill').fillna(method='ffill') lowBound = rl_mean - coeff * rl_std highBound = rl_mean + coeff * rl_std elif method == "rmedian": rl_med = data.rolling(window=window, center=True).median().fillna( method='bfill').fillna(method='ffill') lowBound = rl_med - coeff highBound = rl_med + coeff elif method == "iqr": Q1 = data.quantile(.25) Q3 = data.quantile(.75) IQR = Q3 - Q1 lowBound = Q1 - coeff * IQR highBound = Q3 + coeff * IQR elif method == "qtl": lowBound = data.quantile(.005) highBound = data.quantile(.995) else: print ("Method chosen does not exist") lowBound = None highBound = None return lowBound, highBound
[ "def", "_calc_outliers_bounds", "(", "self", ",", "data", ",", "method", ",", "coeff", ",", "window", ")", ":", "if", "method", "==", "\"std\"", ":", "lowBound", "=", "(", "data", ".", "mean", "(", "axis", "=", "0", ")", "-", "coeff", "*", "data", ".", "std", "(", "axis", "=", "0", ")", ")", ".", "values", "[", "0", "]", "highBound", "=", "(", "data", ".", "mean", "(", "axis", "=", "0", ")", "+", "coeff", "*", "data", ".", "std", "(", "axis", "=", "0", ")", ")", ".", "values", "[", "0", "]", "elif", "method", "==", "\"rstd\"", ":", "rl_mean", "=", "data", ".", "rolling", "(", "window", "=", "window", ")", ".", "mean", "(", "how", "=", "any", ")", "rl_std", "=", "data", ".", "rolling", "(", "window", "=", "window", ")", ".", "std", "(", "how", "=", "any", ")", ".", "fillna", "(", "method", "=", "'bfill'", ")", ".", "fillna", "(", "method", "=", "'ffill'", ")", "lowBound", "=", "rl_mean", "-", "coeff", "*", "rl_std", "highBound", "=", "rl_mean", "+", "coeff", "*", "rl_std", "elif", "method", "==", "\"rmedian\"", ":", "rl_med", "=", "data", ".", "rolling", "(", "window", "=", "window", ",", "center", "=", "True", ")", ".", "median", "(", ")", ".", "fillna", "(", "method", "=", "'bfill'", ")", ".", "fillna", "(", "method", "=", "'ffill'", ")", "lowBound", "=", "rl_med", "-", "coeff", "highBound", "=", "rl_med", "+", "coeff", "# Coeff is multip for std and IQR or threshold for rolling median", "elif", "method", "==", "\"iqr\"", ":", "Q1", "=", "data", ".", "quantile", "(", ".25", ")", "# Coeff is multip for std or % of quartile", "Q3", "=", "data", ".", "quantile", "(", ".75", ")", "IQR", "=", "Q3", "-", "Q1", "lowBound", "=", "Q1", "-", "coeff", "*", "IQR", "highBound", "=", "Q3", "+", "coeff", "*", "IQR", "elif", "method", "==", "\"qtl\"", ":", "lowBound", "=", "data", ".", "quantile", "(", ".005", ")", "highBound", "=", "data", ".", "quantile", "(", ".995", ")", "else", ":", "print", "(", "\"Method chosen does not exist\"", ")", "lowBound", "=", "None", "highBound", "=", "None", "return", "lowBound", ",", "highBound" ]
Calculate the lower and higher bound for outlier detection. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. window : int Size of the moving window. Returns ------- (float, float) Lower and higher bound for detecting outliers.
[ "Calculate", "the", "lower", "and", "higher", "bound", "for", "outlier", "detection", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "method", ":", "str", "Method", "to", "use", "for", "calculating", "the", "lower", "and", "higher", "bounds", ".", "coeff", ":", "int", "Coefficient", "to", "use", "in", "calculation", ".", "window", ":", "int", "Size", "of", "the", "moving", "window", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L641-L698
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.display_outliers
def display_outliers(self, data, method, coeff, window=10): """ Returns dataframe with outliers. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. window : int Size of the moving window. Returns ------- pd.DataFrame() Dataframe containing outliers. """ lowBound, highBound = self._calc_outliers_bounds(data, method, coeff, window) data = self.display_outOfBound(data, lowBound, highBound) return data
python
def display_outliers(self, data, method, coeff, window=10): lowBound, highBound = self._calc_outliers_bounds(data, method, coeff, window) data = self.display_outOfBound(data, lowBound, highBound) return data
[ "def", "display_outliers", "(", "self", ",", "data", ",", "method", ",", "coeff", ",", "window", "=", "10", ")", ":", "lowBound", ",", "highBound", "=", "self", ".", "_calc_outliers_bounds", "(", "data", ",", "method", ",", "coeff", ",", "window", ")", "data", "=", "self", ".", "display_outOfBound", "(", "data", ",", "lowBound", ",", "highBound", ")", "return", "data" ]
Returns dataframe with outliers. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. window : int Size of the moving window. Returns ------- pd.DataFrame() Dataframe containing outliers.
[ "Returns", "dataframe", "with", "outliers", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "method", ":", "str", "Method", "to", "use", "for", "calculating", "the", "lower", "and", "higher", "bounds", ".", "coeff", ":", "int", "Coefficient", "to", "use", "in", "calculation", ".", "window", ":", "int", "Size", "of", "the", "moving", "window", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L701-L724
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.count_outliers
def count_outliers(self, data, method, coeff, output, window=10): """ Count the number of outliers in dataframe. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. output : str Sting indicating the output of function (number or percent) window : int Size of the moving window. Returns ------- int/float Count of out of bounds data (int or float) """ lowBound, highBound = self._calc_outliers_bounds(data, method, coeff, window) count = self.count_outOfBound(data, lowBound, highBound, output=output) return count
python
def count_outliers(self, data, method, coeff, output, window=10): lowBound, highBound = self._calc_outliers_bounds(data, method, coeff, window) count = self.count_outOfBound(data, lowBound, highBound, output=output) return count
[ "def", "count_outliers", "(", "self", ",", "data", ",", "method", ",", "coeff", ",", "output", ",", "window", "=", "10", ")", ":", "lowBound", ",", "highBound", "=", "self", ".", "_calc_outliers_bounds", "(", "data", ",", "method", ",", "coeff", ",", "window", ")", "count", "=", "self", ".", "count_outOfBound", "(", "data", ",", "lowBound", ",", "highBound", ",", "output", "=", "output", ")", "return", "count" ]
Count the number of outliers in dataframe. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. output : str Sting indicating the output of function (number or percent) window : int Size of the moving window. Returns ------- int/float Count of out of bounds data (int or float)
[ "Count", "the", "number", "of", "outliers", "in", "dataframe", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "method", ":", "str", "Method", "to", "use", "for", "calculating", "the", "lower", "and", "higher", "bounds", ".", "coeff", ":", "int", "Coefficient", "to", "use", "in", "calculation", ".", "output", ":", "str", "Sting", "indicating", "the", "output", "of", "function", "(", "number", "or", "percent", ")", "window", ":", "int", "Size", "of", "the", "moving", "window", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L727-L752
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.remove_outliers
def remove_outliers(self, data, method, coeff, window=10): """ Remove the outliers in dataframe. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. window : int Size of the moving window. Returns ------- pd.DataFrame() Dataframe with its outliers removed. """ lowBound, highBound = self._calc_outliers_bounds(data, method, coeff, window) data = self.remove_outOfBound(data, lowBound, highBound) return data
python
def remove_outliers(self, data, method, coeff, window=10): lowBound, highBound = self._calc_outliers_bounds(data, method, coeff, window) data = self.remove_outOfBound(data, lowBound, highBound) return data
[ "def", "remove_outliers", "(", "self", ",", "data", ",", "method", ",", "coeff", ",", "window", "=", "10", ")", ":", "lowBound", ",", "highBound", "=", "self", ".", "_calc_outliers_bounds", "(", "data", ",", "method", ",", "coeff", ",", "window", ")", "data", "=", "self", ".", "remove_outOfBound", "(", "data", ",", "lowBound", ",", "highBound", ")", "return", "data" ]
Remove the outliers in dataframe. Parameters ---------- data : pd.DataFrame() Input dataframe. method : str Method to use for calculating the lower and higher bounds. coeff : int Coefficient to use in calculation. window : int Size of the moving window. Returns ------- pd.DataFrame() Dataframe with its outliers removed.
[ "Remove", "the", "outliers", "in", "dataframe", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "method", ":", "str", "Method", "to", "use", "for", "calculating", "the", "lower", "and", "higher", "bounds", ".", "coeff", ":", "int", "Coefficient", "to", "use", "in", "calculation", ".", "window", ":", "int", "Size", "of", "the", "moving", "window", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L755-L778
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.count_if
def count_if(self, data, condition, val, output="number"): """ Count the number of values that match the condition. Parameters ---------- data : pd.DataFrame() Input dataframe. condition : str Condition to match. val : float Value to compare against. output : str Sting indicating the output of function (number or percent) Returns ------- int/float Count of values that match the condition (int or float) """ if condition == "=": count = self._find_equal_to_values(data,val).sum() elif condition == ">": count = self._find_greater_than_values(data,val).sum() elif condition == "<": count = self._find_less_than_values(data,val).sum() elif condition == ">=": count = self._find_greater_than_or_equal_to_values(data,val).sum() elif condition == "<=": count = self._find_less_than_or_equal_to_values(data,val).sum() elif condition == "!=": count = self._find_different_from_values(data,val).sum() if output == "number": return count elif output == "percent": return count/data.shape[0]*1.0*100 return count
python
def count_if(self, data, condition, val, output="number"): if condition == "=": count = self._find_equal_to_values(data,val).sum() elif condition == ">": count = self._find_greater_than_values(data,val).sum() elif condition == "<": count = self._find_less_than_values(data,val).sum() elif condition == ">=": count = self._find_greater_than_or_equal_to_values(data,val).sum() elif condition == "<=": count = self._find_less_than_or_equal_to_values(data,val).sum() elif condition == "!=": count = self._find_different_from_values(data,val).sum() if output == "number": return count elif output == "percent": return count/data.shape[0]*1.0*100 return count
[ "def", "count_if", "(", "self", ",", "data", ",", "condition", ",", "val", ",", "output", "=", "\"number\"", ")", ":", "if", "condition", "==", "\"=\"", ":", "count", "=", "self", ".", "_find_equal_to_values", "(", "data", ",", "val", ")", ".", "sum", "(", ")", "elif", "condition", "==", "\">\"", ":", "count", "=", "self", ".", "_find_greater_than_values", "(", "data", ",", "val", ")", ".", "sum", "(", ")", "elif", "condition", "==", "\"<\"", ":", "count", "=", "self", ".", "_find_less_than_values", "(", "data", ",", "val", ")", ".", "sum", "(", ")", "elif", "condition", "==", "\">=\"", ":", "count", "=", "self", ".", "_find_greater_than_or_equal_to_values", "(", "data", ",", "val", ")", ".", "sum", "(", ")", "elif", "condition", "==", "\"<=\"", ":", "count", "=", "self", ".", "_find_less_than_or_equal_to_values", "(", "data", ",", "val", ")", ".", "sum", "(", ")", "elif", "condition", "==", "\"!=\"", ":", "count", "=", "self", ".", "_find_different_from_values", "(", "data", ",", "val", ")", ".", "sum", "(", ")", "if", "output", "==", "\"number\"", ":", "return", "count", "elif", "output", "==", "\"percent\"", ":", "return", "count", "/", "data", ".", "shape", "[", "0", "]", "*", "1.0", "*", "100", "return", "count" ]
Count the number of values that match the condition. Parameters ---------- data : pd.DataFrame() Input dataframe. condition : str Condition to match. val : float Value to compare against. output : str Sting indicating the output of function (number or percent) Returns ------- int/float Count of values that match the condition (int or float)
[ "Count", "the", "number", "of", "values", "that", "match", "the", "condition", ".", "Parameters", "----------", "data", ":", "pd", ".", "DataFrame", "()", "Input", "dataframe", ".", "condition", ":", "str", "Condition", "to", "match", ".", "val", ":", "float", "Value", "to", "compare", "against", ".", "output", ":", "str", "Sting", "indicating", "the", "output", "of", "function", "(", "number", "or", "percent", ")" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L907-L946
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.find_uuid
def find_uuid(self, obj, column_name): """ Find uuid. Parameters ---------- obj : ??? the object returned by the MDAL Query column_name : str input point returned from MDAL Query Returns ------- str the uuid that correlates with the data """ keys = obj.context.keys() for i in keys: if column_name in obj.context[i]['?point']: uuid = i return i
python
def find_uuid(self, obj, column_name): keys = obj.context.keys() for i in keys: if column_name in obj.context[i]['?point']: uuid = i return i
[ "def", "find_uuid", "(", "self", ",", "obj", ",", "column_name", ")", ":", "keys", "=", "obj", ".", "context", ".", "keys", "(", ")", "for", "i", "in", "keys", ":", "if", "column_name", "in", "obj", ".", "context", "[", "i", "]", "[", "'?point'", "]", ":", "uuid", "=", "i", "return", "i" ]
Find uuid. Parameters ---------- obj : ??? the object returned by the MDAL Query column_name : str input point returned from MDAL Query Returns ------- str the uuid that correlates with the data
[ "Find", "uuid", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L952-L975
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.identify_missing
def identify_missing(self, df, check_start=True): """ Identify missing data. Parameters ---------- df : pd.DataFrame() Dataframe to check for missing data. check_start : bool turns 0 to 1 for the first observation, to display the start of the data as the beginning of the missing data event Returns ------- pd.DataFrame(), str dataframe where 1 indicates missing data and 0 indicates reported data, returns the column name generated from the MDAL Query """ # Check start changes the first value of df to 1, when the data stream is initially missing # This allows the diff function to acknowledge the missing data data_missing = df.isnull() * 1 col_name = str(data_missing.columns[0]) # When there is no data stream at the beginning we change it to 1 if check_start & data_missing[col_name][0] == 1: data_missing[col_name][0] = 0 return data_missing, col_name
python
def identify_missing(self, df, check_start=True): data_missing = df.isnull() * 1 col_name = str(data_missing.columns[0]) if check_start & data_missing[col_name][0] == 1: data_missing[col_name][0] = 0 return data_missing, col_name
[ "def", "identify_missing", "(", "self", ",", "df", ",", "check_start", "=", "True", ")", ":", "# Check start changes the first value of df to 1, when the data stream is initially missing", "# This allows the diff function to acknowledge the missing data", "data_missing", "=", "df", ".", "isnull", "(", ")", "*", "1", "col_name", "=", "str", "(", "data_missing", ".", "columns", "[", "0", "]", ")", "# When there is no data stream at the beginning we change it to 1", "if", "check_start", "&", "data_missing", "[", "col_name", "]", "[", "0", "]", "==", "1", ":", "data_missing", "[", "col_name", "]", "[", "0", "]", "=", "0", "return", "data_missing", ",", "col_name" ]
Identify missing data. Parameters ---------- df : pd.DataFrame() Dataframe to check for missing data. check_start : bool turns 0 to 1 for the first observation, to display the start of the data as the beginning of the missing data event Returns ------- pd.DataFrame(), str dataframe where 1 indicates missing data and 0 indicates reported data, returns the column name generated from the MDAL Query
[ "Identify", "missing", "data", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L978-L1006
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.diff_boolean
def diff_boolean(self, df, column_name=None, uuid=None, duration=True, min_event_filter='3 hours'): """ takes the dataframe of missing values, and returns a dataframe that indicates the length of each event where data was continuously missing Parameters ---------- df : pd.DataFrame() Dataframe to check for missing data (must be in boolean format where 1 indicates missing data. column_name : str the original column name produced by MDAL Query uuid : str the uuid associated with the meter, if known duration : bool If True, the duration will be displayed in the results. If false the column will be dropped. min_event_filter : str Filters out the events that are less than the given time period Returns ------- pd.DataFrame() dataframe with the start time of the event (as the index), end time of the event (first time when data is reported) """ if uuid == None: uuid = 'End' data_gaps = df[(df.diff() == 1) | (df.diff() == -1)].dropna() data_gaps["duration"] = abs(data_gaps.index.to_series().diff(periods=-1)) data_gaps[uuid] = data_gaps.index + (data_gaps["duration"]) data_gaps = data_gaps[data_gaps["duration"] > pd.Timedelta(min_event_filter)] data_gaps = data_gaps[data_gaps[column_name] == 1] data_gaps.pop(column_name) if not duration: data_gaps.pop('duration') data_gaps.index = data_gaps.index.strftime(date_format="%Y-%m-%d %H:%M:%S") data_gaps[uuid] = data_gaps[uuid].dt.strftime(date_format="%Y-%m-%d %H:%M:%S") return data_gaps
python
def diff_boolean(self, df, column_name=None, uuid=None, duration=True, min_event_filter='3 hours'): if uuid == None: uuid = 'End' data_gaps = df[(df.diff() == 1) | (df.diff() == -1)].dropna() data_gaps["duration"] = abs(data_gaps.index.to_series().diff(periods=-1)) data_gaps[uuid] = data_gaps.index + (data_gaps["duration"]) data_gaps = data_gaps[data_gaps["duration"] > pd.Timedelta(min_event_filter)] data_gaps = data_gaps[data_gaps[column_name] == 1] data_gaps.pop(column_name) if not duration: data_gaps.pop('duration') data_gaps.index = data_gaps.index.strftime(date_format="%Y-%m-%d %H:%M:%S") data_gaps[uuid] = data_gaps[uuid].dt.strftime(date_format="%Y-%m-%d %H:%M:%S") return data_gaps
[ "def", "diff_boolean", "(", "self", ",", "df", ",", "column_name", "=", "None", ",", "uuid", "=", "None", ",", "duration", "=", "True", ",", "min_event_filter", "=", "'3 hours'", ")", ":", "if", "uuid", "==", "None", ":", "uuid", "=", "'End'", "data_gaps", "=", "df", "[", "(", "df", ".", "diff", "(", ")", "==", "1", ")", "|", "(", "df", ".", "diff", "(", ")", "==", "-", "1", ")", "]", ".", "dropna", "(", ")", "data_gaps", "[", "\"duration\"", "]", "=", "abs", "(", "data_gaps", ".", "index", ".", "to_series", "(", ")", ".", "diff", "(", "periods", "=", "-", "1", ")", ")", "data_gaps", "[", "uuid", "]", "=", "data_gaps", ".", "index", "+", "(", "data_gaps", "[", "\"duration\"", "]", ")", "data_gaps", "=", "data_gaps", "[", "data_gaps", "[", "\"duration\"", "]", ">", "pd", ".", "Timedelta", "(", "min_event_filter", ")", "]", "data_gaps", "=", "data_gaps", "[", "data_gaps", "[", "column_name", "]", "==", "1", "]", "data_gaps", ".", "pop", "(", "column_name", ")", "if", "not", "duration", ":", "data_gaps", ".", "pop", "(", "'duration'", ")", "data_gaps", ".", "index", "=", "data_gaps", ".", "index", ".", "strftime", "(", "date_format", "=", "\"%Y-%m-%d %H:%M:%S\"", ")", "data_gaps", "[", "uuid", "]", "=", "data_gaps", "[", "uuid", "]", ".", "dt", ".", "strftime", "(", "date_format", "=", "\"%Y-%m-%d %H:%M:%S\"", ")", "return", "data_gaps" ]
takes the dataframe of missing values, and returns a dataframe that indicates the length of each event where data was continuously missing Parameters ---------- df : pd.DataFrame() Dataframe to check for missing data (must be in boolean format where 1 indicates missing data. column_name : str the original column name produced by MDAL Query uuid : str the uuid associated with the meter, if known duration : bool If True, the duration will be displayed in the results. If false the column will be dropped. min_event_filter : str Filters out the events that are less than the given time period Returns ------- pd.DataFrame() dataframe with the start time of the event (as the index), end time of the event (first time when data is reported)
[ "takes", "the", "dataframe", "of", "missing", "values", "and", "returns", "a", "dataframe", "that", "indicates", "the", "length", "of", "each", "event", "where", "data", "was", "continuously", "missing" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L1009-L1050
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.analyze_quality_table
def analyze_quality_table(self, obj,low_bound=None, high_bound=None): """ Takes in an the object returned by the MDAL query, and analyzes the quality of the data for each column in the df. Returns a df of data quality metrics To Do ----- Need to make it specific for varying meters and label it for each type, Either separate functions or make the function broader Parameters ---------- obj : ??? the object returned by the MDAL Query low_bound : float all data equal to or below this value will be interpreted as missing data high_bound : float all data above this value will be interpreted as missing Returns ------- pd.DataFrame() returns data frame with % missing data, average duration of missing data event and standard deviation of that duration for each column of data """ data = obj.df N_rows = 3 N_cols = data.shape[1] d = pd.DataFrame(np.zeros((N_rows, N_cols)), index=['% Missing', 'AVG Length Missing', 'Std dev. Missing'], columns=[data.columns]) if low_bound: data = data.where(data >= low_bound) if high_bound: data=data.where(data < high_bound) for i in range(N_cols): data_per_meter = data.iloc[:, [i]] data_missing, meter = self.identify_missing(data_per_meter) percentage = data_missing.sum() / (data.shape[0]) * 100 data_gaps = self.diff_boolean(data_missing, column_name=meter) missing_mean = data_gaps.mean() std_dev = data_gaps.std() d.loc["% Missing", meter] = percentage[meter] d.loc["AVG Length Missing", meter] = missing_mean['duration'] d.loc["Std dev. Missing", meter] = std_dev['duration'] return d
python
def analyze_quality_table(self, obj,low_bound=None, high_bound=None): data = obj.df N_rows = 3 N_cols = data.shape[1] d = pd.DataFrame(np.zeros((N_rows, N_cols)), index=['% Missing', 'AVG Length Missing', 'Std dev. Missing'], columns=[data.columns]) if low_bound: data = data.where(data >= low_bound) if high_bound: data=data.where(data < high_bound) for i in range(N_cols): data_per_meter = data.iloc[:, [i]] data_missing, meter = self.identify_missing(data_per_meter) percentage = data_missing.sum() / (data.shape[0]) * 100 data_gaps = self.diff_boolean(data_missing, column_name=meter) missing_mean = data_gaps.mean() std_dev = data_gaps.std() d.loc["% Missing", meter] = percentage[meter] d.loc["AVG Length Missing", meter] = missing_mean['duration'] d.loc["Std dev. Missing", meter] = std_dev['duration'] return d
[ "def", "analyze_quality_table", "(", "self", ",", "obj", ",", "low_bound", "=", "None", ",", "high_bound", "=", "None", ")", ":", "data", "=", "obj", ".", "df", "N_rows", "=", "3", "N_cols", "=", "data", ".", "shape", "[", "1", "]", "d", "=", "pd", ".", "DataFrame", "(", "np", ".", "zeros", "(", "(", "N_rows", ",", "N_cols", ")", ")", ",", "index", "=", "[", "'% Missing'", ",", "'AVG Length Missing'", ",", "'Std dev. Missing'", "]", ",", "columns", "=", "[", "data", ".", "columns", "]", ")", "if", "low_bound", ":", "data", "=", "data", ".", "where", "(", "data", ">=", "low_bound", ")", "if", "high_bound", ":", "data", "=", "data", ".", "where", "(", "data", "<", "high_bound", ")", "for", "i", "in", "range", "(", "N_cols", ")", ":", "data_per_meter", "=", "data", ".", "iloc", "[", ":", ",", "[", "i", "]", "]", "data_missing", ",", "meter", "=", "self", ".", "identify_missing", "(", "data_per_meter", ")", "percentage", "=", "data_missing", ".", "sum", "(", ")", "/", "(", "data", ".", "shape", "[", "0", "]", ")", "*", "100", "data_gaps", "=", "self", ".", "diff_boolean", "(", "data_missing", ",", "column_name", "=", "meter", ")", "missing_mean", "=", "data_gaps", ".", "mean", "(", ")", "std_dev", "=", "data_gaps", ".", "std", "(", ")", "d", ".", "loc", "[", "\"% Missing\"", ",", "meter", "]", "=", "percentage", "[", "meter", "]", "d", ".", "loc", "[", "\"AVG Length Missing\"", ",", "meter", "]", "=", "missing_mean", "[", "'duration'", "]", "d", ".", "loc", "[", "\"Std dev. Missing\"", ",", "meter", "]", "=", "std_dev", "[", "'duration'", "]", "return", "d" ]
Takes in an the object returned by the MDAL query, and analyzes the quality of the data for each column in the df. Returns a df of data quality metrics To Do ----- Need to make it specific for varying meters and label it for each type, Either separate functions or make the function broader Parameters ---------- obj : ??? the object returned by the MDAL Query low_bound : float all data equal to or below this value will be interpreted as missing data high_bound : float all data above this value will be interpreted as missing Returns ------- pd.DataFrame() returns data frame with % missing data, average duration of missing data event and standard deviation of that duration for each column of data
[ "Takes", "in", "an", "the", "object", "returned", "by", "the", "MDAL", "query", "and", "analyzes", "the", "quality", "of", "the", "data", "for", "each", "column", "in", "the", "df", ".", "Returns", "a", "df", "of", "data", "quality", "metrics" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L1053-L1108
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.analyze_quality_graph
def analyze_quality_graph(self, obj): """ Takes in an the object returned by the MDAL query, and analyzes the quality of the data for each column in the df in the form of graphs. The Graphs returned show missing data events over time, and missing data frequency during each hour of the day To Do ----- Need to make it specific for varying meters and label it for each type, Either separate functions or make the function broader Parameters ---------- obj : ??? the object returned by the MDAL Query """ data = obj.df for i in range(data.shape[1]): data_per_meter = data.iloc[:, [i]] # need to make this work or change the structure data_missing, meter = self.identify_missing(data_per_meter) percentage = data_missing.sum() / (data.shape[0]) * 100 print('Percentage Missing of ' + meter + ' data: ' + str(int(percentage)) + '%') data_missing.plot(figsize=(18, 5), x_compat=True, title=meter + " Missing Data over the Time interval") data_gaps = self.diff_boolean(data_missing, column_name=meter) data_missing['Hour'] = data_missing.index.hour ymax = int(data_missing.groupby('Hour').sum().max() + 10) data_missing.groupby('Hour').sum().plot(ylim=(0, ymax), figsize=(18, 5), title=meter + " Time of Day of Missing Data") print(data_gaps)
python
def analyze_quality_graph(self, obj): data = obj.df for i in range(data.shape[1]): data_per_meter = data.iloc[:, [i]] data_missing, meter = self.identify_missing(data_per_meter) percentage = data_missing.sum() / (data.shape[0]) * 100 print('Percentage Missing of ' + meter + ' data: ' + str(int(percentage)) + '%') data_missing.plot(figsize=(18, 5), x_compat=True, title=meter + " Missing Data over the Time interval") data_gaps = self.diff_boolean(data_missing, column_name=meter) data_missing['Hour'] = data_missing.index.hour ymax = int(data_missing.groupby('Hour').sum().max() + 10) data_missing.groupby('Hour').sum().plot(ylim=(0, ymax), figsize=(18, 5), title=meter + " Time of Day of Missing Data") print(data_gaps)
[ "def", "analyze_quality_graph", "(", "self", ",", "obj", ")", ":", "data", "=", "obj", ".", "df", "for", "i", "in", "range", "(", "data", ".", "shape", "[", "1", "]", ")", ":", "data_per_meter", "=", "data", ".", "iloc", "[", ":", ",", "[", "i", "]", "]", "# need to make this work or change the structure", "data_missing", ",", "meter", "=", "self", ".", "identify_missing", "(", "data_per_meter", ")", "percentage", "=", "data_missing", ".", "sum", "(", ")", "/", "(", "data", ".", "shape", "[", "0", "]", ")", "*", "100", "print", "(", "'Percentage Missing of '", "+", "meter", "+", "' data: '", "+", "str", "(", "int", "(", "percentage", ")", ")", "+", "'%'", ")", "data_missing", ".", "plot", "(", "figsize", "=", "(", "18", ",", "5", ")", ",", "x_compat", "=", "True", ",", "title", "=", "meter", "+", "\" Missing Data over the Time interval\"", ")", "data_gaps", "=", "self", ".", "diff_boolean", "(", "data_missing", ",", "column_name", "=", "meter", ")", "data_missing", "[", "'Hour'", "]", "=", "data_missing", ".", "index", ".", "hour", "ymax", "=", "int", "(", "data_missing", ".", "groupby", "(", "'Hour'", ")", ".", "sum", "(", ")", ".", "max", "(", ")", "+", "10", ")", "data_missing", ".", "groupby", "(", "'Hour'", ")", ".", "sum", "(", ")", ".", "plot", "(", "ylim", "=", "(", "0", ",", "ymax", ")", ",", "figsize", "=", "(", "18", ",", "5", ")", ",", "title", "=", "meter", "+", "\" Time of Day of Missing Data\"", ")", "print", "(", "data_gaps", ")" ]
Takes in an the object returned by the MDAL query, and analyzes the quality of the data for each column in the df in the form of graphs. The Graphs returned show missing data events over time, and missing data frequency during each hour of the day To Do ----- Need to make it specific for varying meters and label it for each type, Either separate functions or make the function broader Parameters ---------- obj : ??? the object returned by the MDAL Query
[ "Takes", "in", "an", "the", "object", "returned", "by", "the", "MDAL", "query", "and", "analyzes", "the", "quality", "of", "the", "data", "for", "each", "column", "in", "the", "df", "in", "the", "form", "of", "graphs", ".", "The", "Graphs", "returned", "show", "missing", "data", "events", "over", "time", "and", "missing", "data", "frequency", "during", "each", "hour", "of", "the", "day" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L1111-L1147
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Clean_Data.py
Clean_Data.event_duration
def event_duration(self, obj, dictionary, low_bound=None, high_bound=None): """ Takes in an object and returns a dictionary with the missing data events (start and end) for each column in the inputted object (will map to a uuid) To Do ----- Need to make it specific for varying meters and label it for each type, Either separate functions or make the function broader Parameters ---------- obj : ??? the object returned by the MDAL Query dictionary : dict name of the dictionary low_bound : float all data equal to or below this value will be interpreted as missing data high_bound : float all data above this value will be interpreted as missing data Returns ------- dict dictionary with the format: {uuid:{start of event 1: end of event 1, start of event 2: end of event 2, ...}, uuid:{..}} """ data = obj.df N_cols = data.shape[1] if low_bound: data = data.where(data >= low_bound) if high_bound: data=data.where(data < high_bound) for i in range(N_cols): data_per_meter = data.iloc[:, [i]] data_missing, meter = self.identify_missing(data_per_meter) uuid = self.find_uuid(obj, column_name=meter) data_gaps = self.diff_boolean(data_missing, meter, uuid) dictionary_solo = data_gaps.to_dict() dictionary[uuid] = dictionary_solo[uuid] # dictionary[uuid]=data_gaps # uncomment to get a dictionary of dfs return dictionary
python
def event_duration(self, obj, dictionary, low_bound=None, high_bound=None): data = obj.df N_cols = data.shape[1] if low_bound: data = data.where(data >= low_bound) if high_bound: data=data.where(data < high_bound) for i in range(N_cols): data_per_meter = data.iloc[:, [i]] data_missing, meter = self.identify_missing(data_per_meter) uuid = self.find_uuid(obj, column_name=meter) data_gaps = self.diff_boolean(data_missing, meter, uuid) dictionary_solo = data_gaps.to_dict() dictionary[uuid] = dictionary_solo[uuid] return dictionary
[ "def", "event_duration", "(", "self", ",", "obj", ",", "dictionary", ",", "low_bound", "=", "None", ",", "high_bound", "=", "None", ")", ":", "data", "=", "obj", ".", "df", "N_cols", "=", "data", ".", "shape", "[", "1", "]", "if", "low_bound", ":", "data", "=", "data", ".", "where", "(", "data", ">=", "low_bound", ")", "if", "high_bound", ":", "data", "=", "data", ".", "where", "(", "data", "<", "high_bound", ")", "for", "i", "in", "range", "(", "N_cols", ")", ":", "data_per_meter", "=", "data", ".", "iloc", "[", ":", ",", "[", "i", "]", "]", "data_missing", ",", "meter", "=", "self", ".", "identify_missing", "(", "data_per_meter", ")", "uuid", "=", "self", ".", "find_uuid", "(", "obj", ",", "column_name", "=", "meter", ")", "data_gaps", "=", "self", ".", "diff_boolean", "(", "data_missing", ",", "meter", ",", "uuid", ")", "dictionary_solo", "=", "data_gaps", ".", "to_dict", "(", ")", "dictionary", "[", "uuid", "]", "=", "dictionary_solo", "[", "uuid", "]", "# dictionary[uuid]=data_gaps # uncomment to get a dictionary of dfs", "return", "dictionary" ]
Takes in an object and returns a dictionary with the missing data events (start and end) for each column in the inputted object (will map to a uuid) To Do ----- Need to make it specific for varying meters and label it for each type, Either separate functions or make the function broader Parameters ---------- obj : ??? the object returned by the MDAL Query dictionary : dict name of the dictionary low_bound : float all data equal to or below this value will be interpreted as missing data high_bound : float all data above this value will be interpreted as missing data Returns ------- dict dictionary with the format: {uuid:{start of event 1: end of event 1, start of event 2: end of event 2, ...}, uuid:{..}}
[ "Takes", "in", "an", "object", "and", "returns", "a", "dictionary", "with", "the", "missing", "data", "events", "(", "start", "and", "end", ")", "for", "each", "column", "in", "the", "inputted", "object", "(", "will", "map", "to", "a", "uuid", ")" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Clean_Data.py#L1151-L1199
SoftwareDefinedBuildings/XBOS
models/ciee/thermal_model.py
execute_schedule
def execute_schedule(day, sched, popt, initial_temperature): """ sched is a list of (hsp, csp) setpoints at 30m intervals """ output = [] actions = [] prev_temp = initial_temperature weather = predict_weather(day) print len(sched), len(weather) for idx, epoch in enumerate(sched): if prev_temp < epoch[0]: # hsp next_temp = next_temperature(popt, prev_temp, 1, weather[idx]) # 1 is heat actions.append(1) elif prev_temp > epoch[1]: # csp next_temp = next_temperature(popt, prev_temp, 2, weather[idx]) # 2 is cool actions.append(2) else: next_temp = next_temperature(popt, prev_temp, 0, weather[idx]) # 0 is off actions.append(0) print prev_temp, weather[idx], actions[-1], next_temp output.append(next_temp) prev_temp = next_temp return output, actions
python
def execute_schedule(day, sched, popt, initial_temperature): output = [] actions = [] prev_temp = initial_temperature weather = predict_weather(day) print len(sched), len(weather) for idx, epoch in enumerate(sched): if prev_temp < epoch[0]: next_temp = next_temperature(popt, prev_temp, 1, weather[idx]) actions.append(1) elif prev_temp > epoch[1]: next_temp = next_temperature(popt, prev_temp, 2, weather[idx]) actions.append(2) else: next_temp = next_temperature(popt, prev_temp, 0, weather[idx]) actions.append(0) print prev_temp, weather[idx], actions[-1], next_temp output.append(next_temp) prev_temp = next_temp return output, actions
[ "def", "execute_schedule", "(", "day", ",", "sched", ",", "popt", ",", "initial_temperature", ")", ":", "output", "=", "[", "]", "actions", "=", "[", "]", "prev_temp", "=", "initial_temperature", "weather", "=", "predict_weather", "(", "day", ")", "print", "len", "(", "sched", ")", ",", "len", "(", "weather", ")", "for", "idx", ",", "epoch", "in", "enumerate", "(", "sched", ")", ":", "if", "prev_temp", "<", "epoch", "[", "0", "]", ":", "# hsp", "next_temp", "=", "next_temperature", "(", "popt", ",", "prev_temp", ",", "1", ",", "weather", "[", "idx", "]", ")", "# 1 is heat", "actions", ".", "append", "(", "1", ")", "elif", "prev_temp", ">", "epoch", "[", "1", "]", ":", "# csp", "next_temp", "=", "next_temperature", "(", "popt", ",", "prev_temp", ",", "2", ",", "weather", "[", "idx", "]", ")", "# 2 is cool", "actions", ".", "append", "(", "2", ")", "else", ":", "next_temp", "=", "next_temperature", "(", "popt", ",", "prev_temp", ",", "0", ",", "weather", "[", "idx", "]", ")", "# 0 is off", "actions", ".", "append", "(", "0", ")", "print", "prev_temp", ",", "weather", "[", "idx", "]", ",", "actions", "[", "-", "1", "]", ",", "next_temp", "output", ".", "append", "(", "next_temp", ")", "prev_temp", "=", "next_temp", "return", "output", ",", "actions" ]
sched is a list of (hsp, csp) setpoints at 30m intervals
[ "sched", "is", "a", "list", "of", "(", "hsp", "csp", ")", "setpoints", "at", "30m", "intervals" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/models/ciee/thermal_model.py#L72-L95
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Clean_Data.py
Clean_Data.clean_data
def clean_data(self, resample=True, freq='h', resampler='mean', interpolate=True, limit=1, method='linear', remove_na=True, remove_na_how='any', remove_outliers=True, sd_val=3, remove_out_of_bounds=True, low_bound=0, high_bound=9998): """ Clean dataframe. Parameters ---------- resample : bool Indicates whether to resample data or not. freq : str Resampling frequency i.e. d, h, 15T... resampler : str Resampling type i.e. mean, max. interpolate : bool Indicates whether to interpolate data or not. limit : int Interpolation limit. method : str Interpolation method. remove_na : bool Indicates whether to remove NAs or not. remove_na_how : str Specificies how to remove NA i.e. all, any... remove_outliers : bool Indicates whether to remove outliers or not. sd_val : int Standard Deviation Value (specifices how many SDs away is a point considered an outlier) remove_out_of_bounds : bool Indicates whether to remove out of bounds datapoints or not. low_bound : int Low bound of the data. high_bound : int High bound of the data. """ # Store copy of the original data data = self.original_data if resample: try: data = self.resample_data(data, freq, resampler) except Exception as e: raise e if interpolate: try: data = self.interpolate_data(data, limit=limit, method=method) except Exception as e: raise e if remove_na: try: data = self.remove_na(data, remove_na_how) except Exception as e: raise e if remove_outliers: try: data = self.remove_outliers(data, sd_val) except Exception as e: raise e if remove_out_of_bounds: try: data = self.remove_out_of_bounds(data, low_bound, high_bound) except Exception as e: raise e self.cleaned_data = data
python
def clean_data(self, resample=True, freq='h', resampler='mean', interpolate=True, limit=1, method='linear', remove_na=True, remove_na_how='any', remove_outliers=True, sd_val=3, remove_out_of_bounds=True, low_bound=0, high_bound=9998): data = self.original_data if resample: try: data = self.resample_data(data, freq, resampler) except Exception as e: raise e if interpolate: try: data = self.interpolate_data(data, limit=limit, method=method) except Exception as e: raise e if remove_na: try: data = self.remove_na(data, remove_na_how) except Exception as e: raise e if remove_outliers: try: data = self.remove_outliers(data, sd_val) except Exception as e: raise e if remove_out_of_bounds: try: data = self.remove_out_of_bounds(data, low_bound, high_bound) except Exception as e: raise e self.cleaned_data = data
[ "def", "clean_data", "(", "self", ",", "resample", "=", "True", ",", "freq", "=", "'h'", ",", "resampler", "=", "'mean'", ",", "interpolate", "=", "True", ",", "limit", "=", "1", ",", "method", "=", "'linear'", ",", "remove_na", "=", "True", ",", "remove_na_how", "=", "'any'", ",", "remove_outliers", "=", "True", ",", "sd_val", "=", "3", ",", "remove_out_of_bounds", "=", "True", ",", "low_bound", "=", "0", ",", "high_bound", "=", "9998", ")", ":", "# Store copy of the original data", "data", "=", "self", ".", "original_data", "if", "resample", ":", "try", ":", "data", "=", "self", ".", "resample_data", "(", "data", ",", "freq", ",", "resampler", ")", "except", "Exception", "as", "e", ":", "raise", "e", "if", "interpolate", ":", "try", ":", "data", "=", "self", ".", "interpolate_data", "(", "data", ",", "limit", "=", "limit", ",", "method", "=", "method", ")", "except", "Exception", "as", "e", ":", "raise", "e", "if", "remove_na", ":", "try", ":", "data", "=", "self", ".", "remove_na", "(", "data", ",", "remove_na_how", ")", "except", "Exception", "as", "e", ":", "raise", "e", "if", "remove_outliers", ":", "try", ":", "data", "=", "self", ".", "remove_outliers", "(", "data", ",", "sd_val", ")", "except", "Exception", "as", "e", ":", "raise", "e", "if", "remove_out_of_bounds", ":", "try", ":", "data", "=", "self", ".", "remove_out_of_bounds", "(", "data", ",", "low_bound", ",", "high_bound", ")", "except", "Exception", "as", "e", ":", "raise", "e", "self", ".", "cleaned_data", "=", "data" ]
Clean dataframe. Parameters ---------- resample : bool Indicates whether to resample data or not. freq : str Resampling frequency i.e. d, h, 15T... resampler : str Resampling type i.e. mean, max. interpolate : bool Indicates whether to interpolate data or not. limit : int Interpolation limit. method : str Interpolation method. remove_na : bool Indicates whether to remove NAs or not. remove_na_how : str Specificies how to remove NA i.e. all, any... remove_outliers : bool Indicates whether to remove outliers or not. sd_val : int Standard Deviation Value (specifices how many SDs away is a point considered an outlier) remove_out_of_bounds : bool Indicates whether to remove out of bounds datapoints or not. low_bound : int Low bound of the data. high_bound : int High bound of the data.
[ "Clean", "dataframe", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Clean_Data.py#L198-L269
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.get_global_count
def get_global_count(self): """ Return global count (used for naming of .json and .png files) Returns ------- int Global count """ # Check current number of json files in results directory and dump current json in new file path_to_json = self.results_folder_name + '/' json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')] Wrapper.global_count = len(json_files) + 1 return Wrapper.global_count
python
def get_global_count(self): path_to_json = self.results_folder_name + '/' json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')] Wrapper.global_count = len(json_files) + 1 return Wrapper.global_count
[ "def", "get_global_count", "(", "self", ")", ":", "# Check current number of json files in results directory and dump current json in new file", "path_to_json", "=", "self", ".", "results_folder_name", "+", "'/'", "json_files", "=", "[", "pos_json", "for", "pos_json", "in", "os", ".", "listdir", "(", "path_to_json", ")", "if", "pos_json", ".", "endswith", "(", "'.json'", ")", "]", "Wrapper", ".", "global_count", "=", "len", "(", "json_files", ")", "+", "1", "return", "Wrapper", ".", "global_count" ]
Return global count (used for naming of .json and .png files) Returns ------- int Global count
[ "Return", "global", "count", "(", "used", "for", "naming", "of", ".", "json", "and", ".", "png", "files", ")" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L114-L128
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.write_json
def write_json(self): """ Dump data into json file. """ with open(self.results_folder_name + '/results-' + str(self.get_global_count()) + '.json', 'a') as f: json.dump(self.result, f)
python
def write_json(self): with open(self.results_folder_name + '/results-' + str(self.get_global_count()) + '.json', 'a') as f: json.dump(self.result, f)
[ "def", "write_json", "(", "self", ")", ":", "with", "open", "(", "self", ".", "results_folder_name", "+", "'/results-'", "+", "str", "(", "self", ".", "get_global_count", "(", ")", ")", "+", "'.json'", ",", "'a'", ")", "as", "f", ":", "json", ".", "dump", "(", "self", ".", "result", ",", "f", ")" ]
Dump data into json file.
[ "Dump", "data", "into", "json", "file", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L143-L147
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.site_analysis
def site_analysis(self, folder_name, site_install_mapping, end_date): """ Summarize site data into a single table. folder_name : str Folder where all site data resides. site_event_mapping : dic Dictionary of site name to date of installation. end_date : str End date of data collected. """ def count_number_of_days(site, end_date): """ Counts the number of days between two dates. Parameters ---------- site : str Key to a dic containing site_name -> pelican installation date. end_date : str End date. Returns ------- int Number of days """ start_date = site_install_mapping[site] start_date = start_date.split('-') start = date(int(start_date[0]), int(start_date[1]), int(start_date[2])) end_date = end_date.split('-') end = date(int(end_date[0]), int(end_date[1]), int(end_date[2])) delta = end - start return delta.days if not folder_name or not isinstance(folder_name, str): raise TypeError("folder_name should be type string") else: list_json_files = [] df = pd.DataFrame() temp_df = pd.DataFrame() json_files = [f for f in os.listdir(folder_name) if f.endswith('.json')] for json_file in json_files: with open(folder_name + json_file) as f: js = json.load(f) num_days = count_number_of_days(js['Site'], end_date) e_abs_sav = round(js['Energy Savings (absolute)'] / 1000, 2) # Energy Absolute Savings e_perc_sav = round(js['Energy Savings (%)'], 2) # Energy Percent Savings ann_e_abs_sav = (e_abs_sav / num_days) * 365 # Annualized Energy Absolute Savings d_abs_sav = round(js['User Comments']['Dollar Savings (absolute)'], 2) # Dollar Absolute Savings d_perc_sav = round(js['User Comments']['Dollar Savings (%)'], 2) # Dollar Percent Savings ann_d_abs_sav = (d_abs_sav / num_days) * 365 # Annualized Dollar Absolute Savings temp_df = pd.DataFrame({ 'Site': js['Site'], '#Days since Pelican Installation': num_days, 'Energy Savings (%)': e_perc_sav, 'Energy Savings (kWh)': e_abs_sav, 'Annualized Energy Savings (kWh)': ann_e_abs_sav, 'Dollar Savings (%)': d_perc_sav, 'Dollar Savings ($)': d_abs_sav, 'Annualized Dollar Savings ($)': ann_d_abs_sav, 'Best Model': js['Model']['Optimal Model\'s Metrics']['name'], 'Adj R2': round(js['Model']['Optimal Model\'s Metrics']['adj_cross_val_score'], 2), 'RMSE': round(js['Model']['Optimal Model\'s Metrics']['rmse'], 2), 'MAPE': js['Model']['Optimal Model\'s Metrics']['mape'], 'Uncertainity': js['Uncertainity'], }, index=[0]) df = df.append(temp_df) df.set_index('Site', inplace=True) return df
python
def site_analysis(self, folder_name, site_install_mapping, end_date): def count_number_of_days(site, end_date): start_date = site_install_mapping[site] start_date = start_date.split('-') start = date(int(start_date[0]), int(start_date[1]), int(start_date[2])) end_date = end_date.split('-') end = date(int(end_date[0]), int(end_date[1]), int(end_date[2])) delta = end - start return delta.days if not folder_name or not isinstance(folder_name, str): raise TypeError("folder_name should be type string") else: list_json_files = [] df = pd.DataFrame() temp_df = pd.DataFrame() json_files = [f for f in os.listdir(folder_name) if f.endswith('.json')] for json_file in json_files: with open(folder_name + json_file) as f: js = json.load(f) num_days = count_number_of_days(js['Site'], end_date) e_abs_sav = round(js['Energy Savings (absolute)'] / 1000, 2) e_perc_sav = round(js['Energy Savings (%)'], 2) ann_e_abs_sav = (e_abs_sav / num_days) * 365 d_abs_sav = round(js['User Comments']['Dollar Savings (absolute)'], 2) d_perc_sav = round(js['User Comments']['Dollar Savings (%)'], 2) ann_d_abs_sav = (d_abs_sav / num_days) * 365 temp_df = pd.DataFrame({ 'Site': js['Site'], ' 'Energy Savings (%)': e_perc_sav, 'Energy Savings (kWh)': e_abs_sav, 'Annualized Energy Savings (kWh)': ann_e_abs_sav, 'Dollar Savings (%)': d_perc_sav, 'Dollar Savings ($)': d_abs_sav, 'Annualized Dollar Savings ($)': ann_d_abs_sav, 'Best Model': js['Model']['Optimal Model\'s Metrics']['name'], 'Adj R2': round(js['Model']['Optimal Model\'s Metrics']['adj_cross_val_score'], 2), 'RMSE': round(js['Model']['Optimal Model\'s Metrics']['rmse'], 2), 'MAPE': js['Model']['Optimal Model\'s Metrics']['mape'], 'Uncertainity': js['Uncertainity'], }, index=[0]) df = df.append(temp_df) df.set_index('Site', inplace=True) return df
[ "def", "site_analysis", "(", "self", ",", "folder_name", ",", "site_install_mapping", ",", "end_date", ")", ":", "def", "count_number_of_days", "(", "site", ",", "end_date", ")", ":", "\"\"\" Counts the number of days between two dates.\n\n Parameters\n ----------\n site : str\n Key to a dic containing site_name -> pelican installation date.\n end_date : str\n End date.\n\n Returns\n -------\n int\n Number of days\n\n \"\"\"", "start_date", "=", "site_install_mapping", "[", "site", "]", "start_date", "=", "start_date", ".", "split", "(", "'-'", ")", "start", "=", "date", "(", "int", "(", "start_date", "[", "0", "]", ")", ",", "int", "(", "start_date", "[", "1", "]", ")", ",", "int", "(", "start_date", "[", "2", "]", ")", ")", "end_date", "=", "end_date", ".", "split", "(", "'-'", ")", "end", "=", "date", "(", "int", "(", "end_date", "[", "0", "]", ")", ",", "int", "(", "end_date", "[", "1", "]", ")", ",", "int", "(", "end_date", "[", "2", "]", ")", ")", "delta", "=", "end", "-", "start", "return", "delta", ".", "days", "if", "not", "folder_name", "or", "not", "isinstance", "(", "folder_name", ",", "str", ")", ":", "raise", "TypeError", "(", "\"folder_name should be type string\"", ")", "else", ":", "list_json_files", "=", "[", "]", "df", "=", "pd", ".", "DataFrame", "(", ")", "temp_df", "=", "pd", ".", "DataFrame", "(", ")", "json_files", "=", "[", "f", "for", "f", "in", "os", ".", "listdir", "(", "folder_name", ")", "if", "f", ".", "endswith", "(", "'.json'", ")", "]", "for", "json_file", "in", "json_files", ":", "with", "open", "(", "folder_name", "+", "json_file", ")", "as", "f", ":", "js", "=", "json", ".", "load", "(", "f", ")", "num_days", "=", "count_number_of_days", "(", "js", "[", "'Site'", "]", ",", "end_date", ")", "e_abs_sav", "=", "round", "(", "js", "[", "'Energy Savings (absolute)'", "]", "/", "1000", ",", "2", ")", "# Energy Absolute Savings", "e_perc_sav", "=", "round", "(", "js", "[", "'Energy Savings (%)'", "]", ",", "2", ")", "# Energy Percent Savings", "ann_e_abs_sav", "=", "(", "e_abs_sav", "/", "num_days", ")", "*", "365", "# Annualized Energy Absolute Savings", "d_abs_sav", "=", "round", "(", "js", "[", "'User Comments'", "]", "[", "'Dollar Savings (absolute)'", "]", ",", "2", ")", "# Dollar Absolute Savings", "d_perc_sav", "=", "round", "(", "js", "[", "'User Comments'", "]", "[", "'Dollar Savings (%)'", "]", ",", "2", ")", "# Dollar Percent Savings", "ann_d_abs_sav", "=", "(", "d_abs_sav", "/", "num_days", ")", "*", "365", "# Annualized Dollar Absolute Savings", "temp_df", "=", "pd", ".", "DataFrame", "(", "{", "'Site'", ":", "js", "[", "'Site'", "]", ",", "'#Days since Pelican Installation'", ":", "num_days", ",", "'Energy Savings (%)'", ":", "e_perc_sav", ",", "'Energy Savings (kWh)'", ":", "e_abs_sav", ",", "'Annualized Energy Savings (kWh)'", ":", "ann_e_abs_sav", ",", "'Dollar Savings (%)'", ":", "d_perc_sav", ",", "'Dollar Savings ($)'", ":", "d_abs_sav", ",", "'Annualized Dollar Savings ($)'", ":", "ann_d_abs_sav", ",", "'Best Model'", ":", "js", "[", "'Model'", "]", "[", "'Optimal Model\\'s Metrics'", "]", "[", "'name'", "]", ",", "'Adj R2'", ":", "round", "(", "js", "[", "'Model'", "]", "[", "'Optimal Model\\'s Metrics'", "]", "[", "'adj_cross_val_score'", "]", ",", "2", ")", ",", "'RMSE'", ":", "round", "(", "js", "[", "'Model'", "]", "[", "'Optimal Model\\'s Metrics'", "]", "[", "'rmse'", "]", ",", "2", ")", ",", "'MAPE'", ":", "js", "[", "'Model'", "]", "[", "'Optimal Model\\'s Metrics'", "]", "[", "'mape'", "]", ",", "'Uncertainity'", ":", "js", "[", "'Uncertainity'", "]", ",", "}", ",", "index", "=", "[", "0", "]", ")", "df", "=", "df", ".", "append", "(", "temp_df", ")", "df", ".", "set_index", "(", "'Site'", ",", "inplace", "=", "True", ")", "return", "df" ]
Summarize site data into a single table. folder_name : str Folder where all site data resides. site_event_mapping : dic Dictionary of site name to date of installation. end_date : str End date of data collected.
[ "Summarize", "site", "data", "into", "a", "single", "table", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L150-L235
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.read_json
def read_json(self, file_name=None, input_json=None, imported_data=pd.DataFrame()): """ Read input json file. Notes ----- The input json file should include ALL parameters. Parameters ---------- file_name : str Filename to be read. input_json : dict JSON object to be read. imported_data : pd.DataFrame() Pandas Dataframe containing data. """ if not file_name and not input_json or file_name and input_json: raise TypeError('Provide either json file or json object to read.') # Read json file if file_name: if not isinstance(file_name, str) or not file_name.endswith('.json') or not os.path.isfile('./'+file_name): raise TypeError('File name should be a valid .json file residing in current directory.') else: f = open(file_name) input_json = json.load(f) if imported_data.empty: import_json = input_json['Import'] imported_data = self.import_data(file_name=import_json['File Name'], folder_name=import_json['Folder Name'], head_row=import_json['Head Row'], index_col=import_json['Index Col'], convert_col=import_json['Convert Col'], concat_files=import_json['Concat Files'], save_file=import_json['Save File']) clean_json = input_json['Clean'] cleaned_data = self.clean_data(imported_data, rename_col=clean_json['Rename Col'], drop_col=clean_json['Drop Col'], resample=clean_json['Resample'], freq=clean_json['Frequency'], resampler=clean_json['Resampler'], interpolate=clean_json['Interpolate'], limit=clean_json['Limit'], method=clean_json['Method'], remove_na=clean_json['Remove NA'], remove_na_how=clean_json['Remove NA How'], remove_outliers=clean_json['Remove Outliers'], sd_val=clean_json['SD Val'], remove_out_of_bounds=clean_json['Remove Out of Bounds'], low_bound=clean_json['Low Bound'], high_bound=clean_json['High Bound'], save_file=clean_json['Save File']) preproc_json = input_json['Preprocess'] preprocessed_data = self.preprocess_data(cleaned_data, cdh_cpoint=preproc_json['CDH CPoint'], hdh_cpoint=preproc_json['HDH CPoint'], col_hdh_cdh=preproc_json['HDH CDH Calc Col'], col_degree=preproc_json['Col Degree'], degree=preproc_json['Degree'], standardize=preproc_json['Standardize'], normalize=preproc_json['Normalize'], year=preproc_json['Year'], month=preproc_json['Month'], week=preproc_json['Week'], tod=preproc_json['Time of Day'], dow=preproc_json['Day of Week'], save_file=preproc_json['Save File']) model_json = input_json['Model'] model_data = self.model(preprocessed_data, ind_col=model_json['Independent Col'], dep_col=model_json['Dependent Col'], project_ind_col=model_json['Projection Independent Col'], baseline_period=model_json['Baseline Period'], projection_period=model_json['Projection Period'], exclude_time_period=model_json['Exclude Time Period'], alphas=model_json['Alphas'], cv=model_json['CV'], plot=model_json['Plot'], figsize=model_json['Fig Size'])
python
def read_json(self, file_name=None, input_json=None, imported_data=pd.DataFrame()): if not file_name and not input_json or file_name and input_json: raise TypeError('Provide either json file or json object to read.') if file_name: if not isinstance(file_name, str) or not file_name.endswith('.json') or not os.path.isfile('./'+file_name): raise TypeError('File name should be a valid .json file residing in current directory.') else: f = open(file_name) input_json = json.load(f) if imported_data.empty: import_json = input_json['Import'] imported_data = self.import_data(file_name=import_json['File Name'], folder_name=import_json['Folder Name'], head_row=import_json['Head Row'], index_col=import_json['Index Col'], convert_col=import_json['Convert Col'], concat_files=import_json['Concat Files'], save_file=import_json['Save File']) clean_json = input_json['Clean'] cleaned_data = self.clean_data(imported_data, rename_col=clean_json['Rename Col'], drop_col=clean_json['Drop Col'], resample=clean_json['Resample'], freq=clean_json['Frequency'], resampler=clean_json['Resampler'], interpolate=clean_json['Interpolate'], limit=clean_json['Limit'], method=clean_json['Method'], remove_na=clean_json['Remove NA'], remove_na_how=clean_json['Remove NA How'], remove_outliers=clean_json['Remove Outliers'], sd_val=clean_json['SD Val'], remove_out_of_bounds=clean_json['Remove Out of Bounds'], low_bound=clean_json['Low Bound'], high_bound=clean_json['High Bound'], save_file=clean_json['Save File']) preproc_json = input_json['Preprocess'] preprocessed_data = self.preprocess_data(cleaned_data, cdh_cpoint=preproc_json['CDH CPoint'], hdh_cpoint=preproc_json['HDH CPoint'], col_hdh_cdh=preproc_json['HDH CDH Calc Col'], col_degree=preproc_json['Col Degree'], degree=preproc_json['Degree'], standardize=preproc_json['Standardize'], normalize=preproc_json['Normalize'], year=preproc_json['Year'], month=preproc_json['Month'], week=preproc_json['Week'], tod=preproc_json['Time of Day'], dow=preproc_json['Day of Week'], save_file=preproc_json['Save File']) model_json = input_json['Model'] model_data = self.model(preprocessed_data, ind_col=model_json['Independent Col'], dep_col=model_json['Dependent Col'], project_ind_col=model_json['Projection Independent Col'], baseline_period=model_json['Baseline Period'], projection_period=model_json['Projection Period'], exclude_time_period=model_json['Exclude Time Period'], alphas=model_json['Alphas'], cv=model_json['CV'], plot=model_json['Plot'], figsize=model_json['Fig Size'])
[ "def", "read_json", "(", "self", ",", "file_name", "=", "None", ",", "input_json", "=", "None", ",", "imported_data", "=", "pd", ".", "DataFrame", "(", ")", ")", ":", "if", "not", "file_name", "and", "not", "input_json", "or", "file_name", "and", "input_json", ":", "raise", "TypeError", "(", "'Provide either json file or json object to read.'", ")", "# Read json file", "if", "file_name", ":", "if", "not", "isinstance", "(", "file_name", ",", "str", ")", "or", "not", "file_name", ".", "endswith", "(", "'.json'", ")", "or", "not", "os", ".", "path", ".", "isfile", "(", "'./'", "+", "file_name", ")", ":", "raise", "TypeError", "(", "'File name should be a valid .json file residing in current directory.'", ")", "else", ":", "f", "=", "open", "(", "file_name", ")", "input_json", "=", "json", ".", "load", "(", "f", ")", "if", "imported_data", ".", "empty", ":", "import_json", "=", "input_json", "[", "'Import'", "]", "imported_data", "=", "self", ".", "import_data", "(", "file_name", "=", "import_json", "[", "'File Name'", "]", ",", "folder_name", "=", "import_json", "[", "'Folder Name'", "]", ",", "head_row", "=", "import_json", "[", "'Head Row'", "]", ",", "index_col", "=", "import_json", "[", "'Index Col'", "]", ",", "convert_col", "=", "import_json", "[", "'Convert Col'", "]", ",", "concat_files", "=", "import_json", "[", "'Concat Files'", "]", ",", "save_file", "=", "import_json", "[", "'Save File'", "]", ")", "clean_json", "=", "input_json", "[", "'Clean'", "]", "cleaned_data", "=", "self", ".", "clean_data", "(", "imported_data", ",", "rename_col", "=", "clean_json", "[", "'Rename Col'", "]", ",", "drop_col", "=", "clean_json", "[", "'Drop Col'", "]", ",", "resample", "=", "clean_json", "[", "'Resample'", "]", ",", "freq", "=", "clean_json", "[", "'Frequency'", "]", ",", "resampler", "=", "clean_json", "[", "'Resampler'", "]", ",", "interpolate", "=", "clean_json", "[", "'Interpolate'", "]", ",", "limit", "=", "clean_json", "[", "'Limit'", "]", ",", "method", "=", "clean_json", "[", "'Method'", "]", ",", "remove_na", "=", "clean_json", "[", "'Remove NA'", "]", ",", "remove_na_how", "=", "clean_json", "[", "'Remove NA How'", "]", ",", "remove_outliers", "=", "clean_json", "[", "'Remove Outliers'", "]", ",", "sd_val", "=", "clean_json", "[", "'SD Val'", "]", ",", "remove_out_of_bounds", "=", "clean_json", "[", "'Remove Out of Bounds'", "]", ",", "low_bound", "=", "clean_json", "[", "'Low Bound'", "]", ",", "high_bound", "=", "clean_json", "[", "'High Bound'", "]", ",", "save_file", "=", "clean_json", "[", "'Save File'", "]", ")", "preproc_json", "=", "input_json", "[", "'Preprocess'", "]", "preprocessed_data", "=", "self", ".", "preprocess_data", "(", "cleaned_data", ",", "cdh_cpoint", "=", "preproc_json", "[", "'CDH CPoint'", "]", ",", "hdh_cpoint", "=", "preproc_json", "[", "'HDH CPoint'", "]", ",", "col_hdh_cdh", "=", "preproc_json", "[", "'HDH CDH Calc Col'", "]", ",", "col_degree", "=", "preproc_json", "[", "'Col Degree'", "]", ",", "degree", "=", "preproc_json", "[", "'Degree'", "]", ",", "standardize", "=", "preproc_json", "[", "'Standardize'", "]", ",", "normalize", "=", "preproc_json", "[", "'Normalize'", "]", ",", "year", "=", "preproc_json", "[", "'Year'", "]", ",", "month", "=", "preproc_json", "[", "'Month'", "]", ",", "week", "=", "preproc_json", "[", "'Week'", "]", ",", "tod", "=", "preproc_json", "[", "'Time of Day'", "]", ",", "dow", "=", "preproc_json", "[", "'Day of Week'", "]", ",", "save_file", "=", "preproc_json", "[", "'Save File'", "]", ")", "model_json", "=", "input_json", "[", "'Model'", "]", "model_data", "=", "self", ".", "model", "(", "preprocessed_data", ",", "ind_col", "=", "model_json", "[", "'Independent Col'", "]", ",", "dep_col", "=", "model_json", "[", "'Dependent Col'", "]", ",", "project_ind_col", "=", "model_json", "[", "'Projection Independent Col'", "]", ",", "baseline_period", "=", "model_json", "[", "'Baseline Period'", "]", ",", "projection_period", "=", "model_json", "[", "'Projection Period'", "]", ",", "exclude_time_period", "=", "model_json", "[", "'Exclude Time Period'", "]", ",", "alphas", "=", "model_json", "[", "'Alphas'", "]", ",", "cv", "=", "model_json", "[", "'CV'", "]", ",", "plot", "=", "model_json", "[", "'Plot'", "]", ",", "figsize", "=", "model_json", "[", "'Fig Size'", "]", ")" ]
Read input json file. Notes ----- The input json file should include ALL parameters. Parameters ---------- file_name : str Filename to be read. input_json : dict JSON object to be read. imported_data : pd.DataFrame() Pandas Dataframe containing data.
[ "Read", "input", "json", "file", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L238-L298
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.search
def search(self, file_name, imported_data=None): """ Run models on different data configurations. Note ---- The input json file should include ALL parameters. Parameters ---------- file_name : str Optional json file to read parameters. imported_data : pd.DataFrame() Pandas Dataframe containing data. """ resample_freq=['15T', 'h', 'd'] time_freq = { 'year' : [True, False, False, False, False], 'month' : [False, True, False, False, False], 'week' : [False, False, True, False, False], 'tod' : [False, False, False, True, False], 'dow' : [False, False, False, False, True], } optimal_score = float('-inf') optimal_model = None # CSV Files if not imported_data: with open(file_name) as f: input_json = json.load(f) import_json = input_json['Import'] imported_data = self.import_data(file_name=import_json['File Name'], folder_name=import_json['Folder Name'], head_row=import_json['Head Row'], index_col=import_json['Index Col'], convert_col=import_json['Convert Col'], concat_files=import_json['Concat Files'], save_file=import_json['Save File']) with open(file_name) as f: input_json = json.load(f) for x in resample_freq: # Resample data interval input_json['Clean']['Frequency'] = x for i in range(len(time_freq.items())): # Add time features input_json['Preprocess']['Year'] = time_freq['year'][i] input_json['Preprocess']['Month'] = time_freq['month'][i] input_json['Preprocess']['Week'] = time_freq['week'][i] input_json['Preprocess']['Time of Day'] = time_freq['tod'][i] input_json['Preprocess']['Day of Week'] = time_freq['dow'][i] # Putting comment in json file to indicate which parameters have been changed time_feature = None for key in time_freq: if time_freq[key][i]: time_feature = key self.result['Comment'] = 'Freq: ' + x + ', ' + 'Time Feature: ' + time_feature # Read parameters in input_json self.read_json(file_name=None, input_json=input_json, imported_data=imported_data) # Keep track of highest adj_r2 score if self.result['Model']['Optimal Model\'s Metrics']['adj_r2'] > optimal_score: optimal_score = self.result['Model']['Optimal Model\'s Metrics']['adj_r2'] optimal_model_file_name = self.results_folder_name + '/results-' + str(self.get_global_count()) + '.json' # Wrapper.global_count += 1 print('Most optimal model: ', optimal_model_file_name) freq = self.result['Comment'].split(' ')[1][:-1] time_feat = self.result['Comment'].split(' ')[-1] print('Freq: ', freq, 'Time Feature: ', time_feat)
python
def search(self, file_name, imported_data=None): resample_freq=['15T', 'h', 'd'] time_freq = { 'year' : [True, False, False, False, False], 'month' : [False, True, False, False, False], 'week' : [False, False, True, False, False], 'tod' : [False, False, False, True, False], 'dow' : [False, False, False, False, True], } optimal_score = float('-inf') optimal_model = None if not imported_data: with open(file_name) as f: input_json = json.load(f) import_json = input_json['Import'] imported_data = self.import_data(file_name=import_json['File Name'], folder_name=import_json['Folder Name'], head_row=import_json['Head Row'], index_col=import_json['Index Col'], convert_col=import_json['Convert Col'], concat_files=import_json['Concat Files'], save_file=import_json['Save File']) with open(file_name) as f: input_json = json.load(f) for x in resample_freq: input_json['Clean']['Frequency'] = x for i in range(len(time_freq.items())): input_json['Preprocess']['Year'] = time_freq['year'][i] input_json['Preprocess']['Month'] = time_freq['month'][i] input_json['Preprocess']['Week'] = time_freq['week'][i] input_json['Preprocess']['Time of Day'] = time_freq['tod'][i] input_json['Preprocess']['Day of Week'] = time_freq['dow'][i] time_feature = None for key in time_freq: if time_freq[key][i]: time_feature = key self.result['Comment'] = 'Freq: ' + x + ', ' + 'Time Feature: ' + time_feature self.read_json(file_name=None, input_json=input_json, imported_data=imported_data) if self.result['Model']['Optimal Model\'s Metrics']['adj_r2'] > optimal_score: optimal_score = self.result['Model']['Optimal Model\'s Metrics']['adj_r2'] optimal_model_file_name = self.results_folder_name + '/results-' + str(self.get_global_count()) + '.json' print('Most optimal model: ', optimal_model_file_name) freq = self.result['Comment'].split(' ')[1][:-1] time_feat = self.result['Comment'].split(' ')[-1] print('Freq: ', freq, 'Time Feature: ', time_feat)
[ "def", "search", "(", "self", ",", "file_name", ",", "imported_data", "=", "None", ")", ":", "resample_freq", "=", "[", "'15T'", ",", "'h'", ",", "'d'", "]", "time_freq", "=", "{", "'year'", ":", "[", "True", ",", "False", ",", "False", ",", "False", ",", "False", "]", ",", "'month'", ":", "[", "False", ",", "True", ",", "False", ",", "False", ",", "False", "]", ",", "'week'", ":", "[", "False", ",", "False", ",", "True", ",", "False", ",", "False", "]", ",", "'tod'", ":", "[", "False", ",", "False", ",", "False", ",", "True", ",", "False", "]", ",", "'dow'", ":", "[", "False", ",", "False", ",", "False", ",", "False", ",", "True", "]", ",", "}", "optimal_score", "=", "float", "(", "'-inf'", ")", "optimal_model", "=", "None", "# CSV Files", "if", "not", "imported_data", ":", "with", "open", "(", "file_name", ")", "as", "f", ":", "input_json", "=", "json", ".", "load", "(", "f", ")", "import_json", "=", "input_json", "[", "'Import'", "]", "imported_data", "=", "self", ".", "import_data", "(", "file_name", "=", "import_json", "[", "'File Name'", "]", ",", "folder_name", "=", "import_json", "[", "'Folder Name'", "]", ",", "head_row", "=", "import_json", "[", "'Head Row'", "]", ",", "index_col", "=", "import_json", "[", "'Index Col'", "]", ",", "convert_col", "=", "import_json", "[", "'Convert Col'", "]", ",", "concat_files", "=", "import_json", "[", "'Concat Files'", "]", ",", "save_file", "=", "import_json", "[", "'Save File'", "]", ")", "with", "open", "(", "file_name", ")", "as", "f", ":", "input_json", "=", "json", ".", "load", "(", "f", ")", "for", "x", "in", "resample_freq", ":", "# Resample data interval", "input_json", "[", "'Clean'", "]", "[", "'Frequency'", "]", "=", "x", "for", "i", "in", "range", "(", "len", "(", "time_freq", ".", "items", "(", ")", ")", ")", ":", "# Add time features", "input_json", "[", "'Preprocess'", "]", "[", "'Year'", "]", "=", "time_freq", "[", "'year'", "]", "[", "i", "]", "input_json", "[", "'Preprocess'", "]", "[", "'Month'", "]", "=", "time_freq", "[", "'month'", "]", "[", "i", "]", "input_json", "[", "'Preprocess'", "]", "[", "'Week'", "]", "=", "time_freq", "[", "'week'", "]", "[", "i", "]", "input_json", "[", "'Preprocess'", "]", "[", "'Time of Day'", "]", "=", "time_freq", "[", "'tod'", "]", "[", "i", "]", "input_json", "[", "'Preprocess'", "]", "[", "'Day of Week'", "]", "=", "time_freq", "[", "'dow'", "]", "[", "i", "]", "# Putting comment in json file to indicate which parameters have been changed", "time_feature", "=", "None", "for", "key", "in", "time_freq", ":", "if", "time_freq", "[", "key", "]", "[", "i", "]", ":", "time_feature", "=", "key", "self", ".", "result", "[", "'Comment'", "]", "=", "'Freq: '", "+", "x", "+", "', '", "+", "'Time Feature: '", "+", "time_feature", "# Read parameters in input_json", "self", ".", "read_json", "(", "file_name", "=", "None", ",", "input_json", "=", "input_json", ",", "imported_data", "=", "imported_data", ")", "# Keep track of highest adj_r2 score", "if", "self", ".", "result", "[", "'Model'", "]", "[", "'Optimal Model\\'s Metrics'", "]", "[", "'adj_r2'", "]", ">", "optimal_score", ":", "optimal_score", "=", "self", ".", "result", "[", "'Model'", "]", "[", "'Optimal Model\\'s Metrics'", "]", "[", "'adj_r2'", "]", "optimal_model_file_name", "=", "self", ".", "results_folder_name", "+", "'/results-'", "+", "str", "(", "self", ".", "get_global_count", "(", ")", ")", "+", "'.json'", "# Wrapper.global_count += 1", "print", "(", "'Most optimal model: '", ",", "optimal_model_file_name", ")", "freq", "=", "self", ".", "result", "[", "'Comment'", "]", ".", "split", "(", "' '", ")", "[", "1", "]", "[", ":", "-", "1", "]", "time_feat", "=", "self", ".", "result", "[", "'Comment'", "]", ".", "split", "(", "' '", ")", "[", "-", "1", "]", "print", "(", "'Freq: '", ",", "freq", ",", "'Time Feature: '", ",", "time_feat", ")" ]
Run models on different data configurations. Note ---- The input json file should include ALL parameters. Parameters ---------- file_name : str Optional json file to read parameters. imported_data : pd.DataFrame() Pandas Dataframe containing data.
[ "Run", "models", "on", "different", "data", "configurations", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L302-L373
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.import_data
def import_data(self, file_name='*', folder_name='.', head_row=0, index_col=0, convert_col=True, concat_files=False, save_file=True): """ Imports csv file(s) and stores the result in self.imported_data. Note ---- 1. If folder exists out of current directory, folder_name should contain correct regex 2. Assuming there's no file called "\*.csv" Parameters ---------- file_name : str CSV file to be imported. Defaults to '\*' - all csv files in the folder. folder_name : str Folder where file resides. Defaults to '.' - current directory. head_row : int Skips all rows from 0 to head_row-1 index_col : int Skips all columns from 0 to index_col-1 convert_col : bool Convert columns to numeric type concat_files : bool Appends data from files to result dataframe save_file : bool Specifies whether to save file or not. Defaults to True. Returns ------- pd.DataFrame() Dataframe containing imported data. """ # Create instance and import the data import_data_obj = Import_Data() import_data_obj.import_csv(file_name=file_name, folder_name=folder_name, head_row=head_row, index_col=index_col, convert_col=convert_col, concat_files=concat_files) # Store imported data in wrapper class self.imported_data = import_data_obj.data # Logging self.result['Import'] = { 'File Name': file_name, 'Folder Name': folder_name, 'Head Row': head_row, 'Index Col': index_col, 'Convert Col': convert_col, 'Concat Files': concat_files, 'Save File': save_file } if save_file: f = self.results_folder_name + '/imported_data-' + str(self.get_global_count()) + '.csv' self.imported_data.to_csv(f) self.result['Import']['Saved File'] = f else: self.result['Import']['Saved File'] = '' return self.imported_data
python
def import_data(self, file_name='*', folder_name='.', head_row=0, index_col=0, convert_col=True, concat_files=False, save_file=True): import_data_obj = Import_Data() import_data_obj.import_csv(file_name=file_name, folder_name=folder_name, head_row=head_row, index_col=index_col, convert_col=convert_col, concat_files=concat_files) self.imported_data = import_data_obj.data self.result['Import'] = { 'File Name': file_name, 'Folder Name': folder_name, 'Head Row': head_row, 'Index Col': index_col, 'Convert Col': convert_col, 'Concat Files': concat_files, 'Save File': save_file } if save_file: f = self.results_folder_name + '/imported_data-' + str(self.get_global_count()) + '.csv' self.imported_data.to_csv(f) self.result['Import']['Saved File'] = f else: self.result['Import']['Saved File'] = '' return self.imported_data
[ "def", "import_data", "(", "self", ",", "file_name", "=", "'*'", ",", "folder_name", "=", "'.'", ",", "head_row", "=", "0", ",", "index_col", "=", "0", ",", "convert_col", "=", "True", ",", "concat_files", "=", "False", ",", "save_file", "=", "True", ")", ":", "# Create instance and import the data", "import_data_obj", "=", "Import_Data", "(", ")", "import_data_obj", ".", "import_csv", "(", "file_name", "=", "file_name", ",", "folder_name", "=", "folder_name", ",", "head_row", "=", "head_row", ",", "index_col", "=", "index_col", ",", "convert_col", "=", "convert_col", ",", "concat_files", "=", "concat_files", ")", "# Store imported data in wrapper class", "self", ".", "imported_data", "=", "import_data_obj", ".", "data", "# Logging", "self", ".", "result", "[", "'Import'", "]", "=", "{", "'File Name'", ":", "file_name", ",", "'Folder Name'", ":", "folder_name", ",", "'Head Row'", ":", "head_row", ",", "'Index Col'", ":", "index_col", ",", "'Convert Col'", ":", "convert_col", ",", "'Concat Files'", ":", "concat_files", ",", "'Save File'", ":", "save_file", "}", "if", "save_file", ":", "f", "=", "self", ".", "results_folder_name", "+", "'/imported_data-'", "+", "str", "(", "self", ".", "get_global_count", "(", ")", ")", "+", "'.csv'", "self", ".", "imported_data", ".", "to_csv", "(", "f", ")", "self", ".", "result", "[", "'Import'", "]", "[", "'Saved File'", "]", "=", "f", "else", ":", "self", ".", "result", "[", "'Import'", "]", "[", "'Saved File'", "]", "=", "''", "return", "self", ".", "imported_data" ]
Imports csv file(s) and stores the result in self.imported_data. Note ---- 1. If folder exists out of current directory, folder_name should contain correct regex 2. Assuming there's no file called "\*.csv" Parameters ---------- file_name : str CSV file to be imported. Defaults to '\*' - all csv files in the folder. folder_name : str Folder where file resides. Defaults to '.' - current directory. head_row : int Skips all rows from 0 to head_row-1 index_col : int Skips all columns from 0 to index_col-1 convert_col : bool Convert columns to numeric type concat_files : bool Appends data from files to result dataframe save_file : bool Specifies whether to save file or not. Defaults to True. Returns ------- pd.DataFrame() Dataframe containing imported data.
[ "Imports", "csv", "file", "(", "s", ")", "and", "stores", "the", "result", "in", "self", ".", "imported_data", ".", "Note", "----", "1", ".", "If", "folder", "exists", "out", "of", "current", "directory", "folder_name", "should", "contain", "correct", "regex", "2", ".", "Assuming", "there", "s", "no", "file", "called", "\\", "*", ".", "csv" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L376-L436
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.clean_data
def clean_data(self, data, rename_col=None, drop_col=None, resample=True, freq='h', resampler='mean', interpolate=True, limit=1, method='linear', remove_na=True, remove_na_how='any', remove_outliers=True, sd_val=3, remove_out_of_bounds=True, low_bound=0, high_bound=float('inf'), save_file=True): """ Cleans dataframe according to user specifications and stores result in self.cleaned_data. Parameters ---------- data : pd.DataFrame() Dataframe to be cleaned. rename_col : list(str) List of new column names. drop_col : list(str) Columns to be dropped. resample : bool Indicates whether to resample data or not. freq : str Resampling frequency i.e. d, h, 15T... resampler : str Resampling type i.e. mean, max. interpolate : bool Indicates whether to interpolate data or not. limit : int Interpolation limit. method : str Interpolation method. remove_na : bool Indicates whether to remove NAs or not. remove_na_how : str Specificies how to remove NA i.e. all, any... remove_outliers : bool Indicates whether to remove outliers or not. sd_val : int Standard Deviation Value (specifices how many SDs away is a point considered an outlier) remove_out_of_bounds : bool Indicates whether to remove out of bounds datapoints or not. low_bound : int Low bound of the data. high_bound : int High bound of the data. save_file : bool Specifies whether to save file or not. Defaults to True. Returns ------- pd.DataFrame() Dataframe containing cleaned data. """ # Check to ensure data is a pandas dataframe if not isinstance(data, pd.DataFrame): raise TypeError('data has to be a pandas dataframe.') # Create instance and clean the data clean_data_obj = Clean_Data(data) clean_data_obj.clean_data(resample=resample, freq=freq, resampler=resampler, interpolate=interpolate, limit=limit, method=method, remove_na=remove_na, remove_na_how=remove_na_how, remove_outliers=remove_outliers, sd_val=sd_val, remove_out_of_bounds=remove_out_of_bounds, low_bound=low_bound, high_bound=high_bound) # Correlation plot # fig = self.plot_data_obj.correlation_plot(clean_data_obj.cleaned_data) # fig.savefig(self.results_folder_name + '/correlation_plot-' + str(Wrapper.global_count) + '.png') if rename_col: # Rename columns of dataframe clean_data_obj.rename_columns(rename_col) if drop_col: # Drop columns of dataframe clean_data_obj.drop_columns(drop_col) # Store cleaned data in wrapper class self.cleaned_data = clean_data_obj.cleaned_data # Logging self.result['Clean'] = { 'Rename Col': rename_col, 'Drop Col': drop_col, 'Resample': resample, 'Frequency': freq, 'Resampler': resampler, 'Interpolate': interpolate, 'Limit': limit, 'Method': method, 'Remove NA': remove_na, 'Remove NA How': remove_na_how, 'Remove Outliers': remove_outliers, 'SD Val': sd_val, 'Remove Out of Bounds': remove_out_of_bounds, 'Low Bound': low_bound, 'High Bound': str(high_bound) if high_bound == float('inf') else high_bound, 'Save File': save_file } if save_file: f = self.results_folder_name + '/cleaned_data-' + str(self.get_global_count()) + '.csv' self.cleaned_data.to_csv(f) self.result['Clean']['Saved File'] = f else: self.result['Clean']['Saved File'] = '' return self.cleaned_data
python
def clean_data(self, data, rename_col=None, drop_col=None, resample=True, freq='h', resampler='mean', interpolate=True, limit=1, method='linear', remove_na=True, remove_na_how='any', remove_outliers=True, sd_val=3, remove_out_of_bounds=True, low_bound=0, high_bound=float('inf'), save_file=True): if not isinstance(data, pd.DataFrame): raise TypeError('data has to be a pandas dataframe.') clean_data_obj = Clean_Data(data) clean_data_obj.clean_data(resample=resample, freq=freq, resampler=resampler, interpolate=interpolate, limit=limit, method=method, remove_na=remove_na, remove_na_how=remove_na_how, remove_outliers=remove_outliers, sd_val=sd_val, remove_out_of_bounds=remove_out_of_bounds, low_bound=low_bound, high_bound=high_bound) if rename_col: clean_data_obj.rename_columns(rename_col) if drop_col: clean_data_obj.drop_columns(drop_col) self.cleaned_data = clean_data_obj.cleaned_data self.result['Clean'] = { 'Rename Col': rename_col, 'Drop Col': drop_col, 'Resample': resample, 'Frequency': freq, 'Resampler': resampler, 'Interpolate': interpolate, 'Limit': limit, 'Method': method, 'Remove NA': remove_na, 'Remove NA How': remove_na_how, 'Remove Outliers': remove_outliers, 'SD Val': sd_val, 'Remove Out of Bounds': remove_out_of_bounds, 'Low Bound': low_bound, 'High Bound': str(high_bound) if high_bound == float('inf') else high_bound, 'Save File': save_file } if save_file: f = self.results_folder_name + '/cleaned_data-' + str(self.get_global_count()) + '.csv' self.cleaned_data.to_csv(f) self.result['Clean']['Saved File'] = f else: self.result['Clean']['Saved File'] = '' return self.cleaned_data
[ "def", "clean_data", "(", "self", ",", "data", ",", "rename_col", "=", "None", ",", "drop_col", "=", "None", ",", "resample", "=", "True", ",", "freq", "=", "'h'", ",", "resampler", "=", "'mean'", ",", "interpolate", "=", "True", ",", "limit", "=", "1", ",", "method", "=", "'linear'", ",", "remove_na", "=", "True", ",", "remove_na_how", "=", "'any'", ",", "remove_outliers", "=", "True", ",", "sd_val", "=", "3", ",", "remove_out_of_bounds", "=", "True", ",", "low_bound", "=", "0", ",", "high_bound", "=", "float", "(", "'inf'", ")", ",", "save_file", "=", "True", ")", ":", "# Check to ensure data is a pandas dataframe", "if", "not", "isinstance", "(", "data", ",", "pd", ".", "DataFrame", ")", ":", "raise", "TypeError", "(", "'data has to be a pandas dataframe.'", ")", "# Create instance and clean the data", "clean_data_obj", "=", "Clean_Data", "(", "data", ")", "clean_data_obj", ".", "clean_data", "(", "resample", "=", "resample", ",", "freq", "=", "freq", ",", "resampler", "=", "resampler", ",", "interpolate", "=", "interpolate", ",", "limit", "=", "limit", ",", "method", "=", "method", ",", "remove_na", "=", "remove_na", ",", "remove_na_how", "=", "remove_na_how", ",", "remove_outliers", "=", "remove_outliers", ",", "sd_val", "=", "sd_val", ",", "remove_out_of_bounds", "=", "remove_out_of_bounds", ",", "low_bound", "=", "low_bound", ",", "high_bound", "=", "high_bound", ")", "# Correlation plot", "# fig = self.plot_data_obj.correlation_plot(clean_data_obj.cleaned_data)", "# fig.savefig(self.results_folder_name + '/correlation_plot-' + str(Wrapper.global_count) + '.png')", "if", "rename_col", ":", "# Rename columns of dataframe", "clean_data_obj", ".", "rename_columns", "(", "rename_col", ")", "if", "drop_col", ":", "# Drop columns of dataframe", "clean_data_obj", ".", "drop_columns", "(", "drop_col", ")", "# Store cleaned data in wrapper class", "self", ".", "cleaned_data", "=", "clean_data_obj", ".", "cleaned_data", "# Logging", "self", ".", "result", "[", "'Clean'", "]", "=", "{", "'Rename Col'", ":", "rename_col", ",", "'Drop Col'", ":", "drop_col", ",", "'Resample'", ":", "resample", ",", "'Frequency'", ":", "freq", ",", "'Resampler'", ":", "resampler", ",", "'Interpolate'", ":", "interpolate", ",", "'Limit'", ":", "limit", ",", "'Method'", ":", "method", ",", "'Remove NA'", ":", "remove_na", ",", "'Remove NA How'", ":", "remove_na_how", ",", "'Remove Outliers'", ":", "remove_outliers", ",", "'SD Val'", ":", "sd_val", ",", "'Remove Out of Bounds'", ":", "remove_out_of_bounds", ",", "'Low Bound'", ":", "low_bound", ",", "'High Bound'", ":", "str", "(", "high_bound", ")", "if", "high_bound", "==", "float", "(", "'inf'", ")", "else", "high_bound", ",", "'Save File'", ":", "save_file", "}", "if", "save_file", ":", "f", "=", "self", ".", "results_folder_name", "+", "'/cleaned_data-'", "+", "str", "(", "self", ".", "get_global_count", "(", ")", ")", "+", "'.csv'", "self", ".", "cleaned_data", ".", "to_csv", "(", "f", ")", "self", ".", "result", "[", "'Clean'", "]", "[", "'Saved File'", "]", "=", "f", "else", ":", "self", ".", "result", "[", "'Clean'", "]", "[", "'Saved File'", "]", "=", "''", "return", "self", ".", "cleaned_data" ]
Cleans dataframe according to user specifications and stores result in self.cleaned_data. Parameters ---------- data : pd.DataFrame() Dataframe to be cleaned. rename_col : list(str) List of new column names. drop_col : list(str) Columns to be dropped. resample : bool Indicates whether to resample data or not. freq : str Resampling frequency i.e. d, h, 15T... resampler : str Resampling type i.e. mean, max. interpolate : bool Indicates whether to interpolate data or not. limit : int Interpolation limit. method : str Interpolation method. remove_na : bool Indicates whether to remove NAs or not. remove_na_how : str Specificies how to remove NA i.e. all, any... remove_outliers : bool Indicates whether to remove outliers or not. sd_val : int Standard Deviation Value (specifices how many SDs away is a point considered an outlier) remove_out_of_bounds : bool Indicates whether to remove out of bounds datapoints or not. low_bound : int Low bound of the data. high_bound : int High bound of the data. save_file : bool Specifies whether to save file or not. Defaults to True. Returns ------- pd.DataFrame() Dataframe containing cleaned data.
[ "Cleans", "dataframe", "according", "to", "user", "specifications", "and", "stores", "result", "in", "self", ".", "cleaned_data", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L439-L543
SoftwareDefinedBuildings/XBOS
apps/Data_quality_analysis/Wrapper.py
Wrapper.uncertainity_equation
def uncertainity_equation(self, model_data_obj, E_measured, E_predicted, confidence_level): """ model_data_obj : Model_Data() An instance of Model_Data() which is a user defined class. E_measured : pd.Series() Actual values of energy in the post-retrofit period. E_predicted : pd.Series() Predicted values of energy in the post-retrofit period. confidence_level : float Confidence level of uncertainity in decimal, i.e. 90% = 0.9 """ # Number of rows in baseline period n = model_data_obj.baseline_in.shape[0] # Number of columns in baseline period p = model_data_obj.baseline_in.shape[1] # Number of rows in post period m = E_measured.count() # t-stats value # CHECK: degrees of freedom = E_predicted.count() - 1? t = stats.t.ppf(confidence_level, E_predicted.count() - 1) # Rho - Autocorrelation coefficient residuals = E_measured - E_predicted auto_corr = residuals.autocorr(lag=1) rho = pow(auto_corr, 0.5) # Effective number of points after accounting for autocorrelation n_prime = n * ((1 - rho) / (1 + rho)) # Coefficient of variation of RMSE # CHECK: Is the denominator correct? cv_rmse = pow(sum(pow(E_measured - E_predicted, 2) / (n - p)), 0.5) / (sum(E_measured) / E_measured.count()) # Bracket in the numerator - refer to page 20 numerator_bracket = pow((n / n_prime) * (1 + (2 / n_prime)) * (1 / m), 0.5) # Esave should be absolute value? f = abs(sum(E_measured - E_predicted) / sum(model_data_obj.y_true)) # Main equation uncertainity = t * ((1.26 * cv_rmse * numerator_bracket) / f) return uncertainity
python
def uncertainity_equation(self, model_data_obj, E_measured, E_predicted, confidence_level): n = model_data_obj.baseline_in.shape[0] p = model_data_obj.baseline_in.shape[1] m = E_measured.count() t = stats.t.ppf(confidence_level, E_predicted.count() - 1) residuals = E_measured - E_predicted auto_corr = residuals.autocorr(lag=1) rho = pow(auto_corr, 0.5) n_prime = n * ((1 - rho) / (1 + rho)) cv_rmse = pow(sum(pow(E_measured - E_predicted, 2) / (n - p)), 0.5) / (sum(E_measured) / E_measured.count()) numerator_bracket = pow((n / n_prime) * (1 + (2 / n_prime)) * (1 / m), 0.5) f = abs(sum(E_measured - E_predicted) / sum(model_data_obj.y_true)) uncertainity = t * ((1.26 * cv_rmse * numerator_bracket) / f) return uncertainity
[ "def", "uncertainity_equation", "(", "self", ",", "model_data_obj", ",", "E_measured", ",", "E_predicted", ",", "confidence_level", ")", ":", "# Number of rows in baseline period", "n", "=", "model_data_obj", ".", "baseline_in", ".", "shape", "[", "0", "]", "# Number of columns in baseline period", "p", "=", "model_data_obj", ".", "baseline_in", ".", "shape", "[", "1", "]", "# Number of rows in post period", "m", "=", "E_measured", ".", "count", "(", ")", "# t-stats value", "# CHECK: degrees of freedom = E_predicted.count() - 1?", "t", "=", "stats", ".", "t", ".", "ppf", "(", "confidence_level", ",", "E_predicted", ".", "count", "(", ")", "-", "1", ")", "# Rho - Autocorrelation coefficient", "residuals", "=", "E_measured", "-", "E_predicted", "auto_corr", "=", "residuals", ".", "autocorr", "(", "lag", "=", "1", ")", "rho", "=", "pow", "(", "auto_corr", ",", "0.5", ")", "# Effective number of points after accounting for autocorrelation", "n_prime", "=", "n", "*", "(", "(", "1", "-", "rho", ")", "/", "(", "1", "+", "rho", ")", ")", "# Coefficient of variation of RMSE", "# CHECK: Is the denominator correct?", "cv_rmse", "=", "pow", "(", "sum", "(", "pow", "(", "E_measured", "-", "E_predicted", ",", "2", ")", "/", "(", "n", "-", "p", ")", ")", ",", "0.5", ")", "/", "(", "sum", "(", "E_measured", ")", "/", "E_measured", ".", "count", "(", ")", ")", "# Bracket in the numerator - refer to page 20", "numerator_bracket", "=", "pow", "(", "(", "n", "/", "n_prime", ")", "*", "(", "1", "+", "(", "2", "/", "n_prime", ")", ")", "*", "(", "1", "/", "m", ")", ",", "0.5", ")", "# Esave should be absolute value? ", "f", "=", "abs", "(", "sum", "(", "E_measured", "-", "E_predicted", ")", "/", "sum", "(", "model_data_obj", ".", "y_true", ")", ")", "# Main equation ", "uncertainity", "=", "t", "*", "(", "(", "1.26", "*", "cv_rmse", "*", "numerator_bracket", ")", "/", "f", ")", "return", "uncertainity" ]
model_data_obj : Model_Data() An instance of Model_Data() which is a user defined class. E_measured : pd.Series() Actual values of energy in the post-retrofit period. E_predicted : pd.Series() Predicted values of energy in the post-retrofit period. confidence_level : float Confidence level of uncertainity in decimal, i.e. 90% = 0.9
[ "model_data_obj", ":", "Model_Data", "()", "An", "instance", "of", "Model_Data", "()", "which", "is", "a", "user", "defined", "class", ".", "E_measured", ":", "pd", ".", "Series", "()", "Actual", "values", "of", "energy", "in", "the", "post", "-", "retrofit", "period", ".", "E_predicted", ":", "pd", ".", "Series", "()", "Predicted", "values", "of", "energy", "in", "the", "post", "-", "retrofit", "period", ".", "confidence_level", ":", "float", "Confidence", "level", "of", "uncertainity", "in", "decimal", "i", ".", "e", ".", "90%", "=", "0", ".", "9" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/Data_quality_analysis/Wrapper.py#L754-L801
SoftwareDefinedBuildings/XBOS
python/xbos/services/mdal.py
MDALClient.do_query
def do_query(self, query, timeout=DEFAULT_TIMEOUT, tz=pytz.timezone("US/Pacific")): """ Query structure is as follows: query = { # We bind UUIDs found as the result of a Brick query to a variable name # that we can use later. # Each variable definition has the following: # - name: how we will refer to this group of UUIDs # - definition: a Brick query. The SELECT clause should return variables that end in '_uuid', which can be found as the # object of a 'bf:uuid' relationship # - units: what units we want to retrieve this stream as. Currently supports W/kW, Wh/kWh, F/C, Lux "Variables": [ {"Name": "meter", "Definition": "SELECT ?meter_uuid WHERE { ?meter rdf:type/rdfs:subClassOf* brick:Electric_Meter . ?meter bf:uuid ?meter_uuid . };", "Units": "kW", }, {"Name": "temp", "Definition": "SELECT ?temp_uuid WHERE { ?temp rdf:type/rdfs:subClassOf* brick:Temperature_Sensor . ?temp bf:uuid ?temp_uuid . };", "Units": "F", }, ], # this is the composition of the data matrix we are returning. Below, all the uuids for the "meter" variable will be placed before # all of the uuids for the "temp" variable. We cannot guarantee order of uuids within those groups, but the ordering of the groups # will be preserved. Explicit UUIDs can also be used here "Composition": ["meter", "temp"], # If we are retrieving statistical data, then we need to say which statistical elements we want to download. # The options are RAW, MEAN, MIN, MAX and COUNT. To query multiple, you can OR them together (e.g. MEAN|MAX). # This maps 1-1 to the "Composition" field "Selectors": [MEAN, MEAN], # Themporal parameters for the query. Retrieves data in the range [T0, T1]. By convention, T0 < T1, # but MDAL will take care of it if this is reversed. # WindowSize is the size of the resample window in nanoseconds # if Aligned is true, then MDAL will snap all data to the begining of the window (e.g. if 5min window + Aligned=true, # then all timestamps will be on 00:05:00, 00:10:00, 00:15:00, etc) "Time": { "T0": "2017-08-01 00:00:00", "T1": "2017-08-08 00:00:00", "WindowSize": '2h', "Aligned": True, }, } """ nonce = str(random.randint(0, 2**32)) query['Nonce'] = nonce ev = threading.Event() response = {} def _handleresult(msg): got_response = False for po in msg.payload_objects: if po.type_dotted != (2,0,10,4): continue data = msgpack.unpackb(po.content) if data['Nonce'] != query['Nonce']: continue if 'error' in data: response['error'] = data['error'] response['df'] = None got_response=True continue uuids = [str(uuid.UUID(bytes=x)) for x in data['Rows']] data = data_capnp.StreamCollection.from_bytes_packed(data['Data']) if hasattr(data, 'times') and len(data.times): times = list(data.times) if len(times) == 0: response['df'] = pd.DataFrame(columns=uuids) got_response = True break df = pd.DataFrame(index=pd.to_datetime(times, unit='ns', utc=False)) for idx, s in enumerate(data.streams): if len(s.values) == 0: df[uuids[idx]] = None else: df[uuids[idx]] = s.values df.index = df.index.tz_localize(pytz.utc).tz_convert(tz) response['df'] = df got_response = True else: df = pd.DataFrame() for idx, s in enumerate(data.streams): if hasattr(s, 'times'): newdf = pd.DataFrame(list(s.values), index=list(s.times), columns=[uuids[idx]]) newdf.index = pd.to_datetime(newdf.index, unit='ns').tz_localize(pytz.utc).tz_convert(tz) df = df.join(newdf, how='outer') else: raise Exception("Does this ever happen? Tell gabe!") response['df'] = df got_response = True df = response.get('df') if df is not None: response['df'] = df#[df.index.duplicated(keep='first')] if got_response: ev.set() h = self.c.subscribe("{0}/s.mdal/_/i.mdal/signal/{1}".format(self.url, self.vk[:-1]), _handleresult) po = PayloadObject((2,0,10,3), None, msgpack.packb(query)) self.c.publish("{0}/s.mdal/_/i.mdal/slot/query".format(self.url), payload_objects=(po,)) ev.wait(timeout) self.c.unsubscribe(h) if 'error' in response: raise Exception(response['error']) return response
python
def do_query(self, query, timeout=DEFAULT_TIMEOUT, tz=pytz.timezone("US/Pacific")): nonce = str(random.randint(0, 2**32)) query['Nonce'] = nonce ev = threading.Event() response = {} def _handleresult(msg): got_response = False for po in msg.payload_objects: if po.type_dotted != (2,0,10,4): continue data = msgpack.unpackb(po.content) if data['Nonce'] != query['Nonce']: continue if 'error' in data: response['error'] = data['error'] response['df'] = None got_response=True continue uuids = [str(uuid.UUID(bytes=x)) for x in data['Rows']] data = data_capnp.StreamCollection.from_bytes_packed(data['Data']) if hasattr(data, 'times') and len(data.times): times = list(data.times) if len(times) == 0: response['df'] = pd.DataFrame(columns=uuids) got_response = True break df = pd.DataFrame(index=pd.to_datetime(times, unit='ns', utc=False)) for idx, s in enumerate(data.streams): if len(s.values) == 0: df[uuids[idx]] = None else: df[uuids[idx]] = s.values df.index = df.index.tz_localize(pytz.utc).tz_convert(tz) response['df'] = df got_response = True else: df = pd.DataFrame() for idx, s in enumerate(data.streams): if hasattr(s, 'times'): newdf = pd.DataFrame(list(s.values), index=list(s.times), columns=[uuids[idx]]) newdf.index = pd.to_datetime(newdf.index, unit='ns').tz_localize(pytz.utc).tz_convert(tz) df = df.join(newdf, how='outer') else: raise Exception("Does this ever happen? Tell gabe!") response['df'] = df got_response = True df = response.get('df') if df is not None: response['df'] = df if got_response: ev.set() h = self.c.subscribe("{0}/s.mdal/_/i.mdal/signal/{1}".format(self.url, self.vk[:-1]), _handleresult) po = PayloadObject((2,0,10,3), None, msgpack.packb(query)) self.c.publish("{0}/s.mdal/_/i.mdal/slot/query".format(self.url), payload_objects=(po,)) ev.wait(timeout) self.c.unsubscribe(h) if 'error' in response: raise Exception(response['error']) return response
[ "def", "do_query", "(", "self", ",", "query", ",", "timeout", "=", "DEFAULT_TIMEOUT", ",", "tz", "=", "pytz", ".", "timezone", "(", "\"US/Pacific\"", ")", ")", ":", "nonce", "=", "str", "(", "random", ".", "randint", "(", "0", ",", "2", "**", "32", ")", ")", "query", "[", "'Nonce'", "]", "=", "nonce", "ev", "=", "threading", ".", "Event", "(", ")", "response", "=", "{", "}", "def", "_handleresult", "(", "msg", ")", ":", "got_response", "=", "False", "for", "po", "in", "msg", ".", "payload_objects", ":", "if", "po", ".", "type_dotted", "!=", "(", "2", ",", "0", ",", "10", ",", "4", ")", ":", "continue", "data", "=", "msgpack", ".", "unpackb", "(", "po", ".", "content", ")", "if", "data", "[", "'Nonce'", "]", "!=", "query", "[", "'Nonce'", "]", ":", "continue", "if", "'error'", "in", "data", ":", "response", "[", "'error'", "]", "=", "data", "[", "'error'", "]", "response", "[", "'df'", "]", "=", "None", "got_response", "=", "True", "continue", "uuids", "=", "[", "str", "(", "uuid", ".", "UUID", "(", "bytes", "=", "x", ")", ")", "for", "x", "in", "data", "[", "'Rows'", "]", "]", "data", "=", "data_capnp", ".", "StreamCollection", ".", "from_bytes_packed", "(", "data", "[", "'Data'", "]", ")", "if", "hasattr", "(", "data", ",", "'times'", ")", "and", "len", "(", "data", ".", "times", ")", ":", "times", "=", "list", "(", "data", ".", "times", ")", "if", "len", "(", "times", ")", "==", "0", ":", "response", "[", "'df'", "]", "=", "pd", ".", "DataFrame", "(", "columns", "=", "uuids", ")", "got_response", "=", "True", "break", "df", "=", "pd", ".", "DataFrame", "(", "index", "=", "pd", ".", "to_datetime", "(", "times", ",", "unit", "=", "'ns'", ",", "utc", "=", "False", ")", ")", "for", "idx", ",", "s", "in", "enumerate", "(", "data", ".", "streams", ")", ":", "if", "len", "(", "s", ".", "values", ")", "==", "0", ":", "df", "[", "uuids", "[", "idx", "]", "]", "=", "None", "else", ":", "df", "[", "uuids", "[", "idx", "]", "]", "=", "s", ".", "values", "df", ".", "index", "=", "df", ".", "index", ".", "tz_localize", "(", "pytz", ".", "utc", ")", ".", "tz_convert", "(", "tz", ")", "response", "[", "'df'", "]", "=", "df", "got_response", "=", "True", "else", ":", "df", "=", "pd", ".", "DataFrame", "(", ")", "for", "idx", ",", "s", "in", "enumerate", "(", "data", ".", "streams", ")", ":", "if", "hasattr", "(", "s", ",", "'times'", ")", ":", "newdf", "=", "pd", ".", "DataFrame", "(", "list", "(", "s", ".", "values", ")", ",", "index", "=", "list", "(", "s", ".", "times", ")", ",", "columns", "=", "[", "uuids", "[", "idx", "]", "]", ")", "newdf", ".", "index", "=", "pd", ".", "to_datetime", "(", "newdf", ".", "index", ",", "unit", "=", "'ns'", ")", ".", "tz_localize", "(", "pytz", ".", "utc", ")", ".", "tz_convert", "(", "tz", ")", "df", "=", "df", ".", "join", "(", "newdf", ",", "how", "=", "'outer'", ")", "else", ":", "raise", "Exception", "(", "\"Does this ever happen? Tell gabe!\"", ")", "response", "[", "'df'", "]", "=", "df", "got_response", "=", "True", "df", "=", "response", ".", "get", "(", "'df'", ")", "if", "df", "is", "not", "None", ":", "response", "[", "'df'", "]", "=", "df", "#[df.index.duplicated(keep='first')]", "if", "got_response", ":", "ev", ".", "set", "(", ")", "h", "=", "self", ".", "c", ".", "subscribe", "(", "\"{0}/s.mdal/_/i.mdal/signal/{1}\"", ".", "format", "(", "self", ".", "url", ",", "self", ".", "vk", "[", ":", "-", "1", "]", ")", ",", "_handleresult", ")", "po", "=", "PayloadObject", "(", "(", "2", ",", "0", ",", "10", ",", "3", ")", ",", "None", ",", "msgpack", ".", "packb", "(", "query", ")", ")", "self", ".", "c", ".", "publish", "(", "\"{0}/s.mdal/_/i.mdal/slot/query\"", ".", "format", "(", "self", ".", "url", ")", ",", "payload_objects", "=", "(", "po", ",", ")", ")", "ev", ".", "wait", "(", "timeout", ")", "self", ".", "c", ".", "unsubscribe", "(", "h", ")", "if", "'error'", "in", "response", ":", "raise", "Exception", "(", "response", "[", "'error'", "]", ")", "return", "response" ]
Query structure is as follows: query = { # We bind UUIDs found as the result of a Brick query to a variable name # that we can use later. # Each variable definition has the following: # - name: how we will refer to this group of UUIDs # - definition: a Brick query. The SELECT clause should return variables that end in '_uuid', which can be found as the # object of a 'bf:uuid' relationship # - units: what units we want to retrieve this stream as. Currently supports W/kW, Wh/kWh, F/C, Lux "Variables": [ {"Name": "meter", "Definition": "SELECT ?meter_uuid WHERE { ?meter rdf:type/rdfs:subClassOf* brick:Electric_Meter . ?meter bf:uuid ?meter_uuid . };", "Units": "kW", }, {"Name": "temp", "Definition": "SELECT ?temp_uuid WHERE { ?temp rdf:type/rdfs:subClassOf* brick:Temperature_Sensor . ?temp bf:uuid ?temp_uuid . };", "Units": "F", }, ], # this is the composition of the data matrix we are returning. Below, all the uuids for the "meter" variable will be placed before # all of the uuids for the "temp" variable. We cannot guarantee order of uuids within those groups, but the ordering of the groups # will be preserved. Explicit UUIDs can also be used here "Composition": ["meter", "temp"], # If we are retrieving statistical data, then we need to say which statistical elements we want to download. # The options are RAW, MEAN, MIN, MAX and COUNT. To query multiple, you can OR them together (e.g. MEAN|MAX). # This maps 1-1 to the "Composition" field "Selectors": [MEAN, MEAN], # Themporal parameters for the query. Retrieves data in the range [T0, T1]. By convention, T0 < T1, # but MDAL will take care of it if this is reversed. # WindowSize is the size of the resample window in nanoseconds # if Aligned is true, then MDAL will snap all data to the begining of the window (e.g. if 5min window + Aligned=true, # then all timestamps will be on 00:05:00, 00:10:00, 00:15:00, etc) "Time": { "T0": "2017-08-01 00:00:00", "T1": "2017-08-08 00:00:00", "WindowSize": '2h', "Aligned": True, }, }
[ "Query", "structure", "is", "as", "follows", ":" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/python/xbos/services/mdal.py#L64-L167
SoftwareDefinedBuildings/XBOS
dashboards/sitedash/occupancy.py
get_occupancy
def get_occupancy(last, bucketsize): """ We deliver historical occupancy up until "now". If the building has occupancy sensors, we pull that data and aggregate it by zone. Take mean occupancy per zone (across all sensors). If building does *not* have occupancy sensors, then we need to read the results from some occupancy file. """ if last not in ['hour','day','week']: return "Must be hour, day, week" start_date = get_start(last) zones = defaultdict(list) prediction_start = datetime.now(config.TZ) md = config.HOD.do_query(occupancy_query) if md['Rows'] is not None: for row in md['Rows']: zones[row['?zone']].append(row['?occ_uuid']) q = occupancy_data_query.copy() q["Time"] = { "T0": start_date.strftime("%Y-%m-%d %H:%M:%S %Z"), "T1": prediction_start.strftime("%Y-%m-%d %H:%M:%S %Z"), "WindowSize": bucketsize, "Aligned": True, } resp = config.MDAL.do_query(q, timeout=120) if 'error' in resp: print 'ERROR', resp return df = resp['df'].fillna(method='ffill') for zone, uuidlist in zones.items(): if len(uuidlist) > 0: zones[zone] = json.loads(df[uuidlist].mean(axis=1).to_json()) else: zones[zone] = {} # get predicted output prediction_end = get_tomorrow() predicted = list(rrule.rrule(freq=rrule.HOURLY, dtstart=prediction_start, until=prediction_end)) for zone, occdict in zones.items(): for date in predicted: occdict[int(int(date.strftime('%s'))*1000)] = 'predicted' # prediction zones[zone] = occdict else: md = config.HOD.do_query(zone_query) zonenames = [x['?zone'].lower() for x in md['Rows']] conn = sqlite3.connect('occupancy_schedule.db') sql = conn.cursor() for zone in zonenames: query = "SELECT * FROM schedules WHERE site='{0}' and zone='{1}' and dayofweek='{2}'".format(config.SITE, zone, prediction_start.strftime('%A').lower()) res = sql.execute(query).fetchall() records = {'time': [], 'occ': [], 'zone': []} for sqlrow in res: hour, minute = sqlrow[3].split(':') time = datetime(year=prediction_start.year, month=prediction_start.month, day=prediction_start.day, hour=int(hour), minute=int(minute), tzinfo=prediction_start.tzinfo) occ = sqlrow[5] zone = sqlrow[1] records['time'].append(time) records['occ'].append(occ) records['zone'].append(zone) df = pd.DataFrame.from_records(records) df = df.set_index(df.pop('time')) if len(df) ==0: continue sched = df.resample(bucketsize.replace('m','T')).ffill() zones[zone] = json.loads(sched['occ'].to_json()) conn.close() return zones
python
def get_occupancy(last, bucketsize): if last not in ['hour','day','week']: return "Must be hour, day, week" start_date = get_start(last) zones = defaultdict(list) prediction_start = datetime.now(config.TZ) md = config.HOD.do_query(occupancy_query) if md['Rows'] is not None: for row in md['Rows']: zones[row['?zone']].append(row['?occ_uuid']) q = occupancy_data_query.copy() q["Time"] = { "T0": start_date.strftime("%Y-%m-%d %H:%M:%S %Z"), "T1": prediction_start.strftime("%Y-%m-%d %H:%M:%S %Z"), "WindowSize": bucketsize, "Aligned": True, } resp = config.MDAL.do_query(q, timeout=120) if 'error' in resp: print 'ERROR', resp return df = resp['df'].fillna(method='ffill') for zone, uuidlist in zones.items(): if len(uuidlist) > 0: zones[zone] = json.loads(df[uuidlist].mean(axis=1).to_json()) else: zones[zone] = {} prediction_end = get_tomorrow() predicted = list(rrule.rrule(freq=rrule.HOURLY, dtstart=prediction_start, until=prediction_end)) for zone, occdict in zones.items(): for date in predicted: occdict[int(int(date.strftime('%s'))*1000)] = 'predicted' zones[zone] = occdict else: md = config.HOD.do_query(zone_query) zonenames = [x['?zone'].lower() for x in md['Rows']] conn = sqlite3.connect('occupancy_schedule.db') sql = conn.cursor() for zone in zonenames: query = "SELECT * FROM schedules WHERE site='{0}' and zone='{1}' and dayofweek='{2}'".format(config.SITE, zone, prediction_start.strftime('%A').lower()) res = sql.execute(query).fetchall() records = {'time': [], 'occ': [], 'zone': []} for sqlrow in res: hour, minute = sqlrow[3].split(':') time = datetime(year=prediction_start.year, month=prediction_start.month, day=prediction_start.day, hour=int(hour), minute=int(minute), tzinfo=prediction_start.tzinfo) occ = sqlrow[5] zone = sqlrow[1] records['time'].append(time) records['occ'].append(occ) records['zone'].append(zone) df = pd.DataFrame.from_records(records) df = df.set_index(df.pop('time')) if len(df) ==0: continue sched = df.resample(bucketsize.replace('m','T')).ffill() zones[zone] = json.loads(sched['occ'].to_json()) conn.close() return zones
[ "def", "get_occupancy", "(", "last", ",", "bucketsize", ")", ":", "if", "last", "not", "in", "[", "'hour'", ",", "'day'", ",", "'week'", "]", ":", "return", "\"Must be hour, day, week\"", "start_date", "=", "get_start", "(", "last", ")", "zones", "=", "defaultdict", "(", "list", ")", "prediction_start", "=", "datetime", ".", "now", "(", "config", ".", "TZ", ")", "md", "=", "config", ".", "HOD", ".", "do_query", "(", "occupancy_query", ")", "if", "md", "[", "'Rows'", "]", "is", "not", "None", ":", "for", "row", "in", "md", "[", "'Rows'", "]", ":", "zones", "[", "row", "[", "'?zone'", "]", "]", ".", "append", "(", "row", "[", "'?occ_uuid'", "]", ")", "q", "=", "occupancy_data_query", ".", "copy", "(", ")", "q", "[", "\"Time\"", "]", "=", "{", "\"T0\"", ":", "start_date", ".", "strftime", "(", "\"%Y-%m-%d %H:%M:%S %Z\"", ")", ",", "\"T1\"", ":", "prediction_start", ".", "strftime", "(", "\"%Y-%m-%d %H:%M:%S %Z\"", ")", ",", "\"WindowSize\"", ":", "bucketsize", ",", "\"Aligned\"", ":", "True", ",", "}", "resp", "=", "config", ".", "MDAL", ".", "do_query", "(", "q", ",", "timeout", "=", "120", ")", "if", "'error'", "in", "resp", ":", "print", "'ERROR'", ",", "resp", "return", "df", "=", "resp", "[", "'df'", "]", ".", "fillna", "(", "method", "=", "'ffill'", ")", "for", "zone", ",", "uuidlist", "in", "zones", ".", "items", "(", ")", ":", "if", "len", "(", "uuidlist", ")", ">", "0", ":", "zones", "[", "zone", "]", "=", "json", ".", "loads", "(", "df", "[", "uuidlist", "]", ".", "mean", "(", "axis", "=", "1", ")", ".", "to_json", "(", ")", ")", "else", ":", "zones", "[", "zone", "]", "=", "{", "}", "# get predicted output", "prediction_end", "=", "get_tomorrow", "(", ")", "predicted", "=", "list", "(", "rrule", ".", "rrule", "(", "freq", "=", "rrule", ".", "HOURLY", ",", "dtstart", "=", "prediction_start", ",", "until", "=", "prediction_end", ")", ")", "for", "zone", ",", "occdict", "in", "zones", ".", "items", "(", ")", ":", "for", "date", "in", "predicted", ":", "occdict", "[", "int", "(", "int", "(", "date", ".", "strftime", "(", "'%s'", ")", ")", "*", "1000", ")", "]", "=", "'predicted'", "# prediction", "zones", "[", "zone", "]", "=", "occdict", "else", ":", "md", "=", "config", ".", "HOD", ".", "do_query", "(", "zone_query", ")", "zonenames", "=", "[", "x", "[", "'?zone'", "]", ".", "lower", "(", ")", "for", "x", "in", "md", "[", "'Rows'", "]", "]", "conn", "=", "sqlite3", ".", "connect", "(", "'occupancy_schedule.db'", ")", "sql", "=", "conn", ".", "cursor", "(", ")", "for", "zone", "in", "zonenames", ":", "query", "=", "\"SELECT * FROM schedules WHERE site='{0}' and zone='{1}' and dayofweek='{2}'\"", ".", "format", "(", "config", ".", "SITE", ",", "zone", ",", "prediction_start", ".", "strftime", "(", "'%A'", ")", ".", "lower", "(", ")", ")", "res", "=", "sql", ".", "execute", "(", "query", ")", ".", "fetchall", "(", ")", "records", "=", "{", "'time'", ":", "[", "]", ",", "'occ'", ":", "[", "]", ",", "'zone'", ":", "[", "]", "}", "for", "sqlrow", "in", "res", ":", "hour", ",", "minute", "=", "sqlrow", "[", "3", "]", ".", "split", "(", "':'", ")", "time", "=", "datetime", "(", "year", "=", "prediction_start", ".", "year", ",", "month", "=", "prediction_start", ".", "month", ",", "day", "=", "prediction_start", ".", "day", ",", "hour", "=", "int", "(", "hour", ")", ",", "minute", "=", "int", "(", "minute", ")", ",", "tzinfo", "=", "prediction_start", ".", "tzinfo", ")", "occ", "=", "sqlrow", "[", "5", "]", "zone", "=", "sqlrow", "[", "1", "]", "records", "[", "'time'", "]", ".", "append", "(", "time", ")", "records", "[", "'occ'", "]", ".", "append", "(", "occ", ")", "records", "[", "'zone'", "]", ".", "append", "(", "zone", ")", "df", "=", "pd", ".", "DataFrame", ".", "from_records", "(", "records", ")", "df", "=", "df", ".", "set_index", "(", "df", ".", "pop", "(", "'time'", ")", ")", "if", "len", "(", "df", ")", "==", "0", ":", "continue", "sched", "=", "df", ".", "resample", "(", "bucketsize", ".", "replace", "(", "'m'", ",", "'T'", ")", ")", ".", "ffill", "(", ")", "zones", "[", "zone", "]", "=", "json", ".", "loads", "(", "sched", "[", "'occ'", "]", ".", "to_json", "(", ")", ")", "conn", ".", "close", "(", ")", "return", "zones" ]
We deliver historical occupancy up until "now". If the building has occupancy sensors, we pull that data and aggregate it by zone. Take mean occupancy per zone (across all sensors). If building does *not* have occupancy sensors, then we need to read the results from some occupancy file.
[ "We", "deliver", "historical", "occupancy", "up", "until", "now", ".", "If", "the", "building", "has", "occupancy", "sensors", "we", "pull", "that", "data", "and", "aggregate", "it", "by", "zone", ".", "Take", "mean", "occupancy", "per", "zone", "(", "across", "all", "sensors", ")", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/dashboards/sitedash/occupancy.py#L41-L110
SoftwareDefinedBuildings/XBOS
apps/prediction_service_framework/prediction_service.py
run_service
def run_service(prediction_fxn, namespace, prediction_type, block=False): """ Supported prediction_type: - hvac - occupancy """ subscribe = '{0}/s.predictions/+/i.{1}/slot/request'.format(namespace, prediction_type) if block: print 'Blocking is True! This will loop forever and program will not exit until killed' else: print 'Blocking is False! This will run in background until program exits or is killed' print 'Subscribe on', subscribe def run(): c = get_client(config.AGENT, config.ENTITY) def cb(msg): po = msgpack.unpackb(msg.payload_objects[0].content) if not isinstance(po, dict): return client_id = msg.uri.split('/')[2] start = po.get('predstart') start = parse(start) if start else get_today() end = po.get('predend') end = parse(end) if end else get_today()+datetime.timedelta(days=1) resolution = po.get('resolution', '1h') result = prediction_fxn(start, end, resolution) po = PayloadObject((2,0,0,0), None, msgpack.packb(result)) publish = '{0}/s.predictions/{1}/i.{2}/signal/response'.format(namespace, client_id, prediction_type) print "Respond on", publish c.publish(publish, payload_objects=(po,)) c.subscribe(subscribe, cb) while True: time.sleep(10) t = threading.Thread(target=run) t.daemon = True t.start() while block: time.sleep(10) return t
python
def run_service(prediction_fxn, namespace, prediction_type, block=False): subscribe = '{0}/s.predictions/+/i.{1}/slot/request'.format(namespace, prediction_type) if block: print 'Blocking is True! This will loop forever and program will not exit until killed' else: print 'Blocking is False! This will run in background until program exits or is killed' print 'Subscribe on', subscribe def run(): c = get_client(config.AGENT, config.ENTITY) def cb(msg): po = msgpack.unpackb(msg.payload_objects[0].content) if not isinstance(po, dict): return client_id = msg.uri.split('/')[2] start = po.get('predstart') start = parse(start) if start else get_today() end = po.get('predend') end = parse(end) if end else get_today()+datetime.timedelta(days=1) resolution = po.get('resolution', '1h') result = prediction_fxn(start, end, resolution) po = PayloadObject((2,0,0,0), None, msgpack.packb(result)) publish = '{0}/s.predictions/{1}/i.{2}/signal/response'.format(namespace, client_id, prediction_type) print "Respond on", publish c.publish(publish, payload_objects=(po,)) c.subscribe(subscribe, cb) while True: time.sleep(10) t = threading.Thread(target=run) t.daemon = True t.start() while block: time.sleep(10) return t
[ "def", "run_service", "(", "prediction_fxn", ",", "namespace", ",", "prediction_type", ",", "block", "=", "False", ")", ":", "subscribe", "=", "'{0}/s.predictions/+/i.{1}/slot/request'", ".", "format", "(", "namespace", ",", "prediction_type", ")", "if", "block", ":", "print", "'Blocking is True! This will loop forever and program will not exit until killed'", "else", ":", "print", "'Blocking is False! This will run in background until program exits or is killed'", "print", "'Subscribe on'", ",", "subscribe", "def", "run", "(", ")", ":", "c", "=", "get_client", "(", "config", ".", "AGENT", ",", "config", ".", "ENTITY", ")", "def", "cb", "(", "msg", ")", ":", "po", "=", "msgpack", ".", "unpackb", "(", "msg", ".", "payload_objects", "[", "0", "]", ".", "content", ")", "if", "not", "isinstance", "(", "po", ",", "dict", ")", ":", "return", "client_id", "=", "msg", ".", "uri", ".", "split", "(", "'/'", ")", "[", "2", "]", "start", "=", "po", ".", "get", "(", "'predstart'", ")", "start", "=", "parse", "(", "start", ")", "if", "start", "else", "get_today", "(", ")", "end", "=", "po", ".", "get", "(", "'predend'", ")", "end", "=", "parse", "(", "end", ")", "if", "end", "else", "get_today", "(", ")", "+", "datetime", ".", "timedelta", "(", "days", "=", "1", ")", "resolution", "=", "po", ".", "get", "(", "'resolution'", ",", "'1h'", ")", "result", "=", "prediction_fxn", "(", "start", ",", "end", ",", "resolution", ")", "po", "=", "PayloadObject", "(", "(", "2", ",", "0", ",", "0", ",", "0", ")", ",", "None", ",", "msgpack", ".", "packb", "(", "result", ")", ")", "publish", "=", "'{0}/s.predictions/{1}/i.{2}/signal/response'", ".", "format", "(", "namespace", ",", "client_id", ",", "prediction_type", ")", "print", "\"Respond on\"", ",", "publish", "c", ".", "publish", "(", "publish", ",", "payload_objects", "=", "(", "po", ",", ")", ")", "c", ".", "subscribe", "(", "subscribe", ",", "cb", ")", "while", "True", ":", "time", ".", "sleep", "(", "10", ")", "t", "=", "threading", ".", "Thread", "(", "target", "=", "run", ")", "t", ".", "daemon", "=", "True", "t", ".", "start", "(", ")", "while", "block", ":", "time", ".", "sleep", "(", "10", ")", "return", "t" ]
Supported prediction_type: - hvac - occupancy
[ "Supported", "prediction_type", ":", "-", "hvac", "-", "occupancy" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/prediction_service_framework/prediction_service.py#L13-L49
SoftwareDefinedBuildings/XBOS
dashboards/sitedash/app.py
prevmonday
def prevmonday(num): """ Return unix SECOND timestamp of "num" mondays ago """ today = get_today() lastmonday = today - timedelta(days=today.weekday(), weeks=num) return lastmonday
python
def prevmonday(num): today = get_today() lastmonday = today - timedelta(days=today.weekday(), weeks=num) return lastmonday
[ "def", "prevmonday", "(", "num", ")", ":", "today", "=", "get_today", "(", ")", "lastmonday", "=", "today", "-", "timedelta", "(", "days", "=", "today", ".", "weekday", "(", ")", ",", "weeks", "=", "num", ")", "return", "lastmonday" ]
Return unix SECOND timestamp of "num" mondays ago
[ "Return", "unix", "SECOND", "timestamp", "of", "num", "mondays", "ago" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/dashboards/sitedash/app.py#L74-L80
SoftwareDefinedBuildings/XBOS
apps/consumption/iec.py
med_filt
def med_filt(x, k=201): """Apply a length-k median filter to a 1D array x. Boundaries are extended by repeating endpoints. """ if x.ndim > 1: x = np.squeeze(x) med = np.median(x) assert k % 2 == 1, "Median filter length must be odd." assert x.ndim == 1, "Input must be one-dimensional." k2 = (k - 1) // 2 y = np.zeros((len(x), k), dtype=x.dtype) y[:, k2] = x for i in range(k2): j = k2 - i y[j:, i] = x[:-j] y[:j, i] = x[0] y[:-j, -(i + 1)] = x[j:] y[-j:, -(i + 1)] = med return np.median(y, axis=1)
python
def med_filt(x, k=201): if x.ndim > 1: x = np.squeeze(x) med = np.median(x) assert k % 2 == 1, "Median filter length must be odd." assert x.ndim == 1, "Input must be one-dimensional." k2 = (k - 1) // 2 y = np.zeros((len(x), k), dtype=x.dtype) y[:, k2] = x for i in range(k2): j = k2 - i y[j:, i] = x[:-j] y[:j, i] = x[0] y[:-j, -(i + 1)] = x[j:] y[-j:, -(i + 1)] = med return np.median(y, axis=1)
[ "def", "med_filt", "(", "x", ",", "k", "=", "201", ")", ":", "if", "x", ".", "ndim", ">", "1", ":", "x", "=", "np", ".", "squeeze", "(", "x", ")", "med", "=", "np", ".", "median", "(", "x", ")", "assert", "k", "%", "2", "==", "1", ",", "\"Median filter length must be odd.\"", "assert", "x", ".", "ndim", "==", "1", ",", "\"Input must be one-dimensional.\"", "k2", "=", "(", "k", "-", "1", ")", "//", "2", "y", "=", "np", ".", "zeros", "(", "(", "len", "(", "x", ")", ",", "k", ")", ",", "dtype", "=", "x", ".", "dtype", ")", "y", "[", ":", ",", "k2", "]", "=", "x", "for", "i", "in", "range", "(", "k2", ")", ":", "j", "=", "k2", "-", "i", "y", "[", "j", ":", ",", "i", "]", "=", "x", "[", ":", "-", "j", "]", "y", "[", ":", "j", ",", "i", "]", "=", "x", "[", "0", "]", "y", "[", ":", "-", "j", ",", "-", "(", "i", "+", "1", ")", "]", "=", "x", "[", "j", ":", "]", "y", "[", "-", "j", ":", ",", "-", "(", "i", "+", "1", ")", "]", "=", "med", "return", "np", ".", "median", "(", "y", ",", "axis", "=", "1", ")" ]
Apply a length-k median filter to a 1D array x. Boundaries are extended by repeating endpoints.
[ "Apply", "a", "length", "-", "k", "median", "filter", "to", "a", "1D", "array", "x", ".", "Boundaries", "are", "extended", "by", "repeating", "endpoints", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/consumption/iec.py#L114-L132
SoftwareDefinedBuildings/XBOS
apps/occupancy/OccupancyThanos.py
find_similar_days
def find_similar_days(training_data, now, observation_length, k, method=hamming_distance): min_time = training_data.index[0] + timedelta(minutes=observation_length) # Find moments in our dataset that have the same hour/minute and is_weekend() == weekend. selector = ((training_data.index.minute == now.minute) & (training_data.index.hour == now.hour) & (training_data.index > min_time)) """ if now.weekday() < 5: selector = ( (training_data.index.minute == now.minute) & (training_data.index.hour == now.hour) & (training_data.index > min_time) & (training_data.index.weekday < 5) ) else: selector = ( (training_data.index.minute == now.minute) & (training_data.index.hour == now.hour) & (training_data.index > min_time) & (training_data.index.weekday >= 5) ) """ similar_moments = training_data[selector][:-1] obs_td = timedelta(minutes=observation_length) similar_moments['Similarity'] = [ method( training_data[(training_data.index >= now - obs_td) & (training_data.index <= now)].get_values(), training_data[(training_data.index >= i - obs_td) & (training_data.index <= i)].get_values() ) for i in similar_moments.index ] indexes = (similar_moments.sort_values('Similarity', ascending=True) .head(k).index) return indexes
python
def find_similar_days(training_data, now, observation_length, k, method=hamming_distance): min_time = training_data.index[0] + timedelta(minutes=observation_length) selector = ((training_data.index.minute == now.minute) & (training_data.index.hour == now.hour) & (training_data.index > min_time)) similar_moments = training_data[selector][:-1] obs_td = timedelta(minutes=observation_length) similar_moments['Similarity'] = [ method( training_data[(training_data.index >= now - obs_td) & (training_data.index <= now)].get_values(), training_data[(training_data.index >= i - obs_td) & (training_data.index <= i)].get_values() ) for i in similar_moments.index ] indexes = (similar_moments.sort_values('Similarity', ascending=True) .head(k).index) return indexes
[ "def", "find_similar_days", "(", "training_data", ",", "now", ",", "observation_length", ",", "k", ",", "method", "=", "hamming_distance", ")", ":", "min_time", "=", "training_data", ".", "index", "[", "0", "]", "+", "timedelta", "(", "minutes", "=", "observation_length", ")", "# Find moments in our dataset that have the same hour/minute and is_weekend() == weekend.", "selector", "=", "(", "(", "training_data", ".", "index", ".", "minute", "==", "now", ".", "minute", ")", "&", "(", "training_data", ".", "index", ".", "hour", "==", "now", ".", "hour", ")", "&", "(", "training_data", ".", "index", ">", "min_time", ")", ")", "similar_moments", "=", "training_data", "[", "selector", "]", "[", ":", "-", "1", "]", "obs_td", "=", "timedelta", "(", "minutes", "=", "observation_length", ")", "similar_moments", "[", "'Similarity'", "]", "=", "[", "method", "(", "training_data", "[", "(", "training_data", ".", "index", ">=", "now", "-", "obs_td", ")", "&", "(", "training_data", ".", "index", "<=", "now", ")", "]", ".", "get_values", "(", ")", ",", "training_data", "[", "(", "training_data", ".", "index", ">=", "i", "-", "obs_td", ")", "&", "(", "training_data", ".", "index", "<=", "i", ")", "]", ".", "get_values", "(", ")", ")", "for", "i", "in", "similar_moments", ".", "index", "]", "indexes", "=", "(", "similar_moments", ".", "sort_values", "(", "'Similarity'", ",", "ascending", "=", "True", ")", ".", "head", "(", "k", ")", ".", "index", ")", "return", "indexes" ]
if now.weekday() < 5: selector = ( (training_data.index.minute == now.minute) & (training_data.index.hour == now.hour) & (training_data.index > min_time) & (training_data.index.weekday < 5) ) else: selector = ( (training_data.index.minute == now.minute) & (training_data.index.hour == now.hour) & (training_data.index > min_time) & (training_data.index.weekday >= 5) )
[ "if", "now", ".", "weekday", "()", "<", "5", ":", "selector", "=", "(", "(", "training_data", ".", "index", ".", "minute", "==", "now", ".", "minute", ")", "&", "(", "training_data", ".", "index", ".", "hour", "==", "now", ".", "hour", ")", "&", "(", "training_data", ".", "index", ">", "min_time", ")", "&", "(", "training_data", ".", "index", ".", "weekday", "<", "5", ")", ")", "else", ":", "selector", "=", "(", "(", "training_data", ".", "index", ".", "minute", "==", "now", ".", "minute", ")", "&", "(", "training_data", ".", "index", ".", "hour", "==", "now", ".", "hour", ")", "&", "(", "training_data", ".", "index", ">", "min_time", ")", "&", "(", "training_data", ".", "index", ".", "weekday", ">", "=", "5", ")", ")" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/occupancy/OccupancyThanos.py#L48-L88
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Wrapper.py
Wrapper.preprocess_data
def preprocess_data(self, data, hdh_cpoint=65, cdh_cpoint=65, col_hdh_cdh=None, col_degree=None, degree=None, standardize=False, normalize=False, year=False, month=False, week=False, tod=False, dow=False, save_file=True): """ Preprocesses dataframe according to user specifications and stores result in self.preprocessed_data. Parameters ---------- data : pd.DataFrame() Dataframe to be preprocessed. hdh_cpoint : int Heating degree hours. Defaults to 65. cdh_cpoint : int Cooling degree hours. Defaults to 65. col_hdh_cdh : str Column name which contains the outdoor air temperature. col_degree : list(str) Column to exponentiate. degree : list(str) Exponentiation degree. standardize : bool Standardize data. normalize : bool Normalize data. year : bool Year. month : bool Month. week : bool Week. tod : bool Time of Day. dow : bool Day of Week. save_file : bool Specifies whether to save file or not. Defaults to True. Returns ------- pd.DataFrame() Dataframe containing preprocessed data. """ # Check to ensure data is a pandas dataframe if not isinstance(data, pd.DataFrame): raise SystemError('data has to be a pandas dataframe.') # Create instance preprocess_data_obj = Preprocess_Data(data) if col_hdh_cdh: preprocess_data_obj.add_degree_days(col=col_hdh_cdh, hdh_cpoint=hdh_cpoint, cdh_cpoint=cdh_cpoint) preprocess_data_obj.add_col_features(col=col_degree, degree=degree) if standardize: preprocess_data_obj.standardize() if normalize: preprocess_data_obj.normalize() preprocess_data_obj.add_time_features(year=year, month=month, week=week, tod=tod, dow=dow) # Store preprocessed data in wrapper class self.preprocessed_data = preprocess_data_obj.preprocessed_data # Logging self.result['Preprocess'] = { 'HDH CPoint': hdh_cpoint, 'CDH CPoint': cdh_cpoint, 'HDH CDH Calc Col': col_hdh_cdh, 'Col Degree': col_degree, 'Degree': degree, 'Standardize': standardize, 'Normalize': normalize, 'Year': year, 'Month': month, 'Week': week, 'Time of Day': tod, 'Day of Week': dow, 'Save File': save_file } if save_file: f = self.results_folder_name + '/preprocessed_data-' + str(self.get_global_count()) + '.csv' self.preprocessed_data.to_csv(f) self.result['Preprocess']['Saved File'] = f else: self.result['Preprocess']['Saved File'] = '' return self.preprocessed_data
python
def preprocess_data(self, data, hdh_cpoint=65, cdh_cpoint=65, col_hdh_cdh=None, col_degree=None, degree=None, standardize=False, normalize=False, year=False, month=False, week=False, tod=False, dow=False, save_file=True): if not isinstance(data, pd.DataFrame): raise SystemError('data has to be a pandas dataframe.') preprocess_data_obj = Preprocess_Data(data) if col_hdh_cdh: preprocess_data_obj.add_degree_days(col=col_hdh_cdh, hdh_cpoint=hdh_cpoint, cdh_cpoint=cdh_cpoint) preprocess_data_obj.add_col_features(col=col_degree, degree=degree) if standardize: preprocess_data_obj.standardize() if normalize: preprocess_data_obj.normalize() preprocess_data_obj.add_time_features(year=year, month=month, week=week, tod=tod, dow=dow) self.preprocessed_data = preprocess_data_obj.preprocessed_data self.result['Preprocess'] = { 'HDH CPoint': hdh_cpoint, 'CDH CPoint': cdh_cpoint, 'HDH CDH Calc Col': col_hdh_cdh, 'Col Degree': col_degree, 'Degree': degree, 'Standardize': standardize, 'Normalize': normalize, 'Year': year, 'Month': month, 'Week': week, 'Time of Day': tod, 'Day of Week': dow, 'Save File': save_file } if save_file: f = self.results_folder_name + '/preprocessed_data-' + str(self.get_global_count()) + '.csv' self.preprocessed_data.to_csv(f) self.result['Preprocess']['Saved File'] = f else: self.result['Preprocess']['Saved File'] = '' return self.preprocessed_data
[ "def", "preprocess_data", "(", "self", ",", "data", ",", "hdh_cpoint", "=", "65", ",", "cdh_cpoint", "=", "65", ",", "col_hdh_cdh", "=", "None", ",", "col_degree", "=", "None", ",", "degree", "=", "None", ",", "standardize", "=", "False", ",", "normalize", "=", "False", ",", "year", "=", "False", ",", "month", "=", "False", ",", "week", "=", "False", ",", "tod", "=", "False", ",", "dow", "=", "False", ",", "save_file", "=", "True", ")", ":", "# Check to ensure data is a pandas dataframe", "if", "not", "isinstance", "(", "data", ",", "pd", ".", "DataFrame", ")", ":", "raise", "SystemError", "(", "'data has to be a pandas dataframe.'", ")", "# Create instance", "preprocess_data_obj", "=", "Preprocess_Data", "(", "data", ")", "if", "col_hdh_cdh", ":", "preprocess_data_obj", ".", "add_degree_days", "(", "col", "=", "col_hdh_cdh", ",", "hdh_cpoint", "=", "hdh_cpoint", ",", "cdh_cpoint", "=", "cdh_cpoint", ")", "preprocess_data_obj", ".", "add_col_features", "(", "col", "=", "col_degree", ",", "degree", "=", "degree", ")", "if", "standardize", ":", "preprocess_data_obj", ".", "standardize", "(", ")", "if", "normalize", ":", "preprocess_data_obj", ".", "normalize", "(", ")", "preprocess_data_obj", ".", "add_time_features", "(", "year", "=", "year", ",", "month", "=", "month", ",", "week", "=", "week", ",", "tod", "=", "tod", ",", "dow", "=", "dow", ")", "# Store preprocessed data in wrapper class", "self", ".", "preprocessed_data", "=", "preprocess_data_obj", ".", "preprocessed_data", "# Logging", "self", ".", "result", "[", "'Preprocess'", "]", "=", "{", "'HDH CPoint'", ":", "hdh_cpoint", ",", "'CDH CPoint'", ":", "cdh_cpoint", ",", "'HDH CDH Calc Col'", ":", "col_hdh_cdh", ",", "'Col Degree'", ":", "col_degree", ",", "'Degree'", ":", "degree", ",", "'Standardize'", ":", "standardize", ",", "'Normalize'", ":", "normalize", ",", "'Year'", ":", "year", ",", "'Month'", ":", "month", ",", "'Week'", ":", "week", ",", "'Time of Day'", ":", "tod", ",", "'Day of Week'", ":", "dow", ",", "'Save File'", ":", "save_file", "}", "if", "save_file", ":", "f", "=", "self", ".", "results_folder_name", "+", "'/preprocessed_data-'", "+", "str", "(", "self", ".", "get_global_count", "(", ")", ")", "+", "'.csv'", "self", ".", "preprocessed_data", ".", "to_csv", "(", "f", ")", "self", ".", "result", "[", "'Preprocess'", "]", "[", "'Saved File'", "]", "=", "f", "else", ":", "self", ".", "result", "[", "'Preprocess'", "]", "[", "'Saved File'", "]", "=", "''", "return", "self", ".", "preprocessed_data" ]
Preprocesses dataframe according to user specifications and stores result in self.preprocessed_data. Parameters ---------- data : pd.DataFrame() Dataframe to be preprocessed. hdh_cpoint : int Heating degree hours. Defaults to 65. cdh_cpoint : int Cooling degree hours. Defaults to 65. col_hdh_cdh : str Column name which contains the outdoor air temperature. col_degree : list(str) Column to exponentiate. degree : list(str) Exponentiation degree. standardize : bool Standardize data. normalize : bool Normalize data. year : bool Year. month : bool Month. week : bool Week. tod : bool Time of Day. dow : bool Day of Week. save_file : bool Specifies whether to save file or not. Defaults to True. Returns ------- pd.DataFrame() Dataframe containing preprocessed data.
[ "Preprocesses", "dataframe", "according", "to", "user", "specifications", "and", "stores", "result", "in", "self", ".", "preprocessed_data", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Wrapper.py#L544-L634
SoftwareDefinedBuildings/XBOS
apps/data_analysis/XBOS_data_analytics/Wrapper.py
Wrapper.model
def model(self, data, ind_col=None, dep_col=None, project_ind_col=None, baseline_period=[None, None], projection_period=None, exclude_time_period=None, alphas=np.logspace(-4,1,30), cv=3, plot=True, figsize=None, custom_model_func=None): """ Split data into baseline and projection periods, run models on them and display metrics & plots. Parameters ---------- data : pd.DataFrame() Dataframe to model. ind_col : list(str) Independent column(s) of dataframe. Defaults to all columns except the last. dep_col : str Dependent column of dataframe. project_ind_col : list(str) Independent column(s) to use for projection. If none, use ind_col. baseline_period : list(str) List of time periods to split the data into baseline periods. It needs to have a start and an end date. projection_period : list(str) List of time periods to split the data into projection periods. It needs to have a start and an end date. exclude_time_period : list(str) List of time periods to exclude for modeling. alphas : list(int) List of alphas to run regression on. cv : int Number of folds for cross-validation. plot : bool Specifies whether to save plots or not. figsize : tuple Size of the plots. custom_model_func : function Model with specific hyper-parameters provided by user. Returns ------- dict Metrics of the optimal/best model. """ # Check to ensure data is a pandas dataframe if not isinstance(data, pd.DataFrame): raise SystemError('data has to be a pandas dataframe.') # Create instance model_data_obj = Model_Data(data, ind_col, dep_col, alphas, cv, exclude_time_period, baseline_period, projection_period) # Split data into baseline and projection model_data_obj.split_data() # Logging self.result['Model'] = { 'Independent Col': ind_col, 'Dependent Col': dep_col, 'Projection Independent Col': project_ind_col, 'Baseline Period': baseline_period, 'Projection Period': projection_period, 'Exclude Time Period': exclude_time_period, 'Alphas': list(alphas), 'CV': cv, 'Plot': plot, 'Fig Size': figsize } # Runs all models on the data and returns optimal model all_metrics = model_data_obj.run_models() self.result['Model']['All Model\'s Metrics'] = all_metrics # CHECK: Define custom model's parameter and return types in documentation. if custom_model_func: self.result['Model']['Custom Model\'s Metrics'] = model_data_obj.custom_model(custom_model_func) # Fit optimal model to data self.result['Model']['Optimal Model\'s Metrics'] = model_data_obj.best_model_fit() if plot: # Use project_ind_col if projecting into the future (no input data other than weather data) input_col = model_data_obj.input_col if not project_ind_col else project_ind_col fig, y_true, y_pred = self.plot_data_obj.baseline_projection_plot(model_data_obj.y_true, model_data_obj.y_pred, model_data_obj.baseline_period, model_data_obj.projection_period, model_data_obj.best_model_name, model_data_obj.best_metrics['adj_r2'], model_data_obj.original_data, input_col, model_data_obj.output_col, model_data_obj.best_model, self.result['Site']) fig.savefig(self.results_folder_name + '/baseline_projection_plot-' + str(self.get_global_count()) + '.png') if not y_true.empty and not y_pred.empty: saving_absolute = (y_pred - y_true).sum() saving_perc = (saving_absolute / y_pred.sum()) * 100 self.result['Energy Savings (%)'] = float(saving_perc) self.result['Energy Savings (absolute)'] = saving_absolute # Temporary self.project_df['true'] = y_true self.project_df['pred'] = y_pred # Calculate uncertainity of savings self.result['Uncertainity'] = self.uncertainity_equation(model_data_obj, y_true, y_pred, 0.9) else: print('y_true: ', y_true) print('y_pred: ', y_pred) print('Error: y_true and y_pred are empty. Default to -1.0 savings.') self.result['Energy Savings (%)'] = float(-1.0) self.result['Energy Savings (absolute)'] = float(-1.0) return self.best_metrics
python
def model(self, data, ind_col=None, dep_col=None, project_ind_col=None, baseline_period=[None, None], projection_period=None, exclude_time_period=None, alphas=np.logspace(-4,1,30), cv=3, plot=True, figsize=None, custom_model_func=None): if not isinstance(data, pd.DataFrame): raise SystemError('data has to be a pandas dataframe.') model_data_obj = Model_Data(data, ind_col, dep_col, alphas, cv, exclude_time_period, baseline_period, projection_period) model_data_obj.split_data() self.result['Model'] = { 'Independent Col': ind_col, 'Dependent Col': dep_col, 'Projection Independent Col': project_ind_col, 'Baseline Period': baseline_period, 'Projection Period': projection_period, 'Exclude Time Period': exclude_time_period, 'Alphas': list(alphas), 'CV': cv, 'Plot': plot, 'Fig Size': figsize } all_metrics = model_data_obj.run_models() self.result['Model']['All Model\'s Metrics'] = all_metrics if custom_model_func: self.result['Model']['Custom Model\'s Metrics'] = model_data_obj.custom_model(custom_model_func) self.result['Model']['Optimal Model\'s Metrics'] = model_data_obj.best_model_fit() if plot: input_col = model_data_obj.input_col if not project_ind_col else project_ind_col fig, y_true, y_pred = self.plot_data_obj.baseline_projection_plot(model_data_obj.y_true, model_data_obj.y_pred, model_data_obj.baseline_period, model_data_obj.projection_period, model_data_obj.best_model_name, model_data_obj.best_metrics['adj_r2'], model_data_obj.original_data, input_col, model_data_obj.output_col, model_data_obj.best_model, self.result['Site']) fig.savefig(self.results_folder_name + '/baseline_projection_plot-' + str(self.get_global_count()) + '.png') if not y_true.empty and not y_pred.empty: saving_absolute = (y_pred - y_true).sum() saving_perc = (saving_absolute / y_pred.sum()) * 100 self.result['Energy Savings (%)'] = float(saving_perc) self.result['Energy Savings (absolute)'] = saving_absolute self.project_df['true'] = y_true self.project_df['pred'] = y_pred self.result['Uncertainity'] = self.uncertainity_equation(model_data_obj, y_true, y_pred, 0.9) else: print('y_true: ', y_true) print('y_pred: ', y_pred) print('Error: y_true and y_pred are empty. Default to -1.0 savings.') self.result['Energy Savings (%)'] = float(-1.0) self.result['Energy Savings (absolute)'] = float(-1.0) return self.best_metrics
[ "def", "model", "(", "self", ",", "data", ",", "ind_col", "=", "None", ",", "dep_col", "=", "None", ",", "project_ind_col", "=", "None", ",", "baseline_period", "=", "[", "None", ",", "None", "]", ",", "projection_period", "=", "None", ",", "exclude_time_period", "=", "None", ",", "alphas", "=", "np", ".", "logspace", "(", "-", "4", ",", "1", ",", "30", ")", ",", "cv", "=", "3", ",", "plot", "=", "True", ",", "figsize", "=", "None", ",", "custom_model_func", "=", "None", ")", ":", "# Check to ensure data is a pandas dataframe", "if", "not", "isinstance", "(", "data", ",", "pd", ".", "DataFrame", ")", ":", "raise", "SystemError", "(", "'data has to be a pandas dataframe.'", ")", "# Create instance", "model_data_obj", "=", "Model_Data", "(", "data", ",", "ind_col", ",", "dep_col", ",", "alphas", ",", "cv", ",", "exclude_time_period", ",", "baseline_period", ",", "projection_period", ")", "# Split data into baseline and projection", "model_data_obj", ".", "split_data", "(", ")", "# Logging", "self", ".", "result", "[", "'Model'", "]", "=", "{", "'Independent Col'", ":", "ind_col", ",", "'Dependent Col'", ":", "dep_col", ",", "'Projection Independent Col'", ":", "project_ind_col", ",", "'Baseline Period'", ":", "baseline_period", ",", "'Projection Period'", ":", "projection_period", ",", "'Exclude Time Period'", ":", "exclude_time_period", ",", "'Alphas'", ":", "list", "(", "alphas", ")", ",", "'CV'", ":", "cv", ",", "'Plot'", ":", "plot", ",", "'Fig Size'", ":", "figsize", "}", "# Runs all models on the data and returns optimal model", "all_metrics", "=", "model_data_obj", ".", "run_models", "(", ")", "self", ".", "result", "[", "'Model'", "]", "[", "'All Model\\'s Metrics'", "]", "=", "all_metrics", "# CHECK: Define custom model's parameter and return types in documentation.", "if", "custom_model_func", ":", "self", ".", "result", "[", "'Model'", "]", "[", "'Custom Model\\'s Metrics'", "]", "=", "model_data_obj", ".", "custom_model", "(", "custom_model_func", ")", "# Fit optimal model to data", "self", ".", "result", "[", "'Model'", "]", "[", "'Optimal Model\\'s Metrics'", "]", "=", "model_data_obj", ".", "best_model_fit", "(", ")", "if", "plot", ":", "# Use project_ind_col if projecting into the future (no input data other than weather data)", "input_col", "=", "model_data_obj", ".", "input_col", "if", "not", "project_ind_col", "else", "project_ind_col", "fig", ",", "y_true", ",", "y_pred", "=", "self", ".", "plot_data_obj", ".", "baseline_projection_plot", "(", "model_data_obj", ".", "y_true", ",", "model_data_obj", ".", "y_pred", ",", "model_data_obj", ".", "baseline_period", ",", "model_data_obj", ".", "projection_period", ",", "model_data_obj", ".", "best_model_name", ",", "model_data_obj", ".", "best_metrics", "[", "'adj_r2'", "]", ",", "model_data_obj", ".", "original_data", ",", "input_col", ",", "model_data_obj", ".", "output_col", ",", "model_data_obj", ".", "best_model", ",", "self", ".", "result", "[", "'Site'", "]", ")", "fig", ".", "savefig", "(", "self", ".", "results_folder_name", "+", "'/baseline_projection_plot-'", "+", "str", "(", "self", ".", "get_global_count", "(", ")", ")", "+", "'.png'", ")", "if", "not", "y_true", ".", "empty", "and", "not", "y_pred", ".", "empty", ":", "saving_absolute", "=", "(", "y_pred", "-", "y_true", ")", ".", "sum", "(", ")", "saving_perc", "=", "(", "saving_absolute", "/", "y_pred", ".", "sum", "(", ")", ")", "*", "100", "self", ".", "result", "[", "'Energy Savings (%)'", "]", "=", "float", "(", "saving_perc", ")", "self", ".", "result", "[", "'Energy Savings (absolute)'", "]", "=", "saving_absolute", "# Temporary", "self", ".", "project_df", "[", "'true'", "]", "=", "y_true", "self", ".", "project_df", "[", "'pred'", "]", "=", "y_pred", "# Calculate uncertainity of savings", "self", ".", "result", "[", "'Uncertainity'", "]", "=", "self", ".", "uncertainity_equation", "(", "model_data_obj", ",", "y_true", ",", "y_pred", ",", "0.9", ")", "else", ":", "print", "(", "'y_true: '", ",", "y_true", ")", "print", "(", "'y_pred: '", ",", "y_pred", ")", "print", "(", "'Error: y_true and y_pred are empty. Default to -1.0 savings.'", ")", "self", ".", "result", "[", "'Energy Savings (%)'", "]", "=", "float", "(", "-", "1.0", ")", "self", ".", "result", "[", "'Energy Savings (absolute)'", "]", "=", "float", "(", "-", "1.0", ")", "return", "self", ".", "best_metrics" ]
Split data into baseline and projection periods, run models on them and display metrics & plots. Parameters ---------- data : pd.DataFrame() Dataframe to model. ind_col : list(str) Independent column(s) of dataframe. Defaults to all columns except the last. dep_col : str Dependent column of dataframe. project_ind_col : list(str) Independent column(s) to use for projection. If none, use ind_col. baseline_period : list(str) List of time periods to split the data into baseline periods. It needs to have a start and an end date. projection_period : list(str) List of time periods to split the data into projection periods. It needs to have a start and an end date. exclude_time_period : list(str) List of time periods to exclude for modeling. alphas : list(int) List of alphas to run regression on. cv : int Number of folds for cross-validation. plot : bool Specifies whether to save plots or not. figsize : tuple Size of the plots. custom_model_func : function Model with specific hyper-parameters provided by user. Returns ------- dict Metrics of the optimal/best model.
[ "Split", "data", "into", "baseline", "and", "projection", "periods", "run", "models", "on", "them", "and", "display", "metrics", "&", "plots", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/apps/data_analysis/XBOS_data_analytics/Wrapper.py#L637-L749
SoftwareDefinedBuildings/XBOS
python/xbos/services/pundat.py
make_dataframe
def make_dataframe(result): """ Turns the results of one of the data API calls into a pandas dataframe """ import pandas as pd ret = {} if isinstance(result,dict): if 'timeseries' in result: result = result['timeseries'] for uuid, data in result.items(): df = pd.DataFrame(data) if len(df.columns) == 5: # statistical data df.columns = ['time','min','mean','max','count'] else: df.columns = ['time','value'] df['time'] = pd.to_datetime(df['time'],unit='ns') df = df.set_index(df.pop('time')) ret[uuid] = df return ret
python
def make_dataframe(result): import pandas as pd ret = {} if isinstance(result,dict): if 'timeseries' in result: result = result['timeseries'] for uuid, data in result.items(): df = pd.DataFrame(data) if len(df.columns) == 5: df.columns = ['time','min','mean','max','count'] else: df.columns = ['time','value'] df['time'] = pd.to_datetime(df['time'],unit='ns') df = df.set_index(df.pop('time')) ret[uuid] = df return ret
[ "def", "make_dataframe", "(", "result", ")", ":", "import", "pandas", "as", "pd", "ret", "=", "{", "}", "if", "isinstance", "(", "result", ",", "dict", ")", ":", "if", "'timeseries'", "in", "result", ":", "result", "=", "result", "[", "'timeseries'", "]", "for", "uuid", ",", "data", "in", "result", ".", "items", "(", ")", ":", "df", "=", "pd", ".", "DataFrame", "(", "data", ")", "if", "len", "(", "df", ".", "columns", ")", "==", "5", ":", "# statistical data", "df", ".", "columns", "=", "[", "'time'", ",", "'min'", ",", "'mean'", ",", "'max'", ",", "'count'", "]", "else", ":", "df", ".", "columns", "=", "[", "'time'", ",", "'value'", "]", "df", "[", "'time'", "]", "=", "pd", ".", "to_datetime", "(", "df", "[", "'time'", "]", ",", "unit", "=", "'ns'", ")", "df", "=", "df", ".", "set_index", "(", "df", ".", "pop", "(", "'time'", ")", ")", "ret", "[", "uuid", "]", "=", "df", "return", "ret" ]
Turns the results of one of the data API calls into a pandas dataframe
[ "Turns", "the", "results", "of", "one", "of", "the", "data", "API", "calls", "into", "a", "pandas", "dataframe" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/python/xbos/services/pundat.py#L247-L265
SoftwareDefinedBuildings/XBOS
python/xbos/services/pundat.py
merge_dfs
def merge_dfs(dfs, resample=None, do_mean=False, do_sum=False, do_min=False, do_max=False): """ dfs is a dictionary of key => dataframe This method resamples each of the dataframes if a period is provided (http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases) """ if len(dfs) == 0: raise Exception("No dataframes provided") df = dfs.values()[0] name = dfs.keys()[0] df.columns = map(lambda x: name+"_"+x if not x.startswith(name) else x, df.columns) if resample is not None: df = df.resample(resample) if do_mean: df = df.mean() elif do_sum: df = df.sum() elif do_min: df = df.min() elif do_max: df = df.max() else: df = df.mean() if len(dfs) > 1: for name, newdf in dfs.items()[1:]: if resample is not None: newdf = newdf.resample(resample) if do_mean: newdf = newdf.mean() elif do_sum: newdf = newdf.sum() elif do_min: newdf = newdf.min() elif do_max: newdf = newdf.max() else: newdf = newdf.mean() newdf.columns = map(lambda x: name+"_"+x if not x.startswith(name) else x, newdf.columns) df = df.merge(newdf, left_index=True, right_index=True, how='outer') return df
python
def merge_dfs(dfs, resample=None, do_mean=False, do_sum=False, do_min=False, do_max=False): if len(dfs) == 0: raise Exception("No dataframes provided") df = dfs.values()[0] name = dfs.keys()[0] df.columns = map(lambda x: name+"_"+x if not x.startswith(name) else x, df.columns) if resample is not None: df = df.resample(resample) if do_mean: df = df.mean() elif do_sum: df = df.sum() elif do_min: df = df.min() elif do_max: df = df.max() else: df = df.mean() if len(dfs) > 1: for name, newdf in dfs.items()[1:]: if resample is not None: newdf = newdf.resample(resample) if do_mean: newdf = newdf.mean() elif do_sum: newdf = newdf.sum() elif do_min: newdf = newdf.min() elif do_max: newdf = newdf.max() else: newdf = newdf.mean() newdf.columns = map(lambda x: name+"_"+x if not x.startswith(name) else x, newdf.columns) df = df.merge(newdf, left_index=True, right_index=True, how='outer') return df
[ "def", "merge_dfs", "(", "dfs", ",", "resample", "=", "None", ",", "do_mean", "=", "False", ",", "do_sum", "=", "False", ",", "do_min", "=", "False", ",", "do_max", "=", "False", ")", ":", "if", "len", "(", "dfs", ")", "==", "0", ":", "raise", "Exception", "(", "\"No dataframes provided\"", ")", "df", "=", "dfs", ".", "values", "(", ")", "[", "0", "]", "name", "=", "dfs", ".", "keys", "(", ")", "[", "0", "]", "df", ".", "columns", "=", "map", "(", "lambda", "x", ":", "name", "+", "\"_\"", "+", "x", "if", "not", "x", ".", "startswith", "(", "name", ")", "else", "x", ",", "df", ".", "columns", ")", "if", "resample", "is", "not", "None", ":", "df", "=", "df", ".", "resample", "(", "resample", ")", "if", "do_mean", ":", "df", "=", "df", ".", "mean", "(", ")", "elif", "do_sum", ":", "df", "=", "df", ".", "sum", "(", ")", "elif", "do_min", ":", "df", "=", "df", ".", "min", "(", ")", "elif", "do_max", ":", "df", "=", "df", ".", "max", "(", ")", "else", ":", "df", "=", "df", ".", "mean", "(", ")", "if", "len", "(", "dfs", ")", ">", "1", ":", "for", "name", ",", "newdf", "in", "dfs", ".", "items", "(", ")", "[", "1", ":", "]", ":", "if", "resample", "is", "not", "None", ":", "newdf", "=", "newdf", ".", "resample", "(", "resample", ")", "if", "do_mean", ":", "newdf", "=", "newdf", ".", "mean", "(", ")", "elif", "do_sum", ":", "newdf", "=", "newdf", ".", "sum", "(", ")", "elif", "do_min", ":", "newdf", "=", "newdf", ".", "min", "(", ")", "elif", "do_max", ":", "newdf", "=", "newdf", ".", "max", "(", ")", "else", ":", "newdf", "=", "newdf", ".", "mean", "(", ")", "newdf", ".", "columns", "=", "map", "(", "lambda", "x", ":", "name", "+", "\"_\"", "+", "x", "if", "not", "x", ".", "startswith", "(", "name", ")", "else", "x", ",", "newdf", ".", "columns", ")", "df", "=", "df", ".", "merge", "(", "newdf", ",", "left_index", "=", "True", ",", "right_index", "=", "True", ",", "how", "=", "'outer'", ")", "return", "df" ]
dfs is a dictionary of key => dataframe This method resamples each of the dataframes if a period is provided (http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases)
[ "dfs", "is", "a", "dictionary", "of", "key", "=", ">", "dataframe", "This", "method", "resamples", "each", "of", "the", "dataframes", "if", "a", "period", "is", "provided", "(", "http", ":", "//", "pandas", ".", "pydata", ".", "org", "/", "pandas", "-", "docs", "/", "stable", "/", "timeseries", ".", "html#offset", "-", "aliases", ")" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/python/xbos/services/pundat.py#L267-L296
SoftwareDefinedBuildings/XBOS
python/xbos/services/pundat.py
DataClient.query
def query(self, query, archiver="", timeout=DEFAULT_TIMEOUT): """ Runs the given pundat query and returns the results as a Python object. Arguments: [query]: the query string [archiver]: if specified, this is the archiver to use. Else, it will run on the first archiver passed into the constructor for the client [timeout]: time in seconds to wait for a response from the archiver """ if archiver == "": archiver = self.archivers[0] nonce = random.randint(0, 2**32) ev = threading.Event() response = {} def _handleresult(msg): # decode, throw away if not correct nonce got_response = False error = getError(nonce, msg) if error is not None: got_response = True response["error"] = error metadata = getMetadata(nonce, msg) if metadata is not None: got_response = True response["metadata"] = metadata timeseries = getTimeseries(nonce, msg) if timeseries is not None: got_response = True response["timeseries"] = timeseries if got_response: ev.set() vk = self.vk[:-1] # remove last part of VK because archiver doesn't expect it # set up receiving self.c.subscribe("{0}/s.giles/_/i.archiver/signal/{1},queries".format(archiver, vk), _handleresult) # execute query q_struct = msgpack.packb({"Query": query, "Nonce": nonce}) po = PayloadObject((2,0,8,1), None, q_struct) self.c.publish("{0}/s.giles/_/i.archiver/slot/query".format(archiver), payload_objects=(po,)) ev.wait(timeout) if len(response) == 0: # no results raise TimeoutException("Query of {0} timed out".format(query)) return response
python
def query(self, query, archiver="", timeout=DEFAULT_TIMEOUT): if archiver == "": archiver = self.archivers[0] nonce = random.randint(0, 2**32) ev = threading.Event() response = {} def _handleresult(msg): got_response = False error = getError(nonce, msg) if error is not None: got_response = True response["error"] = error metadata = getMetadata(nonce, msg) if metadata is not None: got_response = True response["metadata"] = metadata timeseries = getTimeseries(nonce, msg) if timeseries is not None: got_response = True response["timeseries"] = timeseries if got_response: ev.set() vk = self.vk[:-1] self.c.subscribe("{0}/s.giles/_/i.archiver/signal/{1},queries".format(archiver, vk), _handleresult) q_struct = msgpack.packb({"Query": query, "Nonce": nonce}) po = PayloadObject((2,0,8,1), None, q_struct) self.c.publish("{0}/s.giles/_/i.archiver/slot/query".format(archiver), payload_objects=(po,)) ev.wait(timeout) if len(response) == 0: raise TimeoutException("Query of {0} timed out".format(query)) return response
[ "def", "query", "(", "self", ",", "query", ",", "archiver", "=", "\"\"", ",", "timeout", "=", "DEFAULT_TIMEOUT", ")", ":", "if", "archiver", "==", "\"\"", ":", "archiver", "=", "self", ".", "archivers", "[", "0", "]", "nonce", "=", "random", ".", "randint", "(", "0", ",", "2", "**", "32", ")", "ev", "=", "threading", ".", "Event", "(", ")", "response", "=", "{", "}", "def", "_handleresult", "(", "msg", ")", ":", "# decode, throw away if not correct nonce", "got_response", "=", "False", "error", "=", "getError", "(", "nonce", ",", "msg", ")", "if", "error", "is", "not", "None", ":", "got_response", "=", "True", "response", "[", "\"error\"", "]", "=", "error", "metadata", "=", "getMetadata", "(", "nonce", ",", "msg", ")", "if", "metadata", "is", "not", "None", ":", "got_response", "=", "True", "response", "[", "\"metadata\"", "]", "=", "metadata", "timeseries", "=", "getTimeseries", "(", "nonce", ",", "msg", ")", "if", "timeseries", "is", "not", "None", ":", "got_response", "=", "True", "response", "[", "\"timeseries\"", "]", "=", "timeseries", "if", "got_response", ":", "ev", ".", "set", "(", ")", "vk", "=", "self", ".", "vk", "[", ":", "-", "1", "]", "# remove last part of VK because archiver doesn't expect it", "# set up receiving", "self", ".", "c", ".", "subscribe", "(", "\"{0}/s.giles/_/i.archiver/signal/{1},queries\"", ".", "format", "(", "archiver", ",", "vk", ")", ",", "_handleresult", ")", "# execute query", "q_struct", "=", "msgpack", ".", "packb", "(", "{", "\"Query\"", ":", "query", ",", "\"Nonce\"", ":", "nonce", "}", ")", "po", "=", "PayloadObject", "(", "(", "2", ",", "0", ",", "8", ",", "1", ")", ",", "None", ",", "q_struct", ")", "self", ".", "c", ".", "publish", "(", "\"{0}/s.giles/_/i.archiver/slot/query\"", ".", "format", "(", "archiver", ")", ",", "payload_objects", "=", "(", "po", ",", ")", ")", "ev", ".", "wait", "(", "timeout", ")", "if", "len", "(", "response", ")", "==", "0", ":", "# no results", "raise", "TimeoutException", "(", "\"Query of {0} timed out\"", ".", "format", "(", "query", ")", ")", "return", "response" ]
Runs the given pundat query and returns the results as a Python object. Arguments: [query]: the query string [archiver]: if specified, this is the archiver to use. Else, it will run on the first archiver passed into the constructor for the client [timeout]: time in seconds to wait for a response from the archiver
[ "Runs", "the", "given", "pundat", "query", "and", "returns", "the", "results", "as", "a", "Python", "object", "." ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/python/xbos/services/pundat.py#L61-L111
SoftwareDefinedBuildings/XBOS
python/xbos/services/pundat.py
DataClient.uuids
def uuids(self, where, archiver="", timeout=DEFAULT_TIMEOUT): """ Using the given where-clause, finds all UUIDs that match Arguments: [where]: the where clause (e.g. 'path like "keti"', 'SourceName = "TED Main"') [archiver]: if specified, this is the archiver to use. Else, it will run on the first archiver passed into the constructor for the client [timeout]: time in seconds to wait for a response from the archiver """ resp = self.query("select uuid where {0}".format(where), archiver, timeout) uuids = [] for r in resp["metadata"]: uuids.append(r["uuid"]) return uuids
python
def uuids(self, where, archiver="", timeout=DEFAULT_TIMEOUT): resp = self.query("select uuid where {0}".format(where), archiver, timeout) uuids = [] for r in resp["metadata"]: uuids.append(r["uuid"]) return uuids
[ "def", "uuids", "(", "self", ",", "where", ",", "archiver", "=", "\"\"", ",", "timeout", "=", "DEFAULT_TIMEOUT", ")", ":", "resp", "=", "self", ".", "query", "(", "\"select uuid where {0}\"", ".", "format", "(", "where", ")", ",", "archiver", ",", "timeout", ")", "uuids", "=", "[", "]", "for", "r", "in", "resp", "[", "\"metadata\"", "]", ":", "uuids", ".", "append", "(", "r", "[", "\"uuid\"", "]", ")", "return", "uuids" ]
Using the given where-clause, finds all UUIDs that match Arguments: [where]: the where clause (e.g. 'path like "keti"', 'SourceName = "TED Main"') [archiver]: if specified, this is the archiver to use. Else, it will run on the first archiver passed into the constructor for the client [timeout]: time in seconds to wait for a response from the archiver
[ "Using", "the", "given", "where", "-", "clause", "finds", "all", "UUIDs", "that", "match" ]
train
https://github.com/SoftwareDefinedBuildings/XBOS/blob/c12d4fb14518ea3ae98c471c28e0710fdf74dd25/python/xbos/services/pundat.py#L113-L127