Skip to content

Commit

Permalink
Merge pull request #5 from tonegas/features/4-automotic-coveralls
Browse files Browse the repository at this point in the history
Features/4 automotic coveralls
  • Loading branch information
tonegas authored Dec 4, 2024
2 parents eff3d70 + ce65169 commit d8b0d04
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 32 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Test Coveralls

on:
push:
branches: [ "main"]
pull_request:
branches: [ "main" ]

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v3
with:
python-version: 3.12
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install coverage coveralls
pip install -e .
- name: Tests with coverage
run: |
coverage run --omit='./results/*','./docs/*','./examples/*' -m unittest discover tests
- name: Coveralls
uses: coverallsapp/github-action@v2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python package
name: Run tests

on:
push:
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion nnodely/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from nnodely.output import Output

# Network elements
from nnodely.activation import Relu, Tanh
from nnodely.activation import Relu, Tanh, ELU
from nnodely.fir import Fir
from nnodely.linear import Linear
from nnodely.arithmetic import Add, Sum, Sub, Mul, Pow, Neg
Expand Down
29 changes: 15 additions & 14 deletions tests/test_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ModelyTrainingTest(unittest.TestCase):
def TestAlmostEqual(self, data1, data2, precision=4):
assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2, dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
if type(data1) == type(data2) == list:
self.assertEqual(len(data1), len(data2))
for pred, label in zip(data1, data2):
self.TestAlmostEqual(pred, label, precision=precision)
else:
Expand All @@ -42,9 +43,9 @@ def test_losses_compare(self):
dataset = {'in1': [1,1,1,1,1,1,1,1,1,1], 'out1': [2,2,2,2,2,2,2,2,2,2], 'out2': [5,5,5,5,5,5,5,5,5,5]}
test.loadData(name='dataset', source=dataset)
test.trainModel(optimizer='SGD', num_of_epochs=5, lr=0.5)
self.TestAlmostEqual( [[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]], test.prediction['train_dataset_0.70']['error1']['A'])
self.TestAlmostEqual([[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]]] ,test.prediction['train_dataset_0.70']['error1']['B'])
self.TestAlmostEqual( [[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]], test.prediction['train_dataset_0.70']['error2']['A'])
self.TestAlmostEqual( [[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]], test.prediction['train_dataset_0.70']['error1']['A'])
self.TestAlmostEqual([[[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]]] ,test.prediction['train_dataset_0.70']['error1']['B'])
self.TestAlmostEqual( [[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]], test.prediction['train_dataset_0.70']['error2']['A'])
self.TestAlmostEqual([1.0, 16.0, 1.0, 16.0, 1.0], test.training['error1']['train'])
self.TestAlmostEqual([16.0, 1.0, 16.0, 1.0, 16.0], test.training['error1']['val'])
self.TestAlmostEqual([16.0, 1.0, 16.0, 1.0, 16.0], test.training['error2']['train'])
Expand All @@ -59,9 +60,9 @@ def test_losses_compare(self):

test.neuralizeModel(clear_model=True)
test.trainModel(optimizer='SGD', splits=[60,20,20], num_of_epochs=5, lr=0.5, train_batch_size=2)
self.TestAlmostEqual( [[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]], test.prediction['train_dataset_0.60']['error1']['A'])
self.TestAlmostEqual([[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]],[[6.0]]] ,test.prediction['train_dataset_0.60']['error1']['B'])
self.TestAlmostEqual( [[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]], test.prediction['train_dataset_0.70']['error2']['A'])
self.TestAlmostEqual( [[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]], test.prediction['train_dataset_0.60']['error1']['A'])
self.TestAlmostEqual([[[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]]] ,test.prediction['train_dataset_0.60']['error1']['B'])
self.TestAlmostEqual( [[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]], test.prediction['train_dataset_0.60']['error2']['A'])
self.TestAlmostEqual([6.0, 11.0, 6.0, 11.0, 6.0], test.training['error1']['train'])
self.TestAlmostEqual([16.0, 1.0, 16.0, 1.0, 16.0], test.training['error1']['val'])
self.TestAlmostEqual([11.0, 6.0, 11.0, 6.0, 11.0], test.training['error2']['train'])
Expand All @@ -78,7 +79,7 @@ def test_losses_compare_closed_loop_state(self):
a = Parameter('a', values=[[1]])
output1 = Output('out', Fir(parameter=a)(input1.last()))

test = Modely(visualizer=TextVisualizer(5),seed=42)
test = Modely(visualizer=None,seed=42)
test.addModel('model', output1)
test.addClosedLoop(output1, input1)
test.addMinimize('error1', target1.last(), output1)
Expand All @@ -88,9 +89,9 @@ def test_losses_compare_closed_loop_state(self):
dataset = {'in1': [1,1,1,1,1,1,1,1,1,1], 'out1': [2,2,2,2,2,2,2,2,2,2], 'out2': [5,5,5,5,5,5,5,5,5,5]}
test.loadData(name='dataset', source=dataset)
test.trainModel(optimizer='SGD', num_of_epochs=5, lr=0.5)
self.TestAlmostEqual([[[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]]], test.prediction['train_dataset_0.70']['error1']['A'])
self.TestAlmostEqual([[[[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]]]] ,test.prediction['train_dataset_0.70']['error1']['B'])
self.TestAlmostEqual([[[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]]], test.prediction['train_dataset_0.70']['error2']['A'])
self.TestAlmostEqual([[[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]]], test.prediction['train_dataset_0.70']['error1']['A'])
self.TestAlmostEqual([[[[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]]]] ,test.prediction['train_dataset_0.70']['error1']['B'])
self.TestAlmostEqual([[[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]]], test.prediction['train_dataset_0.70']['error2']['A'])
self.TestAlmostEqual([1.0, 16.0, 1.0, 16.0, 1.0], test.training['error1']['train'])
self.TestAlmostEqual([16.0, 1.0, 16.0, 1.0, 16.0], test.training['error1']['val'])
self.TestAlmostEqual([16.0, 1.0, 16.0, 1.0, 16.0], test.training['error2']['train'])
Expand All @@ -105,9 +106,9 @@ def test_losses_compare_closed_loop_state(self):

test.neuralizeModel(clear_model=True)
test.trainModel(optimizer='SGD', splits=[60,20,20], num_of_epochs=5, lr=0.5, train_batch_size=2)
self.TestAlmostEqual([[[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]]], test.prediction['train_dataset_0.60']['error1']['A'])
self.TestAlmostEqual([[[[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]]]] ,test.prediction['train_dataset_0.60']['error1']['B'])
self.TestAlmostEqual([[[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]]], test.prediction['train_dataset_0.70']['error2']['A'])
self.TestAlmostEqual([[[[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]], [[2.0]]]], test.prediction['train_dataset_0.60']['error1']['A'])
self.TestAlmostEqual([[[[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]], [[6.0]]]] ,test.prediction['train_dataset_0.60']['error1']['B'])
self.TestAlmostEqual([[[[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]], [[5.0]]]], test.prediction['train_dataset_0.60']['error2']['A'])
self.TestAlmostEqual([6.0, 11.0, 6.0, 11.0, 6.0], test.training['error1']['train'])
self.TestAlmostEqual([16.0, 1.0, 16.0, 1.0, 16.0], test.training['error1']['val'])
self.TestAlmostEqual([11.0, 6.0, 11.0, 6.0, 11.0], test.training['error2']['train'])
Expand Down Expand Up @@ -141,7 +142,7 @@ def test_losses_compare_closed_loop(self):
a = Parameter('a', values=[[1]])
output1 = Output('out', Fir(parameter=a)(input1.last()))

test = Modely(visualizer=TextVisualizer(5),seed=42)
test = Modely(visualizer=None,seed=42)
test.addModel('model', output1)
test.addMinimize('error1', target1.last(), output1)
test.addMinimize('error2', target2.last(), output1)
Expand Down
26 changes: 19 additions & 7 deletions tests/test_model_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ModelyPredictTest(unittest.TestCase):

def TestAlmostEqual(self, data1, data2, precision=4):
assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2, dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
if type(data1) == type(data2) == list:
if type(data1) == type(data2) == list:
self.assertEqual(len(data1),len(data2))
for pred, label in zip(data1, data2):
self.TestAlmostEqual(pred, label, precision=precision)
else:
Expand All @@ -60,7 +61,19 @@ def test_single_in(self):
results = test({'in1': [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]], 'in2': [[5], [7], [9]]})
self.assertEqual(3, len(results['out']))
self.TestAlmostEqual([33.74938201904297, 40.309326171875, 46.86927032470703], results['out'])


def test_activation(self):
torch.manual_seed(1)
in1 = Input('in1')
out_fun = ELU(in1.last()) + Relu(in1.last()) + Tanh(in1.last())
out = Output('out', out_fun)
test = Modely(visualizer=None, seed=1)
test.addModel('out',out)
test.neuralizeModel()
results = test({'in1': [-1, -0.5, 0, 0.2, 2, 10]})
self.assertEqual(6, len(results['out']))
self.TestAlmostEqual([-1.3937146663665771,-0.8555865287780762,0,0.5973753333091736,4.964027404785156,21.0], results['out'])

def test_single_in_window(self):
# Here there is more sample for each time step but the dimensions of the input is 1
in1 = Input('in1')
Expand Down Expand Up @@ -360,7 +373,7 @@ def test_fir_and_parameter(self):
test = Modely(visualizer=None)
test.addModel('out',[out1, out2, out3, out4, out5])
test.neuralizeModel(0.5)
# Time -2, -1, 0, 1, 2, 3, 4
# Time -3, -2, -1, 0, 1, 2, 3
input = [-2, -1, 0, 1, 2, 3, 12]
results = test({'x': input})

Expand All @@ -369,7 +382,7 @@ def test_fir_and_parameter(self):
self.assertEqual((2,), np.array(results['out2']).shape)
self.TestAlmostEqual([-2, -4], results['out2'])
self.assertEqual((2, 1, 2), np.array(results['out3']).shape)
self.TestAlmostEqual([[[-2,1], [-4,2]]], results['out3'])
self.TestAlmostEqual([[[-2,1]], [[-4,2]]], results['out3'])
self.assertEqual((2, 1, 2), np.array(results['out4']).shape)
self.TestAlmostEqual([[[6.0, -2.0]], [[10.0, 0.0]]], results['out4'])
self.assertEqual((2, 1, 2), np.array(results['out5']).shape)
Expand Down Expand Up @@ -535,8 +548,8 @@ def test_parametric_function_and_fir(self):
test.neuralizeModel(0.1)
with self.assertRaises(StopIteration): ## TODO: change to KeyError when checking the inputs
test({'in1': [[1, 2, 2, 4]]})
results = test({'in1': [1, 2, 2, 4], 'in2': [1, 2, 2, 4]})

results = test({'in1': [1, 2, 2, 4], 'in2': [1, 2, 2, 4]})
self.TestAlmostEqual(results['out'], [-0.4379930794239044])
results = test({'in1': [[1, 2, 2, 4]], 'in2': [[1, 2, 2, 4]]}, sampled=True)
self.TestAlmostEqual(results['out'], [-0.4379930794239044])
Expand All @@ -547,7 +560,6 @@ def test_parametric_function_and_fir(self):
results = test({'in1': [[1, 2, 2, 4], [1, 2, 2, 4]], 'in2': [[1, 2, 2, 4], [5, 5, 5, 5]]}, sampled=True)
self.TestAlmostEqual(results['out'], [-0.4379930794239044, 0.5163354873657227])

self.TestAlmostEqual(results['out'], [-0.4379930794239044])
results = test({'in1': [[1, 2, 2, 4]], 'in2': [[1, 2, 2, 4]]}, sampled=True)
self.TestAlmostEqual(results['out'], [-0.4379930794239044])
results = test({'in1': [1, 2, 2, 4, 5], 'in2': [1, 2, 2, 4]})
Expand Down Expand Up @@ -621,6 +633,7 @@ def test_parametric_function_and_fir_with_parameters(self):
test.neuralizeModel(0.1)
with self.assertRaises(StopIteration):
test({'in1': [[1, 2, 2, 4]]})

results = test({'in1': [1, 2, 2, 4], 'in2': [1, 2, 2, 4]})
self.TestAlmostEqual(results['out'], [0.3850506544113159])
results = test({'in1': [[1, 2, 2, 4]], 'in2': [[1, 2, 2, 4]]}, sampled=True)
Expand All @@ -632,7 +645,6 @@ def test_parametric_function_and_fir_with_parameters(self):
results = test({'in1': [[1, 2, 2, 4], [1, 2, 2, 4]], 'in2': [[1, 2, 2, 4], [5, 5, 5, 5]]}, sampled=True)
self.TestAlmostEqual(results['out'], [0.3850506544113159, 1.446676254272461])

self.TestAlmostEqual(results['out'], [0.3850506544113159])
results = test({'in1': [[1, 2, 2, 4]], 'in2': [[1, 2, 2, 4]]}, sampled=True)
self.TestAlmostEqual(results['out'], [0.3850506544113159])
results = test({'in1': [1, 2, 2, 4, 5], 'in2': [1, 2, 2, 4]})
Expand Down
3 changes: 2 additions & 1 deletion tests/test_model_predict_recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class ModelyRecurrentPredictTest(unittest.TestCase):

def TestAlmostEqual(self, data1, data2, precision=4):
assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2, dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
if type(data1) == type(data2) == list:
if type(data1) == type(data2) == list:
self.assertEqual(len(data1), len(data2))
for pred, label in zip(data1, data2):
self.TestAlmostEqual(pred, label, precision=precision)
else:
Expand Down
1 change: 1 addition & 0 deletions tests/test_parameters_of_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ModelyTrainingTestParameter(unittest.TestCase):
def TestAlmostEqual(self, data1, data2, precision=4):
assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2, dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
if type(data1) == type(data2) == list:
self.assertEqual(len(data1), len(data2))
for pred, label in zip(data1, data2):
self.TestAlmostEqual(pred, label, precision=precision)
else:
Expand Down
1 change: 1 addition & 0 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ModelyTrainingTest(unittest.TestCase):
def TestAlmostEqual(self, data1, data2, precision=4):
assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2, dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
if type(data1) == type(data2) == list:
self.assertEqual(len(data1), len(data2))
for pred, label in zip(data1, data2):
self.TestAlmostEqual(pred, label, precision=precision)
else:
Expand Down
1 change: 1 addition & 0 deletions tests/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ModelyTrainingTest(unittest.TestCase):
def TestAlmostEqual(self, data1, data2, precision=4):
assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2, dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
if type(data1) == type(data2) == list:
self.assertEqual(len(data1), len(data2))
for pred, label in zip(data1, data2):
self.TestAlmostEqual(pred, label, precision=precision)
else:
Expand Down
16 changes: 9 additions & 7 deletions tests/test_train_recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class ModelyTrainingTest(unittest.TestCase):
def assertAlmostEqual(self, data1, data2, precision=3):
if type(data1) == type(data2) == list:
assert np.asarray(data1, dtype=np.float32).ndim == np.asarray(data2,dtype=np.float32).ndim, f'Inputs must have the same dimension! Received {type(data1)} and {type(data2)}'
self.assertEqual(len(data1), len(data2))
for pred, label in zip(data1, data2):
self.assertAlmostEqual(pred, label, precision=precision)
elif type(data1) == type(data2) == dict:
for (pred_key,pred_value), (label_key,label_value) in zip(data1.items(), data2.items()):
self.assertAlmostEqual(pred_value, label_value, precision=precision)
self.assertEqual(len(data1.items()), len(data2.items()))
for (pred_key,pred_value), (label_key,label_value) in zip(data1.items(), data2.items()):
self.assertAlmostEqual(pred_value, label_value, precision=precision)
else:
super().assertAlmostEqual(data1, data2, places=precision)

Expand Down Expand Up @@ -844,15 +846,15 @@ def test_training_values_fir_connect_train_linear_more_window(self):
test.internals['inout_1_2']['XY'])
self.assertAlmostEqual({'inout': [[[0.0], [0.0], [0.0], [W[0][0][0] * 4.0 + W[0][1][0] * 2.0 + b[0][0]],
[W[0][0][0] * 6.0 + W[0][1][0] * 5.0 + b[0][0]]]]},
test.internals['inout_1_0']['state'])
test.internals['inout_1_0']['connect'])
self.assertAlmostEqual({'inout': [[[0.0], [0.0], [W[0][0][0] * 4.0 + W[0][1][0] * 2.0 + b[0][0]],
[W[0][0][0] * 6.0 + W[0][1][0] * 5.0 + b[0][0]],
[W[0][0][0] * 4.0 + W[0][1][0] * 5.0 + b[0][0]]]]},
test.internals['inout_1_1']['state'])
test.internals['inout_1_1']['connect'])
self.assertAlmostEqual({'inout': [
[[0.0], [W[0][0][0] * 4.0 + W[0][1][0] * 2.0 + b[0][0]], [W[0][0][0] * 6.0 + W[0][1][0] * 5.0 + b[0][0]],
[W[0][0][0] * 4.0 + W[0][1][0] * 5.0 + b[0][0]], [W[0][0][0] * 0.0 + W[0][1][0] * 0.0 + b[0][0]]]]},
test.internals['inout_1_2']['state'])
test.internals['inout_1_2']['connect'])
with self.assertRaises(KeyError):
test.internals['inout_2_0']

Expand Down Expand Up @@ -1396,7 +1398,7 @@ def test_train_compare_state_and_closed_loop(self):
target1 = Input('target1')
target2 = Input('target2', dimensions=2)

test = Modely(visualizer=TextVisualizer(), seed=42)
test = Modely(visualizer=None, seed=42)
test.addModel('model', [output1,output2])
test.addMinimize('error1', output1, target1.sw(2))
test.addMinimize('error2', output2, target2.last())
Expand All @@ -1418,7 +1420,7 @@ def test_train_compare_state_and_closed_loop(self):
target1 = Input('target1')
target2 = Input('target2', dimensions=2)

test2 = Modely(visualizer=TextVisualizer(), seed=42)
test2 = Modely(visualizer=None, seed=42)
test2.addModel('model', [output1, output2])
test2.addMinimize('error1', output1, target1.sw(2))
test2.addMinimize('error2', output2, target2.last())
Expand Down
1 change: 0 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def test_linear_interp_with_batched_input_1(self):
y_data = torch.tensor([[0.5],[0.6],[0.7],[0.8],[0.9],[1.0],[1.1],[1.2],[1.3],[1.4]])

y = linear_interp(x,x_data,y_data)
print('Output y of linear_interp with batches:\n', y)
self.assertEqual(y.shape, x.shape) # check that the output has the same shape as the input

if __name__ == '__main__':
Expand Down

0 comments on commit d8b0d04

Please sign in to comment.