data_utils.py 1.7 KB

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