Skip to content

Instantly share code, notes, and snippets.

View deephavendatalabs's full-sized avatar

Deephaven Data Labs deephavendatalabs

View GitHub Profile
import tensorflow as tf
import numpy as np
from deephaven import npy
import math
keras = tf.keras
def table_cache_gen(feature_table, label_table, cache_size=1_000_000, batch_size=32, dtype=tf.float32):
"""
Creates a generator to use a Deephaven table with Tensorflow for large data sets.
:param feature_table: Deephaven table storing the features or X values.
train_gen = get_table_generator(x_train_table, y_train_table, cache_size=500_000, batch_size=16, dtype=tf.float32)
hist = model.fit(train_gen, epochs=10)
import tensorflow as tf
from deephaven import npy
t = db.t(...) # Some table query
tensor = tf.constant(npy.numpy_slice(t, 0, t.size(), dtype=tf.float32.as_numpy_dtype))
from deephaven import npy
import numpy as np
import pandas as pd
t = db.t(...) # Some table query
df = pd.DataFrame(
data=npy.numpy_slice(t, 0, t.size(), dtype=np.float32),
columns=t.getDefinition().getColumnNamesArray()
)
from deephaven import npy
import numpy as np
t = db.t(...) # Some table query
np_arr = npy.numpy_slice(t, 0, t.size(), dtype=np.float32)
class OMS {
...
// Record the trade to our input table
async recordTrade(trade) {
console.log(`Recording trade for: ${trade.Symbol}`);
let inputTable = this.tradeTable;
try {
await inputTable.addRow({
Symbol: trade.Symbol,
Timestamp: iris.DateWrapper.ofJsDate(new Date()).asNumber(), // Timestamp is used to uniquely identify. Should really use a tradeID
class OMS {
...
async placeOrder(e) {
const tradeColNames = this.orderTable.columns.map(c => c.name);
const rowData = e.detail.row.dataColumns;
let orderObj = {Exchange: 'NASDAQ'};
for (let i = 0; i < tradeColNames.length; i++) {
orderObj[tradeColNames[i]] = rowData[i][e.detail.index];
}
class OMS {
...
// Returns Promise<boolean> if trade is accepted
fakeOMS(order) {
console.log(`Placing order for: ${order.Symbol}`);
return new Promise((resolve, reject) => {
setTimeout(
resolve,
Math.floor(Math.random() * 10000), // Wait between 0 and 10 seconds
Math.random() > 0.1
class OMS {
...
setupTradeListener() {
let table = this.orderTable;
this.lastKnownSize = table.size;
// Use sizechanged to see if there was more than 1 row added in the last second
table.addEventListener('sizechanged', e => {
table.setViewport(this.lastKnownSize, e.detail);
this.lastKnownSize = e.detail;
class OMS {
constructor(client) {
const pqName = 'OMS Example';
const orderTableName = 'orders';
const tradeTableName = 'trades';
this.client = client;
this.query = null;
this.orderTable = null;
this.tradeTable = null;
this.lastKnownSize = 0;