Example #1
0
 def tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
     ici_dates = tm.time_series_filter(data)
     values = data[ICI_PREFIX].loc[ici_dates.index, key].to_numpy()
     tensor = np.zeros((len(values), len(tm.channel_map)))
     for i, value in enumerate(values):
         tensor[i, tm.channel_map[value]] = 1
     return tensor
Example #2
0
def tensor_from_file_aortic_stenosis_category(
    tm: TensorMap,
    data: PatientData,
) -> np.ndarray:
    """Categorizes aortic stenosis as none, mild, moderate, or severe
    from aortic valve mean gradient"""
    echo_dates = tm.time_series_filter(data)
    av_mean_gradient_key = continuous_tmap_names_and_keys["av_mean_gradient"]
    av_mean_gradients = data[ECHO_PREFIX].loc[echo_dates.index,
                                              av_mean_gradient_key]

    # Initialize tensor array of zeros where each row is the channel map
    num_categories = len(tm.channel_map)
    tensor = np.zeros((len(av_mean_gradients), num_categories))

    # Iterate through the peak velocities and mean gradients from all echos
    for idx, av_mean_gradient in enumerate(av_mean_gradients):
        if av_mean_gradient < 20:
            category = "mild"
        elif 20 <= av_mean_gradient < 40:
            category = "moderate"
        elif av_mean_gradient >= 40:
            category = "severe"
        else:
            continue
        tensor[idx, tm.channel_map[category]] = 1
    return tensor
Example #3
0
def admin_age_tensor_from_file(
    tm: TensorMap, data: PatientData, **kwargs
) -> np.ndarray:

    if "visits" in kwargs:
        visits = kwargs["visits"]
        if isinstance(visits, str):
            visits = [visits]
    else:
        visits = tm.time_series_filter(data)

    shape = (len(visits),) + tm.shape
    tensor = np.zeros(shape)

    for i, visit in enumerate(visits):
        try:
            path = f"{tm.path_prefix}/{visit}"
            admit_date = get_unix_timestamps(data[path].attrs["admin_date"])
            birth_date = get_unix_timestamps(data[path].attrs["birth_date"])
            age = admit_date - birth_date
            tensor[i] = age / 60 / 60 / 24 / 365
        except (ValueError, KeyError) as e:
            logging.debug(f"Could not get age from {data.id}/{visit}")
            logging.debug(e)

    return tensor
Example #4
0
def tff_any(tm: TensorMap, data: PatientData) -> np.ndarray:
    surgery_dates = tm.time_series_filter(data)
    tensor = data[STS_PREFIX].loc[surgery_dates.index,
                                  sts_outcome_keys].to_numpy()
    tensor = tensor.any(axis=1).astype(int)
    tensor = np.array([binarize("any", x) for x in tensor])
    if not is_dynamic_shape(tm):
        tensor = tensor[0]
    return tensor
Example #5
0
 def tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
     surgery_dates = tm.time_series_filter(data)
     tensor = data[STS_PREFIX].loc[surgery_dates.index, key].to_numpy()
     if tm.channel_map is None:
         raise ValueError(f"{tm.name} channel map is None")
     tensor = np.array([one_hot(tm.channel_map, x) for x in tensor])
     if not is_dynamic_shape(tm):
         tensor = tensor[0]
     return tensor
Example #6
0
 def tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
     surgery_dates = tm.time_series_filter(data)
     tensor = data[STS_PREFIX].loc[surgery_dates.index, key].to_numpy()
     tensor = np.array(
         [binarize(key, x, negative_value, positive_value)
          for x in tensor], )
     if not is_dynamic_shape(tm):
         tensor = tensor[0]
     return tensor
Example #7
0
def sex_double_tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
    visit = tm.time_series_filter(data)[0]
    shape = (2,) + tm.shape
    tensor = np.zeros(shape)
    path = f"{tm.path_prefix}/{visit}"
    value = data[path].attrs["sex"]
    tensor[:, tm.channel_map[value.lower()]] = np.array([1, 1])

    return tensor
Example #8
0
 def tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
     echo_dates = tm.time_series_filter(data)
     mean_gradient_key = continuous_tmap_names_and_keys["av_mean_gradient"]
     mean_gradients = data[ECHO_PREFIX].loc[echo_dates.index,
                                            mean_gradient_key]
     tensor = np.zeros((len(echo_dates), 2))
     for idx, mean_gradient in enumerate(mean_gradients):
         if np.isnan(mean_gradient):
             continue
         tensor[idx, 1 if low <= mean_gradient < high else 0] = 1
     return tensor
Example #9
0
    def _tensor_from_file(tm: TensorMap, data: PatientData, **kwargs) -> np.ndarray:
        unix_dates = kwargs.get("unix_dates")
        if "visits" in kwargs:
            visits = kwargs["visits"]
            if isinstance(visits, str):
                visits = [visits]
        else:
            visits = tm.time_series_filter(data)

        temp = None
        finalize = False
        if tm.is_timeseries:
            temp = [data[f"{tm.path_prefix}/{v}"].attrs[key] for v in visits]
            max_len = max(map(len, temp))
            shape = (len(visits), max_len)
        else:
            shape = (len(visits),) + tm.shape

        if tm.is_categorical or tm.is_continuous or (tm.is_event and unix_dates):
            tensor = np.zeros(shape)
        elif tm.is_language or (tm.is_event and not unix_dates):
            tensor = np.full(shape, "", object)
            finalize = True
        elif tm.is_timeseries and temp is not None:
            if isinstance(temp[0][0], np.number):
                tensor = np.zeros(shape)
            else:
                tensor = np.full(shape, "", object)
                finalize = True
        else:
            raise ValueError("Unknown interpretation for static ICU data")

        for i, visit in enumerate(visits):
            try:
                path = f"{tm.path_prefix}/{visit}"
                value = data[path].attrs[key] if temp is None else temp[i]
                if tm.channel_map:
                    tensor[i, tm.channel_map[value.lower()]] = 1
                elif tm.is_event and unix_dates:
                    tensor[i] = get_unix_timestamps(value)
                else:
                    tensor[i] = value
            except (ValueError, KeyError) as e:
                logging.debug(f"Error getting {key} from {data.id}/{visit}")
                logging.debug(e)

        if finalize:
            tensor = np.array(tensor, dtype=str)
        return tensor
Example #10
0
def length_of_stay_tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
    visits = tm.time_series_filter(data)
    shape = (len(visits),) + tm.shape
    tensor = np.zeros(shape)

    for i, visit in enumerate(visits):
        try:
            path = f"{tm.path_prefix}/{visit}"
            end_date = get_unix_timestamps(data[path].attrs["end_date"])
            start_date = get_unix_timestamps(data[path].attrs["admin_date"])
            tensor[i] = (end_date - start_date) / 60 / 60
        except (ValueError, KeyError) as e:
            logging.debug(f"Could not get length of stay from {data.id}/{visit}")
            logging.debug(e)

    return tensor
Example #11
0
def _days_between_tensor_from_file(tm: TensorMap,
                                   data: PatientData) -> np.ndarray:
    # Time series filter will be updated to return days for this tensor from file
    days = tm.time_series_filter(data)
    return days.to_numpy()[:, None]
Example #12
0
 def tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
     echo_dates = tm.time_series_filter(data)
     tensor = data[ECHO_PREFIX].loc[echo_dates.index, key].to_numpy()
     if is_dynamic_shape(tm):
         tensor = tensor[:, None]
     return tensor
Example #13
0
def mrn_tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
    mrn = str(data.id)
    return np.array([mrn] * len(tm.time_series_filter(data)))[:, None]
Example #14
0
def visit_tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
    return np.array(tm.time_series_filter(data))[:, None]
Example #15
0
 def tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
     ici_dates = tm.time_series_filter(data)
     positive_values = data[ICI_PREFIX].loc[ici_dates.index, key].to_numpy()
     negative_values = 1 - positive_values
     tensor = np.array([negative_values, positive_values]).T
     return tensor
Example #16
0
 def tensor_from_file(tm: TensorMap, data: PatientData) -> np.ndarray:
     ici_dates = tm.time_series_filter(data)
     tensor = data[ICI_PREFIX].loc[ici_dates.index, key].to_numpy()[:, None]
     return tensor