Skip to content

Instantly share code, notes, and snippets.

@DavidLutton
Created April 9, 2019 21:19
Show Gist options
  • Save DavidLutton/415343b805cdbbf2558d74356133391b to your computer and use it in GitHub Desktop.
Save DavidLutton/415343b805cdbbf2558d74356133391b to your computer and use it in GitHub Desktop.
Convert a CSV from a logging DMM to an Bokeh plot
from pathlib import Path
from pprint import pprint
import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show, save
from bokeh.models import ColumnDataSource
for source in Path('.').glob('*.csv'):
print(source.stem)
target = Path(source.stem + '.html')
if target.exists():
df = pd.read_csv(
source,
encoding='utf_16',
sep=',\t',
names=['Index', 'Detector', 'Magnitude', 'Unit', 'Time', 'Date'],
na_values='OL',
engine='python', #
)
df['Time'] = pd.to_timedelta(df['Time']) # overload time as string to timestamp
df['Seconds'] = df['Time'].dt.total_seconds() # time to seconds into day
df['Seconds'] = df['Seconds'] - min(df['Seconds']) # shift to start the values at zero
# df = df.set_index('Time')[['Magnitude']] #
df = df.where(df['Magnitude'] > 7.5) # where data less than value change to NA
df = df.fillna(method='backfill', limit=25) # backfill NA values up to 25 counts
df = df.dropna() # drop remaining NA rows
pprint(df.describe())
print('\n'*2)
output_file(target) # set the tartget file to target
# create a new plot with a title and axis labels
p = figure(title=source.stem.capitalize(), x_axis_label='Seconds', y_axis_label='Voltage', width=1800, height=900)
p.line(x='Seconds', y='Magnitude', source=ColumnDataSource(df), line_width=2) # use Seconds and Mangitude data from df
save(p, title=source.stem) # Save p to configured output
# show(p) # opens the html output with default browser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment