Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace creation examples added #12

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The following types are supported, but serialize to json-compatible types destru
| `datetime.datetime` | `str` | `time(12, 0, 0)` | `"12:00:00"` | |
| `datetime.date` | `str` | `date(2021, 1, 1)` | `"2021-01-01"` | |
| `datetime.time` | `str` | `datetime(2021, 1, 1)` | `"2021-01-01T00:00:00+00:00"` | [RFC 3339](https://tools.ietf.org/html/rfc3339) format, compatible with `isoformat()` |
| `datetime.timedelta` | `float` | `timedelta(days=1, milliseconds=1)` | `86400.001` | |
| `datetime.timedelta` | `float` | `timedelta(days=1, milliseconds=1)` | `86400.001` | Total seconds |
| `np.array` | `list` | `np.array([1, 2, 3])` | `[1, 2, 3]` | |
| `set` | `list` | `{"a", "b", "c"}` | `["a", "b", "c"]` | |
| `tuple` | `list` | `(1, 2, 3)` | `[1, 2, 3]` | |
Expand Down
173 changes: 173 additions & 0 deletions docs/examples/creating_traces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
The input of data into the system is only restricted to the types on the [Conversions Page](conversions.md), how the data is structured is left mostly up to the user.

Data relating to individuals and activities is often temporal, and falls into some standard patterns outlined below.

## Initialization

Let's create a client, subject and activity to work with:

```py title="Initialization"
import watz

client = watz.Client(secret="my api key")

client.subject_create([
watz.models.NewSubject(email="foo@bar.com"),
])

activities = client.activity_create([
watz.models.NewActivity(subject_uid="foo@bar.com")
])

act_uid = activities[0].uid
```

## Static Data

Some data is static, meaning it is not recorded over time.

Examples include:

- A subject's date of birth
- A subject's height
- Calories burnt during an activitiy
- Information about sensors recording during an activity

The data can be structured arbitrarily, some examples are shown below:

```py title="Static Data"
import datetime as dt

client.trace_create([

# <-- Subject traces

watz.models.NewTrace(
parent_uid="foo@bar.com",
name="profile",
data={
"name": "foo bar",
"dob": dt.date(1990, 1, 1),
"height": {
"value": 180,
"unit": "cm",
},
"gender": "m",
},
),

# <-- Activity traces

watz.models.NewTrace(
parent_uid=act_uid,
name="calories",
data=678,
),
watz.models.NewTrace(
parent_uid=act_uid,
name="sensors",
data=[
{
"name": "accelerometer",
"id": "123",
},
{
"name": "gyroscope",
"id": "456",
}],
),
])
```

## Temporal Data
A lot of data is temporal, meaning it is recorded over time, where each recording is associated with a timestamp.

Examples include:

- A subject's weight
- A subject's resting heart rate
- Heart rate or power output during an activity
- GPS coordinates during an activity

The data can be structured arbitrarily, some examples are shown below:

```py title="Temporal Data"
import datetime as dt

client.trace_create([

# <-- Subject traces

watz.models.NewTrace(
parent_uid="foo@bar.com",
name="weight",
data={
"unit": "kg",
"values": [
(65.3, dt.datetime(2021, 1, 1)),
(65.2, dt.datetime(2021, 1, 2)),
],
},
),
watz.models.NewTrace(
parent_uid="foo@bar.com",
name="resting_heart_rate",
data={
"unit": "bpm",
"values": [65, 64],
"timestamps": [
dt.datetime(2021, 1, 1),
dt.datetime(2021, 1, 2),
],
},
),

# <-- Activity traces

# If attributes share a temporal trace, they could be stored together:
watz.models.NewTrace(
parent_uid=act_uid,
name="hr/power",
data={
"unit": "bpm",
"hr": [102, 110],
"power": [200, 210],
"timestamps": [
dt.datetime(2021, 1, 1),
dt.datetime(2021, 1, 2),
],
},
),

# Alternatively, traces could reference a shared temporal trace:
watz.models.NewTrace(
parent_uid=act_uid,
name="coord_ts",
data=[
dt.datetime(2021, 1, 1),
dt.datetime(2021, 1, 2),
]
),
watz.models.NewTrace(
parent_uid=act_uid,
name="lat",
data={
"values": [1.234, 1.235],
"ts_trace_name": "coord_ts",
},
),
watz.models.NewTrace(
parent_uid=act_uid,
name="long",
data={
"values": [1.234, 1.235],
"ts_trace_name": "coord_ts",
},
),
])
```
!!! note
`datetime.datetime` is used here for simplicity, using pre-serialized timestamps would make no difference.

!!! note
Support for updating & deleting existing traces coming soon.
1 change: 1 addition & 0 deletions docs/examples/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Creating Traces](creating_traces.md)
Loading