From 517ebc1dd309e1594515a16717a928f34d1b7ef5 Mon Sep 17 00:00:00 2001 From: Trenton Embry Date: Sat, 5 Oct 2024 12:58:06 -0400 Subject: [PATCH] add missing test utils --- test_hw.py | 96 +++++++++++++++++++++++++++++++++++++++++++ test_utils.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 test_hw.py create mode 100644 test_utils.py diff --git a/test_hw.py b/test_hw.py new file mode 100644 index 0000000..421005c --- /dev/null +++ b/test_hw.py @@ -0,0 +1,96 @@ +''' + + + + + +Do NOT edit this file unless told to do so by your instructor. + + + + + + + + + +''' +#TODO Find a more scalable approach + +import random + +import string +import statistics +import sys +import pathlib + +import test_utils +sys.meta_path.append(test_utils.NotebookFinder()) +loaded = test_utils.NotebookLoader("./unit_3_linear_regression_hw.ipynb") +loaded.load_module("unit_3_linear_regression_hw") + + +import unit_3_linear_regression_hw +import numpy as np +import pandas as pd +import warnings + +# These are the tests that are visible to you, this will help you debug your own problems + +def test_extract_customer_data(): + annual_income_head = np.array([ [45000], + [55000], + [65000], + [30000], + [47000], + [61000], + [54000], + [43000], + [70000], + [50000], + [46000]]) + + assert np.array_equal(annual_income_head, unit_3_linear_regression_hw.extract_customer_data().loc[0:10, ["annual_income"]].values), "Data was not extracted correctly" + + +def test_train_linear_regression_model(): + # Made up data + x_data1 = [0, 2, 3, 4, 5, 6] + y_data1 = [0, 1, 2, 3, 4, 5] + x_data1 = pd.DataFrame(x_data1) + y_data1 = pd.DataFrame(y_data1) + model = unit_3_linear_regression_hw.train_linear_regression_model(x_data1, y_data1) + + assert type(model) != None, "Model was returned as None, did you fill out this function?" + assert round(model.score(x_data1, y_data1), 3) == 0.980, "Model score did not match - are you fitting the data?" + + x_data2 = pd.DataFrame([9, 12, 13, 14, 19, 60]) + y_data2 = pd.DataFrame([1, 2, 3, 4, 5, 6]) + model2 = unit_3_linear_regression_hw.train_linear_regression_model(x_data2, y_data2) + + assert type(model2) != None, "Model was returned as None, did you fill out this function?" + assert round(model2.score(x_data2, y_data2), 3) == 0.588 + + +def test_get_model_metrics(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + x_data1 = [1, 2, 2.2, 4, 5, 6] + y_data1 = [0, 1, 2, 3, 4, 5] + x_data1 = pd.DataFrame(x_data1) + y_data1 = pd.DataFrame(y_data1) + + + try: + model = unit_3_linear_regression_hw.train_linear_regression_model(x_data1, y_data1) + assert model != None + except: + assert False, "train_linear_regression model must be correct to get this test correct" + + metrics = unit_3_linear_regression_hw.print_model_metrics(model, x_data1, y_data1) + + assert np.round(metrics["Coefficients"], 3) == 0.950 + assert np.round(metrics["Intercepts"], 3) == -0.700 + assert np.round(metrics["Score"], 3) == 0.972 + # coe = [[0.01208716]] + # int = [-268.26390507] + # score = 0.9686662678992661 diff --git a/test_utils.py b/test_utils.py new file mode 100644 index 0000000..2440e30 --- /dev/null +++ b/test_utils.py @@ -0,0 +1,111 @@ +''' + + + + + +Do NOT edit this file unless told to do so by your instructor. + + + + + + + + + +''' + +### Code borrowed from https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html +import io, os, sys, types +from IPython import get_ipython +from nbformat import read +from IPython.core.interactiveshell import InteractiveShell +import matplotlib + + +def find_notebook(fullname, path=None): + """find a notebook, given its fully qualified name and an optional path + + This turns "foo.bar" into "foo/bar.ipynb" + and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar + does not exist. + """ + name = fullname.rsplit('.', 1)[-1] + if not path: + path = [''] + for d in path: + nb_path = os.path.join(d, name + ".ipynb") + if os.path.isfile(nb_path): + return nb_path + # let import Notebook_Name find "Notebook Name.ipynb" + nb_path = nb_path.replace("_", " ") + if os.path.isfile(nb_path): + return nb_path + +class NotebookLoader(object): + """Module Loader for Jupyter Notebooks""" + + def __init__(self, path=None): + self.shell = InteractiveShell.instance() + self.path = path + + def load_module(self, fullname): + """import a notebook as a module""" + path = find_notebook(fullname, self.path) + + print("importing Jupyter notebook from %s" % path) + + # load the notebook object + with io.open(path, 'r', encoding='utf-8') as f: + nb = read(f, 4) + + # create the module and add it to sys.modules + # if name in sys.modules: + # return sys.modules[name] + mod = types.ModuleType(fullname) + mod.__file__ = path + mod.__loader__ = self + mod.__dict__['get_ipython'] = get_ipython + sys.modules[fullname] = mod + + # extra work to ensure that magics that would affect the user_ns + # actually affect the notebook module's ns + save_user_ns = self.shell.user_ns + self.shell.user_ns = mod.__dict__ + + try: + for cell in nb.cells: + if cell.cell_type == 'code': + # transform the input to executable Python + code = self.shell.input_transformer_manager.transform_cell(cell.source) + # run the code in themodule + try: + exec(code, mod.__dict__) + except NotImplementedError: + pass + finally: + self.shell.user_ns = save_user_ns + return mod + def enable_gui(self): + print("enable gui") + +class NotebookFinder(object): + """Module finder that locates Jupyter Notebooks""" + + def __init__(self): + self.loaders = {} + + def find_module(self, fullname, path=None): + nb_path = find_notebook(fullname, path) + if not nb_path: + return + + key = path + if path: + # lists aren't hashable + key = os.path.sep.join(path) + + if key not in self.loaders: + self.loaders[key] = NotebookLoader(path) + return self.loaders[key] \ No newline at end of file