Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
edeng23 committed Aug 11, 2024
1 parent 892e399 commit 233b41e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,70 @@
# HeartSync
Integrate heart rate data from Apple Health with your Google Calendar events. Analyze and visualize the correlation between your daily activities and heart rate, and discover which events made your heart race

<img src="example.gif" alt="HeartSync in Action" width="600"/>

## Overview

This project analyzes heart rate data from Apple Health XML exports and correlates it with events from your Google Calendar.

## Installation

### Prerequisites

- Python 3.7 or higher
- A Google Cloud project with access to the Google Calendar API
- Apple Health XML data export

### Setup

1. Clone the repository:

```bash
git clone [email protected]:edeng23/HeartSync.git
cd HeartSync
```

2. Install the required Python packages:

```bash
pip install -r requirements.txt
```

3. Set up Google Calendar API credentials:
- Create a project on the [Google Cloud Console](https://console.cloud.google.com/).
- Enable the Google Calendar API for your project.
- Download the `credentials.json` file and save it in the project directory.

4. Export Apple Watch data:
- **Export Health Data from the iPhone:**
1. Open the **Health** app on your iPhone.
2. Tap on your profile picture in the top right corner to access your health profile.
3. Scroll down and tap on **Export All Health Data**.
4. A confirmation dialog will appear. Tap **Export** to confirm.
5. The Health app will prepare your data, which might take a few minutes.
6. Once the data is ready, you'll be given the option to save it to a location of your choice. Save the file (it will be named `export.zip`) to a convenient location.
- **Unzip the Exported Data:**
1. Locate the `export.zip` file on your computer and unzip it.
2. Inside the unzipped folder, you'll find an XML file named `export.xml`. This file contains all your health data, including heart rate data.
- **Move the `export.xml` File:**
1. Copy the `export.xml` file to the directory where your script is located.

## Usage

1. Ensure you have your Apple Health XML export file and your `credentials.json` file in the project directory.

2. Run the script with the following command:

```bash
python heartsync.py <export_file_path> <google_credentials_path>
```

- `<export_file_path>`: Path to your Apple Health XML export file.
- `<google_credentials_path>`: Path to your Google API `credentials.json` file.

Example:

```bash
python heartsync.py export.xml credentials.json
```

3. The script will authenticate with Google Calendar, retrieve events, and correlate them with the heart rate data. The results will be displayed in your terminal.
Binary file added example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 2 additions & 6 deletions heartsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def extract_heart_rate_data(file_path):


def get_calendar_events(service):
now = datetime.datetime.utcnow().isoformat() + "Z" # 'Z' indicates UTC time
now = datetime.datetime.utcnow().isoformat() + "Z"
one_year_ago = (
datetime.datetime.utcnow() - datetime.timedelta(days=365)
).isoformat() + "Z"
Expand All @@ -83,7 +83,7 @@ def get_calendar_events(service):
timeMax=now,
singleEvents=True,
orderBy="startTime",
maxResults=250, # Adjust this number if you want to try smaller batches
maxResults=250,
pageToken=page_token,
)
.execute()
Expand All @@ -102,7 +102,6 @@ def get_calendar_events(service):
start_dt = pd.to_datetime(start)
end_dt = pd.to_datetime(end)

# Localize or convert to UTC as necessary
if start_dt.tzinfo is None:
start_dt = start_dt.tz_localize("UTC")
else:
Expand All @@ -124,9 +123,7 @@ def safe_float(num, default=float("-inf")):
return num if num is not None else default


# Main execution block:
if __name__ == "__main__":
# Parsing arguments and initializing data extraction
parser = argparse.ArgumentParser()
parser.add_argument("export_file_path", help="Path to the exported XML file")
parser.add_argument(
Expand All @@ -137,7 +134,6 @@ def safe_float(num, default=float("-inf")):
service = authenticate_google_calendar(args.google_credentials_path)
event_list = get_calendar_events(service)

# Processing events and heart rate matches
for event in event_list:
mask = (df["time"] >= event["start"]) & (df["time"] <= event["end"])
matching_records = df.loc[mask, "value"].astype(float)
Expand Down

0 comments on commit 233b41e

Please sign in to comment.