SciPy
SciPy (website) is a popular tool set that includes NumPy, Pandas, and SymPy. SciKit Learn (website) is also supported. For evaluating exercises written using SciPy we're using the NumPy testing and unittest frameworks.

Hello Pi

Let's start with a Hello Pi exercise to explain the basics of evaluation in SciPy. Here is a solution file:
exercise.py
1
import numpy as np
2
3
print("Hello ", np.pi)
Copied!
Evaluation of this simple "Hello Pi" exercise is also very simple. All we have to do is to create a class extending unittest's TestCase class and declare a test method inside. The test method will get the string printed in stdout and compare it with expected output ("Hello World").
evaluate.py
1
import sys
2
from unittest import TestCase
3
4
from numpy.testing import assert_equal
5
6
class Evaluate(TestCase):
7
def test_hello_pi(self):
8
import exercise # Imports and runs student's solution
9
output = sys.stdout.getvalue() # Returns output since this function started
10
assert_equal(output, 'Hello 3.141592653589793\n')
Copied!
Let's see what we did in the above solution code.
    1st line, the sys module is imported which will help us get the output of student's code
    2nd line, TestCase class is imported to define our test class Evaluate and it is defined on the 4th line
    5th line, our first test function is defined
    6th line, the exercise module that is implemented by students is imported. Since the solution code is not in a function, it is enough to import the module to run the student's code. We have to import it inside the test function to be able to get the output.
    7th line, we get the output of exercise module. sys.stdout.getvalue() does exactly what we want here. It will only get the strings written to stdout since the test function has started. So you cannot get any outputs written before the test_hello_world() has begun.
    8th line asserts whether the output is correct and returns feedback if it's not.

Python 3

SciPy is based on Python and the documentation for Python covers a number of useful test scenarios. Please review those before continuing.

Example 1: NumPy

exercise.py
1
import numpy as np
2
3
a = np.array([1,2,3,4])
4
b = a.reshape(2,2)
Copied!
evaluate.py
1
from unittest import TestCase
2
3
from numpy.testing import assert_equal
4
5
class Evaluate(TestCase):
6
def test_array(self):
7
import exercise
8
assert_equal(exercise.a, [1, 2, 3, 4])
9
assert_equal(exercise.b, [[1, 2], [3, 4]])
Copied!

Example 2: Pandas

exercise.py
1
import pandas as pd
2
3
dates = pd.date_range('20130101', periods=6)
Copied!
evaluate.py
1
import datetime
2
from unittest import TestCase
3
4
from numpy.testing import assert_equal
5
6
class Evaluate(TestCase):
7
def test_array(self):
8
import exercise
9
assert_equal(exercise.dates[0].date(), datetime.date(2013, 1, 1))
10
assert_equal(exercise.dates[-1].date(), datetime.date(2013, 1, 6))
Copied!

Example 3: Data Loading From File

exercise.py
1
import pandas as pd
2
3
data = pd.read_csv("data.csv", header=None)
Copied!
data.csv
1
3,2,1
Copied!
evaluate.py
1
from unittest import TestCase
2
3
from numpy.testing import assert_equal
4
5
class Evaluate(TestCase):
6
def test_array(self):
7
import exercise
8
assert_equal(exercise.data.to_numpy().ravel(), [3,2,1])
Copied!

Example 4: Data Loading From Url

exercise.py
1
import pandas as pd
2
3
data = pd.read_csv('https://storage.googleapis.com/applied-dl/heart.csv')
Copied!
evaluate.py
1
from unittest import TestCase
2
3
class Evaluate(TestCase):
4
def test_array(self):
5
import exercise
6
self.assertTrue(exercise.data.to_numpy().size > 0)
Copied!

Example 5: SymPy

exercise.py
1
from sympy.physics.units import mass, acceleration
2
from sympy.physics.units.systems.si import dimsys_SI
3
4
F = mass * acceleration
5
6
deps = dimsys_SI.get_dimensional_dependencies(F)
Copied!
evaluate.py
1
from unittest import TestCase
2
3
from numpy.testing import assert_equal
4
5
class Evaluate(TestCase):
6
def test_array(self):
7
import exercise
8
assert_equal(exercise.deps, {'length': 1, 'mass': 1, 'time': -2})
Copied!

Example 6: SciKit Learn

exercise.py
1
from sklearn import datasets
2
from sklearn import svm
3
digits = datasets.load_digits()
4
clf = svm.SVC(gamma=0.001, C=100.)
5
clf.fit(digits.data[:-1], digits.target[:-1])
6
results = clf.predict(digits.data[-1:])
Copied!
evaluate.py
1
from unittest import TestCase
2
3
from numpy.testing import assert_array_equal
4
5
class Evaluate(TestCase):
6
def test_array(self):
7
import exercise
8
assert_array_equal(exercise.results, [8])
Copied!
Last modified 1yr ago