pat2vec.pat2vec_get_methods.get_method_drugs

Functions

calculate_drug_features(order_name_df_dict, ...)

Calculates drug features for each order type based on config flags.

create_drug_features_dataframe(...)

Creates the final drug features DataFrame.

get_current_pat_drugs(...[, config_obj, ...])

Retrieves drug/medication features for a patient within a date range.

prepare_drug_datetime(drugs_data, ...[, ...])

Prepares the datetime column for drug data processing.

search_drug_orders([...])

Searches for drug/medication orders within a date range.

pat2vec.pat2vec_get_methods.get_method_drugs.search_drug_orders(cohort_searcher_with_terms_and_search=None, client_id_codes=None, drug_time_field='order_createdwhen', start_year='1995', start_month='01', start_day='01', end_year='2025', end_month='12', end_day='12', additional_custom_search_string=None)[source]

Searches for drug/medication orders within a date range.

Uses a cohort searcher to find medication orders for specified patients.

Parameters:
  • cohort_searcher_with_terms_and_search (Optional[Callable]) – The function for cohort searching. Defaults to None.

  • client_id_codes (Optional[Union[str, List[str]]]) – The client ID code(s) of the patient(s). Defaults to None.

  • drug_time_field (str) – The timestamp field for filtering drug orders. Defaults to ‘order_createdwhen’.

  • start_year (str) – Start year for the search. Defaults to ‘1995’.

  • start_month (str) – Start month for the search. Defaults to ‘01’.

  • start_day (str) – Start day for the search. Defaults to ‘01’.

  • end_year (str) – End year for the search. Defaults to ‘2025’.

  • end_month (str) – End month for the search. Defaults to ‘12’.

  • end_day (str) – End day for the search. Defaults to ‘12’.

  • additional_custom_search_string (Optional[str]) – An additional string to append to the search query. Defaults to None.

Returns:

A DataFrame containing the raw drug order data.

Return type:

pd.DataFrame

Raises:

ValueError – If essential arguments are None.

pat2vec.pat2vec_get_methods.get_method_drugs.prepare_drug_datetime(drugs_data, drug_time_field, batch_mode=False)[source]

Prepares the datetime column for drug data processing.

Creates a ‘datetime’ column by either copying the specified time field (in batch mode) or converting it to datetime objects.

Parameters:
  • drugs_data (pd.DataFrame) – Raw drug order data.

  • drug_time_field (str) – The name of the time field to process.

  • batch_mode (bool) – Whether the function is running in batch mode. Defaults to False.

Returns:

The input DataFrame with an added ‘datetime’ column.

Return type:

pd.DataFrame

pat2vec.pat2vec_get_methods.get_method_drugs.calculate_drug_features(order_name_df_dict, order_name_list, drugs_arg_dict, batch_mode=False)[source]

Calculates drug features for each order type based on config flags.

Computes features like the number of orders, days since the last order, and the time span between the first and last order, depending on the flags in drugs_arg_dict.

Parameters:
  • order_name_df_dict (Dict[str, pd.DataFrame]) – A dictionary mapping drug order names to their corresponding DataFrames.

  • order_name_list (List[str]) – A list of unique order names to process.

  • drugs_arg_dict (Dict) – A dictionary of flags indicating which features to calculate.

  • batch_mode (bool) – Whether the function is running in batch mode. Defaults to False.

Returns:

A dictionary of calculated features.

Return type:

Dict

pat2vec.pat2vec_get_methods.get_method_drugs.create_drug_features_dataframe(current_pat_client_id_code, drug_features, original_data)[source]

Creates the final drug features DataFrame.

Combines the patient’s ID with the calculated drug features into a single-row DataFrame.

Parameters:
  • current_pat_client_id_code (str) – The patient’s client ID.

  • drug_features (Dict) – The dictionary of calculated features.

  • original_data (pd.DataFrame) – The original drug data, used for reference.

Returns:

A single-row DataFrame containing the final features.

Return type:

pd.DataFrame

pat2vec.pat2vec_get_methods.get_method_drugs.get_current_pat_drugs(current_pat_client_id_code, target_date_range, pat_batch, config_obj=None, cohort_searcher_with_terms_and_search=None)[source]

Retrieves drug/medication features for a patient within a date range.

This function fetches drug order data, either from a pre-loaded batch or by searching. It then calculates time-based features for each type of drug order based on the provided configuration.

Parameters:
  • current_pat_client_id_code (str) – The client ID code of the patient.

  • target_date_range (Tuple) – A tuple representing the target date range.

  • pat_batch (pd.DataFrame) – The DataFrame containing patient data for batch mode.

  • config_obj (Optional[object]) – Configuration object containing settings like batch_mode, drug_time_field, and feature engineering arguments. Defaults to None.

  • cohort_searcher_with_terms_and_search (Optional[Callable]) – The function for cohort searching. Defaults to None.

Returns:

A DataFrame containing drug order features for the

specified patient.

Return type:

pd.DataFrame