data_utils.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # data.py
  2. import numpy as np
  3. """
  4. LUNG data extraction (with NaN filtering)
  5. This file does one job:
  6. 1) Read lung SUV percentile data from `suv_dict`
  7. 2) Build one feature per patient:
  8. x[i] = max value (ignoring NaNs) of the chosen percentile for patient i
  9. 3) Read lung labels from `flags_dict`:
  10. y[i] = 0 (NC) or 1 (AE)
  11. 4) Remove patients where x is NaN (and keep y aligned)
  12. Important:
  13. - Logistic regression (sklearn) cannot use NaNs in X.
  14. - So we filter NaNs here to always return clean data.
  15. Inputs:
  16. - perc: percentile number (1..100), e.g. 95
  17. - suv_dict: dict loaded from the SUV .mat file
  18. - flags_dict: dict loaded from the flags .mat file
  19. - nr_patient: number of patients to use (default 58)
  20. Outputs:
  21. - x: 1D numpy array (NaNs removed)
  22. - y: 1D numpy array (aligned with x)
  23. """
  24. def get_data(perc, suv_dict, flags_dict):
  25. """
  26. Extract SUV percentile feature and AE labels.
  27. perc = 95 -> uses column index 94
  28. """
  29. # SUV matrix: (patients × percentiles × organs)
  30. suv = suv_dict["lung_SUVperc_COMBINED"]
  31. # Flags: AE indicator
  32. flags = flags_dict["flags"]
  33. # We only use first 58 patients and column 3 for AE
  34. y = flags[:58, 3].astype(int).ravel()
  35. # Percentile index (95th -> column 94)
  36. p_idx = perc - 1
  37. # Extract feature: max over organs at that percentile
  38. x = np.nanmax(suv[:58, :, p_idx], axis=1).astype(float)
  39. # Remove NaNs
  40. mask = np.isfinite(x)
  41. return x[mask], y[mask]