# Examples from Lab 1/22/2026 (week 1)

#---------------------------------------------------------------
# keyword parameter demo

def function(a=1, b=2, c=3, d=4):
    print(a, b, c, d)

# function()           => 1 2 3 4
# function(10, 20)     => 10 20 3 4
# function(d=99, a=0)  => 0 2 3 99

#---------------------------------------------------------------
# writing and reading data from text files

import os, random

# tell python where to look for files
os.chdir("/Users/jmarshall/bioai/w01-prep/lab1")

# verify current working directory
print("Current directory is:")
print(os.getcwd())

def write_data(filename, numlines=100):
    with open(filename, "w") as fp:
        for i in range(numlines):
            a = random.uniform(-10, 10)
            b = random.uniform(50, 200)
            # we'll use f-strings for string formatting
            fp.write(f"{i:5} {a:10.3f} {b:10.3f}\n")
        print(f"Wrote {numlines} lines of data to {filename}")

def read_data(filename):
    with open(filename, "r") as fp:
        all_lines = fp.readlines()
        for line in all_lines:
            s1, s2, s3 = line.split()
            i = int(s1)
            a = float(s2)
            b = float(s3)
            print(f"i = {i}, a = {a:8}, b = {b:10}")

#---------------------------------------------------------------
# basic plotting with matplotlib.pyplot

import numpy as np
import matplotlib.pyplot as plt

# provide lists of x-values and their y-values
plt.plot([1, 2, 3, 4, 5], [1, 4, 9, 16, 25])
plt.show()

# another example
plt.plot([-1, -2, -3, -4, -5], [1, 4, 9, 16, 25])
plt.show()

#---------------------------------------------------------------
# plotting mathematical functions

# two ways to create an array of x values
x = np.arange(0, 10, 0.2)    # interval size 0.2

x = np.linspace(0, 10, 100)  # 100 equally-spaced data points

# plot the function x**2 as a red solid line
plt.plot(x, x**2, 'r-')
plt.show()

# plot x**2 and x**3 on the same graph
x = np.arange(0, 10, 0.2)
plt.plot(x, x**2, 'r--')  # red dashed line
plt.plot(x, x**3, 'bo')   # blue circles
# set x-axis min and max
plt.xlim(0, 10)
# set y-axis min and max
plt.ylim(0, 500)
plt.show()

# a more complicated function: x + |sin(8x)| where 0 < x < pi
x = np.linspace(0, np.pi, 1000)
plt.plot(x, x + np.abs(np.sin(8*x)), 'm-')
plt.show()

#---------------------------------------------------------------
# reading in data from a text file

# create a file of 100 random data points
write_data("mydata.txt")

# create a numpy array containing the data
data = np.loadtxt("mydata.txt")

# extract the first column
x_values = data[:,0]

# extract the second column
a_values = data[:,1]

# extract the third column
b_values = data[:,2]

# plot the data
plt.plot(x_values, a_values, 'r--')
plt.plot(x_values, b_values, 'bo')
plt.xlim(0, 100)
plt.ylim(-20, 200)
plt.show()

#---------------------------------------------------------------
