Spaces:
Running on Zero
Running on Zero
File size: 16,069 Bytes
b1ea329 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | """Unit tests for topic-analysis markdown table column normalisation."""
import unittest
import pandas as pd
from tools.llm_api_call import (
TOPIC_TABLE_EXPECTED_COLS,
_ensure_standard_topic_table_columns,
_four_column_table_has_sentiment,
reconstruct_markdown_table_from_reference_df,
)
class TestEnsureStandardTopicTableColumns(unittest.TestCase):
def test_six_column_table_with_name_collision_is_unique(self):
"""Positional rename used to create duplicate 'General topic' labels."""
df = pd.DataFrame(
{
"Topic": ["Housing"],
"Detail": ["Repairs delay"],
"General topic": ["Services"],
"Response ID": ["1"],
"Summary": ["Delayed repairs"],
"Extra": ["noise"],
}
)
out = _ensure_standard_topic_table_columns(df, batch_size_number=1)
self.assertEqual(list(out.columns), TOPIC_TABLE_EXPECTED_COLS)
self.assertFalse(out.columns.duplicated().any())
self.assertEqual(out.loc[0, "General topic"], "Services")
self.assertEqual(out.loc[0, "Response ID"], "1")
# Concat must not raise the historical reindexing error
other = pd.DataFrame(
{
"General topic": ["Other"],
"Subtopic": ["X"],
"Sentiment": ["Not assessed"],
}
)
combined = pd.concat(
[out[["General topic", "Subtopic", "Sentiment"]], other],
ignore_index=True,
)
self.assertEqual(len(combined), 2)
def test_float_nan_general_topic_becomes_string(self):
df = pd.DataFrame(
{
"General topic": [float("nan"), float("nan")],
"Subtopic": ["A", "B"],
"Sentiment": ["Neutral", "Positive"],
"Response ID": [1.0, 2.0],
"Summary": ["s1", "s2"],
}
)
out = _ensure_standard_topic_table_columns(df, batch_size_number=5)
self.assertEqual(out["General topic"].dtype, object)
# .str accessor must work after normalisation
stripped = out["General topic"].str.strip()
self.assertTrue((stripped == "").all())
self.assertEqual(out.loc[0, "Response ID"], "1.0")
def test_empty_named_general_topic_prefers_positional_values(self):
"""Blank 'General topic' header should not block real topic text in col0."""
df = pd.DataFrame(
{
"Topic": ["Housing"],
"Subtopic": ["Repairs"],
"General topic": [float("nan")],
"Response ID": ["1"],
"Summary": ["Delayed repairs"],
"Extra": ["noise"],
}
)
out = _ensure_standard_topic_table_columns(df, batch_size_number=5)
self.assertEqual(out.loc[0, "General topic"], "Housing")
self.assertEqual(out.loc[0, "Subtopic"], "Repairs")
self.assertFalse(out.columns.duplicated().any())
def test_three_column_table_gets_defaults(self):
df = pd.DataFrame(
{
"General topic": ["Transport"],
"Subtopic": ["Buses"],
"Summary": ["Need more buses"],
}
)
out = _ensure_standard_topic_table_columns(df, batch_size_number=1)
self.assertEqual(list(out.columns), TOPIC_TABLE_EXPECTED_COLS)
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
self.assertEqual(out.loc[0, "Response ID"], "1")
self.assertEqual(out.loc[0, "General topic"], "Transport")
def test_empty_dataframe(self):
out = _ensure_standard_topic_table_columns(pd.DataFrame(), batch_size_number=5)
self.assertTrue(out.empty)
self.assertEqual(list(out.columns), TOPIC_TABLE_EXPECTED_COLS)
def test_duplicate_input_columns_are_deduped(self):
df = pd.DataFrame(
[["Housing", "Repairs", "Negative", "1", "summary"]],
columns=[
"General topic",
"Subtopic",
"Sentiment",
"Response ID",
"Summary",
],
)
# Force duplicate labels the way a bad select used to
df = pd.concat([df, df[["General topic"]]], axis=1)
out = _ensure_standard_topic_table_columns(df, batch_size_number=5)
self.assertEqual(list(out.columns), TOPIC_TABLE_EXPECTED_COLS)
self.assertFalse(out.columns.duplicated().any())
self.assertEqual(out.loc[0, "General topic"], "Housing")
class TestNoSentimentTableLayouts(unittest.TestCase):
def test_named_four_column_preserves_response_ids(self):
df = pd.DataFrame(
{
"General topic": ["Housing"],
"Subtopic": ["Repairs"],
"Response ID": ["3, 4"],
"Summary": ["Delayed repairs"],
}
)
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=False
)
self.assertEqual(out.loc[0, "Response ID"], "3, 4")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
def test_unnamed_four_column_maps_third_column_as_response_id(self):
df = pd.DataFrame([["Housing", "Repairs", "3, 4", "Delayed repairs"]])
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=False
)
self.assertEqual(out.loc[0, "Response ID"], "3, 4")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
def test_five_column_trailing_extra_does_not_steal_response_id(self):
df = pd.DataFrame(
{
"General topic": ["Housing"],
"Subtopic": ["Repairs"],
"Response ID": ["3, 4"],
"Summary": ["Delayed repairs"],
"Extra": [""],
}
)
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=False
)
self.assertEqual(out.loc[0, "Response ID"], "3, 4")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
def test_unnamed_five_column_trailing_extra_without_sentiment(self):
df = pd.DataFrame([["Housing", "Repairs", "3, 4", "Delayed repairs", ""]])
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=False
)
self.assertEqual(out.loc[0, "Response ID"], "3, 4")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
def test_three_column_summary_is_not_used_as_response_id(self):
df = pd.DataFrame(
{
"General topic": ["Transport"],
"Subtopic": ["Buses"],
"Summary": ["Need more buses"],
}
)
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=False
)
self.assertEqual(out.loc[0, "Summary"], "Need more buses")
self.assertEqual(out.loc[0, "Response ID"], "")
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
def test_sentiment_layout_still_maps_when_assessing_sentiment(self):
df = pd.DataFrame([["Housing", "Repairs", "Negative", "3", "summary"]])
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=True
)
self.assertEqual(out.loc[0, "Sentiment"], "Negative")
self.assertEqual(out.loc[0, "Response ID"], "3")
self.assertEqual(out.loc[0, "Summary"], "summary")
def test_three_column_placeholder_table_maps_ids_not_sentiment(self):
"""Force-zero-shot tables often have Placeholder, Subtopic, Response ID."""
df = pd.DataFrame(
{
"Placeholder": ["Not assessed", "Not assessed"],
"Subtopics": ["Housing repairs", "Buses"],
"Response refs": ["1, 2", "3"],
}
)
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=True
)
self.assertEqual(out.loc[0, "Response ID"], "1, 2")
self.assertEqual(out.loc[1, "Response ID"], "3")
self.assertEqual(out.loc[0, "Subtopic"], "Housing repairs")
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
def test_four_column_with_response_id_header_is_not_sentiment(self):
df = pd.DataFrame(
{
"General topic": ["Housing"],
"Subtopic": ["Repairs"],
"Response ID": ["3, 4"],
"Summary": ["Delayed repairs"],
}
)
self.assertFalse(_four_column_table_has_sentiment(df, assess_sentiment=False))
def test_four_column_numeric_ids_are_not_treated_as_sentiment(self):
df = pd.DataFrame([["Housing", "Repairs", "1, 2", "Delayed repairs"]])
self.assertFalse(_four_column_table_has_sentiment(df, assess_sentiment=False))
def test_reconstruct_omits_sentiment_column_when_not_assessed(self):
reference_df = pd.DataFrame(
{
"General topic": ["Housing", "Housing"],
"Subtopic": ["Repairs", "Repairs"],
"Sentiment": ["Not assessed", "Not assessed"],
"Response ID": [1, 2],
"Summary": ["Delayed repairs", "Delayed repairs"],
}
)
markdown, out_df = reconstruct_markdown_table_from_reference_df(
reference_df, sentiment_checkbox="Do not assess sentiment"
)
self.assertIn("| General topic | Subtopic | Response ID | Summary |", markdown)
self.assertNotIn("| Sentiment |", markdown)
self.assertEqual(out_df.loc[0, "Sentiment"], "Not assessed")
self.assertIn("1", out_df.loc[0, "Response ID"])
self.assertIn("2", out_df.loc[0, "Response ID"])
class TestTopicConfidenceColumn(unittest.TestCase):
def test_named_six_column_table_maps_confidence(self):
df = pd.DataFrame(
{
"General topic": ["Housing"],
"Subtopic": ["Repairs"],
"Sentiment": ["Negative"],
"Response ID": ["1, 2"],
"Confidence": ["0.82"],
"Summary": ["Delayed repairs"],
}
)
out = _ensure_standard_topic_table_columns(
df,
batch_size_number=5,
assess_sentiment=True,
include_confidence=True,
)
self.assertIn("Confidence", out.columns)
self.assertEqual(out.loc[0, "Confidence"], "0.82")
self.assertEqual(out.loc[0, "Response ID"], "1, 2")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
def test_unnamed_six_column_maps_confidence_before_summary(self):
df = pd.DataFrame(
[["Housing", "Repairs", "Negative", "3", "0.4", "Delayed repairs"]]
)
out = _ensure_standard_topic_table_columns(
df,
batch_size_number=5,
assess_sentiment=True,
include_confidence=True,
)
self.assertEqual(out.loc[0, "Response ID"], "3")
self.assertEqual(out.loc[0, "Confidence"], "0.4")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
def test_five_column_without_sentiment_maps_confidence(self):
df = pd.DataFrame(
{
"General topic": ["Housing"],
"Subtopic": ["Repairs"],
"Response ID": ["3, 4"],
"Confidence": ["80%"],
"Summary": ["Delayed repairs"],
}
)
out = _ensure_standard_topic_table_columns(
df,
batch_size_number=5,
assess_sentiment=False,
include_confidence=True,
)
self.assertEqual(out.loc[0, "Response ID"], "3, 4")
self.assertEqual(out.loc[0, "Confidence"], "80%")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
self.assertEqual(out.loc[0, "Sentiment"], "Not assessed")
def test_missing_confidence_does_not_steal_summary(self):
df = pd.DataFrame(
{
"General topic": ["Transport"],
"Subtopic": ["Buses"],
"Sentiment": ["Negative"],
"Response ID": ["1"],
"Summary": ["Need more buses"],
}
)
out = _ensure_standard_topic_table_columns(
df,
batch_size_number=5,
assess_sentiment=True,
include_confidence=True,
)
self.assertEqual(out.loc[0, "Summary"], "Need more buses")
self.assertEqual(out.loc[0, "Response ID"], "1")
self.assertEqual(out.loc[0, "Confidence"], "")
def test_response_ids_are_not_consumed_as_confidence(self):
df = pd.DataFrame([["Housing", "Repairs", "1, 2", "Delayed repairs"]])
out = _ensure_standard_topic_table_columns(
df,
batch_size_number=5,
assess_sentiment=False,
include_confidence=True,
)
self.assertEqual(out.loc[0, "Response ID"], "1, 2")
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
self.assertEqual(out.loc[0, "Confidence"], "")
def test_without_flag_confidence_is_omitted(self):
df = pd.DataFrame(
{
"General topic": ["Housing"],
"Subtopic": ["Repairs"],
"Sentiment": ["Negative"],
"Response ID": ["1"],
"Confidence": ["0.9"],
"Summary": ["Delayed repairs"],
}
)
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, include_confidence=False
)
self.assertEqual(list(out.columns), TOPIC_TABLE_EXPECTED_COLS)
self.assertNotIn("Confidence", out.columns)
self.assertEqual(out.loc[0, "Summary"], "Delayed repairs")
def test_reconstruct_includes_confidence_column(self):
reference_df = pd.DataFrame(
{
"General topic": ["Housing", "Housing"],
"Subtopic": ["Repairs", "Repairs"],
"Sentiment": ["Negative", "Negative"],
"Response ID": [1, 2],
"Confidence": [0.9, 0.4],
"Summary": ["Delayed repairs", "Delayed repairs"],
}
)
markdown, out_df = reconstruct_markdown_table_from_reference_df(
reference_df,
sentiment_checkbox="Negative or Positive",
include_topic_confidence_radio="Yes",
)
self.assertIn(
"| General topic | Subtopic | Sentiment | Response ID | Confidence | Summary |",
markdown,
)
self.assertEqual(len(out_df), 2)
self.assertIn("0.90", markdown)
self.assertIn("0.40", markdown)
def test_main_heading_alias_maps_to_general_topic(self):
df = pd.DataFrame(
{
"Main heading": ["Behaviour at school"],
"Subheading": ["Absences"],
"Summary": ["Alex had increasing absences."],
}
)
out = _ensure_standard_topic_table_columns(
df, batch_size_number=5, assess_sentiment=False
)
self.assertEqual(out.loc[0, "General topic"], "Behaviour at school")
self.assertEqual(out.loc[0, "Subtopic"], "Absences")
self.assertEqual(out.loc[0, "Summary"], "Alex had increasing absences.")
self.assertNotEqual(out.loc[0, "Sentiment"], "Alex had increasing absences.")
if __name__ == "__main__":
unittest.main()
|