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

tonegas / nnodely / 12162489906

04 Dec 2024 03:04PM UTC coverage: 93.934% (-0.2%) from 94.155%
12162489906

push

github

web-flow
Merge pull request #6 from tonegas/release/0.15.0

Release/0.15.0

37 of 42 new or added lines in 8 files covered. (88.1%)

59 existing lines in 7 files now uncovered.

8625 of 9182 relevant lines covered (93.93%)

0.94 hits per line

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

98.11
/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 import relation
1✔
6
relation.CHECK_NAMES = False
1✔
7

8
from nnodely.logger import logging, nnLogger
1✔
9
log = nnLogger(__name__, logging.CRITICAL)
1✔
10
log.setAllLevel(logging.CRITICAL)
1✔
11

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

14
# 3 Tests
15
# Test of visualizers
16

17
class ModelyTestVisualizer(unittest.TestCase):
1✔
18
    def __init__(self, *args, **kwargs):
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
        parfun_x = ParamFun(myFun, parameters=[K_x], constants=[c_v])
1✔
39
        parfun_y = ParamFun(myFun, parameters=[K_y])
1✔
40
        #parfun_z = ParamFun(myFun)
41
        parfun_zz = ParamFun(myFun)
1✔
42
        fir_w = Fir(parameter=w_5)(x.tw(5))
1✔
43
        fir_t = Fir(parameter=t_5)(y.tw(5))
1✔
44
        time_part = TimePart(x.tw(5), i=1, j=3)
1✔
45
        sample_select = SampleSelect(x.sw(5), i=1)
1✔
46

47
        def fuzzyfun(x):
1✔
48
            return torch.sin(x)
×
49

50
        fuzzy = Fuzzify(output_dimension=4, range=[0, 4], functions=fuzzyfun)(x.tw(1))
1✔
51
        fuzzyTriang = Fuzzify(centers=[1, 2, 3, 7])(x.tw(1))
1✔
52

53
        self.out = Output('out', Fir(parfun_x(x.tw(1)) + parfun_y(y.tw(1), c_v)))
1✔
54
        # out = Output('out', Fir(parfun_x(x.tw(1))+parfun_y(y.tw(1),c_v)+parfun_z(x.tw(5),t_5,c_5)))
55
        self.out2 = Output('out2', Add(w, x.tw(1)) + Add(t, y.tw(1)) + Add(w, c))
1✔
56
        self.out3 = Output('out3', Add(fir_w, fir_t))
1✔
57
        self.out4 = Output('out4', Linear(output_dimension=1)(fuzzy+fuzzyTriang))
1✔
58
        self.out5 = Output('out5', Fir(time_part) + Fir(sample_select))
1✔
59
        self.out6 = Output('out6', LocalModel(output_function=Fir())(x.tw(1), fuzzy))
1✔
60
        self.out7 = Output('out7', parfun_zz(z.last()))
1✔
61

62
    def test_export_textvisualizer(self):
1✔
63
        test = Modely(visualizer=TextVisualizer(5), seed=42)
1✔
64
        test.addModel('modelA', self.out)
1✔
65
        test.addModel('modelB', [self.out2, self.out3, self.out4])
1✔
66
        test.addModel('modelC', [self.out4, self.out5, self.out6])
1✔
67
        test.addModel('modelD', self.out7)
1✔
68
        test.addMinimize('error1', self.x.last(), self.out)
1✔
69
        test.addMinimize('error2', self.y.last(), self.out3, loss_function='rmse')
1✔
70
        test.addMinimize('error3', self.z.last(), self.out6, loss_function='rmse')
1✔
71

72
        test.neuralizeModel(0.5)
1✔
73

74
        data_x = np.arange(0.0, 1, 0.1)
1✔
75
        data_y = np.arange(0.0, 1, 0.1)
1✔
76
        a, b = -1.0, 2.0
1✔
77
        dataset = {'x': data_x, 'y': data_y, 'z': a * data_x + b * data_y}
1✔
78
        params = {'num_of_epochs': 10, 'lr': 0.01}
1✔
79
        test.loadData(name='dataset', source=dataset)  # Create the datastest.trainModel(optimizer='SGD', training_params=params)  # Train the traced model
1✔
80
        test.trainModel(optimizer='SGD', training_params=params)
1✔
81

82
    def test_export_mplvisualizer(self):
1✔
83
        m = MPLVisualizer(5)
1✔
84
        test = Modely(visualizer=m, seed=42)
1✔
85
        test.addModel('modelA', self.out)
1✔
86
        test.addModel('modelB', [self.out2, self.out3, self.out4])
1✔
87
        test.addModel('modelC', [self.out4, self.out5, self.out6])
1✔
88
        test.addModel('modelD', self.out7)
1✔
89
        test.addMinimize('error1', self.x.last(), self.out)
1✔
90
        test.addMinimize('error2', self.y.last(), self.out3, loss_function='rmse')
1✔
91
        test.addMinimize('error3', self.z.last(), self.out6, loss_function='rmse')
1✔
92

93
        test.neuralizeModel(0.5)
1✔
94

95
        data_x = np.arange(0.0, 10, 0.1)
1✔
96
        data_y = np.arange(0.0, 10, 0.1)
1✔
97
        a, b = -1.0, 2.0
1✔
98
        dataset = {'x': data_x, 'y': data_y, 'z': a * data_x + b * data_y}
1✔
99
        params = {'num_of_epochs': 10, 'lr': 0.01}
1✔
100
        test.loadData(name='dataset', source=dataset)  # Create the dataset
1✔
101
        test.trainModel(optimizer='SGD', training_params=params)  # Train the traced model
1✔
102
        test.trainModel(optimizer='SGD', training_params=params)
1✔
103
        m.closeResult()
1✔
104
        m.closeTraining()
1✔
105
        list_of_functions = list(test.model_def['Functions'].keys())
1✔
106
        with self.assertRaises(ValueError):
1✔
107
            m.showFunctions(list_of_functions[1])
1✔
108
        m.closeFunctions()
1✔
109
        m.showFunctions(list_of_functions[0])
1✔
110
        m.showFunctions(list_of_functions[4])
1✔
111
        m.showFunctions(list_of_functions[3])
1✔
112
        m.closeFunctions()
1✔
113

114
    def test_export_mplvisualizer2(self):
1✔
115
        x = Input('x')
1✔
116
        F = Input('F')
1✔
117
        def myFun(K1, K2, p1, p2):
1✔
118
            import torch
1✔
119
            return p1 * K1 + p2 * torch.sin(K2)
1✔
120

121
        parfun = ParamFun(myFun)
1✔
122
        out = Output('fun', parfun(x.last(), F.last()))
1✔
123
        m = MPLVisualizer()
1✔
124
        example = Modely(visualizer=m)
1✔
125
        example.addModel('out', out)
1✔
126
        example.neuralizeModel()
1✔
127
        m.showFunctions(list(example.model_def['Functions'].keys()), xlim=[[-5, 5], [-1, 1]])
1✔
128
        m.closeFunctions()
1✔
129

130
    # def test_export_mplnotebookvisualizer(self):
131
    #     m = MPLNotebookVisualizer(5)
132
    #     test = Modely(visualizer=m, seed=42)
133
    #     test.addModel('modelA', self.out)
134
    #     test.addModel('modelB', [self.out2, self.out3, self.out4])
135
    #     test.addModel('modelC', [self.out4, self.out5, self.out6])
136
    #     test.addMinimize('error1', self.x.last(), self.out)
137
    #     test.addMinimize('error2', self.y.last(), self.out3, loss_function='rmse')
138
    #     test.addMinimize('error3', self.z.last(), self.out6, loss_function='rmse')
139
    #
140
    #     test.neuralizeModel(0.5)
141
    #
142
    #     data_x = np.arange(0.0, 10, 0.1)
143
    #     data_y = np.arange(0.0, 10, 0.1)
144
    #     a, b = -1.0, 2.0
145
    #     dataset = {'x': data_x, 'y': data_y, 'z': a * data_x + b * data_y}
146
    #     params = {'num_of_epochs': 1, 'lr': 0.01}
147
    #     test.loadData(name='dataset', source=dataset)  # Create the dataset
148
    #     test.trainModel(optimizer='SGD', training_params=params)  # Train the traced model
149
    #     test.trainModel(optimizer='SGD', training_params=params)
150

151

152
if __name__ == '__main__':
1✔
UNCOV
153
    unittest.main()
×
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

© 2025 Coveralls, Inc