Skip to content

Commit ed0d00e

Browse files
committed
formatting
1 parent bf3cc4e commit ed0d00e

File tree

2 files changed

+76
-80
lines changed

2 files changed

+76
-80
lines changed

source/extensions/omni.isaac.lab/omni/isaac/lab/utils/noise/noise_model.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@
1919

2020
def constant_noise(data: torch.Tensor, cfg: noise_cfg.ConstantNoiseCfg) -> torch.Tensor:
2121
"""Applies a constant noise bias to a given data set.
22-
22+
2323
Args:
2424
data: The unmodified data set to apply noise to.
2525
cfg: The configuration parameters for constant noise.
26-
26+
2727
Returns:
28-
The data modified by the noise parameters provided.
28+
The data modified by the noise parameters provided.
2929
"""
3030

3131
# fix tensor device for bias on first call and update config parameters
32-
if isinstance(cfg.bias,torch.Tensor):
32+
if isinstance(cfg.bias, torch.Tensor):
3333
if cfg.bias.device is not data.device:
34-
cfg.bias = cfg.bias.to(data.device,)
34+
cfg.bias = cfg.bias.to(
35+
data.device,
36+
)
3537

3638
if cfg.operation == "add":
3739
return data + cfg.bias
@@ -45,23 +47,23 @@ def constant_noise(data: torch.Tensor, cfg: noise_cfg.ConstantNoiseCfg) -> torch
4547

4648
def uniform_noise(data: torch.Tensor, cfg: noise_cfg.UniformNoiseCfg) -> torch.Tensor:
4749
"""Applies a uniform noise to a given data set.
48-
50+
4951
Args:
5052
data: The unmodified data set to apply noise to.
5153
cfg: The configuration parameters for uniform noise.
52-
54+
5355
Returns:
54-
The data modified by the noise parameters provided.
56+
The data modified by the noise parameters provided.
5557
"""
56-
58+
5759
# fix tensor device for n_max on first call and update config parameters
58-
if isinstance(cfg.n_max,torch.Tensor):
60+
if isinstance(cfg.n_max, torch.Tensor):
5961
if cfg.n_max.device is not data.device:
6062
cfg.n_max = cfg.n_max.to(data.device)
6163
# fix tensor device for n_min on first call and update config parameters
62-
if isinstance(cfg.n_min,torch.Tensor):
64+
if isinstance(cfg.n_min, torch.Tensor):
6365
if cfg.n_min.device is not data.device:
64-
cfg.n_min = cfg.n_min.to(data.device)
66+
cfg.n_min = cfg.n_min.to(data.device)
6567

6668
if cfg.operation == "add":
6769
return data + torch.rand_like(data) * (cfg.n_max - cfg.n_min) + cfg.n_min
@@ -75,23 +77,23 @@ def uniform_noise(data: torch.Tensor, cfg: noise_cfg.UniformNoiseCfg) -> torch.T
7577

7678
def gaussian_noise(data: torch.Tensor, cfg: noise_cfg.GaussianNoiseCfg) -> torch.Tensor:
7779
"""Applies a gaussian noise to a given data set.
78-
80+
7981
Args:
8082
data: The unmodified data set to apply noise to.
8183
cfg: The configuration parameters for gaussian noise.
82-
84+
8385
Returns:
84-
The data modified by the noise parameters provided.
86+
The data modified by the noise parameters provided.
8587
"""
8688

8789
# fix tensor device for mean on first call and update config parameters
88-
if isinstance(cfg.mean,torch.Tensor):
90+
if isinstance(cfg.mean, torch.Tensor):
8991
if cfg.mean.device is not data.device:
9092
cfg.mean = cfg.mean.to(data.device)
91-
# fix tensor device for std on first call and update config parameters
92-
if isinstance(cfg.std,torch.Tensor):
93+
# fix tensor device for std on first call and update config parameters
94+
if isinstance(cfg.std, torch.Tensor):
9395
if cfg.std.device is not data.device:
94-
cfg.std = cfg.std.to(data.device)
96+
cfg.std = cfg.std.to(data.device)
9597

9698
if cfg.operation == "add":
9799
return data + cfg.mean + cfg.std * torch.randn_like(data)

source/extensions/omni.isaac.lab/test/utils/test_noise.py

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@
1313

1414
"""Rest everything follows."""
1515

16-
import time
1716
import torch
1817
import unittest
19-
from dataclasses import MISSING
2018

2119
import omni.isaac.lab.utils.noise as noise
22-
from omni.isaac.lab.utils import configclass
23-
2420

2521

2622
class TestNoise(unittest.TestCase):
@@ -29,96 +25,94 @@ class TestNoise(unittest.TestCase):
2925
def test_gaussian_noise(self):
3026
"""Test guassian_noise function."""
3127

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"]:
3531
with self.subTest(device=device, noise_device=noise_device, operation=op):
3632
# create random data set
3733
data = torch.rand(10000, 3, device=device)
3834
# 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)
4137
# 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)
4539

4640
for i in range(10):
4741
# apply noise
48-
noisy_data = noise_cfg.func(data,cfg=noise_cfg)
42+
noisy_data = noise_cfg.func(data, cfg=noise_cfg)
4943
# 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)
6255

6356
def test_uniform_noise(self):
6457
"""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):
6962
# create random data set
7063
data = torch.rand(10000, 3, device=device)
7164
# 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)
7467
# 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+
7770
for i in range(10):
7871
# apply noise
79-
noisy_data = noise_cfg.func(data,cfg=noise_cfg)
72+
noisy_data = noise_cfg.func(data, cfg=noise_cfg)
8073
# 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)
9386
self.assertTrue(all(torch.le(noise_cfg.n_min, min_result).tolist()))
9487
self.assertTrue(all(torch.ge(noise_cfg.n_max, max_result).tolist()))
9588

9689
def test_constant_noise(self):
9790
"""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):
10295
# create random data set
10396
data = torch.rand(10000, 3, device=device)
10497
# 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)
10699
# create noise config
107100
noise_cfg = noise.ConstantNoiseCfg(bias=bias, operation=op)
108-
101+
109102
for i in range(10):
110103
# apply noise
111-
noisy_data = noise_cfg.func(data,cfg=noise_cfg)
104+
noisy_data = noise_cfg.func(data, cfg=noise_cfg)
112105
# 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":
118111
bias_result = noisy_data
119112

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+
122116

123117
if __name__ == "__main__":
124-
run_tests()
118+
run_tests()

0 commit comments

Comments
 (0)