13
13
14
14
"""Rest everything follows."""
15
15
16
- import time
17
16
import torch
18
17
import unittest
19
- from dataclasses import MISSING
20
18
21
19
import omni .isaac .lab .utils .noise as noise
22
- from omni .isaac .lab .utils import configclass
23
-
24
20
25
21
26
22
class TestNoise (unittest .TestCase ):
@@ -29,96 +25,94 @@ class TestNoise(unittest.TestCase):
29
25
def test_gaussian_noise (self ):
30
26
"""Test guassian_noise function."""
31
27
32
- for device in ["cpu" ,"cuda" ]:
33
- for noise_device in ["cpu" ,"cuda" ]:
34
- for op in ["add" ,"scale" ,"abs" ]:
28
+ for device in ["cpu" , "cuda" ]:
29
+ for noise_device in ["cpu" , "cuda" ]:
30
+ for op in ["add" , "scale" , "abs" ]:
35
31
with self .subTest (device = device , noise_device = noise_device , operation = op ):
36
32
# create random data set
37
33
data = torch .rand (10000 , 3 , device = device )
38
34
# define standard deviation and mean
39
- std = torch .tensor ([0.1 ,0.2 ,0.3 ],device = noise_device )
40
- mean = torch .tensor ([0.4 ,0.5 ,0.6 ],device = noise_device )
35
+ std = torch .tensor ([0.1 , 0.2 , 0.3 ], device = noise_device )
36
+ mean = torch .tensor ([0.4 , 0.5 , 0.6 ], device = noise_device )
41
37
# create noise config
42
- noise_cfg = noise .GaussianNoiseCfg (std = std ,
43
- mean = mean ,
44
- operation = op )
38
+ noise_cfg = noise .GaussianNoiseCfg (std = std , mean = mean , operation = op )
45
39
46
40
for i in range (10 ):
47
41
# apply noise
48
- noisy_data = noise_cfg .func (data ,cfg = noise_cfg )
42
+ noisy_data = noise_cfg .func (data , cfg = noise_cfg )
49
43
# calculate resulting noise compared to original data set
50
- if op == "add" :
51
- std_result , mean_result = torch .std_mean (noisy_data - data ,dim = 0 )
52
- elif op == "scale" :
53
- std_result , mean_result = torch .std_mean (noisy_data / data ,dim = 0 )
54
- elif op == "abs" :
55
- std_result , mean_result = torch .std_mean (noisy_data ,dim = 0 )
56
-
57
- self .assertTrue (noise_cfg .mean .device ,device )
58
- self .assertTrue (noise_cfg .std .device ,device )
59
- torch .testing .assert_close (noise_cfg .std ,std_result ,atol = 1e-2 ,rtol = 1e-2 )
60
- torch .testing .assert_close (noise_cfg .mean ,mean_result ,atol = 1e-2 ,rtol = 1e-2 )
61
-
44
+ if op == "add" :
45
+ std_result , mean_result = torch .std_mean (noisy_data - data , dim = 0 )
46
+ elif op == "scale" :
47
+ std_result , mean_result = torch .std_mean (noisy_data / data , dim = 0 )
48
+ elif op == "abs" :
49
+ std_result , mean_result = torch .std_mean (noisy_data , dim = 0 )
50
+
51
+ self .assertTrue (noise_cfg .mean .device , device )
52
+ self .assertTrue (noise_cfg .std .device , device )
53
+ torch .testing .assert_close (noise_cfg .std , std_result , atol = 1e-2 , rtol = 1e-2 )
54
+ torch .testing .assert_close (noise_cfg .mean , mean_result , atol = 1e-2 , rtol = 1e-2 )
62
55
63
56
def test_uniform_noise (self ):
64
57
"""Test uniform_noise function."""
65
- for device in ["cpu" ,"cuda" ]:
66
- for noise_device in ["cpu" ,"cuda" ]:
67
- for op in ["add" ,"scale" ,"abs" ]:
68
- with self .subTest (device = device , noise_device = noise_device ,operation = op ):
58
+ for device in ["cpu" , "cuda" ]:
59
+ for noise_device in ["cpu" , "cuda" ]:
60
+ for op in ["add" , "scale" , "abs" ]:
61
+ with self .subTest (device = device , noise_device = noise_device , operation = op ):
69
62
# create random data set
70
63
data = torch .rand (10000 , 3 , device = device )
71
64
# define uniform minimum and maximum
72
- n_min = torch .tensor ([0.1 ,0.2 ,0.3 ],device = noise_device )
73
- n_max = torch .tensor ([0.4 ,0.5 ,0.6 ],device = noise_device )
65
+ n_min = torch .tensor ([0.1 , 0.2 , 0.3 ], device = noise_device )
66
+ n_max = torch .tensor ([0.4 , 0.5 , 0.6 ], device = noise_device )
74
67
# create noise config
75
- noise_cfg = noise .UniformNoiseCfg (n_max = n_max , n_min = n_min ,operation = op )
76
-
68
+ noise_cfg = noise .UniformNoiseCfg (n_max = n_max , n_min = n_min , operation = op )
69
+
77
70
for i in range (10 ):
78
71
# apply noise
79
- noisy_data = noise_cfg .func (data ,cfg = noise_cfg )
72
+ noisy_data = noise_cfg .func (data , cfg = noise_cfg )
80
73
# calculate resulting noise compared to original data set
81
- if op == "add" :
82
- min_result , _ = torch .min (noisy_data - data ,dim = 0 )
83
- max_result , _ = torch .max (noisy_data - data ,dim = 0 )
84
- elif op == "scale" :
85
- min_result , _ = torch .min (torch .div (noisy_data ,data ),dim = 0 )
86
- max_result , _ = torch .max (torch .div (noisy_data ,data ),dim = 0 )
87
- elif op == "abs" :
88
- min_result , _ = torch .min (noisy_data ,dim = 0 )
89
- max_result , _ = torch .max (noisy_data ,dim = 0 )
90
-
91
- self .assertTrue (noise_cfg .n_min .device ,device )
92
- self .assertTrue (noise_cfg .n_max .device ,device )
74
+ if op == "add" :
75
+ min_result , _ = torch .min (noisy_data - data , dim = 0 )
76
+ max_result , _ = torch .max (noisy_data - data , dim = 0 )
77
+ elif op == "scale" :
78
+ min_result , _ = torch .min (torch .div (noisy_data , data ), dim = 0 )
79
+ max_result , _ = torch .max (torch .div (noisy_data , data ), dim = 0 )
80
+ elif op == "abs" :
81
+ min_result , _ = torch .min (noisy_data , dim = 0 )
82
+ max_result , _ = torch .max (noisy_data , dim = 0 )
83
+
84
+ self .assertTrue (noise_cfg .n_min .device , device )
85
+ self .assertTrue (noise_cfg .n_max .device , device )
93
86
self .assertTrue (all (torch .le (noise_cfg .n_min , min_result ).tolist ()))
94
87
self .assertTrue (all (torch .ge (noise_cfg .n_max , max_result ).tolist ()))
95
88
96
89
def test_constant_noise (self ):
97
90
"""Test constant_noise"""
98
- for device in ["cpu" ,"cuda" ]:
99
- for noise_device in ["cpu" ,"cuda" ]:
100
- for op in ["add" ,"scale" ,"abs" ]:
101
- with self .subTest (device = device , noise_device = noise_device ,operation = op ):
91
+ for device in ["cpu" , "cuda" ]:
92
+ for noise_device in ["cpu" , "cuda" ]:
93
+ for op in ["add" , "scale" , "abs" ]:
94
+ with self .subTest (device = device , noise_device = noise_device , operation = op ):
102
95
# create random data set
103
96
data = torch .rand (10000 , 3 , device = device )
104
97
# define a bias
105
- bias = torch .tensor ([0.1 ,0.2 ,0.3 ],device = noise_device )
98
+ bias = torch .tensor ([0.1 , 0.2 , 0.3 ], device = noise_device )
106
99
# create noise config
107
100
noise_cfg = noise .ConstantNoiseCfg (bias = bias , operation = op )
108
-
101
+
109
102
for i in range (10 ):
110
103
# apply noise
111
- noisy_data = noise_cfg .func (data ,cfg = noise_cfg )
104
+ noisy_data = noise_cfg .func (data , cfg = noise_cfg )
112
105
# calculate resulting noise compared to original data set
113
- if op == "add" :
114
- bias_result = noisy_data - data
115
- elif op == "scale" :
116
- bias_result = noisy_data / data
117
- elif op == "abs" :
106
+ if op == "add" :
107
+ bias_result = noisy_data - data
108
+ elif op == "scale" :
109
+ bias_result = noisy_data / data
110
+ elif op == "abs" :
118
111
bias_result = noisy_data
119
112
120
- self .assertTrue (noise_cfg .bias .device ,device )
121
- torch .testing .assert_close (noise_cfg .bias .repeat (data .shape [0 ],1 ),bias_result )
113
+ self .assertTrue (noise_cfg .bias .device , device )
114
+ torch .testing .assert_close (noise_cfg .bias .repeat (data .shape [0 ], 1 ), bias_result )
115
+
122
116
123
117
if __name__ == "__main__" :
124
- run_tests ()
118
+ run_tests ()
0 commit comments