data_utils.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """
  2. Utility functions for data extraction and processing
  3. Authors: Martin Horvat, January 2026
  4. """
  5. import numpy as np
  6. """
  7. Extract data fom dictionaries for specific organ
  8. Input:
  9. organ: string in ["lung", "bowel", "thyroid"]
  10. perc: int, percentiles [1, ... , 100]
  11. suv_dict: dict containing percentiles of suv
  12. flags_dict: dict containing states
  13. """
  14. def get_data(organ, perc, suv_dict, flags_dict, nr_patient = 58):
  15. fname = "get_data"
  16. # get concrete data set
  17. suv = suv_dict[organ + '_SUVperc_COMBINED'][:nr_patient,:,:] # suv percentiles
  18. # index of percentile
  19. perc_idx = perc -1
  20. # computing max SUV percentile per patient, ignoring nans
  21. x = np.nanmax(suv[:,:,perc_idx], axis = 1)
  22. # determining index in the flags based on organ
  23. match organ:
  24. case "lung":
  25. flags_idx = 3
  26. case "bowel":
  27. flags_idx = 1
  28. case "thyroid":
  29. flags_idx = 5
  30. case _:
  31. assert False, f"{fname}::this organ {organ = } is not supported"
  32. # getting state of patients: 0 == NC, 1 == AE
  33. y = flags_dict['flags'][:nr_patient, flags_idx]
  34. return x, y
  35. """
  36. Check if a vector lies within the specified bounds for each dimension.
  37. Parameters:
  38. - vector (np.ndarray): 1D array representing the point to check. Shape: (n,)
  39. - bounds (np.ndarray): 2D array of shape (n, 2), where each row is (min, max) for a dimension.
  40. Returns:
  41. - bool: True if the vector is within bounds in all dimensions, False otherwise.
  42. """
  43. def within_bounds(vector: np.ndarray, bounds: np.ndarray) -> bool:
  44. if vector.shape[-1] != bounds.shape[0]:
  45. raise ValueError("Dimension mismatch: vector length and bounds rows must be equal.")
  46. return np.apply_along_axis(lambda x: np.all((x >= bounds[:, 0]) & (x <= bounds[:, 1])), -1, vector)