diff --git a/yli/survival.py b/yli/survival.py index 4fba928..b5d8586 100644 --- a/yli/survival.py +++ b/yli/survival.py @@ -135,7 +135,7 @@ def calc_survfunc_kaplanmeier(time, status, ci, transform_x=None, transform_y=No return xpoints, ypoints, None, None -def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, transform_x=None, transform_y=None, nan_policy='warn', fig=None, ax=None): +def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, maxiter=None, fail_prob_tolerance=None, se_method=None, zero_tolerance=None, transform_x=None, transform_y=None, nan_policy='warn', fig=None, ax=None): """ Generate a Turnbull estimator plot, which extends the Kaplan–Meier estimator to interval-censored observations @@ -157,6 +157,14 @@ def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, trans :type ci: bool :param step_loc: Proportion along the length of each Turnbull interval to step down the survival function, e.g. 0 for left bound, 1 for right bound, 0.5 for interval midpoint :type step_loc: float + :param maxiter: Maximum number of iterations to attempt + :type maxiter: int + :param fail_prob_tolerance: Terminate algorithm when the absolute change in failure probability in each interval is less than this tolerance + :type fail_prob_tolerance: float + :param se_method: Method for computing standard error or survival probabilities (see hpstat *turnbull* documentation) + :type se_method: str + :param zero_tolerance: Threshold for dropping failure probability when se_method is "oim-drop-zeros" + :type zero_tolerance: float :param transform_x: Function to transform x axis by :type transform_x: callable :param transform_y: Function to transform y axis by @@ -187,11 +195,11 @@ def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, trans for group in groups.groups: subset = groups.get_group(group) - handle = plot_survfunc_turnbull(ax, subset[time_left], subset[time_right], ci, step_loc, transform_x, transform_y) + handle = plot_survfunc_turnbull(ax, subset[time_left], subset[time_right], ci, step_loc, maxiter, fail_prob_tolerance, se_method, zero_tolerance, transform_x, transform_y) handle.set_label('{} = {}'.format(by, group)) else: # No grouping - plot_survfunc_turnbull(ax, df[time_left], df[time_right], ci, step_loc, transform_x, transform_y) + plot_survfunc_turnbull(ax, df[time_left], df[time_right], ci, step_loc, maxiter, fail_prob_tolerance, se_method, zero_tolerance, transform_x, transform_y) if time_units: ax.set_xlabel('{} + {} ({})'.format(time_left, time_right, time_units)) @@ -206,8 +214,8 @@ def turnbull(df, time_left, time_right, by=None, *, ci=True, step_loc=0.5, trans return fig, ax -def plot_survfunc_turnbull(ax, time_left, time_right, ci, step_loc=0.5, transform_x=None, transform_y=None): - xpoints, ypoints, ypoints0, ypoints1 = calc_survfunc_turnbull(time_left, time_right, ci, step_loc, transform_x, transform_y) +def plot_survfunc_turnbull(ax, time_left, time_right, ci, step_loc=0.5, maxiter=None, fail_prob_tolerance=None, se_method=None, zero_tolerance=None, transform_x=None, transform_y=None): + xpoints, ypoints, ypoints0, ypoints1 = calc_survfunc_turnbull(time_left, time_right, ci, step_loc, maxiter, fail_prob_tolerance, se_method, zero_tolerance, transform_x, transform_y) handle = ax.plot(xpoints, ypoints)[0] @@ -216,12 +224,23 @@ def plot_survfunc_turnbull(ax, time_left, time_right, ci, step_loc=0.5, transfor return handle -def calc_survfunc_turnbull(time_left, time_right, ci, step_loc=0.5, transform_x=None, transform_y=None): +def calc_survfunc_turnbull(time_left, time_right, ci, step_loc=0.5, maxiter=None, fail_prob_tolerance=None, se_method=None, zero_tolerance=None, transform_x=None, transform_y=None): # Estimate the survival function # Prepare arguments - # TODO: Pass through other arguments hpstat_args = [config.hpstat_path, 'turnbull', '-', '--output', 'json'] + if maxiter: + hpstat_args.append('--max-iterations') + hpstat_args.append(str(maxiter)) + if fail_prob_tolerance: + hpstat_args.append('--fail-prob-tolerance') + hpstat_args.append(str(fail_prob_tolerance)) + if se_method: + hpstat_args.append('--se-method') + hpstat_args.append(se_method) + if zero_tolerance: + hpstat_args.append('--zero-tolerance') + hpstat_args.append(str(zero_tolerance)) # Export data to CSV csv_buf = io.StringIO()