{"id":28702212,"url":"https://github.com/baeda-polito/contextual-anomaly-detector","last_synced_at":"2025-07-07T14:33:36.108Z","repository":{"id":243709577,"uuid":"364220918","full_name":"baeda-polito/contextual-anomaly-detector","owner":"baeda-polito","description":"Contextual anomaly detection tool application in building energy field based on Matrix Profile algorithm","archived":false,"fork":false,"pushed_at":"2025-06-30T13:51:42.000Z","size":2095493,"stargazers_count":5,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-30T14:42:36.294Z","etag":null,"topics":["data-analytics","docker","energy","energy-consumption","matrix-profile","python"],"latest_commit_sha":null,"homepage":"https://www.sciencedirect.com/science/article/pii/S037877882200473X?casa_token=ingifeiAbY8AAAAA:Pz8XhBoZk1Pfm86yOy6X74hgccIJIVRzgUF_yxCa0Wu2u0keylRm61i37HHxYo87SiWcIuRFoA","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baeda-polito.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-05-04T10:36:34.000Z","updated_at":"2025-06-30T13:51:45.000Z","dependencies_parsed_at":"2024-06-10T20:31:25.684Z","dependency_job_id":"7da57286-c83d-4b8e-b72a-1430e43c3f09","html_url":"https://github.com/baeda-polito/contextual-anomaly-detector","commit_stats":null,"previous_names":["baeda-polito/matrix-profile","baeda-polito/contextual-anomaly-detector"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/baeda-polito/contextual-anomaly-detector","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baeda-polito%2Fcontextual-anomaly-detector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baeda-polito%2Fcontextual-anomaly-detector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baeda-polito%2Fcontextual-anomaly-detector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baeda-polito%2Fcontextual-anomaly-detector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baeda-polito","download_url":"https://codeload.github.com/baeda-polito/contextual-anomaly-detector/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baeda-polito%2Fcontextual-anomaly-detector/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264092226,"owners_count":23556259,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-analytics","docker","energy","energy-consumption","matrix-profile","python"],"created_at":"2025-06-14T12:11:01.188Z","updated_at":"2025-07-07T14:33:36.084Z","avatar_url":"https://github.com/baeda-polito.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Contextual Matrix Profile Calculation Tool\n\nMatrix Profile is an algorithm capable to discover motifs and discords in time series data. It is a powerful tool that\nby calculating the (z-normalized) Euclidean distance between any subsequence within a time series and its nearest\nneighbor it is able to provide insights on potential anomalies and/or repetitive patterns. In the field of building\nenergy management it can be employed to detect anomalies in electrical load timeseries.\n\nThis tool is a Python implementation of the Matrix Profile algorithm that employs contextual information (such as\nexternal air temperature) to identify abnormal pattens in electrical load subsequences that start in predefined sub\ndaily time windows, as shown in the following figure.\n\n![](./docs/example.png)\n\n**Table of Contents**\n\n* [Usage](#usage)\n    * [Data format](#data-format)\n    * [Run locally](#run-locally)\n    * [Run with Docker](#run-with-docker)\n* [Additional Information](#additional-information)\n* [Cite](#cite)\n* [Contributors](#contributors)\n* [License](#license)\n\n## Usage\n\nThe tool comes with a CLI that helps you to execute the script with the desired commands\n\n```console \n$ python -m src.cmp.main -h\n\nMatrix profile\n\npositional arguments:\n  input_file     Path to file\n  variable_name  Variable name\n  output_file    Path to the output file\n\noptions:\n  -h, --help     show this help message and exit\n  -country        Country code (ex: IT, US, ...)\n```\n\nThe arguments to pass to the script are the following:\n\n* `input_file`: The input dataset via an HTTP URL. The tool should then download the dataset from that URL; since it's a\n  pre-signed URL, the tool would not need to deal with authentication—it can just download the dataset directly.\n* `variable_name`: The variable name to be used for the analysis (i.e., the column of the csv that contains the\n  electrical load under analysis).\n* `output_file`: The local path to the output HTML report. The platform would then get that HTML report and upload it to\n  the object storage service for the user to review later.\n* `country`: The country code of the location where the building is located. This is used to get the holidays for that\n  country.\n\nYou can run the main script through the console using either local files or download data from an external url. This\nrepository comes with a sample dataset ([data.csv](.src/cmp/data/data.csv)) that you can use to generate a report and\nyou can pass the local path\nas `input_file` argument as follows:\n\n### Data format\n\nThe tool requires the user to provide a csv file as input that contains electrical power timeseries for a specific\nbuilding, meter or energy system (e.g., whole building electrical power timeseries). The `csv` is a wide table format as\nfollows:\n\n```csv\ntimestamp,column_1,temp\n2019-01-01 00:00:00,116.4,-0.6\n2019-01-01 00:15:00,125.6,-0.9\n2019-01-01 00:30:00,119.2,-1.2\n```\n\nThe csv must have the following columns:\n\n- `timestamp` [case sensitive]: The timestamp of the observation in the format `YYYY-MM-DD HH:MM:SS`. This column is\n  supposed to be in\n  UTC timezone string format. It will be internally transformed by the tool into the index of the dataframe.\n- `temp` [case sensitive]: Contains the external air temperature in Celsius degrees. This column is required to perform\n  thermal sensitive\n  analysis on the electrical load.\n- `column_1`: Then the dataframe may have `N` arbitrary columns that refers to electrical load time series. The user has\n  to specify the column name that refers to the electrical load time series in the `variable_name` argument.\n\n### Run locally\n\nCreate virtual environment and activate it and install dependencies:\n\n- Makefile\n  ```bash\n  make setup\n  ```\n\n- Linux:\n  ```bash\n  python3 -m venv .venv\n  source .venv/bin/activate\n  pip install poetry\n  poetry install\n  ```\n- Windows:\n  ```bash\n  python -m venv venv\n  venv\\Scripts\\activate\n  pip install poetry\n  poetry install\n  ```\n\nNow you can run the script from the console by passing the desired arguments. In the following we pass the sample\ndataset [`data.csv`](src/cmp/data/data.csv) as input file and the variable `Total_Power` as the variable name to be used\nfor the analysis. The output file will be saved in the [`results`](src/cmp/results) folder.\n\n```console\n$ python -m src.cmp.main src/cmp/data/data.csv Total_Power src/cmp/results/reports/report.html\n\n2024-08-13 12:45:42,821 [INFO](src.cmp.utils) ⬇️ Downloading file from \u003csrc/cmp/data/data.csv\u003e\n2024-08-13 12:45:43,070 [INFO](src.cmp.utils) 📊 Data processed successfully\n\n*********************\nCONTEXT 1 : Subsequences of 05:45 h (m = 23) that start in [00:00,01:00) (ctx_from00_00_to01_00_m05_45)\n99.997%        0.0 sec\n\n- Cluster 1 (1.660 s)   -\u003e 1 anomalies\n- Cluster 2 (0.372 s)   -\u003e 3 anomalies\n- Cluster 3 (0.389 s)   -\u003e 4 anomalies\n- Cluster 4 (0.593 s)   -\u003e 5 anomalies\n- Cluster 5 (-)         -\u003e no anomalies green\n\n[...]\n\n2024-08-13 12:46:27,187 [INFO](__main__) TOTAL 0 min 44 s\n2024-08-13 12:46:32,349 [INFO](src.cmp.utils) 🎉 Report generated successfully on src/cmp/results/reports/report.html\n\n```\n\nAt the end of the execution you can find the report in the path specified by the `output_file` argument, in this case\nyou will find it in the [`results`](src/cmp/results) folder.\n\n### Run with Docker\n\nBuild the docker image.\n\n- Makefile\n  ```bash\n  make docker-build\n  ```\n- Linux:\n  ```bash\n  docker build -t cmp .\n  ```\n\nRun the docker image with the same arguments as before\n\n- Makefile\n  ```bash\n  make docker-run\n  ```\n- Linux:\n  ```bash\n  docker run cmp data/data.csv Total_Power results/reports/report.html\n  ```\n\nAt the end of the execution you can find the results in the [`results`](src/cmp/results) folder inside the docker\ncontainer.\n\n## Cite\n\nYou can cite this work by using the following reference or either though [this Bibtex file](./docs/ref.bib) or the\nfollowing plain text citation\n\n\u003e Chiosa, Roberto, et al. \"Towards a self-tuned data analytics-based process for an automatic context-aware detection\n\u003e and\n\u003e diagnosis of anomalies in building energy consumption timeseries.\" Energy and Buildings 270 (2022): 112302.\n\n## Contributors\n\n- Author [Roberto Chiosa](https://github.com/RobertoChiosa)\n- Contributor [Rocco Giudice](https://github.com/Giudice7)\n- Contributor [Vincenzo Viggiano]()\n\n## References\n\n- Series Distance Matrix repository (https://github.com/predict-idlab/seriesdistancematrix)\n- Stumpy Package (https://stumpy.readthedocs.io/en/latest/)\n\n## License\n\nThis code is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaeda-polito%2Fcontextual-anomaly-detector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaeda-polito%2Fcontextual-anomaly-detector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaeda-polito%2Fcontextual-anomaly-detector/lists"}