Example #1
0
def create_event_tmap(tm_name: str, event_name: str, event_type: str):
    tm = None
    name = event_name.replace("|_", "")
    if tm_name == f"{name}_start_date":
        tm = TensorMap(
            name=tm_name,
            shape=(None, None),  # type: ignore
            interpretation=Interpretation.EVENT,
            tensor_from_file=make_event_tensor_from_file(),
            path_prefix=f"edw/*/{event_type}/{event_name}/start_date",
        )
    elif tm_name == f"{name}_end_date":
        tm = TensorMap(
            name=tm_name,
            shape=(None, None),  # type: ignore
            interpretation=Interpretation.EVENT,
            tensor_from_file=make_event_tensor_from_file(),
            path_prefix=f"edw/*/{event_type}/{event_name}/end_date",
        )
    elif tm_name == f"{name}_double":
        tm = TensorMap(
            name=tm_name,
            shape=(2,),
            interpretation=Interpretation.CATEGORICAL,
            tensor_from_file=make_event_outcome_tensor_from_file(
                visit_tm=get_visit_tmap(f"{name}_first_visit"),
                double=True,
            ),
            channel_map={f"no_{name}": 0, name: 1},
            path_prefix=f"edw/*/{event_type}/{event_name}/start_date",
            time_series_limit=2,
        )
    elif tm_name == f"{name}_single":
        tm = TensorMap(
            name=tm_name,
            shape=(1,),
            interpretation=Interpretation.CATEGORICAL,
            tensor_from_file=make_event_outcome_tensor_from_file(
                visit_tm=get_visit_tmap(f"{name}_first_visit"),
                double=False,
            ),
            channel_map={f"no_{name}": 0, name: 1},
            path_prefix=f"edw/*/{event_type}/{event_name}/start_date",
            time_series_limit=2,
        )
    return tm
Example #2
0
def create_arrest_tmap(tm_name: str):
    arrest_list = ["code_start", "rapid_response_start"]
    tm = None

    if tm_name == "arrest_start_date":
        tm = TensorMap(
            name=tm_name,
            shape=(None, None),  # type: ignore
            interpretation=Interpretation.EVENT,
            tensor_from_file=make_general_event_tensor_from_subevents(arrest_list),
            path_prefix="edw/*/events/{}/start_date",
        )
    if tm_name == "arrest_double":
        tm = TensorMap(
            name=tm_name,
            shape=(2,),
            interpretation=Interpretation.CATEGORICAL,
            tensor_from_file=make_general_event_outcome_tensor_from_subevents(
                visit_tm=get_visit_tmap("arrest_first_visit"),
                events_names_list=arrest_list,
                double=True,
            ),
            channel_map={"no_arrest": 0, "arrest": 1},
            path_prefix="edw/*/events/{}/start_date",
            time_series_limit=2,
        )
    if tm_name == "arrest_single":
        tm = TensorMap(
            name=tm_name,
            shape=(1,),
            interpretation=Interpretation.CATEGORICAL,
            tensor_from_file=make_general_event_outcome_tensor_from_subevents(
                visit_tm=get_visit_tmap("arrest_first_visit"),
                events_names_list=arrest_list,
                double=False,
            ),
            channel_map={"no_arrest": 0, "arrest": 1},
            path_prefix="edw/*/events/{}/start_date",
            time_series_limit=2,
        )

    return tm
def create_sliding_window_outcome_tmap(tmap_name: str) -> Optional[TensorMap]:
    match = None
    if not match:
        pattern = re.compile(
            r"(\d+)_hrs_sliding_window_(.*)_to_(.*)_(\d+)_hrs_step"
            r"_(\d+)_hrs_prediction_(\d+)_hrs_gap$", )
        match = pattern.findall(tmap_name)
        if match:
            window, event_proc_tm_1, event_proc_tm_2, step, prediction, gap = match[
                0]
            make_tensor_from_file = make_sliding_window_outcome_tensor_from_file

            visit_tm = get_visit_tmap(
                re.sub(r"(end_date|start_date)", "first_visit",
                       event_proc_tm_2), )
            event_proc_tm_1 = get_signal_tmap(event_proc_tm_1)
            event_proc_tm_2 = get_signal_tmap(event_proc_tm_2)
            window = int(window)
            step = int(step)
            prediction = int(prediction)
            gap = int(gap)
            name = event_proc_tm_2.name
            return TensorMap(
                name=tmap_name,
                shape=(2, ),
                tensor_from_file=make_tensor_from_file(
                    window=window,
                    step=step,
                    prediction=prediction,
                    gap=gap,
                    event_tm_1=event_proc_tm_1,
                    event_tm_2=event_proc_tm_2,
                    visit_tm=visit_tm,
                ),
                channel_map={
                    f"no_{name}": 0,
                    name: 1
                },
                path_prefix=event_proc_tm_2.path_prefix,
                interpretation=Interpretation.CATEGORICAL,
                validators=validator_no_nans,
                time_series_limit=0,
            )
    return None
    def set_tmap_params(self, signal_tm) -> bool:
        pattern = re.compile(
            r"^(i|ii|iii|v)_value_(\d+)_to_(\d+)_hrs_(pre|post)_(.*)$", )
        match = pattern.findall(signal_tm.name)
        if not match:
            return False
        lead, time_1, time_2, period, event_proc_tm = match[0]
        self.lead = lead
        visit_tm = get_visit_tmap(
            re.sub(r"(end_date|start_date)", "first_visit", event_proc_tm), )
        self.visit = visit_tm.tensor_from_file(visit_tm, self)
        event_tm = GET_SIGNAL_TMAP(event_proc_tm)
        event_time = event_tm.tensor_from_file(event_tm,
                                               self,
                                               visits=self.visit)[0][0]
        if period == "pre":
            offset = event_time - time_1 * 60 * 60
        else:
            offset = event_time + time_1 * 60 * 60
        signal_size = (time_2 - time_1) * 60 * 60

        self.sampling_rate = ECG_TMAPS[f"{lead}_sample_freq"].tensor_from_file(
            ECG_TMAPS[f"{lead}_sample_freq"],
            self,
            visit=self.visit,
        )[0]
        if self.sampling_rate.size == 1:
            return True

        last = -1
        for k, _ in enumerate(self.sampling_rate):
            self.sampling_rate[k][1] -= offset
            if self.sampling_rate[k][1] < 0:
                last_negative = k
            if self.sampling_rate[k][1] > signal_size:
                last = k
        self.sampling_rate = self.sampling_rate[last_negative:last]
        self.sampling_rate[0][1] = 0
        self.update_sampling_rate()
        return True
def create_static_around_tmap(tm_name: str):
    pattern = re.compile(r"^age_(.*)_(single|double)$")
    match = pattern.findall(tm_name)
    if match:
        event_proc, samples = match[0]
        visit_tm = get_visit_tmap(
            event_proc.replace("end_date", "first_visit").replace(
                "start_date",
                "first_visit",
            ), )
        samples = 1 if samples == "single" else 2
        return TensorMap(
            name=tm_name,
            shape=(samples, ),
            interpretation=Interpretation.CONTINUOUS,
            tensor_from_file=admin_age_event_visit_tensor_from_file(
                visit_tm=visit_tm,
                samples=samples,
            ),
            path_prefix="edw/*",
        )
    pattern = re.compile(
        r"^(age|length_of_stay)_(\d+)_hrs_sliding_window_(.*)"
        r"_to_(.*)_(\d+)_hrs_step$", )
    match = pattern.findall(tm_name)
    if match:
        signal, window, event_proc_tm_1, event_proc_tm_2, step = match[0]
        visit_tm = get_visit_tmap(
            event_proc_tm_1.replace("end_date", "first_visit").replace(
                "start_date",
                "first_visit",
            ), )
        event_proc_tm_1 = get_signal_tmap(event_proc_tm_1)
        event_proc_tm_2 = get_signal_tmap(event_proc_tm_2)
        window = int(window)
        step = int(step)
        if signal == "age":
            tensor_from_file = admin_age_event_visit_tensor_from_file
        else:
            tensor_from_file = length_of_stay_sliding_window_tensor_from_file
        return TensorMap(
            name=tm_name,
            shape=(1, ),
            interpretation=Interpretation.CONTINUOUS,
            tensor_from_file=tensor_from_file(
                visit_tm=visit_tm,
                window=window,
                step=step,
                event_tm_1=event_proc_tm_1,
                event_tm_2=event_proc_tm_2,
            ),
            path_prefix="edw/*",
        )
    pattern = re.compile(r"^length_of_stay_(\d+)_hrs_(pre|post)_(.*)$")
    match = pattern.findall(tm_name)
    if match:
        time, period, event_proc_tm = match[0]
        visit_tm = get_visit_tmap(
            event_proc_tm.replace("end_date", "first_visit").replace(
                "start_date",
                "first_visit",
            ), )
        hrs_to_event = [int(time)]
        periods = [period]
    else:
        pattern = re.compile(
            r"^length_of_stay_(\d+)_hrs_(pre|post)_(.*)"
            r"_(\d+)_hrs_(pre|post)_(.*)$", )
        match = pattern.findall(tm_name)
        if match:
            time_1, period_1, time_2, period_2, event_proc_tm = match[0]
            hrs_to_event = [int(time_1), int(time_2)]
            periods = [period_1, period_2]
    if match:
        visit_tm = get_visit_tmap(
            event_proc_tm.replace("end_date", "first_visit").replace(
                "start_date",
                "first_visit",
            ), )
        event_proc_tm = get_signal_tmap(event_proc_tm)
        return TensorMap(
            name=tm_name,
            shape=(1, ),
            interpretation=Interpretation.CONTINUOUS,
            tensor_from_file=length_of_stay_event_tensor_from_file(
                visit_tm=visit_tm,
                event_tm=event_proc_tm,
                hrs_to_event=hrs_to_event,
                periods=periods,
            ),
            path_prefix="edw/*",
        )
    return None
def create_sliding_window_tmap(tmap_name: str) -> Optional[TensorMap]:
    match = None

    imputation_type = None
    feature = "raw"
    tmap_match_name = tmap_name

    pattern = re.compile(r"^(.*)_(mean_imputation)$")
    match = pattern.findall(tmap_match_name)
    if match:
        _, imputation_type = match[0]
        tmap_match_name = tmap_match_name.replace(f"_{imputation_type}", "")
        match = None

    pattern = re.compile(fr"^(.*)_({FEATURES})$")
    match = pattern.findall(tmap_match_name)
    if match:
        _, feature = match[0]
        tmap_match_name = tmap_match_name.replace(f"_{feature}", "")
        match = None

    if not match:
        pattern = re.compile(
            r"^(.*)_(\d+)_hrs_sliding_window_(.*)_to_(.*)_(\d+)_hrs_step$", )
        match = pattern.findall(tmap_match_name)
        if match:
            signal_tm, window, event_proc_tm_1, event_proc_tm_2, step = match[
                0]
            make_tensor_from_file = make_sliding_window_tensor_from_file

    if not match:
        return None

    if feature == "raw":
        shape = (None, None)
    else:
        shape = (None, )
    if signal_tm.endswith("_timeseries"):
        shape += (2, )

    time_tm = get_time_tm(signal_tm)
    visit_tm = get_visit_tmap(
        re.sub(r"(end_date|start_date)", "first_visit", event_proc_tm_2), )
    signal_tm = _get_tmap(signal_tm)
    event_proc_tm_1 = get_signal_tmap(event_proc_tm_1)
    event_proc_tm_2 = get_signal_tmap(event_proc_tm_2)
    window = int(window)
    step = int(step)

    return TensorMap(
        name=tmap_name,
        shape=shape,
        tensor_from_file=make_tensor_from_file(
            window=window,
            step=step,
            event_tm_1=event_proc_tm_1,
            event_tm_2=event_proc_tm_2,
            visit_tm=visit_tm,
            signal_tm=signal_tm,
            signal_time_tm=time_tm,
            feature=feature,
            imputation_type=imputation_type,
        ),
        channel_map=signal_tm.channel_map,
        path_prefix=signal_tm.path_prefix,
        interpretation=signal_tm.interpretation,
        validators=validator_no_nans,
        time_series_limit=0,
    )
def create_around_tmap(tmap_name: str) -> Optional[TensorMap]:
    match = None

    time_2 = None
    period_2 = None
    event_proc_tm_2 = None
    imputation_type = None
    feature = "raw"

    shape: Optional[Tuple[Axis, ...]] = None
    make_tensor_from_file = None
    tmap_match_name = tmap_name

    if tmap_name.endswith("_explore"):
        return None

    pattern = re.compile(r"^(.*)_(mean_imputation|sample_and_hold)$")
    match = pattern.findall(tmap_match_name)
    if match:
        _, imputation_type = match[0]
        tmap_match_name = tmap_match_name.replace(f"_{imputation_type}", "")
        match = None

    pattern = re.compile(fr"^(.*)_({FEATURES})$")
    match = pattern.findall(tmap_match_name)
    if match:
        _, feature = match[0]
        tmap_match_name = tmap_match_name.replace(f"_{feature}", "")
        match = None

    if not match:
        pattern = re.compile(
            r"^(.*)_(\d+)_hrs_(pre|post)_(.*)_(\d+)_hrs_(pre|post)_(.*)"
            r"_(\d+)_hrs_window$", )
        match = pattern.findall(tmap_match_name)
        if match:
            (
                signal_tm,
                time_1,
                period_1,
                event_proc_tm,
                time_2,
                period_2,
                event_proc_tm_2,
                window,
            ) = match[0]
            make_tensor_from_file = make_around_event_tensor_from_file
            times = [int(time_1), int(time_2)]
            periods = [period_1, period_2]

    if not match:
        pattern = re.compile(
            r"^(.*)_(\d+)_hrs_(pre|post)_(.*)_(\d+)_hrs_window$", )
        match = pattern.findall(tmap_match_name)
        if match:
            signal_tm, time, period, event_proc_tm, window = match[0]
            make_tensor_from_file = make_around_event_tensor_from_file
            times = [int(time)]
            periods = [period]

    if not match:
        return None

    if signal_tm.endswith("_timeseries"):
        if feature == "raw":
            shape = (None, 2)
        else:
            shape = (2, )
    else:
        if feature == "raw":
            shape = (None, )
        else:
            shape = (1, )

    time_tm = get_time_tm(signal_tm)

    visit_tm = get_visit_tmap(
        re.sub(r"(end_date|start_date)", "first_visit", event_proc_tm), )

    window = int(window)
    signal_tm = _get_tmap(signal_tm)
    event_proc_tms = [get_signal_tmap(event_proc_tm)]
    if event_proc_tm_2 is not None:
        event_proc_tms.append(get_signal_tmap(event_proc_tm_2))

    return TensorMap(
        name=tmap_name,
        shape=shape,
        tensor_from_file=make_tensor_from_file(
            times=times,
            periods=periods,
            window=window,
            feature=feature,
            event_tms=event_proc_tms,
            visit_tm=visit_tm,
            signal_tm=signal_tm,
            signal_time_tm=time_tm,
            imputation_type=imputation_type,
        ),
        channel_map=signal_tm.channel_map,
        path_prefix=signal_tm.path_prefix,
        interpretation=signal_tm.interpretation,
        validators=validator_no_nans,
    )