{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Data Records\n\nData records are a tool for managing time series data collected\nover the course of a measurement. They store aligned time series data\nand meta data about the file in multiple csv files.\n\nAn ActiveRecord is a context manager that will automatically dump\nany data in the memory to the target directory in the event of\nan exception or error.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from rminstr.data_structures import ActiveRecord, ExistingRecord\nimport time\n\nwith ActiveRecord(\n ['column_1', 'column_2'], output_dir='outputs', maxlen=100, minlen=10\n) as rec:\n # add a single sample. Time stamps can be applied automatically\n # or through the timestamp keyword.\n rec.update('column_1', 0.1)\n\n # add multiple samples all at once, time stamps need to be\n # provided in this case\n samples = []\n timestamps = []\n for i in range(50):\n samples.append(i)\n timestamps.append(time.time())\n rec.array_update('column_2', samples, timestamps)\n\n # The most recent sample in a column can be accessed via indexing\n\n last_sample = rec['column_2']\n\n # Columns can be indexed into by their timestamps\n\n time_series = rec.get_time_series(\n 'column_2', t_min=timestamps[5], t_max=timestamps[7]\n )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To read the data back in, point the ``ExistingRecord`` class to\nthe metadata file produced by the ``ActiveRecord``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "opened = ExistingRecord(f'outputs/{rec.metadata_file_name}')\n\n# Read each line in the record one at a time\n# and index into it like you would an active record\nwhile opened.read_next_line():\n pass\nlast_sample = opened['column_1']\n\n\n# Read the entire record at once into a\n# dictionary of ``TimeSeries`` objects.\ncolumns = opened.batch_read()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 0 }