Skip to content

Commit 5386cb8

Browse files
committed
fix percentage error test
1 parent 321f6ab commit 5386cb8

File tree

1 file changed

+41
-45
lines changed

1 file changed

+41
-45
lines changed

tests/post/test_excel_routines.py

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def test_compare(self):
2121
result = Table._compare(df1, df2, ComparisonType.PERCENTAGE)
2222
expected_values = [50, 25, 0]
2323
expected_errors = [
24-
np.sqrt((5 * 10 * 0.1) ** 2 + (5 * 0.15) ** 2) / 5,
25-
np.sqrt((15 * 20 * 0.2) ** 2 + (15 * 0.25) ** 2) / 5,
24+
np.sqrt((5 * 0.1) ** 2 + (5 * 0.15) ** 2) / 5,
25+
np.sqrt((15 * 0.2) ** 2 + (15 * 0.25) ** 2) / 5,
2626
0.65,
2727
]
2828
assert pytest.approx(result["Value"].tolist()) == expected_values
@@ -94,10 +94,10 @@ def test_sort_sphere_index_single(self):
9494
"Sphere_1002_H-2",
9595
]
9696
df = pd.DataFrame(data, index=index)
97-
97+
9898
# Sort the dataframe
9999
sorted_df = SpherePivotTable._sort_sphere_index(df)
100-
100+
101101
# Expected order: isotopes by Z number, then materials alphabetically
102102
# Z=1 (H): 1001, 1002
103103
# Z=26 (Fe): 26056
@@ -111,7 +111,7 @@ def test_sort_sphere_index_single(self):
111111
"Sphere_M101",
112112
"Sphere_M400",
113113
]
114-
114+
115115
assert sorted_df.index.tolist() == expected_order
116116

117117
def test_sort_sphere_index_multi(self):
@@ -127,13 +127,15 @@ def test_sort_sphere_index_multi(self):
127127
]
128128
energies = [1.0, 2.0, 1.0, 2.0, 1.0, 2.0]
129129
values = [10, 20, 30, 40, 50, 60]
130-
130+
131131
df = pd.DataFrame({"Value": values})
132-
df.index = pd.MultiIndex.from_arrays([cases, energies], names=["Case", "Energy"])
133-
132+
df.index = pd.MultiIndex.from_arrays(
133+
[cases, energies], names=["Case", "Energy"]
134+
)
135+
134136
# Sort the dataframe
135137
sorted_df = SpherePivotTable._sort_sphere_index(df)
136-
138+
137139
# Expected order: H-1 first (Z=1), then Fe-56 (Z=26), then M400
138140
expected_cases = [
139141
"Sphere_1001_H-1",
@@ -144,43 +146,44 @@ def test_sort_sphere_index_multi(self):
144146
"Sphere_M400",
145147
]
146148
expected_energies = [1.0, 2.0, 1.0, 2.0, 1.0, 2.0]
147-
149+
148150
result_cases = sorted_df.index.get_level_values(0).tolist()
149151
result_energies = sorted_df.index.get_level_values(1).tolist()
150-
152+
151153
assert result_cases == expected_cases
152154
assert result_energies == expected_energies
153155

154156
def test_sort_sphere_index_empty(self):
155157
"""Test sorting with empty dataframe"""
156158
# Create an empty dataframe
157159
df = pd.DataFrame()
158-
160+
159161
# Sort the dataframe - should return empty df unchanged
160162
sorted_df = SpherePivotTable._sort_sphere_index(df)
161-
163+
162164
assert sorted_df.empty
163165
assert len(sorted_df) == 0
164166

165167
def test_get_sheet_integration(self):
166168
"""Test the full _get_sheet method with sorting applied"""
167169
import tempfile
168170
from jade.config.excel_config import ComparisonType, TableConfig
169-
171+
170172
# Create test data
171173
data = {
172174
"Case": [
173175
"Sphere_M400",
174176
"Sphere_1001_H-1",
175177
"Sphere_26056_Fe-56",
176-
] * 2,
178+
]
179+
* 2,
177180
"Energy": [1.0, 1.0, 1.0, 2.0, 2.0, 2.0],
178181
"Value": [10.0, 20.0, 30.0, 15.0, 25.0, 35.0],
179182
"Error": [0.1, 0.2, 0.3, 0.15, 0.25, 0.35],
180183
}
181184
ref_df = pd.DataFrame(data)
182185
target_df = ref_df.copy()
183-
186+
184187
# Create a mock config
185188
class MockConfig:
186189
x = ["Case"]
@@ -190,33 +193,27 @@ class MockConfig:
190193
comparison_type = ComparisonType.PERCENTAGE
191194
conditional_formatting = None
192195
change_col_names = None
193-
196+
194197
cfg = MockConfig()
195-
198+
196199
# Create a temporary Excel file
197-
with tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False) as f:
198-
writer = pd.ExcelWriter(f.name, engine='xlsxwriter')
199-
200+
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as f:
201+
writer = pd.ExcelWriter(f.name, engine="xlsxwriter")
202+
200203
# Create SpherePivotTable instance
201204
table = SpherePivotTable(
202-
"Test",
203-
writer,
204-
ref_df,
205-
target_df,
206-
cfg,
207-
"ref",
208-
"target"
205+
"Test", writer, ref_df, target_df, cfg, "ref", "target"
209206
)
210-
207+
211208
# Get the sheets
212209
sheets = table._get_sheet()
213-
210+
214211
writer.close()
215-
212+
216213
# Verify the result is sorted
217214
assert len(sheets) == 1
218215
result_df = sheets[0]
219-
216+
220217
# Check that cases are sorted: H-1 (Z=1), Fe-56 (Z=26), M400 (material)
221218
expected_order = ["Sphere_1001_H-1", "Sphere_26056_Fe-56", "Sphere_M400"]
222219
assert result_df.index.tolist() == expected_order
@@ -228,32 +225,31 @@ def test_sort_sphere_index_edge_cases(self):
228225
"Value": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0],
229226
}
230227
index = [
231-
"Sphere_1001_H-1", # Normal 4-digit ZAID
232-
"Sphere_26056_Fe-56", # Normal 5-digit ZAID
233-
"Sphere_M400", # Normal material
234-
"Sphere_123", # Unusual ZAID format (3 digits)
235-
"Sphere_XYZ", # Non-numeric, non-M identifier
236-
"UnknownFormat", # No underscore separator
237-
"Sphere", # Only one part
228+
"Sphere_1001_H-1", # Normal 4-digit ZAID
229+
"Sphere_26056_Fe-56", # Normal 5-digit ZAID
230+
"Sphere_M400", # Normal material
231+
"Sphere_123", # Unusual ZAID format (3 digits)
232+
"Sphere_XYZ", # Non-numeric, non-M identifier
233+
"UnknownFormat", # No underscore separator
234+
"Sphere", # Only one part
238235
]
239236
df = pd.DataFrame(data, index=index)
240-
237+
241238
# Sort the dataframe - should handle edge cases gracefully
242239
sorted_df = SpherePivotTable._sort_sphere_index(df)
243-
240+
244241
# Verify the result is a DataFrame and has same length
245242
assert len(sorted_df) == len(df)
246243
assert isinstance(sorted_df, pd.DataFrame)
247-
244+
248245
# Check that known good formats are sorted correctly
249246
result_index = sorted_df.index.tolist()
250-
247+
251248
# H-1 should come before Fe-56 (both are isotopes with proper format)
252249
h_pos = result_index.index("Sphere_1001_H-1")
253250
fe_pos = result_index.index("Sphere_26056_Fe-56")
254251
assert h_pos < fe_pos, "H-1 should come before Fe-56"
255-
252+
256253
# Edge cases should be treated as materials and come after isotopes
257254
# M400 should be in materials section
258255
assert "Sphere_M400" in result_index
259-

0 commit comments

Comments
 (0)