API References

Compute the log-rank P-value and survival curves based on kaplan meier.

# Name : kaplanmeier.py # Author : E.Taskesen # Date : July. 2018

kaplanmeier.kaplanmeier.compute_coord(survtimes)

Compute coordinates.

kaplanmeier.kaplanmeier.example_data()

Create example data.

kaplanmeier.kaplanmeier.fit(time_event, censoring, labx, verbose=3)

Compute the log-rank P-value and survival curves based on kaplan meier.

Parameters
  • time_event (Float.) – Numpy array with survival-time in years/months/days (not a datetime!)

  • censoring (array-like) – numpy array with censoring (1=event, 0=ongoing). At the time you want to make inferences about durations, it is possible, likely true, that not all the death events have occured yet. In case of patients, you would like to put 1=death as an event.

  • labx (array-like of type string or int) – Class labels. Each class label is seperately plotted.

  • verbose (int, (default: 3)) – Verbosity messages.

Returns

  • logrank_P (float) : P-value

  • logrank_Z (float) : Z-score

  • logrank (float) : fitted logrank_test model

  • labx (list) : Class labels

  • uilabx (list) : Unique Class labels

  • time_event (float) : Time to event

  • censoring (bool) : Censored or not

Return type

dict()

Examples

>>> # Import library
>>> import kaplanmeier as km
>>>
>>> # Example data
>>> df = km.example_data()
>>>
>>> # Fit
>>> results = km.fit(df['time'], df['Died'], df['group'])
>>>
>>> # Plot
>>> km.plot(results)
>>>
>>> km.plot(results, cmap='Set1', cii_lines=True, cii_alpha=0.05)
>>> km.plot(results, cmap=[(1, 0, 0),(0, 0, 1)])
>>> km.plot(results, cmap='Set1', methodtype='custom')
>>>
>>> results['logrank_P']
>>> results['logrank_Z']
kaplanmeier.kaplanmeier.loop(newsurv, y, h_coords, v_coords, lost)

Part of computing coordinates (custom implementation).

kaplanmeier.kaplanmeier.make_class_color_names(data, labx, uilabx, cmap)

Create class colors.

kaplanmeier.kaplanmeier.plot(out, fontsize=12, savepath='', width=10, height=6, cmap='Set1', cii_alpha=0.05, cii_lines='dense', methodtype='lifeline', title=None, full_ylim=False, y_percentage=False)

Make plot.

Parameters
  • out (dict) – Results from the fit function.

  • fontsize (int (default: 12)) – Font size for the graph.

  • savepath (String (default: '')) – Path to store the figure.

  • width (int (default: 10)) – Width of the figure.

  • height (int (default: 10)) – height of the figure.

  • cmap (str (default: 'Set1')) – Specify your own colors for each class-label or use a colormap: https://matplotlib.org/examples/color/colormaps_reference.html. [(1, 0, 0),(0, 0, 1),(..)] ‘Set1’ (default) ‘Set2’ Discrete colors ‘Pastel1’ Discrete colors ‘Paired’ Discrete colors ‘rainbow’ ‘bwr’ Blue-white-red ‘binary’ or ‘binary_r’ ‘seismic’ Blue-white-red ‘Blues’ white-to-blue ‘Reds’ white-to-red

  • cii_alpha (float (default: 0.05)) – Confidence interval (works only when methodtype=’lifelines’).

  • cii_lines (String (default: 'dense')) – Confidence lines (works only when methodtype=’lifelines’). ‘dense’ (default) ‘lifelines’ ‘custom’ or None

  • methodtype (str (default: 'lifeline')) –

    Implementation type. ‘dense’ (dense/filled lines) ‘line’

    None (no lines)

  • title (str (default: None)) – In case of None, the logrank P-values is shown. Title of the plot.

Return type

None.

Examples

>>> # Import library
>>> import kaplanmeier as km
>>>
>>> # Example data
>>> df = km.example_data()
>>>
>>> # Fit
>>> results = km.fit(df['time'], df['Died'], df['group'])
>>>
>>> # Plot
>>> km.plot(results)
>>>
>>> km.plot(results, cmap='Set1', cii_lines=True, cii_alpha=0.05)
>>> km.plot(results, cmap=[(1, 0, 0),(0, 0, 1)])
>>> km.plot(results, cmap='Set1', methodtype='custom')