Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/mesh/io/io_pyvista/test_error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class TestToPyvistaErrors:
def test_unsupported_manifold_dims_raises(self):
"""Test that unsupported manifold dimensions raise ValueError."""
# Create a mesh with 4 manifold dims (not supported by PyVista)
torch.manual_seed(0)
points = torch.randn(10, 5) # 5D spatial
cells = torch.randint(0, 10, (5, 5)) # 4-manifold (5 vertices per cell)
mesh = Mesh(points=points, cells=cells)
Expand Down
4 changes: 4 additions & 0 deletions test/mesh/primitives/test_procedural_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TestPerlinNoiseND:
def test_dimension_agnostic(self, n_dims):
"""Test that Perlin noise works for any number of dimensions."""
n_points = 50
torch.manual_seed(0)
points = torch.randn(n_points, n_dims)

noise = perlin_noise_nd(points, scale=1.0, seed=42)
Expand All @@ -50,6 +51,7 @@ def test_dimension_agnostic(self, n_dims):
@pytest.mark.parametrize("seed", [0, 42, 123, 999])
def test_reproducibility(self, seed):
"""Test that same seed produces same output."""
torch.manual_seed(0)
points = torch.randn(100, 3)

noise1 = perlin_noise_nd(points, scale=1.0, seed=seed)
Expand All @@ -59,6 +61,7 @@ def test_reproducibility(self, seed):

def test_different_seeds_produce_different_output(self):
"""Test that different seeds produce different noise patterns."""
torch.manual_seed(0)
points = torch.randn(100, 3)

noise1 = perlin_noise_nd(points, scale=1.0, seed=42)
Expand All @@ -73,6 +76,7 @@ def test_different_seeds_produce_different_output(self):
@pytest.mark.parametrize("scale", [0.1, 0.5, 1.0, 2.0, 5.0])
def test_scale_parameter(self, scale):
"""Test that scale parameter affects noise frequency."""
torch.manual_seed(0)
points = torch.randn(100, 3)

noise = perlin_noise_nd(points, scale=scale, seed=42)
Expand Down
18 changes: 18 additions & 0 deletions test/mesh/utilities/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_get_cached_returns_none_when_not_set(self):
def test_get_cached_returns_none_for_missing_key(self):
"""Test that get_cached returns None for missing key in existing cache."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
data["_cache"] = TensorDict({"centroids": torch.randn(10, 3)}, batch_size=[10])

result = get_cached(data, "areas")
Expand All @@ -50,6 +51,7 @@ def test_get_cached_returns_none_for_missing_key(self):
def test_get_cached_returns_value_when_set(self):
"""Test that get_cached returns the cached value when present."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
cached_value = torch.randn(10, 3)
data["_cache"] = TensorDict({"centroids": cached_value}, batch_size=[10])

Expand All @@ -73,6 +75,7 @@ class TestSetCached:
def test_set_cached_creates_cache_if_missing(self):
"""Test that set_cached creates _cache TensorDict if not present."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
value = torch.randn(10, 3)

set_cached(data, "centroids", value)
Expand All @@ -83,6 +86,7 @@ def test_set_cached_creates_cache_if_missing(self):
def test_set_cached_stores_value(self):
"""Test that set_cached stores the value correctly."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
value = torch.randn(10, 3)

set_cached(data, "areas", value)
Expand All @@ -93,6 +97,7 @@ def test_set_cached_stores_value(self):
def test_set_cached_overwrites_existing(self):
"""Test that set_cached overwrites existing cached value."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
old_value = torch.randn(10, 3)
new_value = torch.randn(10, 3)

Expand All @@ -106,6 +111,7 @@ def test_set_cached_overwrites_existing(self):
def test_set_cached_multiple_keys(self):
"""Test that set_cached can store multiple keys."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
centroids = torch.randn(10, 3)
areas = torch.randn(10)
normals = torch.randn(10, 3)
Expand Down Expand Up @@ -136,6 +142,7 @@ def test_roundtrip_scalar(self):
def test_roundtrip_1d(self):
"""Test round-trip with 1D tensor."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
value = torch.randn(10)

set_cached(data, "areas", value)
Expand All @@ -147,6 +154,7 @@ def test_roundtrip_1d(self):
def test_roundtrip_2d(self):
"""Test round-trip with 2D tensor."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
value = torch.randn(10, 3)

set_cached(data, "centroids", value)
Expand All @@ -158,6 +166,7 @@ def test_roundtrip_2d(self):
def test_roundtrip_3d(self):
"""Test round-trip with 3D tensor."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
value = torch.randn(10, 3, 3)

set_cached(data, "stress", value)
Expand All @@ -172,6 +181,7 @@ class TestCacheWithExistingData:

def test_cache_does_not_affect_existing_data(self):
"""Test that caching doesn't affect existing non-cache data."""
torch.manual_seed(0)
data = TensorDict({"temperature": torch.randn(10)}, batch_size=[10])
original_temp = data["temperature"].clone()

Expand All @@ -181,6 +191,7 @@ def test_cache_does_not_affect_existing_data(self):

def test_get_cached_ignores_non_cache_keys(self):
"""Test that get_cached only looks in _cache namespace."""
torch.manual_seed(0)
data = TensorDict({"areas": torch.randn(10)}, batch_size=[10])

# Even though "areas" exists at top level, get_cached looks in _cache
Expand All @@ -190,6 +201,7 @@ def test_get_cached_ignores_non_cache_keys(self):

def test_cache_coexists_with_data(self):
"""Test that cache and regular data coexist."""
torch.manual_seed(0)
data = TensorDict(
{
"temperature": torch.randn(10),
Expand All @@ -212,6 +224,7 @@ class TestCacheDevices:
def test_cache_cpu(self):
"""Test caching on CPU TensorDict."""
data = TensorDict({}, batch_size=[10], device="cpu")
torch.manual_seed(0)
value = torch.randn(10, 3, device="cpu")

set_cached(data, "centroids", value)
Expand All @@ -224,6 +237,7 @@ def test_cache_cpu(self):
def test_cache_cuda(self):
"""Test caching on CUDA TensorDict."""
data = TensorDict({}, batch_size=[10], device="cuda")
torch.manual_seed(0)
value = torch.randn(10, 3, device="cuda")

set_cached(data, "centroids", value)
Expand All @@ -242,6 +256,7 @@ class TestCacheDtypes:
def test_cache_various_dtypes(self, dtype):
"""Test caching with various dtypes."""
data = TensorDict({}, batch_size=[10])
torch.manual_seed(0)
if dtype in [torch.float32, torch.float64]:
value = torch.randn(10, dtype=dtype)
else:
Expand All @@ -267,6 +282,7 @@ def test_cell_data_cache_pattern(self):
assert cached_areas is None

# Compute and cache
torch.manual_seed(0)
computed_areas = torch.randn(100)
set_cached(cell_data, "areas", computed_areas)

Expand All @@ -281,6 +297,7 @@ def test_point_data_cache_pattern(self):
point_data = TensorDict({}, batch_size=[500])

# Cache point normals
torch.manual_seed(0)
normals = torch.randn(500, 3)
set_cached(point_data, "normals", normals)

Expand All @@ -294,6 +311,7 @@ def test_multiple_caches_pattern(self):
cell_data = TensorDict({}, batch_size=[100])

# Cache multiple properties
torch.manual_seed(0)
set_cached(cell_data, "centroids", torch.randn(100, 3))
set_cached(cell_data, "areas", torch.randn(100))
set_cached(cell_data, "normals", torch.randn(100, 3))
Expand Down
1 change: 1 addition & 0 deletions test/mesh/utilities/test_scatter_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def test_all_aggregation_modes(self, aggregation):
@pytest.mark.parametrize("n_dst", [1, 2, 5, 10])
def test_various_n_dst(self, n_dst):
"""Test with various destination counts."""
torch.manual_seed(0)
src_data = torch.randn(20)
src_to_dst = torch.randint(0, n_dst, (20,))

Expand Down
2 changes: 2 additions & 0 deletions test/metrics/test_metrics_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def test_climate_acc_mse(test_data, device, rtol: float = 1e-3, atol: float = 1e


def test_climate_reductions(test_data, device, rtol: float = 1e-3, atol: float = 1e-3):
torch.manual_seed(0)
channels, lon, lat, pred_tensor_np, targ_tensor_np, time_means = test_data
pred_tensor = torch.from_numpy(pred_tensor_np).expand(channels, -1, -1).to(device)
lat = torch.from_numpy(lat).to(device)
Expand Down Expand Up @@ -247,6 +248,7 @@ def test_climate_reductions(test_data, device, rtol: float = 1e-3, atol: float =


def test_climate_efi(test_data, device, rtol: float = 1e-1, atol: float = 1e-1):
torch.manual_seed(0)
one = torch.ones((1, 1), dtype=torch.float32, device=device)
bin_edges = hist.linspace(-10 * one, 10 * one, 30)
bin_mids = 0.5 * bin_edges[1:] + 0.5 * bin_edges[:-1]
Expand Down
10 changes: 10 additions & 0 deletions test/metrics/test_metrics_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def get_disagreements(inputs, bins, counts, test):

@pytest.mark.parametrize("input_shape", [(1, 72, 144)])
def test_histogram(device, input_shape, rtol: float = 1e-3, atol: float = 1e-3):
torch.manual_seed(0)
np.random.seed(0)
x = torch.randn([10, *input_shape], device=device)
y = torch.randn([5, *input_shape], device=device)

Expand Down Expand Up @@ -224,6 +226,7 @@ def fair_crps(pred, obs, dim=-1):


def test_fair_crps_greater_than_zero(device):
torch.manual_seed(0)
pred = torch.randn(5, 10, device=device)
obs = torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0], device=device)
assert torch.all(fair_crps(pred, obs, dim=-1) > 0)
Expand All @@ -233,6 +236,7 @@ def test_fair_crps_is_fair(device):
# fair means that a random prediction should outperform a non-random one on average
# This is not always true of ``crps``...try replacing fair_crps function
# below with ``crps``
torch.manual_seed(0)
n = 256
random_pred = torch.randn(n, 2, device=device)
cheating_pred = torch.zeros((n, 2), device=device)
Expand Down Expand Up @@ -341,6 +345,7 @@ def test_crps_finite(device, num, biased):
def test_crps(device, rtol: float = 1e-3, atol: float = 1e-3):
# Uses eq (5) from Gneiting et al. https://doi.org/10.1175/MWR2904.1
# crps(N(0, 1), 0.0) = 2 / sqrt(2*pi) - 1/sqrt(pi) ~= 0.23...
torch.manual_seed(0)
x = torch.randn((1_000_000, 1), device=device, dtype=torch.float32)
y = torch.zeros((1,), device=device, dtype=torch.float32)

Expand Down Expand Up @@ -526,6 +531,7 @@ def test_crps(device, rtol: float = 1e-3, atol: float = 1e-3):
@pytest.mark.parametrize("mean", [3.0])
@pytest.mark.parametrize("variance", [0.1])
def test_wasserstein(device, mean, variance, rtol: float = 1e-3, atol: float = 1e-3):
torch.manual_seed(0)
mean = torch.as_tensor([mean], device=device, dtype=torch.float32)
variance = torch.as_tensor([variance], device=device, dtype=torch.float32)

Expand Down Expand Up @@ -596,6 +602,7 @@ def test_means_var(device, rtol: float = 1e-3, atol: float = 1e-3):
if not torch.cuda.is_available():
pytest.skip("CUDA required for this test.")

torch.manual_seed(0)
DistributedManager._shared_state = {}
if (device == "cuda:0") and (not DistributedManager.is_initialized()):
os.environ["MASTER_ADDR"] = "localhost"
Expand Down Expand Up @@ -697,6 +704,8 @@ def test_means_var(device, rtol: float = 1e-3, atol: float = 1e-3):


def test_calibration(device, rtol: float = 1e-2, atol: float = 1e-2):
torch.manual_seed(0)
np.random.seed(0)
x = torch.randn((10_000, 30, 30), device=device, dtype=torch.float32)
y = torch.randn((30, 30), device=device, dtype=torch.float32)

Expand Down Expand Up @@ -766,6 +775,7 @@ def test_calibration(device, rtol: float = 1e-2, atol: float = 1e-2):


def test_entropy(device, rtol: float = 1e-2, atol: float = 1e-2):
torch.manual_seed(0)
one = torch.ones([1], device=device, dtype=torch.float32)

x = torch.randn((50_000, 10, 10), device=device, dtype=torch.float32)
Expand Down
5 changes: 5 additions & 0 deletions test/models/meshgraphnet/test_meshgraphnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def test_meshgraphnet_checkpoint(device, pytestconfig, set_physicsnemo_force_te)

from physicsnemo.models.meshgraphnet import MeshGraphNet

# Seed for reproducibility
torch.manual_seed(0)
np.random.seed(0)
random.seed(0)

# Construct MGN model
model_1 = MeshGraphNet(
input_dim_nodes=4,
Expand Down