def test_source(): now = datetime.now() # TODO : generate sample data with hypothesis df = pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[now, now + timedelta(milliseconds=1)], ) dm = DataModel(name="TestDataModel", data=df) ds = dm.source assert isinstance(ds, DataSource) assert ds.name == "TestDataModel" assert ds.column_names == df.reset_index().columns.to_list() for c in df.columns: assert (ds.data[c] == df[c]).all() assert ds in dm._rendered_datasources
def test_render(): now = datetime.now() # TODO : generate sample data with hypothesis df = pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[now, now + timedelta(milliseconds=1)], ) dm = DataModel(name="TestDataModel", data=df) dv = DataView(model=dm) assert dv.model == dm rendered = dv.plot assert isinstance(rendered, Plot) assert dv.model == dm rendered2 = dv.plot assert isinstance(rendered, Plot) assert rendered2 != rendered
def __init__(self, sample_size=255): self.sample_size = sample_size self.pregen_frame = pandas.DataFrame( data={"RandomWalk": RandomWalk().sample(sample_size)}) self.current = 0 self.model = DataModel(data=self.pregen_frame[:self.current], name="Random Walk", debug=False)
def __init__(self, sample_size=255): self.sample_size = sample_size self.pregen_frame = pandas.DataFrame( data={"MarkovChain": MarkovChain().sample(sample_size)} ) self.current = 0 self.model = DataModel( data=self.pregen_frame[: self.current], name="Markov Chain", debug=False )
def test_data_patch(): now = datetime.now() # TODO : generate sample data with hypothesis df = pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[now, now + timedelta(milliseconds=1)], ) dm = DataModel(name="TestDataModel", data=df) no_patches = dm._patch(df) assert len(no_patches) == 0 # same index, different values df2 = pandas.DataFrame( data=[ [random.randint(-20, -10), random.randint(-20, -10)], [random.randint(-20, -10), random.randint(-20, -10)], ], columns=["random1", "random2"], index=[now, now + timedelta(milliseconds=1)], ) patches = dm._patch(df2) # Note : we need to drop timestamp index and retrieve the integer index assert patches == { col: [(i, v) for i, v in s.items()] for col, s in df2.reset_index(drop=True).to_dict("series").items() }
def test_data_stream(): now = datetime.now() # TODO : generate sample data with hypothesis df = pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[now, now + timedelta(milliseconds=1)], ) dm = DataModel(name="TestDataModel", data=df) no_streamable = dm._stream(df) assert no_streamable.empty # different indexes df2 = pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[ now + timedelta(milliseconds=2), now + timedelta(milliseconds=3) ], ) streamable = dm._stream(df2) assert (streamable == df2).all().all()
def test_intindexed_data(): # TODO : generate sample data with hypothesis df = pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[0, 1], ) dm = DataModel(name="TestDataModel", data=df) dd = dm.data assert isinstance(dd, pandas.DataFrame) assert (dd == df).all().all()
def test_datetimeindexed_data(): now = datetime.now() # TODO : generate sample data with hypothesis df = pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[now, now + timedelta(milliseconds=1)], ) dm = DataModel(name="TestDataModel", data=df) dd = dm.data assert isinstance(dd, pandas.DataFrame) assert (dd == df).all().all()
async def _internal_example(): # Minimal server test import random import asyncio from datetime import datetime, timedelta # Note : This is "created" before a document output is known # and before a request is sent to the server start = datetime.now() random_data_model = DataModel( name="random_data_model", data=pandas.DataFrame( data=[ [random.randint(-10, 10), random.randint(-10, 10)], [random.randint(-10, 10), random.randint(-10, 10)], ], columns=["random1", "random2"], index=[start, start + timedelta(milliseconds=1)], ), ) # Note : This is "created" before a document output is known and before a request is sent to the server view = DataView(model=random_data_model) view.plot_args( title="Random Test", plot_height=480, tools="xpan, xwheel_zoom, reset", toolbar_location="left", y_axis_location="right", x_axis_type="datetime", sizing_mode="scale_width", ) # filtering view on the left fview = DataView( model=random_data_model, filter= # only show when random2 values are positive lambda dt: dt.random2 > 0 # TODO : convert to pandas syntax... somehow # here (dimension -1) we are talking about attributes, not columns... ) # Producer as a background task async def compute_random(m, M): tick = (random_data_model.data.index.to_list() ) # to help with full data generation while True: now = datetime.now() tick.append(now) print(now) # print in console for explicitness new_data = { "random1": [ random.randint(m, M) # change everything to trigger patch for t in range(len(tick)) # + 1 extra element to stream ], "random2": random_data_model.data["random2"].to_list() + [random.randint(m, M)], # only add one element to stream } # push FULL data updates ! # Note some derivative computation may require more than you think random_data_model( pandas.DataFrame(columns=["random1", "random2"], data=new_data, index=tick)) await asyncio.sleep(1) # bg async task... asyncio.create_task(compute_random(-10, 10)) return random_data_model, view, fview