• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tonegas / nnodely / 14359872492

09 Apr 2025 02:33PM UTC coverage: 97.602% (+0.6%) from 97.035%
14359872492

Pull #86

github

web-flow
Merge ec719935a into e9c323c4f
Pull Request #86: Smallclasses

2291 of 2418 new or added lines in 54 files covered. (94.75%)

3 existing lines in 1 file now uncovered.

11683 of 11970 relevant lines covered (97.6%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.33
/tests/test_visualizer.py
1
import sys, os, unittest, torch, shutil
1✔
2
import numpy as np
1✔
3

4
from nnodely import *
1✔
5
from nnodely.basic.relation import NeuObj
1✔
6
from nnodely.support.logger import logging, nnLogger
1✔
7

8
log = nnLogger(__name__, logging.CRITICAL)
1✔
9
log.setAllLevel(logging.CRITICAL)
1✔
10

11
sys.path.append(os.getcwd())
1✔
12

13
# 3 Tests
14
# Test of visualizers
15

16
class ModelyTestVisualizer(unittest.TestCase):
1✔
17
    def __init__(self, *args, **kwargs):
1✔
18
        NeuObj.clearNames()
1✔
19
        super(ModelyTestVisualizer, self).__init__(*args, **kwargs)
1✔
20

21
        self.x = x = Input('x')
1✔
22
        self.y = y = Input('y')
1✔
23
        self.z = z = Input('z')
1✔
24

25
        ## create the relations
26
        def myFun(K1, p1, p2):
1✔
27
            return K1 * p1 * p2
1✔
28

29
        K_x = Parameter('k_x', dimensions=1, tw=1, init='init_constant', init_params={'value': 1})
1✔
30
        K_y = Parameter('k_y', dimensions=1, tw=1)
1✔
31
        w = Parameter('w', dimensions=1, tw=1, init='init_constant', init_params={'value': 1})
1✔
32
        t = Parameter('t', dimensions=1, tw=1)
1✔
33
        c_v = Constant('c_v', tw=1, values=[[1], [2]])
1✔
34
        c = 5
1✔
35
        w_5 = Parameter('w_5', dimensions=1, tw=5)
1✔
36
        t_5 = Parameter('t_5', dimensions=1, tw=5)
1✔
37
        c_5 = [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
1✔
38
        c_5_2 = Constant('c_5_2', tw=5, values=c_5)
1✔
39
        parfun_x = ParamFun(myFun, parameters_and_constants=[K_x,c_v])
1✔
40
        parfun_y = ParamFun(myFun, parameters_and_constants=[K_y])
1✔
41
        parfun_zz = ParamFun(myFun)
1✔
42
        parfun_2d = ParamFun(myFun, parameters_and_constants=[K_x, K_x])
1✔
43
        parfun_3d = ParamFun(myFun, parameters_and_constants=[K_x])
1✔
44
        fir_w = Fir(W=w_5)(x.tw(5))
1✔
45
        fir_t = Fir(W=t_5)(y.tw(5))
1✔
46
        time_part = TimePart(x.tw(5), i=1, j=3)
1✔
47
        sample_select = SampleSelect(x.sw(5), i=1)
1✔
48

49
        def fuzzyfun(x):
1✔
50
            return torch.sin(x)
×
51

52
        def fuzzyfunth(x):
1✔
53
            return torch.tanh(x)
×
54

55
        fuzzy = Fuzzify(output_dimension=4, range=[0, 4], functions=fuzzyfun)(x.tw(1))
1✔
56
        fuzzyTriang = Fuzzify(centers=[1, 2, 3, 7])(x.tw(1))
1✔
57
        fuzzyRect = Fuzzify(centers=[1, 2, 3, 7], functions='Rectangular')(x.tw(1))
1✔
58
        fuzzyList = Fuzzify(centers=[1, 3, 2, 7], functions=[fuzzyfun,fuzzyfunth])(x.tw(1))
1✔
59

60
        self.out = Output('out', Fir(parfun_x(x.tw(1)) + parfun_y(y.tw(1), c_v)))
1✔
61
        self.out2 = Output('out2', Add(w, x.tw(1)) + Add(t, y.tw(1)) + Add(w, c))
1✔
62
        self.out3 = Output('out3', Add(fir_w, fir_t))
1✔
63
        self.out4 = Output('out4', Linear(output_dimension=1)(fuzzy+fuzzyTriang+fuzzyRect+fuzzyList))
1✔
64
        self.out5 = Output('out5', Fir(time_part) + Fir(sample_select))
1✔
65
        self.out6 = Output('out6', LocalModel(output_function=Fir())(x.tw(1), fuzzy))
1✔
66
        self.out7 = Output('out7', parfun_zz(z.last()))
1✔
67
        self.out8 = Output('out8', Fir(parfun_x(x.tw(1)) + parfun_y(y.tw(1), c_v)) + Fir(parfun_zz(x.tw(5), t_5, c_5_2)))
1✔
68
        self.out9 = Output('out9', Fir(parfun_2d(x.tw(1)) + parfun_3d(x.tw(1),x.tw(1))))
1✔
69

70
    def test_export_textvisualizer(self):
1✔
71
        t = TextVisualizer(5)
1✔
72
        test = Modely(visualizer=t, seed=42, workspace='./results')
1✔
73
        test.addModel('modelA', self.out)
1✔
74
        test.addModel('modelB', [self.out2, self.out3, self.out4])
1✔
75
        test.addModel('modelC', [self.out4, self.out5, self.out6])
1✔
76
        test.addModel('modelD', self.out7)
1✔
77
        test.addMinimize('error1', self.x.last(), self.out)
1✔
78
        test.addMinimize('error2', self.y.last(), self.out3, loss_function='rmse')
1✔
79
        test.addMinimize('error3', self.z.last(), self.out6, loss_function='rmse')
1✔
80

81
        test.neuralizeModel(0.5)
1✔
82

83
        data_x = np.arange(0.0, 5, 0.1)
1✔
84
        data_y = np.arange(0.0, 5, 0.1)
1✔
85
        a, b = -1.0, 2.0
1✔
86
        dataset = {'x': data_x, 'y': data_y, 'z': a * data_x + b * data_y}
1✔
87
        params = {'num_of_epochs': 10, 'lr': 0.01}
1✔
88
        test.loadData(name='dataset', source=dataset)  # Create the datastest.trainModel(optimizer='SGD', training_params=params)  # Train the traced model
1✔
89
        t.showMinimize('error1')
1✔
90
        t.showMinimize('error2')
1✔
91
        t.showMinimize('error3')
1✔
92
        test.trainModel(optimizer='SGD', training_params=params)
1✔
93
        t.showWeights()
1✔
94
        test.trainModel(optimizer='SGD', training_params=params, closed_loop={'x':'out'}, prediction_samples=1)
1✔
95
        test.saveModel()
1✔
96
        test.loadModel()
1✔
97

98
        test.neuralizeModel(0.5)
1✔
99
        test.exportPythonModel()
1✔
100
        test.importPythonModel()
1✔
101
        test.exportReport()
1✔
102

103
    def test_export_mplvisualizer(self):
1✔
104
        m = MPLVisualizer(5)
1✔
105
        test = Modely(visualizer=m, seed=42)
1✔
106
        test.addModel('modelA', self.out)
1✔
107
        test.addModel('modelB', [self.out2, self.out3, self.out4])
1✔
108
        test.addModel('modelC', [self.out4, self.out5, self.out6])
1✔
109
        test.addModel('modelD', self.out7)
1✔
110
        test.addModel('modelE', self.out9)
1✔
111
        test.addMinimize('error1', self.x.last(), self.out)
1✔
112
        test.addMinimize('error2', self.y.last(), self.out3, loss_function='rmse')
1✔
113
        test.addMinimize('error3', self.z.last(), self.out6, loss_function='rmse')
1✔
114

115
        test.neuralizeModel(0.5)
1✔
116

117
        data_x = np.arange(0.0, 10, 0.1)
1✔
118
        data_y = np.arange(0.0, 10, 0.1)
1✔
119
        a, b = -1.0, 2.0
1✔
120
        dataset = {'x': data_x, 'y': data_y, 'z': a * data_x + b * data_y}
1✔
121
        params = {'num_of_epochs': 10, 'lr': 0.01}
1✔
122
        test.loadData(name='dataset', source=dataset)  # Create the dataset
1✔
123
        test.trainModel(optimizer='SGD', training_params=params)  # Train the traced model
1✔
124
        test.trainModel(optimizer='SGD', training_params=params)
1✔
125
        m.closeResult()
1✔
126
        m.closeTraining()
1✔
127
        list_of_functions = list(test.json['Functions'].keys())
1✔
128
        try:
1✔
129
            for f in list_of_functions:
1✔
130
                m.showFunctions(f)
1✔
131
        except ValueError:
1✔
132
            pass
1✔
133
        with self.assertRaises(ValueError):
1✔
134
            m.showFunctions(list_of_functions[1])
1✔
135
        m.closeFunctions()
1✔
136

137
    def test_export_mplvisualizer2(self):
1✔
138
        clearNames(['x', 'F'])
1✔
139
        x = Input('x')
1✔
140
        F = Input('F')
1✔
141
        def myFun(K1, K2, p1, p2):
1✔
142
            import torch
1✔
143
            return p1 * K1 + p2 * torch.sin(K2)
1✔
144

145
        parfun = ParamFun(myFun)
1✔
146
        out = Output('fun', parfun(x.last(), F.last()))
1✔
147
        m = MPLVisualizer()
1✔
148
        example = Modely(visualizer=m)
1✔
149
        example.addModel('out', out)
1✔
150
        example.neuralizeModel()
1✔
151
        m.showFunctions(list(example.json['Functions'].keys()), xlim=[[-5, 5], [-1, 1]])
1✔
152
        m.closeFunctions()
1✔
153

154
    def test_export_mplnotebookvisualizer(self):
1✔
155
        m = MPLNotebookVisualizer(5, test=True)
1✔
156
        test = Modely(visualizer=m, seed=42)
1✔
157
        test.addModel('modelB', [self.out2, self.out3, self.out4])
1✔
158
        test.addModel('modelC', [self.out4, self.out5, self.out6])
1✔
159
        test.addModel('modelD', [self.out9])
1✔
160
        test.addMinimize('error2', self.y.last(), self.out3, loss_function='rmse')
1✔
161
        test.addMinimize('error3', self.z.last(), self.out6, loss_function='rmse')
1✔
162

163
        test.neuralizeModel(1)
1✔
164

165
        data_x = np.arange(0.0, 10, 0.1)
1✔
166
        data_y = np.arange(0.0, 10, 0.1)
1✔
167
        a, b = -1.0, 2.0
1✔
168
        dataset = {'x': data_x, 'y': data_y, 'z': a * data_x + b * data_y}
1✔
169
        params = {'num_of_epochs': 1, 'lr': 0.01}
1✔
170
        test.loadData(name='dataset', source=dataset)  # Create the dataset
1✔
171
        test.trainModel(optimizer='SGD', training_params=params)  # Train the traced model
1✔
172
        list_of_functions = list(test.json['Functions'].keys())
1✔
173
        try:
1✔
174
            for f in list_of_functions:
1✔
175
                m.showFunctions(f)
1✔
NEW
176
        except ValueError:
×
NEW
177
            pass
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc