<jupyter_start><jupyter_text>Graphing teh BerdsOur sample data set for this course is the NLDL Data set from <NAME>.NLDL stands for "Nucleus Laminaris Delay Lines". The data in each file describes the voltage over time at a specific location in the brain.In simple terms an electrical stimulation was applied at the point where sound enters the brain, and then electrodes measured the voltage over time at a range of locations.By measuring the time delay from stimulation to peak voltage for each data file, we can obtain secondary data which can be combined to determine whether the stimulation came from the left ear or right ear.Our goal here is to plot the voltage over time as recorded in a single file.The resulting chart will be a waveform.  Loading the Data We're using an external module so it's best to enable autoreload. This means if we make any changes to the module, the script here will be automatically updated.<jupyter_code>%autoreload 2<jupyter_output><empty_output><jupyter_text>Import the parser. This module can take one or more NLDL files and convert them to Python objects for you.Also import pprint (pretty print) to make the printed data easier to inspect.<jupyter_code>from nldl_parser import *
from pprint import *<jupyter_output><empty_output><jupyter_text>Set the source file. Let's just start with one.<jupyter_code>source_file = 'csv/TEK0000.CSV'<jupyter_output><empty_output><jupyter_text>Create a new `parser`. Pass it the `source_file` and receive back the `data` object.<jupyter_code>parser = nldl_parser.NLDLParser()
data = parser.parse_file(source_file)
print("File Data:")
pprint(data)<jupyter_output><empty_output><jupyter_text>Generating a Chart Before we start, let's make it so that any charts we generate display on this page rather than opening in a new window.We use the `inline` command to plot inside the notebook rather than having a new window open.Importing `Image` from IPython allows us to show images here in our notebook.<jupyter_code>%matplotlib inline 
from IPython.display import Image<jupyter_output><empty_output><jupyter_text>We then need to import the relevant libraries. At this point, all we need is **pyplot** for creating the charts and **numpy** for working with numbers.<jupyter_code>import matplotlib.pyplot as pyplot
import numpy<jupyter_output><empty_output><jupyter_text>A basic line chart is made of `x` and `y` values. To generate a chart, we need (at a bare minimum) the following steps:- Create separate lists of all the values for the **x** axis and all the values for the **y** axis.- Pass the x and y lists to **pyplot.plot()**.- Use **pyplot.show()** to display the result.<jupyter_code>x_data = [0,1,2,3,4,5,6,7,8,9]
y_data = [1,2,4,5,7,9,10,7,5,2]

pyplot.plot(x_data, y_data)
pyplot.show()<jupyter_output><empty_output><jupyter_text>Plotting the NLDL Data In our NLDL data, we want to plot the values from the **readings** list.The **x** axis will be **time**.The **y** axis will be **voltage**.Have another look at the printed NLDL data we created earlier and identify where you can find the **time** and **voltage** for each reading.We need to:- Create lists for the x values and y values.- Look at each reading from the list of readings.- Extract the reading's time and save it to the x values.- Extract the reading's voltage and save it to the y values.- Plot the data.  Extract the X and Y DataHere we need to look at each reading individually and extract the x value (time) and the y value (voltage).Each value will be saved to the relevant list.<jupyter_code>x_data = []
y_data = []

for reading in data['readings']:
    
    time = reading['time']
    time = float(time) * 1000
    x_data.append(time)
    
    voltage = reading['voltage']
    y_data.append(voltage)<jupyter_output><empty_output><jupyter_text>We should also check that our data looks reasonable before we begin plotting it...<jupyter_code>pprint(x_data)
pprint(y_data)<jupyter_output><empty_output><jupyter_text>Create a Basic PlotOnce we have the data, creating a chart is a matter of just two lines.<jupyter_code>pyplot.plot(x_data, y_data)
pyplot.show()<jupyter_output><empty_output><jupyter_text>We can also add some styling to make it look nicer.The title and axis labels can be added using the relevant plot functions.We can also add annotations such as arrows and lines. Here we've added a vertical line to show where **time = 0**, which is the point at which the stimulation was applied. The time before zero is a recording buffer.<jupyter_code>pyplot.style.use('ggplot')

pyplot.title("NLDL Data")
pyplot.xlabel("Time (ms)")
pyplot.ylabel("Voltage")

pyplot.axvline(x=0, color='blue')

pyplot.plot(x_data, y_data)
pyplot.show()<jupyter_output><empty_output><jupyter_text>Last of all, we can save our chart as an image file:<jupyter_code>pyplot.savefig("TEK0000.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08288770053475936, 'fraction_numerical': 0.007404360345536816, 'mean_word_length': 5.432539682539683, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '370338', 'license': 'Ruby', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1438, 'n_words': 626, 'path': 'modules/programming-old/nldl-sample-project/nldl_graphing.ipynb', 'repo_name': 'MEDSCI-736/coursebook', 'size': '4864'}