Skip to content

Commit

Permalink
Merge pull request #302 from CamDavidsonPilon/ix-loc
Browse files Browse the repository at this point in the history
remove ix from the api
  • Loading branch information
CamDavidsonPilon committed Jun 11, 2017
2 parents 65d03e5 + 873dffe commit e7c967d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
### Changelogs

#### 0.10.2
#### 0.11.0
- introduce a new `.plot` function to a fitted `CoxPHFitter` instance. This plots the hazard coefficients and their confidence intervals.
- in all plot methods, the `ix` kwarg has been deprecated in favour of a new `loc` kwarg. This is to align with Pandas deprecating `ix`

#### 0.10.1
- fix in internal normalization for `CoxPHFitter` predict methods.
Expand Down
6 changes: 3 additions & 3 deletions docs/Intro to lifelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,14 @@ gets smaller (as seen by the decreasing rate of change). Let's break the
regimes down between democratic and non-democratic, during the first 20
years:

.. note:: We are using the ``ix`` argument in the call to ``plot`` here: it accepts a ``slice`` and plots only points within that slice.
.. note:: We are using the ``loc`` argument in the call to ``plot`` here: it accepts a ``slice`` and plots only points within that slice.

.. code:: python
naf.fit(T[dem], event_observed=C[dem], label="Democratic Regimes")
ax = naf.plot(ix=slice(0,20))
ax = naf.plot(loc=slice(0,20))
naf.fit(T[~dem], event_observed=C[~dem], label="Non-democratic Regimes")
naf.plot(ax=ax, ix=slice(0,20))
naf.plot(ax=ax, loc=slice(0,20))
plt.title("Cumulative hazard function of different global regimes");
Expand Down
12 changes: 6 additions & 6 deletions lifelines/fitters/aalen_additive_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,12 @@ def predict_expectation(self, X):
t = self.cumulative_hazards_.index
return pd.DataFrame(trapz(self.predict_survival_function(X)[index].values.T, t), index=index)

def plot(self, ix=None, iloc=None, columns=[], legend=True, **kwargs):
def plot(self, loc=None, iloc=None, columns=[], legend=True, **kwargs):
""""
A wrapper around plotting. Matplotlib plot arguments can be passed in, plus:
ix: specify a time-based subsection of the curves to plot, ex:
.plot(ix=slice(0.,10.)) will plot the time values between t=0. and t=10.
.plot(loc=slice(0.,10.)) will plot the time values between t=0. and t=10.
iloc: specify a location-based subsection of the curves to plot, ex:
.plot(iloc=slice(0,10)) will plot the first 10 time points.
columns: If not empty, plot a subset of columns from the cumulative_hazards_. Default all.
Expand All @@ -433,13 +433,13 @@ def shaded_plot(ax, x, y, y_upper, y_lower, **kwargs):
fill_between_steps(x, y_lower, y2=y_upper, ax=ax, alpha=0.25,
color=base_line.get_color(), linewidth=1.0)

assert (ix is None or iloc is None), 'Cannot set both ix and iloc in call to .plot'
assert (loc is None or iloc is None), 'Cannot set both loc and iloc in call to .plot'

get_method = "ix" if ix is not None else "iloc"
if iloc == ix is None:
get_method = "loc" if loc is not None else "iloc"
if iloc == loc is None:
user_submitted_ix = slice(0, None)
else:
user_submitted_ix = ix if ix is not None else iloc
user_submitted_ix = loc if loc is not None else iloc
get_loc = lambda df: getattr(df, get_method)[user_submitted_ix]

if len(columns) == 0:
Expand Down
28 changes: 14 additions & 14 deletions lifelines/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ def set_kwargs_drawstyle(kwargs):
kwargs['drawstyle'] = kwargs.get('drawstyle', 'steps-post')


def create_dataframe_slicer(iloc, ix):
user_did_not_specify_certain_indexes = (iloc is None) and (ix is None)
user_submitted_slice = slice(None) if user_did_not_specify_certain_indexes else coalesce(ix, iloc)
def create_dataframe_slicer(iloc, loc):
user_did_not_specify_certain_indexes = (iloc is None) and (loc is None)
user_submitted_slice = slice(None) if user_did_not_specify_certain_indexes else coalesce(loc, iloc)

get_method = "ix" if ix is not None else "iloc"
get_method = "loc" if loc is not None else "iloc"
return lambda df: getattr(df, get_method)[user_submitted_slice]


Expand All @@ -248,12 +248,12 @@ def plot_loglogs(cls):
Specifies a plot of the log(-log(SV)) versus log(time) where SV is the estimated survival function.
"""

def _plot_loglogs(ix=None, iloc=None, show_censors=False, censor_styles=None, **kwargs):
def _plot_loglogs(loc=None, iloc=None, show_censors=False, censor_styles=None, **kwargs):

loglog = lambda s: np.log(-np.log(s))

if (ix is not None) and (iloc is not None):
raise ValueError('Cannot set both ix and iloc in call to .plot().')
if (loc is not None) and (iloc is not None):
raise ValueError('Cannot set both loc and iloc in call to .plot().')

if censor_styles is None:
censor_styles = {}
Expand All @@ -263,7 +263,7 @@ def _plot_loglogs(ix=None, iloc=None, show_censors=False, censor_styles=None, **
set_kwargs_drawstyle(kwargs)
kwargs['logx'] = True

dataframe_slicer = create_dataframe_slicer(iloc, ix)
dataframe_slicer = create_dataframe_slicer(iloc, loc)

# plot censors
ax = kwargs['ax']
Expand Down Expand Up @@ -311,8 +311,8 @@ def plot_estimate(cls, estimate):
the lines' labels to the legend. Default: False
at_risk_counts: show group sizes at time points. See function
'add_at_risk_counts' for details. Default: False
ix: specify a time-based subsection of the curves to plot, ex:
.plot(ix=slice(0.,10.))
loc: specify a time-based subsection of the curves to plot, ex:
.plot(loc=slice(0.,10.))
will plot the time values between t=0. and t=10.
iloc: specify a location-based subsection of the curves to plot, ex:
.plot(iloc=slice(0,10))
Expand All @@ -324,16 +324,16 @@ def plot_estimate(cls, estimate):
ax: a pyplot axis object
""" % estimate

def plot(ix=None, iloc=None, show_censors=False,
def plot(loc=None, iloc=None, show_censors=False,
censor_styles=None, ci_legend=False, ci_force_lines=False,
ci_alpha=0.25, ci_show=True, at_risk_counts=False,
bandwidth=None, **kwargs):

if censor_styles is None:
censor_styles = {}

if (ix is not None) and (iloc is not None):
raise ValueError('Cannot set both ix and iloc in call to .plot().')
if (loc is not None) and (iloc is not None):
raise ValueError('Cannot set both loc and iloc in call to .plot().')

set_kwargs_ax(kwargs)
set_kwargs_color(kwargs)
Expand All @@ -349,7 +349,7 @@ def plot(ix=None, iloc=None, show_censors=False,
estimate_ = getattr(cls, estimate)
confidence_interval_ = getattr(cls, 'confidence_interval_')

dataframe_slicer = create_dataframe_slicer(iloc, ix)
dataframe_slicer = create_dataframe_slicer(iloc, loc)

# plot censors
ax = kwargs['ax']
Expand Down
4 changes: 2 additions & 2 deletions tests/test_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ def test_aalen_additive_fit_no_censor(self, block):
ax = plt.subplot(d + 1, 1, i + 1)
col = cumulative_hazards.columns[i]
ax = cumulative_hazards[col].loc[:15].plot(legend=False, ax=ax)
ax = aaf.plot(ix=slice(0, 15), ax=ax, columns=[col], legend=False)
ax = aaf.plot(loc=slice(0, 15), ax=ax, columns=[col], legend=False)
plt.show(block=block)
return

Expand Down Expand Up @@ -1187,7 +1187,7 @@ def test_aalen_additive_fit_with_censor(self, block):
ax = plt.subplot(d + 1, 1, i + 1)
col = cumulative_hazards.columns[i]
ax = cumulative_hazards[col].loc[:15].plot(legend=False, ax=ax)
ax = aaf.plot(ix=slice(0, 15), ax=ax, columns=[col], legend=False)
ax = aaf.plot(loc=slice(0, 15), ax=ax, columns=[col], legend=False)
plt.show(block=block)
return

Expand Down
4 changes: 2 additions & 2 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_naf_plotting_slice(self, block):
data2 = np.random.exponential(1, size=(200, 1))
naf = NelsonAalenFitter()
naf.fit(data1)
ax = naf.plot(ix=slice(0, None))
ax = naf.plot(loc=slice(0, None))
naf.fit(data2)
naf.plot(ax=ax, ci_force_lines=True, iloc=slice(100, 180))
self.plt.title('test_naf_plotting_slice')
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_naf_plot_cumulative_hazard_bandwidth_2(self, block):
data1 = np.random.exponential(5, size=(2000, 1))
naf = NelsonAalenFitter()
naf.fit(data1)
naf.plot_hazard(bandwidth=1., ix=slice(0, 7.))
naf.plot_hazard(bandwidth=1., loc=slice(0, 7.))
self.plt.title('test_naf_plot_cumulative_hazard_bandwidth_2')
self.plt.show(block=block)
return
Expand Down

0 comments on commit e7c967d

Please sign in to comment.