Skip to content

Commit

Permalink
feat/more analysis (#7)
Browse files Browse the repository at this point in the history
* chore: 📝 add data tome analysis header to library json

#3

* feat: 🧑‍💻 add size_of_memory method

add size_of_memory method that returns the memory usage of the array

#6

* fix(DataTomeAnalysis): 🚑 fix std method

fix std method to divide by the point_count, not by the reserved size

* test(DataTomeMvAvg): ✅ add test to memory size and partials memory size

* fix(DataTomeMvAvg): 🚑 case when array is resized to a smaller size

* refactor(DataTomeMvAvg): 🔒 change resize to grow method

Change resize to only allow the array to get bigger, avoiding data inconsistencies among other features and future additions. Also, shrinking the array implies deleting data as a non-clear side effect.

* fix(DataTomeAnalysis): 🚑 change partial_std and partial_var to divide by partial_point_count, not by the allocated size

* feat(DataTomeAnalysis): ✨ add min and max methods

Add methods to return min and max values currently stored in the array, also works on partials.

#6

* docs: 📝 improve readme readability

Added ## Getting Started list to make install links easier to find and understand.

* docs: 📝 add github repo url on readme

* docs: 📝 make docs github repo url more clear

* feat(DataTomeAnalysis): ✨ add median

Add median and partial_median methods.

#6

* chore: 🔧 allow extensions.json and settings.json on commits

Allow extensions.json and settings.json on commits to a more standard dev experience among contributors.
  • Loading branch information
AlexandreHiroyuki committed Apr 8, 2024
1 parent f8cc588 commit 7132181
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 37 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
.DS_Store

# Visual Studio project configurations
.vscode
.vscode/*
/.vs

!.vscode/settings.json
!.vscode/extensions.json

# Compilation output
/out
bin/
Expand Down
11 changes: 11 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide",
"vivaxy.vscode-conventional-commits"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"files.associations": {
"array": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"utility": "cpp"
},
"conventionalCommits.scopes": [
"DataTomeAnalysis",
"DataTomeMvAvg"
]
}
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@

Data Tome is a C++ library for data analysis and data filtering on IoT devices. Focus on the developer's experience and performance.

This library is listed in the official [Arduino Library Manager](https://www.arduino.cc/reference/en/libraries/datatome/).
## Getting Started

_**—Recommended Platform—**_ Use the [PlatformIO Registry](https://registry.platformio.org/libraries/alexandrehiroyuki/DataTome) to install the library!

To avoid duplicating data for smaller samples of the same data, _[check the Partials feature](https://alexandrehiroyuki.github.io/DataTomeDocs/docs/category/partials)_!
- This library is listed in the official [Arduino Library Manager](https://www.arduino.cc/reference/en/libraries/datatome/).
- _**—Recommended Platform—**_ Use the [PlatformIO Registry](https://registry.platformio.org/libraries/alexandrehiroyuki/DataTome) to install the library!
- You can see the source code at the [GitHub Repository](https://github.com/AlexandreHiroyuki/DataTome).
- To avoid duplicating data for smaller samples of the same data, _[check the Partials feature](https://alexandrehiroyuki.github.io/DataTomeDocs/docs/category/partials)_!

## Documentation

This library calculates statistical functions using a time-series sample implemented with a circular array that improves the performance.

> Online Docs: _[Click here to see the full documentation](https://alexandrehiroyuki.github.io/DataTomeDocs/)_
>
> GitHub Repository: _[The documentation is coded with docusaurus, and you can visit the repository](https://github.com/AlexandreHiroyuki/DataTomeDocs)_
> Docs GitHub Repository: _[The documentation is coded with docusaurus, and you can visit the repository](https://github.com/AlexandreHiroyuki/DataTomeDocs)_
## Contributing

Expand Down
5 changes: 3 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "DataTome",
"version": "1.0.1",
"version": "1.4.3",
"description": "Data Tome is a C++ library for data analysis and data filtering on IoT devices. Focus on the developer's experience and performance.",
"keywords": "sensors, input, data, processing, analysis, arduino, library, filter, moving average, smooth, standard, deviation, mean, partial, tome, datatome",
"repository": {
Expand All @@ -22,7 +22,8 @@
"platforms": "*",
"headers": [
"DataTome.h",
"DataTomeMvAvg.h"
"DataTomeMvAvg.h",
"DataTomeAnalysis.h"
],
"examples": [
{
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DataTome
version=1.0.1
version=1.4.3
author=Alexandre Hiroyuki Yamauchi <[email protected]>
maintainer=Alexandre Hiroyuki Yamauchi <[email protected]>
sentence=Moving Average and other statistical functions compatible with any number type.
Expand Down
113 changes: 109 additions & 4 deletions src/DataTomeAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,64 @@

#include <DataTomeMvAvg.h>

#include <algorithm>
#include <cmath>
#include <cstdlib>

template <typename TypeOfArray, typename TypeOfSum = TypeOfArray>
class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
private:
public:
DataTomeAnalysis(size_t size) : DataTomeMvAvg<TypeOfArray, TypeOfSum>(size) {}

/** Aliases **/

TypeOfArray mean() { return this->get(); }
size_t count() { return this->point_count(); }

/** Proper Methods **/

TypeOfArray min() {
TypeOfArray min = (*this)[0];
for (size_t i = 1; i < this->point_count(); i++) {
if ((*this)[i] < min) min = (*this)[i];
}

return min;
}

TypeOfArray max() {
TypeOfArray max = (*this)[0];
for (size_t i = 1; i < this->point_count(); i++) {
if ((*this)[i] > max) max = (*this)[i];
}

return max;
}

TypeOfArray median() {
TypeOfArray median = 0;
size_t current_size = this->point_count();

TypeOfArray *temp =
(typeof(temp))malloc(current_size * sizeof(typeof(temp)));

for (size_t i = 0; i < current_size; i++) {
temp[i] = this->at_index(i);
}

std::sort(temp, temp + current_size);

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
} else {
median = temp[current_size / 2];
}

free(temp);

return median;
}

TypeOfArray var() {
TypeOfArray average = this->get();

Expand All @@ -29,21 +78,77 @@ class DataTomeAnalysis : public DataTomeMvAvg<TypeOfArray, TypeOfSum> {
return var;
}

TypeOfArray std() { return sqrt(var() / this->size()); }
TypeOfArray std() { return sqrt(var() / this->point_count()); }

/** Partial Methods **/

/** Aliases **/

TypeOfArray partial_mean(size_t partial_id) {
return this->partial_get(partial_id);
}
size_t partial_count(size_t partial_id) {
return this->partial_size(partial_id);
}

/** Proper Methods **/

TypeOfArray partial_min(size_t partial_id) {
TypeOfArray min = (*this)[0];
for (size_t i = 1; i < this->partial_point_count(partial_id); i++) {
if ((*this)[i] < min) min = (*this)[i];
}

return min;
}

TypeOfArray partial_max(size_t partial_id) {
TypeOfArray max = (*this)[0];
for (size_t i = 1; i < this->partial_point_count(partial_id); i++) {
if ((*this)[i] > max) max = (*this)[i];
}

return max;
}

TypeOfArray partial_median(size_t partial_id) {
TypeOfArray median = 0;
size_t current_size = this->partial_point_count(partial_id);

TypeOfArray *temp =
(typeof(temp))malloc(current_size * sizeof(typeof(temp)));

for (size_t i = 0; i < current_size; i++) {
temp[i] = (*this)[i];
}

std::sort(temp, temp + current_size);

if (current_size % 2 == 0) {
median = (temp[current_size / 2 - 1] + temp[current_size / 2]) / 2;
} else {
median = temp[current_size / 2];
}

free(temp);

return median;
}

TypeOfArray partial_var(size_t partial_id) {
TypeOfArray average = this->partial_get(partial_id);

TypeOfArray var = 0;
for (size_t i = 0; i < this->partial_size(partial_id); i++) {
for (size_t i = 0; i < this->partial_point_count(partial_id); i++) {
var += ((*this)[i] - average) * ((*this)[i] - average);
}

return var;
}

TypeOfArray partial_std(size_t partial_id) {
return sqrt(partial_var(partial_id) / this->partial_size(partial_id));
return sqrt(partial_var(partial_id) /
this->partial_point_count(partial_id));
}
};

Expand Down
47 changes: 31 additions & 16 deletions src/DataTomeMvAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DataTomeMvAvg {
}

public:
// Constructor
/** Constructor **/
DataTomeMvAvg(size_t size)
: _array_size(size),
_current_index(0),
Expand All @@ -39,14 +39,14 @@ class DataTomeMvAvg {
_partial_sums((TypeOfSum *)calloc(1, sizeof(TypeOfSum))),
_partial_sum_sizes((size_t *)calloc(1, sizeof(size_t))) {}

// Destructor
/** Destructor **/
~DataTomeMvAvg() {
free(_array);
free(_partial_sums);
free(_partial_sum_sizes);
}

// Get Result and Access elements
/** Get Result and Access elements **/

DataTomeMvAvg<TypeOfArray, TypeOfSum> &push(TypeOfArray input) {
TypeOfArray last_value = _array[_current_index];
Expand All @@ -71,7 +71,10 @@ class DataTomeMvAvg {
TypeOfArray get() {
return (_average_sum / ((_average_counter == 0) ? 1 : _average_counter));
}
TypeOfArray get(size_t n_points) { return get_by_brute(n_points); }
TypeOfArray get(size_t n_points) {
// @alias get_by_brute
return get_by_brute(n_points);
}

TypeOfArray get_by_brute(size_t n_points) {
if (n_points > _average_counter) n_points = _average_counter;
Expand Down Expand Up @@ -117,29 +120,33 @@ class DataTomeMvAvg {

TypeOfArray at_index(size_t index) { return _array[index]; }

size_t size() { return _array_size; }
size_t size_of_array() { return _array_size; }

size_t size() { return size_of_array(); }

size_t size_of_memory() { return _array_size * sizeof(TypeOfArray); }

size_t point_count() { return _average_counter; }

// Modify array
/** Modify array **/

DataTomeMvAvg<TypeOfArray, TypeOfSum> &resize(size_t new_size) {
if (new_size == _array_size) return *this;
DataTomeMvAvg<TypeOfArray, TypeOfSum> &grow(size_t new_size) {
if (new_size <= _array_size) return *this;

_current_index = new_size - 1;

_array = static_cast<TypeOfArray *>(
realloc(_array, new_size * sizeof(TypeOfArray)));

for (size_t i = _array_size; i < new_size; i++) _array[i] = 0;

if (new_size > _array_size) {
_current_index = new_size - 1;
} else {
if (_current_index >= new_size) _current_index = new_size - 1;
}

_array_size = new_size;
return *this;
}
DataTomeMvAvg<TypeOfArray, TypeOfSum> &resize(size_t new_size) {
// @alias grow
return grow(new_size);
}

DataTomeMvAvg<TypeOfArray, TypeOfSum> &clear() {
memset(_array, 0, sizeof(TypeOfArray) * _array_size);
Expand All @@ -153,7 +160,7 @@ class DataTomeMvAvg {
return *this;
}

// Partial Average methods
/** Partial Average methods **/

size_t partial_create(size_t sum_size) {
if (sum_size > _array_size) {
Expand Down Expand Up @@ -191,12 +198,20 @@ class DataTomeMvAvg {
((_average_counter == 0) ? 1 : _average_counter);
}

size_t partial_size(size_t id) {
size_t partial_size_of_array(size_t id) {
if (id > _partial_sums_counter) return 0;

return _partial_sum_sizes[id];
}

size_t partial_size(size_t id) { return partial_size_of_array(id); }

size_t partial_size_of_memory(size_t id) {
if (id > _partial_sums_counter) return 0;

return _partial_sum_sizes[id] * sizeof(TypeOfArray);
}

size_t partial_point_count(size_t id) {
if (id > _partial_sums_counter) return 0;

Expand Down
Loading

0 comments on commit 7132181

Please sign in to comment.