def _tensor_from_file(tm, hd5, **kwargs): visits = get_visits(tm, hd5, **kwargs) max_size = max(hd5[tm.path_prefix.replace("*", v)].size for v in visits) shape = (len(visits), max_size) flag_dates = kwargs.get("readable_dates") if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) indices = hd5[path][()] is_nan_indices = np.isnan(indices) no_nan_indices = indices[~is_nan_indices].astype(int) ecg_tm = get_signal_tmap(f"{lead}_time") data = ecg_tm.tensor_from_file(ecg_tm, hd5, visit=visit)[0][no_nan_indices] nan_indices = np.where(is_nan_indices)[0] nan_indices -= np.array(range(nan_indices.shape[0])) data = np.append(data, np.array([np.nan] * nan_indices.shape[0])) data = np.insert(data, nan_indices, np.nan)[:indices.shape[0]] if flag_dates: tensor[i][:data.shape[0]] = get_local_timestamps(data) else: tensor[i][:data.shape[0]] = data return tensor
def _tensor_from_file(tm, hd5, **kwargs): visits = get_visits(tm, hd5, **kwargs) flag_dates = kwargs.get("readable_dates") if tm.is_timeseries: max_size = max( hd5[f"{tm.path_prefix}/value".replace("*", v)].size for v in visits ) shape = (len(visits), 2, max_size) if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) data = hd5[f"{path}/value"][()] tensor[i][0][: data.shape[0]] = data unix_array = hd5[f"{path}/time"][()] if flag_dates: tensor[i][1][: unix_array.shape[0]] = get_local_timestamps( unix_array, ) else: tensor[i][1][: unix_array.shape[0]] = unix_array elif tm.is_event or tm.is_continuous: max_size = max(hd5[tm.path_prefix.replace("*", v)].size for v in visits) shape = (len(visits), max_size) tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) data = hd5[path][()] if "|S" in str(data.dtype): data = data.astype("<U32") for element in data: try: float(element) except ValueError: if element.startswith("<") or element.startswith(">"): try: float(element[1:]) data[data == element] = element[1:] except ValueError: data[data == element] = np.nan else: data[data == element] = np.nan data = data.astype(float) tensor[i][: data.shape[0]] = data else: raise ValueError( f"Incorrect interpretation '{tm.interpretation}' for measurement tmap.", ) return tensor
def _tensor_from_file(tm, hd5, **kwargs): visits = get_visits(tm, hd5, **kwargs) flag_dates = kwargs.get("readable_dates") max_size = max(hd5[tm.path_prefix.replace("*", v)].size for v in visits) shape = (len(visits), max_size) if tm.path_prefix.endswith("start_date") and flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) data = hd5[path][()] if path.endswith("start_date") and flag_dates: tensor[i][: data.shape[0]] = get_local_timestamps(data) else: tensor[i][: data.shape[0]] = data return tensor
def _tensor_from_file(tm, hd5, **kwargs): visits = get_visits(tm, hd5, **kwargs) flag_dates = kwargs.get("readable_dates") if tm.is_timeseries: max_size = max( hd5[f"{tm.path_prefix}/dose".replace("*", v)].size for v in visits ) shape = (len(visits), 2, max_size) if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) data = hd5[f"{path}/dose"][()] tensor[i][0][: data.shape[0]] = data unix_array = hd5[f"{path}/start_date"][()] if flag_dates: tensor[i][1][: unix_array.shape[0]] = get_local_timestamps( unix_array, ) else: tensor[i][1][: unix_array.shape[0]] = unix_array elif tm.is_event or tm.is_continuous: max_size = max(hd5[tm.path_prefix.replace("*", v)].size for v in visits) shape = (len(visits), max_size) tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) data = hd5[path][()] tensor[i][: data.shape[0]] = data else: raise ValueError( f"Incorrect interpretation '{tm.interpretation}' " "for medication tmap {tm.name}.", ) return tensor
def _tensor_from_file(tm, hd5, **kwargs): visits = get_visits(tm, hd5, **kwargs) flag_dates = kwargs.get("readable_dates") sizes = [] for visit in visits: size = 0 for event_name in events_names_list: try: size += hd5[ tm.path_prefix.replace("*", visit).replace("{}", event_name) ].size except KeyError: continue sizes.append(size) max_size = max(sizes) shape = (len(visits), max_size) if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visits): subpath = tm.path_prefix.replace("*", visit) data = [] for event_name in events_names_list: try: path = subpath.replace("{}", event_name) data = np.append(data, hd5[path][()]) except KeyError: continue data = np.sort(data) if flag_dates: tensor[i][: data.shape[0]] = get_local_timestamps(data) else: tensor[i][: data.shape[0]] = data return tensor
def _tensor_from_file(tm, hd5, **kwargs): visits = get_visits(tm, hd5, **kwargs) max_size = max(hd5[tm.path_prefix.replace("*", v)].size for v in visits) if tm.is_timeseries: shape = (len(visits), 2, max_size) flag_dates = kwargs.get("readable_dates") if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) time = get_tmap(ref_peak).tensor_from_file( get_tmap(ref_peak), hd5, visit=visit, )[0] data = hd5[path][()] tensor[i][0][:data.shape[0]] = data if flag_dates: tensor[i][1][:time.shape[0]] = get_local_timestamps(time) else: tensor[i][1][:time.shape[0]] = time elif tm.is_continuous: shape = (len(visits), max_size) tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) data = hd5[path][()] tensor[i][:data.shape[0]] = data else: raise ValueError( "Incorrect interpretation " f"'{tm.interpretation}' for ecg_feature tmap {tm.name}.", ) return tensor
def _tensor_from_file(tm, hd5): visits = get_visits(tm, hd5) shape = tm.shape tensor = np.zeros(shape) for visit in visits: path = tm.path_prefix.replace("*", visit) dates = hd5[path][()] if dates.shape[0] > 0: dates = get_local_timestamps(dates) for date in dates: date = str(pd.to_datetime(date)) move_in = np.where( hd5[f"edw/{visit}"].attrs["move_time"].astype(str) < date, )[0][-1] department = (hd5[f"edw/{visit}"].attrs["department_nm"]. astype(str)[move_in]) if both_sources and not has_data(hd5, visit, "bedmaster"): continue try: tensor[tm.channel_map[department.lower()]] += 1 except KeyError: tensor[tm.channel_map["other"]] += 1 return tensor
def _get_local_time(time_array): get_function = get_local_timestamps(time_array) return get_function
def _tensor_from_file(tm, hd5, **kwargs): visits = get_visits(tm, hd5, **kwargs) flag_dates = kwargs.get("readable_dates") if tm.interpretation == Interpretation.TIMESERIES: max_size = max( hd5[f"{tm.path_prefix}/value".replace("*", v)].size for v in visits ) shape = (len(visits), 2, max_size) if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) values = hd5[f"{path}/value"][()].astype(str) for index, value in enumerate(values): if value == "nan": tensor[i][0][index] = value elif position is not None: pressures = [float(pressure) for pressure in value.split("/")] tensor[i][0][index] = pressures[position] else: pressures = [float(pressure) for pressure in value.split("/")] tensor[i][0][index] = ( pressures[0] - pressures[-1] ) / pressures[0] unix_array = hd5[f"{path}/time"][()] if flag_dates: tensor[i][1][: unix_array.shape[0]] = get_local_timestamps( unix_array, ) else: tensor[i][1][: unix_array.shape[0]] = unix_array elif ( tm.interpretation == Interpretation.EVENT or tm.interpretation == Interpretation.CONTINUOUS ): max_size = max(hd5[tm.path_prefix.replace("*", v)].size for v in visits) shape = (len(visits), max_size) tensor = np.zeros(shape) for i, visit in enumerate(visits): path = tm.path_prefix.replace("*", visit) values = hd5[path][()].astype(str) for index, value in enumerate(values): if value == "nan" or tm.interpretation == Interpretation.EVENT: tensor[i][index] = value elif position is not None: pressures = [float(pressure) for pressure in value.split("/")] tensor[i][index] = pressures[position] else: pressures = [float(pressure) for pressure in value.split("/")] if pressures[0] and pressures[0] != 0: tensor[i][index] = ( pressures[0] - pressures[-1] ) / pressures[0] else: tensor[i][index] = pressures[0] else: raise ValueError( f"Incorrect interpretation '{tm.interpretation}' for measurement tmap.", ) return tensor
def _tensor_from_file(tm, hd5, **kwargs) -> np.ndarray: visit_ids = get_visits(tm, hd5, **kwargs) base_path = tm.path_prefix if tm.is_continuous: max_size = max( hd5[f"{base_path}/{field}".replace("*", v)].size for v in visit_ids ) shape: Tuple = (len(visit_ids), max_size) tensor = np.zeros(shape, dtype=dtype) for i, visit in enumerate(visit_ids): path = base_path.replace("*", visit) data = ( hd5[f"{path}/{field}"] if field != "value" else get_value(hd5, path, **kwargs) ) tensor[i][: data.shape[0]] = data[()] elif tm.is_timeseries: flag_dates = kwargs.get("readable_dates") max_size = max( hd5[f"{base_path}/{field}".replace("*", v)].size if kwargs.get("interpolation") != "complete_nans_end" else get_timeseries(hd5, base_path.replace("*", v), field, **kwargs)[ 0 ].size for v in visit_ids ) shape = (len(visit_ids), 2, max_size) if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visit_ids): path = base_path.replace("*", visit) data, unix_array = get_timeseries(hd5, path, field, **kwargs) tensor[i][0][: data.shape[0]] = data if flag_dates: tensor[i][1][: unix_array.shape[0]] = get_local_timestamps( unix_array, ) else: tensor[i][1][: unix_array.shape[0]] = unix_array elif tm.is_event: flag_dates = kwargs.get("readable_dates") max_size = max( hd5[f"{base_path}/{field}".replace("*", v)].size if kwargs.get("interpolation") != "complete_nans_end" else get_timeseries(hd5, base_path.replace("*", v), field, **kwargs)[ 0 ].size for v in visit_ids ) shape = (len(visit_ids), max_size) if flag_dates: tensor = np.zeros(shape, dtype=object) else: tensor = np.zeros(shape) for i, visit in enumerate(visit_ids): path = base_path.replace("*", visit) _, unix_array = get_timeseries(hd5, path, field, **kwargs) if flag_dates: tensor[i][: unix_array.shape[0]] = get_local_timestamps(unix_array) else: tensor[i][: unix_array.shape[0]] = unix_array else: raise ValueError( f"Incorrect interpretation " f"'{tm.interpretation}' for Bedmaster tmap {tm.name}.", ) return tensor