# This script is automatically generated by the abstcal web app based on user input.
# Please carefully review the script and adjust applicable parameters before you run it.
# Author: <NAME>, Ph.D.; Email: <EMAIL>
# Please feel free to contact me if you need any help.

from abstcal import TLFBData, VisitData, AbstinenceCalculator

################################################
# Processing TLFB Data
################################################

# The parameters for processing the TLFB data
# Please update the parameters as applicable.
tlfb_data_params = {'use_raw_date': True, 'cutoff': 0.0, 'subjects': 'all', 'duplicate_mode': 'mean', 'imputation_last_record': 'ffill', 'imputation_mode': 'linear', 'imputation_gap_limit': None, 'outliers_mode': None, 'allowed_min': None, 'allowed_max': None}

# The path to the TLFB data on your computer
tlfb_source = "tests/test_tlfb.csv"

# Create the TLFBData instance
tlfb_data = TLFBData(
    tlfb_source,
    tlfb_data_params["cutoff"],
    tlfb_data_params["subjects"],
    tlfb_data_params["use_raw_date"]
)

# Profile the data
tlfb_data.profile_data(tlfb_data_params['allowed_min'], tlfb_data_params['allowed_max'])

# Drop any records with missing data
tlfb_data.drop_na_records()

# Remove any duplicates
tlfb_data.check_duplicates(tlfb_data_params['duplicate_mode'])

# Recode any outliers
tlfb_data.recode_outliers(tlfb_data_params['allowed_min'], tlfb_data_params['allowed_min'],tlfb_data_params['outliers_mode'])

# Impute the TLFB data
imputation_params = [
    tlfb_data_params["imputation_mode"],
    tlfb_data_params["imputation_last_record"],
    tlfb_data_params["imputation_gap_limit"]
]

tlfb_data.impute_data(*imputation_params)

################################################
# Processing Visit Data
################################################

# The parameters for processing the visit data, you need to update some of them
visit_data_params = {'data_format': 'long', 'use_raw_date': True, 'expected_visits': 'infer', 'subjects': 'all', 'duplicate_mode': 'mean', 'imputation_mode': 'freq', 'anchor_visit': None, 'allowed_min': None, 'allowed_max': None, 'outliers_mode': None}

# The path to the visit data on your computer
visit_source = "tests/test_visit.csv"

# Create the VisitData instance
visit_data = VisitData(
    visit_source,
    visit_data_params["data_format"],
    visit_data_params["expected_visits"],
    visit_data_params["subjects"],
    visit_data_params["use_raw_date"]
)

# Profile the data
visit_data.profile_data(visit_data_params['allowed_min'], visit_data_params['allowed_max'])

# Remove any duplicates
visit_data.check_duplicates(visit_data_params['duplicate_mode'])

# Recode any outliers
visit_data.recode_outliers(visit_data_params['allowed_min'], visit_data_params['allowed_min'],visit_data_params['outliers_mode'])

# Impute missing dates
visit_data.impute_data(visit_data_params['imputation_mode'], anchor_visit=visit_data_params['anchor_visit'])

################################################
# Abstinence Calculation
################################################
# Create the calculator using TLFB and visit data
calculator = AbstinenceCalculator(tlfb_data, visit_data)

# The shared parameters for abstinence calculation
abst_params_shared = {'mode': 'itt', 'including_end': False}

# Create a list to hold calculation results
calculation_results = list()

# Point-prevalence abstinence
# Please update the parameters for point-prevalence abstinence calculations
abst_pp_params = {'visits': [4], 'abst_var_names': 'infer', 'days': [7]}

abstinence_pp = calculator.abstinence_pp(
    abst_pp_params["visits"],
    abst_pp_params["days"],
    abst_pp_params["abst_var_names"],
    abst_params_shared["including_end"],
    abst_params_shared["mode"]
)
calculation_results.append(abstinence_pp)


# Point-Prolonged abstinence
# Please update the parameters for prolonged abstinence calculations
abst_prol_params = {'visits': [], 'abst_var_names': 'infer', 'quit_visit': 1, 'lapse_definitions': ['5 cigs'], 'grace_period': 14}

abstinence_prol = calculator.abstinence_prolonged(
    abst_prol_params["quit_visit"],
    abst_prol_params["visits"],
    abst_prol_params["lapse_definitions"],
    abst_prol_params["grace_period"],
    abst_prol_params["abst_var_names"],
    abst_params_shared["including_end"],
    abst_params_shared["mode"]
)
calculation_results.append(abstinence_prol)


# Continuous abstinence
# Please update the parameters for continuous abstinence calculations
abst_cont_params = {'visits': [4], 'abst_var_names': 'infer', 'start_visit': 1}

abstinence_cont = calculator.abstinence_cont(
    abst_cont_params["start_visit"],
    abst_cont_params["visits"],
    abst_cont_params["abst_var_names"],
    abst_params_shared["including_end"],
    abst_params_shared["mode"]
)
calculation_results.append(abstinence_cont)


# Write the results to files on your computer
# Set the file path to save the abstinence results
abst_filepath = None
# Set the file path to save the lapse results
lapse_filepath = None

# Save the abstinence data
calculator.merge_abst_data([x[0] for x in calculation_results], abst_filepath)

# Save the lapse data
calculator.merge_lapse_data([x[1] for x in calculation_results], lapse_filepath)
# This script is automatically generated by the abstcal web app based on user input.
# Please carefully review the script and adjust applicable parameters before you run it.
# Author: <NAME>, Ph.D.; Email: <EMAIL>
# Please feel free to contact me if you need any help.

from abstcal import TLFBData, VisitData, AbstinenceCalculator

################################################
# Processing TLFB Data
################################################

# The parameters for processing the TLFB data
# Please update the parameters as applicable.
tlfb_data_params = {'use_raw_date': True, 'cutoff': 0.0, 'subjects': 'all', 'duplicate_mode': 'mean', 'imputation_last_record': 'ffill', 'imputation_mode': 'linear', 'imputation_gap_limit': None, 'outliers_mode': None, 'allowed_min': None, 'allowed_max': None}

# The path to the TLFB data on your computer
tlfb_source = "tests/test_tlfb.csv"

# Create the TLFBData instance
tlfb_data = TLFBData(
    tlfb_source,
    tlfb_data_params["cutoff"],
    tlfb_data_params["subjects"],
    tlfb_data_params["use_raw_date"]
)

# Profile the data
tlfb_data.profile_data(tlfb_data_params['allowed_min'], tlfb_data_params['allowed_max'])

# Drop any records with missing data
tlfb_data.drop_na_records()

# Remove any duplicates
tlfb_data.check_duplicates(tlfb_data_params['duplicate_mode'])

# Recode any outliers
tlfb_data.recode_outliers(tlfb_data_params['allowed_min'], tlfb_data_params['allowed_min'],tlfb_data_params['outliers_mode'])

# Impute the TLFB data
imputation_params = [
    tlfb_data_params["imputation_mode"],
    tlfb_data_params["imputation_last_record"],
    tlfb_data_params["imputation_gap_limit"]
]

tlfb_data.impute_data(*imputation_params)

################################################
# Processing Visit Data
################################################

# The parameters for processing the visit data, you need to update some of them
visit_data_params = {'data_format': 'long', 'use_raw_date': True, 'expected_visits': 'infer', 'subjects': 'all', 'duplicate_mode': 'mean', 'imputation_mode': 'freq', 'anchor_visit': None, 'allowed_min': None, 'allowed_max': None, 'outliers_mode': None}

# The path to the visit data on your computer
visit_source = "tests/test_visit.csv"

# Create the VisitData instance
visit_data = VisitData(
    visit_source,
    visit_data_params["data_format"],
    visit_data_params["expected_visits"],
    visit_data_params["subjects"],
    visit_data_params["use_raw_date"]
)

# Profile the data
visit_data.profile_data(visit_data_params['allowed_min'], visit_data_params['allowed_max'])

# Remove any duplicates
visit_data.check_duplicates(visit_data_params['duplicate_mode'])

# Recode any outliers
visit_data.recode_outliers(visit_data_params['allowed_min'], visit_data_params['allowed_min'],visit_data_params['outliers_mode'])

# Impute missing dates
visit_data.impute_data(visit_data_params['imputation_mode'], anchor_visit=visit_data_params['anchor_visit'])

################################################
# Abstinence Calculation
################################################
# Create the calculator using TLFB and visit data
calculator = AbstinenceCalculator(tlfb_data, visit_data)

# The shared parameters for abstinence calculation
abst_params_shared = {'mode': 'itt', 'including_end': False}

# Create a list to hold calculation results
calculation_results = list()

# Point-prevalence abstinence
# Please update the parameters for point-prevalence abstinence calculations
abst_pp_params = {'visits': [4], 'abst_var_names': 'infer', 'days': [7]}

abstinence_pp = calculator.abstinence_pp(
    abst_pp_params["visits"],
    abst_pp_params["days"],
    abst_pp_params["abst_var_names"],
    abst_params_shared["including_end"],
    abst_params_shared["mode"]
)
calculation_results.append(abstinence_pp)


# Point-Prolonged abstinence
# Please update the parameters for prolonged abstinence calculations
abst_prol_params = {'visits': [], 'abst_var_names': 'infer', 'quit_visit': 1, 'lapse_definitions': ['5 cigs'], 'grace_period': 14}

abstinence_prol = calculator.abstinence_prolonged(
    abst_prol_params["quit_visit"],
    abst_prol_params["visits"],
    abst_prol_params["lapse_definitions"],
    abst_prol_params["grace_period"],
    abst_prol_params["abst_var_names"],
    abst_params_shared["including_end"],
    abst_params_shared["mode"]
)
calculation_results.append(abstinence_prol)


# Continuous abstinence
# Please update the parameters for continuous abstinence calculations
abst_cont_params = {'visits': [4], 'abst_var_names': 'infer', 'start_visit': 1}

abstinence_cont = calculator.abstinence_cont(
    abst_cont_params["start_visit"],
    abst_cont_params["visits"],
    abst_cont_params["abst_var_names"],
    abst_params_shared["including_end"],
    abst_params_shared["mode"]
)
calculation_results.append(abstinence_cont)


# Write the results to files on your computer
# Set the file path to save the abstinence results
abst_filepath = None
# Set the file path to save the lapse results
lapse_filepath = None

# Save the abstinence data
calculator.merge_abst_data([x[0] for x in calculation_results], abst_filepath)

# Save the lapse data
calculator.merge_lapse_data([x[1] for x in calculation_results], lapse_filepath)
# This script is automatically generated by the abstcal web app based on user input. # Please carefully review the script and adjust applicable parameters before you run it. # Author: <NAME>, Ph.D.; Email: <EMAIL> # Please feel free to contact me if you need any help. ################################################ # Processing TLFB Data ################################################ # The parameters for processing the TLFB data # Please update the parameters as applicable. # The path to the TLFB data on your computer # Create the TLFBData instance # Profile the data # Drop any records with missing data # Remove any duplicates # Recode any outliers # Impute the TLFB data ################################################ # Processing Visit Data ################################################ # The parameters for processing the visit data, you need to update some of them # The path to the visit data on your computer # Create the VisitData instance # Profile the data # Remove any duplicates # Recode any outliers # Impute missing dates ################################################ # Abstinence Calculation ################################################ # Create the calculator using TLFB and visit data # The shared parameters for abstinence calculation # Create a list to hold calculation results # Point-prevalence abstinence # Please update the parameters for point-prevalence abstinence calculations # Point-Prolonged abstinence # Please update the parameters for prolonged abstinence calculations # Continuous abstinence # Please update the parameters for continuous abstinence calculations # Write the results to files on your computer # Set the file path to save the abstinence results # Set the file path to save the lapse results # Save the abstinence data # Save the lapse data