Skip to content

Commit

Permalink
Update example code for refactor (#80)
Browse files Browse the repository at this point in the history
* Update examples so they work

* Apply suggestions from code review

Co-authored-by: Jake Mulford <jacobmulford@illumon.com>
Co-authored-by: Chip Kent <5250374+chipkent@users.noreply.github.com>

* Heart Rate to Heart_rate

* Apply suggestions from code review

Co-authored-by: Chip Kent <5250374+chipkent@users.noreply.github.com>

* fix column name format

* text to match

Co-authored-by: Amanda L Martin <hythloda@gmail.com>
Co-authored-by: Jake Mulford <jacobmulford@illumon.com>
Co-authored-by: Chip Kent <5250374+chipkent@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 9, 2021
1 parent a7bbd4d commit 213de6d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 63 deletions.
12 changes: 7 additions & 5 deletions CryptoCurrency/script.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
os.system("pip install pycoingecko")
try:
from pycoingecko import CoinGeckoAPI
except ImportError:
os.system("pip install pycoingecko")
from pycoingecko import CoinGeckoAPI

from pycoingecko import CoinGeckoAPI

from deephaven.DBTimeUtils import secondsToTime, millisToTime
from deephaven.DateTimeUtils import secondsToTime, millisToTime
from deephaven.TableTools import merge
from deephaven import DynamicTableWriter
import deephaven.Types as dht
Expand Down Expand Up @@ -63,4 +65,4 @@ def thread_func():
sleep(secondsToSleep)

thread = threading.Thread(target = thread_func)
thread.start()
thread.start()
10 changes: 5 additions & 5 deletions Fit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ These steps can be run with our example data or your own.
Add another directory level if desired to keep various projects segmented.
1. Ensure fitparse is setup (see [below](#using-fitparse-module)).
1. Run the `accessFit.py` file in a Deephaven console.
1. The `heartRateData` table should appear.
1. The `heart_rate_data` table should appear.

![Heart rate table](heartRateTable.png "Heart rate table")
1. Click the **Table Options** menu in the `heartRateData` tab.
![heart_rate_data](heartRateTable.png "heart_rate_data")
1. Click the **Table Options** menu in the `heart_rate_data` tab.
1. Select **Chart Builder**.
1. Select defaults: `Line`, `X-Axis=Timestamp`, `Series=Heart Rate`
1. Select defaults: `Line`, `X-Axis=Timestamp`, `Series=HeartRate`
1. Press **Create** (note: you may need to scroll to see the button.)
1. A chart should appear:

![Heart rate chart](heartRateChart.png "Heart rate chart")
![heart_rate_data](heartRateChart.png "heart_rate_data")

## Using fitparse module

Expand Down
71 changes: 37 additions & 34 deletions Fit/accessFit.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
from deephaven import DynamicTableWriter, Types as dht
from deephaven.DBTimeUtils import convertDateTime
from deephaven.DateTimeUtils import convertDateTime
# Ensure fitparse is installed.
import os
os.system("pip install fitparse")
from fitparse import FitFile
try:
from fitparse import FitFile
except ImportError:
os.system("pip install fitparse")
from fitparse import FitFile

# Change to the name of the downloaded file (including any intermediate directory added to docker)
fitfile = FitFile('/data/ThursMorn.fit')
fitfile = FitFile('/data/examples/Fit/ThursMorn.fit')

# See the size of your file
records = list(fitfile.get_messages('record'))
print("Number of data points:" + str(len(records)))
print("Number of data points: {}".format(len(records)))

# Setup deephaven tables to hold results
# Heart rate
columnNames = ["Timestamp", "Heart Rate"]
columnTypes = [dht.datetime, dht.int_]
hrTableWriter = DynamicTableWriter(columnNames, columnTypes)
heartRateData = hrTableWriter.getTable()
column_names = ["Timestamp", "HeartRate"]
column_types = [dht.datetime, dht.int_]
hr_table_writer = DynamicTableWriter(column_names, column_types)
heart_rate_data = hr_table_writer.getTable()
# Gps data
columnNames = ["Timestamp", "Enhanced Altitude m", "Enhanced Speed m/s", "GPPS Accuracy m", "Position lat semicircles", "Position long semicircles", "Speed m/s"]
columnTypes = [dht.datetime, dht.double, dht.double, dht.int_, dht.int_, dht.int_, dht.double]
gpsTableWriter = DynamicTableWriter(columnNames, columnTypes)
gpsData = gpsTableWriter.getTable()
column_names = ["Timestamp", "EnhancedAltitude", "EnhancedSpeed", "GPSAccuracy", "PositionLat", "PositionLong", "Speed"]
column_types = [dht.datetime, dht.double, dht.double, dht.int_, dht.int_, dht.int_, dht.double]
gps_table_writer = DynamicTableWriter(column_names, column_types)
gps_data = gps_table_writer.getTable()

# Set timezone based on preferences. Fit data may not include timezone
timezone="MT"
timezone=" " + timezone # Ensure there is a blank space before timezone for later parsing.
timezone = "MT"
timezone = " " + timezone # Ensure there is a blank space before timezone for later parsing.

# Process in smaller batches first, until you are happy with the results you are working with
counter = 20
counter = len(records)+1
counter = len(records) + 1

## Debug your files by looking at individual records at a time.
# record = records[0]
Expand Down Expand Up @@ -60,25 +63,25 @@

# Go through all the data entries in this record
items=list(record)
if (mode == "hr" ):
rawHeartRate=str(items[0]).split(" ")[1]
finalHeartRate=int(rawHeartRate)
rawTime=str(items[1]).split(" ")[1]
finalTime=convertDateTime(rawTime.replace(" ", "T") + timezone)
hrTableWriter.logRow(finalTime, finalHeartRate)
if (mode == "hr"):
raw_heart_rate = str(items[0]).split()[1]
final_heart_rate = int(raw_heart_rate)
raw_time = str(items[1]).split()[1]
final_time = convertDateTime(raw_time.replace(" ", "T") + timezone)
hr_table_writer.logRow(final_time, final_heart_rate)

if (mode == "gps" ):
rawTime=str(items[6])[11:30]
finalTime=convertDateTime(rawTime.replace(" ", "T") + timezone)
if (mode == "gps"):
raw_time = str(items[6])[11:30]
final_time = convertDateTime(raw_time.replace(" ", "T") + timezone)

finalAltitude=float(str(items[0]).split(" ")[1])
finalEnhSpeed=float(str(items[1]).split(" ")[1])
finalPosLat=int(str(items[3]).split(" ")[1])
finalPosLong=int(str(items[4]).split(" ")[1])
finalSpeed=float(str(items[5]).split(" ")[1])
final_altitude = float(str(items[0]).split()[1])
final_enh_speed = float(str(items[1]).split()[1])
final_pos_lat = int(str(items[3]).split()[1])
final_pos_long = int(str(items[4]).split()[1])
final_speed = float(str(items[5]).split()[1])

rawGPSAcc=str(items[2]).split(" ")[1]
raw_gps_acc = str(items[2]).split()[1]
# If preferred, the col type for GPS could be set as String, then further processing done even when value is None
if rawGPSAcc != "None":
finalGPSAcc=int(str(items[2]).split(" ")[1])
gpsTableWriter.logRow(finalTime, finalAltitude,finalEnhSpeed,finalGPSAcc,finalPosLat,finalPosLong,finalSpeed)
if raw_gps_acc != "None":
final_gps_acc = int(str(items[2]).split()[1])
gps_table_writer.logRow(final_time, final_altitude, final_enh_speed, final_gps_acc, final_pos_lat, final_pos_long, final_speed)
2 changes: 1 addition & 1 deletion Prometheus/app.d/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""
from deephaven.TableTools import newTable, stringCol, dateTimeCol, doubleCol
from deephaven import DynamicTableWriter
from deephaven.DBTimeUtils import millisToTime
from deephaven.DateTimeUtils import millisToTime
import deephaven.Types as dht
from typing import Callable

Expand Down
35 changes: 17 additions & 18 deletions TickingHeartRate/runTickingHeartRateReplay.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
from deephaven.TableTools import readHeaderlessCsv
from deephaven import read_csv
from deephaven import DynamicTableWriter, Types as dht
from deephaven import DBTimeUtils
from deephaven.DBTimeUtils import autoEpochToTime
from deephaven import DateTimeUtils
import pathlib
import time
import threading

# Max number of csv files to pull in
csvFiles=500
csv_files=500

# Setup deephaven tables to hold Heart Rate results
columnNames = ["Timestamp", "Heart Rate"]
columnTypes = [dht.datetime, dht.int_]
hrTableWriter = DynamicTableWriter(columnNames, columnTypes)
heartRateData = hrTableWriter.getTable()
# Setup deephaven tables to hold heart rate results
column_names = ["Timestamp", "HeartRate"]
column_types = [dht.datetime, dht.int_]
hr_table_writer = DynamicTableWriter(column_names, column_types)
heart_rate_data = hr_table_writer.getTable()

# Function to log data to the dynamic table
def thread_func():
for x in range(1,csvFiles):
nextFile=("/data/csv/%d.csv" % x)
print(nextFile)
path = pathlib.Path(nextFile)
for x in range(1, csv_files):
next_file = ("/data/examples/TickingHeartRate/csv/%d.csv" % x)
print(next_file)
path = pathlib.Path(next_file)
if path.exists() and path.is_file():
nextHR=readHeaderlessCsv(nextFile).update("Timestamp=Column1", "Heart_rate=Column2").select("Timestamp", "Heart_rate")
nextRecord=nextHR.getRecord(0, "Timestamp", "Heart_rate")
timestamp=DBTimeUtils.autoEpochToTime(nextRecord[0])
hrTableWriter.logRow(timestamp, int(nextRecord[1]))
next_hr = read_csv(next_file, headless = True).view("Timestamp=Column1", "HeartRate=Column2")
next_record = next_hr.getRecord(0, "Timestamp", "HeartRate")
timestamp = next_record[0]
hr_table_writer.logRow(timestamp, int(next_record[1]))
time.sleep(1)
else:
print("File does not exist: " + nextFile)
print("File does not exist: " + next_file)

# Thread to log data to the dynamic table
thread = threading.Thread(target = thread_func)
Expand Down

0 comments on commit 213de6d

Please sign in to comment.