Fix Codacy Warnings (#477)

---------

Co-authored-by: Dario Coscia <dariocos99@gmail.com>
This commit is contained in:
Filippo Olivo
2025-03-10 15:38:45 +01:00
committed by Nicola Demo
parent e3790e049a
commit 4177bfbb50
157 changed files with 3473 additions and 3839 deletions

View File

@@ -4,26 +4,27 @@ import pytest
from pina.label_tensor import LabelTensor
data = torch.rand((20, 3))
labels_column = {1: {"name": "space", "dof": ['x', 'y', 'z']}}
labels_column = {1: {"name": "space", "dof": ["x", "y", "z"]}}
labels_row = {0: {"name": "samples", "dof": range(20)}}
labels_list = ['x', 'y', 'z']
labels_list = ["x", "y", "z"]
labels_all = labels_column.copy()
labels_all.update(labels_row)
@pytest.mark.parametrize("labels",
[labels_column, labels_row, labels_all, labels_list])
@pytest.mark.parametrize(
"labels", [labels_column, labels_row, labels_all, labels_list]
)
def test_constructor(labels):
print(LabelTensor(data, labels))
def test_wrong_constructor():
with pytest.raises(ValueError):
LabelTensor(data, ['a', 'b'])
LabelTensor(data, ["a", "b"])
@pytest.mark.parametrize("labels", [labels_column, labels_all])
@pytest.mark.parametrize("labels_te", ['z', ['z'], {'space': ['z']}])
@pytest.mark.parametrize("labels_te", ["z", ["z"], {"space": ["z"]}])
def test_extract_column(labels, labels_te):
tensor = LabelTensor(data, labels)
new = tensor.extract(labels_te)
@@ -34,7 +35,7 @@ def test_extract_column(labels, labels_te):
@pytest.mark.parametrize("labels", [labels_row, labels_all])
@pytest.mark.parametrize("labels_te", [{'samples': [2]}])
@pytest.mark.parametrize("labels_te", [{"samples": [2]}])
def test_extract_row(labels, labels_te):
tensor = LabelTensor(data, labels)
new = tensor.extract(labels_te)
@@ -44,13 +45,10 @@ def test_extract_row(labels, labels_te):
assert torch.all(torch.isclose(data[2].reshape(1, -1), new))
@pytest.mark.parametrize("labels_te", [{
'samples': [2],
'space': ['z']
}, {
'space': 'z',
'samples': 2
}])
@pytest.mark.parametrize(
"labels_te",
[{"samples": [2], "space": ["z"]}, {"space": "z", "samples": 2}],
)
def test_extract_2D(labels_te):
labels = labels_all
tensor = LabelTensor(data, labels)
@@ -64,16 +62,10 @@ def test_extract_2D(labels_te):
def test_extract_3D():
data = torch.rand(20, 3, 4)
labels = {
1: {
"name": "space",
"dof": ['x', 'y', 'z']
},
2: {
"name": "time",
"dof": range(4)
},
1: {"name": "space", "dof": ["x", "y", "z"]},
2: {"name": "time", "dof": range(4)},
}
labels_te = {'space': ['x', 'z'], 'time': range(1, 4)}
labels_te = {"space": ["x", "z"], "time": range(1, 4)}
tensor = LabelTensor(data, labels)
new = tensor.extract(labels_te)
@@ -91,65 +83,65 @@ def test_extract_3D():
def test_concatenation_3D():
data_1 = torch.rand(20, 3, 4)
labels_1 = ['x', 'y', 'z', 'w']
labels_1 = ["x", "y", "z", "w"]
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(50, 3, 4)
labels_2 = ['x', 'y', 'z', 'w']
labels_2 = ["x", "y", "z", "w"]
lt2 = LabelTensor(data_2, labels_2)
lt_cat = LabelTensor.cat([lt1, lt2])
assert lt_cat.shape == (70, 3, 4)
assert lt_cat.full_labels[0]['dof'] == range(70)
assert lt_cat.full_labels[1]['dof'] == range(3)
assert lt_cat.full_labels[2]['dof'] == ['x', 'y', 'z', 'w']
assert lt_cat.full_labels[0]["dof"] == range(70)
assert lt_cat.full_labels[1]["dof"] == range(3)
assert lt_cat.full_labels[2]["dof"] == ["x", "y", "z", "w"]
data_1 = torch.rand(20, 3, 4)
labels_1 = ['x', 'y', 'z', 'w']
labels_1 = ["x", "y", "z", "w"]
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 2, 4)
labels_2 = ['x', 'y', 'z', 'w']
labels_2 = ["x", "y", "z", "w"]
lt2 = LabelTensor(data_2, labels_2)
lt_cat = LabelTensor.cat([lt1, lt2], dim=1)
assert lt_cat.shape == (20, 5, 4)
assert lt_cat.full_labels[0]['dof'] == range(20)
assert lt_cat.full_labels[1]['dof'] == range(5)
assert lt_cat.full_labels[2]['dof'] == ['x', 'y', 'z', 'w']
assert lt_cat.full_labels[0]["dof"] == range(20)
assert lt_cat.full_labels[1]["dof"] == range(5)
assert lt_cat.full_labels[2]["dof"] == ["x", "y", "z", "w"]
data_1 = torch.rand(20, 3, 2)
labels_1 = ['x', 'y']
labels_1 = ["x", "y"]
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 3, 3)
labels_2 = ['z', 'w', 'a']
labels_2 = ["z", "w", "a"]
lt2 = LabelTensor(data_2, labels_2)
lt_cat = LabelTensor.cat([lt1, lt2], dim=2)
assert lt_cat.shape == (20, 3, 5)
assert lt_cat.full_labels[2]['dof'] == ['x', 'y', 'z', 'w', 'a']
assert lt_cat.full_labels[0]['dof'] == range(20)
assert lt_cat.full_labels[1]['dof'] == range(3)
assert lt_cat.full_labels[2]["dof"] == ["x", "y", "z", "w", "a"]
assert lt_cat.full_labels[0]["dof"] == range(20)
assert lt_cat.full_labels[1]["dof"] == range(3)
data_1 = torch.rand(20, 2, 4)
labels_1 = ['x', 'y', 'z', 'w']
labels_1 = ["x", "y", "z", "w"]
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 3, 4)
labels_2 = ['x', 'y', 'z', 'w']
labels_2 = ["x", "y", "z", "w"]
lt2 = LabelTensor(data_2, labels_2)
with pytest.raises(RuntimeError):
LabelTensor.cat([lt1, lt2], dim=2)
data_1 = torch.rand(20, 3, 2)
labels_1 = ['x', 'y']
labels_1 = ["x", "y"]
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 3, 3)
labels_2 = ['z', 'w', 'a']
labels_2 = ["z", "w", "a"]
lt2 = LabelTensor(data_2, labels_2)
lt_cat = LabelTensor.cat([lt1, lt2], dim=2)
assert lt_cat.shape == (20, 3, 5)
assert lt_cat.full_labels[2]['dof'] == ['x', 'y', 'z', 'w', 'a']
assert lt_cat.full_labels[0]['dof'] == range(20)
assert lt_cat.full_labels[1]['dof'] == range(3)
assert lt_cat.full_labels[2]["dof"] == ["x", "y", "z", "w", "a"]
assert lt_cat.full_labels[0]["dof"] == range(20)
assert lt_cat.full_labels[1]["dof"] == range(3)
def test_summation():
lt1 = LabelTensor(torch.ones(20, 3), labels_all)
lt2 = LabelTensor(torch.ones(30, 3), ['x', 'y', 'z'])
lt2 = LabelTensor(torch.ones(30, 3), ["x", "y", "z"])
with pytest.raises(RuntimeError):
LabelTensor.summation([lt1, lt2])
lt1 = LabelTensor(torch.ones(20, 3), labels_all)
@@ -159,7 +151,7 @@ def test_summation():
assert lt_sum.shape[0] == 20
assert lt_sum.shape[1] == 3
assert lt_sum.full_labels[0] == labels_all[0]
assert lt_sum.labels == ['x+x', 'y+y', 'z+z']
assert lt_sum.labels == ["x+x", "y+y", "z+z"]
assert torch.eq(lt_sum.tensor, torch.ones(20, 3) * 2).all()
lt1 = LabelTensor(torch.ones(20, 3), labels_all)
lt2 = LabelTensor(torch.ones(20, 3), labels_all)
@@ -169,84 +161,72 @@ def test_summation():
assert lt_sum.shape[0] == 20
assert lt_sum.shape[1] == 3
assert lt_sum.full_labels[0] == labels_all[0]
assert lt_sum.labels == ['x+x+x', 'y+y+y', 'z+z+z']
assert lt_sum.labels == ["x+x+x", "y+y+y", "z+z+z"]
assert torch.eq(lt_sum.tensor, torch.ones(20, 3) * 2).all()
def test_append_3D():
data_1 = torch.rand(20, 3, 2)
labels_1 = ['x', 'y']
labels_1 = ["x", "y"]
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 3, 2)
labels_2 = ['z', 'w']
labels_2 = ["z", "w"]
lt2 = LabelTensor(data_2, labels_2)
lt1 = lt1.append(lt2)
assert lt1.shape == (20, 3, 4)
assert lt1.full_labels[0]['dof'] == range(20)
assert lt1.full_labels[1]['dof'] == range(3)
assert lt1.full_labels[2]['dof'] == ['x', 'y', 'z', 'w']
assert lt1.full_labels[0]["dof"] == range(20)
assert lt1.full_labels[1]["dof"] == range(3)
assert lt1.full_labels[2]["dof"] == ["x", "y", "z", "w"]
def test_append_2D():
data_1 = torch.rand(20, 2)
labels_1 = ['x', 'y']
labels_1 = ["x", "y"]
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 2)
labels_2 = ['z', 'w']
labels_2 = ["z", "w"]
lt2 = LabelTensor(data_2, labels_2)
lt1 = lt1.append(lt2, mode='cross')
lt1 = lt1.append(lt2, mode="cross")
assert lt1.shape == (400, 4)
assert lt1.full_labels[0]['dof'] == range(400)
assert lt1.full_labels[1]['dof'] == ['x', 'y', 'z', 'w']
assert lt1.full_labels[0]["dof"] == range(400)
assert lt1.full_labels[1]["dof"] == ["x", "y", "z", "w"]
def test_vstack_3D():
data_1 = torch.rand(20, 3, 2)
labels_1 = {
1: {
'dof': ['a', 'b', 'c'],
'name': 'first'
},
2: {
'dof': ['x', 'y'],
'name': 'second'
}
1: {"dof": ["a", "b", "c"], "name": "first"},
2: {"dof": ["x", "y"], "name": "second"},
}
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 3, 2)
labels_1 = {
1: {
'dof': ['a', 'b', 'c'],
'name': 'first'
},
2: {
'dof': ['x', 'y'],
'name': 'second'
}
1: {"dof": ["a", "b", "c"], "name": "first"},
2: {"dof": ["x", "y"], "name": "second"},
}
lt2 = LabelTensor(data_2, labels_1)
lt_stacked = LabelTensor.vstack([lt1, lt2])
assert lt_stacked.shape == (40, 3, 2)
assert lt_stacked.full_labels[0]['dof'] == range(40)
assert lt_stacked.full_labels[1]['dof'] == ['a', 'b', 'c']
assert lt_stacked.full_labels[2]['dof'] == ['x', 'y']
assert lt_stacked.full_labels[1]['name'] == 'first'
assert lt_stacked.full_labels[2]['name'] == 'second'
assert lt_stacked.full_labels[0]["dof"] == range(40)
assert lt_stacked.full_labels[1]["dof"] == ["a", "b", "c"]
assert lt_stacked.full_labels[2]["dof"] == ["x", "y"]
assert lt_stacked.full_labels[1]["name"] == "first"
assert lt_stacked.full_labels[2]["name"] == "second"
def test_vstack_2D():
data_1 = torch.rand(20, 2)
labels_1 = {1: {'dof': ['x', 'y'], 'name': 'second'}}
labels_1 = {1: {"dof": ["x", "y"], "name": "second"}}
lt1 = LabelTensor(data_1, labels_1)
data_2 = torch.rand(20, 2)
labels_1 = {1: {'dof': ['x', 'y'], 'name': 'second'}}
labels_1 = {1: {"dof": ["x", "y"], "name": "second"}}
lt2 = LabelTensor(data_2, labels_1)
lt_stacked = LabelTensor.vstack([lt1, lt2])
assert lt_stacked.shape == (40, 2)
assert lt_stacked.full_labels[0]['dof'] == range(40)
assert lt_stacked.full_labels[1]['dof'] == ['x', 'y']
assert lt_stacked.full_labels[0]['name'] == 0
assert lt_stacked.full_labels[1]['name'] == 'second'
assert lt_stacked.full_labels[0]["dof"] == range(40)
assert lt_stacked.full_labels[1]["dof"] == ["x", "y"]
assert lt_stacked.full_labels[0]["name"] == 0
assert lt_stacked.full_labels[1]["name"] == "second"
def test_sorting():
@@ -256,11 +236,11 @@ def test_sorting():
data[:, 2] = data[:, 2]
data[:, 3] = data[:, 3] * 5
data[:, 4] = data[:, 4] * 3
labels = ['d', 'b', 'a', 'e', 'c']
labels = ["d", "b", "a", "e", "c"]
lt_data = LabelTensor(data, labels)
lt_sorted = LabelTensor.sort_labels(lt_data)
assert lt_sorted.shape == (20, 5)
assert lt_sorted.labels == ['a', 'b', 'c', 'd', 'e']
assert lt_sorted.labels == ["a", "b", "c", "d", "e"]
assert torch.eq(lt_sorted.tensor[:, 0], torch.ones(20) * 1).all()
assert torch.eq(lt_sorted.tensor[:, 1], torch.ones(20) * 2).all()
assert torch.eq(lt_sorted.tensor[:, 2], torch.ones(20) * 3).all()
@@ -272,26 +252,29 @@ def test_sorting():
data[:, 1, :] = data[:, 1] * 2
data[:, 2, :] = data[:, 2]
data[:, 3, :] = data[:, 3] * 3
labels = {1: {'dof': ['d', 'b', 'a', 'c'], 'name': 1}}
labels = {1: {"dof": ["d", "b", "a", "c"], "name": 1}}
lt_data = LabelTensor(data, labels)
lt_sorted = LabelTensor.sort_labels(lt_data, dim=1)
assert lt_sorted.shape == (20, 4, 5)
assert lt_sorted.full_labels[1]['dof'] == ['a', 'b', 'c', 'd']
assert lt_sorted.full_labels[1]["dof"] == ["a", "b", "c", "d"]
assert torch.eq(lt_sorted.tensor[:, 0, :], torch.ones(20, 5) * 1).all()
assert torch.eq(lt_sorted.tensor[:, 1, :], torch.ones(20, 5) * 2).all()
assert torch.eq(lt_sorted.tensor[:, 2, :], torch.ones(20, 5) * 3).all()
assert torch.eq(lt_sorted.tensor[:, 3, :], torch.ones(20, 5) * 4).all()
@pytest.mark.parametrize("labels",
[[f's{i}' for i in range(10)],
{0: {'dof': ['a', 'b', 'c']},
1: {'dof': [f's{i}' for i in range(10)]}}])
@pytest.mark.parametrize(
"labels",
[
[f"s{i}" for i in range(10)],
{0: {"dof": ["a", "b", "c"]}, 1: {"dof": [f"s{i}" for i in range(10)]}},
],
)
def test_cat_bool(labels):
out = torch.randn((3, 10))
out = LabelTensor(out, labels)
selected = out[torch.tensor([True, True, False])]
assert selected.shape == (2, 10)
assert selected.stored_labels[1]['dof'] == [f's{i}' for i in range(10)]
assert selected.stored_labels[1]["dof"] == [f"s{i}" for i in range(10)]
if isinstance(labels, dict):
assert selected.stored_labels[0]['dof'] == ['a', 'b']
assert selected.stored_labels[0]["dof"] == ["a", "b"]

View File

@@ -4,7 +4,7 @@ import pytest
from pina import LabelTensor
data = torch.rand((20, 3))
labels = ['a', 'b', 'c']
labels = ["a", "b", "c"]
def test_constructor():
@@ -13,7 +13,7 @@ def test_constructor():
def test_wrong_constructor():
with pytest.raises(ValueError):
LabelTensor(data, ['a', 'b'])
LabelTensor(data, ["a", "b"])
def test_labels():
@@ -25,7 +25,7 @@ def test_labels():
def test_extract():
label_to_extract = ['a', 'c']
label_to_extract = ["a", "c"]
tensor = LabelTensor(data, labels)
new = tensor.extract(label_to_extract)
assert new.labels == label_to_extract
@@ -34,7 +34,7 @@ def test_extract():
def test_extract_onelabel():
label_to_extract = ['a']
label_to_extract = ["a"]
tensor = LabelTensor(data, labels)
new = tensor.extract(label_to_extract)
assert new.ndim == 2
@@ -44,18 +44,19 @@ def test_extract_onelabel():
def test_wrong_extract():
label_to_extract = ['a', 'cc']
label_to_extract = ["a", "cc"]
tensor = LabelTensor(data, labels)
with pytest.raises(ValueError):
tensor.extract(label_to_extract)
def test_extract_order():
label_to_extract = ['c', 'a']
label_to_extract = ["c", "a"]
tensor = LabelTensor(data, labels)
new = tensor.extract(label_to_extract)
expected = torch.cat((data[:, 2].reshape(-1, 1), data[:, 0].reshape(-1, 1)),
dim=1)
expected = torch.cat(
(data[:, 2].reshape(-1, 1), data[:, 0].reshape(-1, 1)), dim=1
)
assert new.labels == label_to_extract
assert new.shape[1] == len(label_to_extract)
assert torch.all(torch.isclose(expected, new))
@@ -63,31 +64,31 @@ def test_extract_order():
def test_merge():
tensor = LabelTensor(data, labels)
tensor_a = tensor.extract('a')
tensor_b = tensor.extract('b')
tensor_c = tensor.extract('c')
tensor_a = tensor.extract("a")
tensor_b = tensor.extract("b")
tensor_c = tensor.extract("c")
tensor_bc = tensor_b.append(tensor_c)
assert torch.allclose(tensor_bc, tensor.extract(['b', 'c']))
assert torch.allclose(tensor_bc, tensor.extract(["b", "c"]))
def test_merge2():
tensor = LabelTensor(data, labels)
tensor_b = tensor.extract('b')
tensor_c = tensor.extract('c')
tensor_b = tensor.extract("b")
tensor_c = tensor.extract("c")
tensor_bc = tensor_b.append(tensor_c)
assert torch.allclose(tensor_bc, tensor.extract(['b', 'c']))
assert torch.allclose(tensor_bc, tensor.extract(["b", "c"]))
def test_getitem():
tensor = LabelTensor(data, labels)
tensor_view = tensor['a']
assert tensor_view.labels == ['a']
tensor_view = tensor["a"]
assert tensor_view.labels == ["a"]
assert torch.allclose(tensor_view.flatten(), data[:, 0])
tensor_view = tensor['a', 'c']
assert tensor_view.labels == ['a', 'c']
tensor_view = tensor["a", "c"]
assert tensor_view.labels == ["a", "c"]
assert torch.allclose(tensor_view, data[:, 0::2])