Skip to content

Commit

Permalink
improved outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
okasag committed Aug 17, 2022
1 parent 9b96544 commit 154ce32
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 31 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
/samplefit.egg-info/*
MANIFEST

# exclude testing files
samplefit_replication.ipynb
samplefit_testing.ipynb
# exclude replication and testing files
replication/

# exclude data and html files
data/
Expand Down
34 changes: 24 additions & 10 deletions samplefit/Reliability.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ def score(self):
return super().score()


def anneal(self, share=0.05, n_boot=None):
def anneal(self, share=0.1, n_boot=None):
"""
Sample annealing based on the reliability scores via the RSR algorithm.
Parameters
----------
share : float or NoneType
Share of sample that gets annealed based on the most unreliable
observations. Default is 0.05.
observations. Default is 0.1.
n_boot : int or NoneType
Number of bootstrap replications for inference. If None specified,
asymptotic approximation is used for inference instead. For valid
Expand Down Expand Up @@ -493,7 +493,8 @@ def summary(self,
xname=None,
title=None,
alpha=0.05,
percentile=False):
percentile=False,
get_table=False):
"""
Summary of the sample fit via the RSR algorithm.
Expand All @@ -513,6 +514,9 @@ def summary(self,
Percentile method for confidence intervals based on bootstrapping.
If bootstrapping has not been used for fitting, it is ignored.
Default is False.
get_table : bool
If a summary table should be returned or not. If True, a pandas
DataFrame with estimation results is returned. Default is False.
Returns
-------
Expand Down Expand Up @@ -559,7 +563,8 @@ def summary(self,
xname=xname,
title=title,
alpha=alpha,
percentile=percentile
percentile=percentile,
get_table=get_table
)


Expand Down Expand Up @@ -599,7 +604,8 @@ def plot(self,
figsize=None,
ylim=None,
xlabel=None,
dpi=None):
dpi=None,
fname=None):
"""
Plot the Annealing based on the reliability scores via the RSR
of class `Sample()`.
Expand Down Expand Up @@ -635,6 +641,9 @@ def plot(self,
Label for the x axis for the exog variable. Default is 'xname'.
dpi : float, int or NoneType
The resolution for matplotlib scatter plot. Default is 100.
fname : str or NoneType
Valid figure name to save the plot. If None, generic name is used.
Default is None.
Returns
Expand Down Expand Up @@ -686,7 +695,8 @@ def plot(self,
figsize=figsize,
ylim=ylim,
xlabel=xlabel,
dpi=dpi
dpi=dpi,
fname=fname
)


Expand Down Expand Up @@ -782,7 +792,8 @@ def plot(self,
s=None,
ylim=None,
xlabel=None,
dpi=None):
dpi=None,
fname=None):
"""
Plot the Reliability Scores based on the RSR algorithm.
Expand Down Expand Up @@ -816,8 +827,10 @@ def plot(self,
Label for the x axis for the exog variable. Default is 'xname'.
dpi : float, int or NoneType
The resolution for matplotlib scatter plot. Default is 100.
fname : str or NoneType
Valid figure name to save the plot. If None, generic name is used.
Default is None.
Returns
-------
Dictionary of matplotlib figures. Prints annealing plots.
Expand Down Expand Up @@ -867,5 +880,6 @@ def plot(self,
s=s,
ylim=ylim,
xlabel=xlabel,
dpi=dpi
dpi=dpi,
fname=fname
)
83 changes: 65 additions & 18 deletions samplefit/_BaseResultsReliability.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,14 @@ def conf_int(self, alpha=0.05, percentile=False):

# function to print summary
def summary(self, yname=None, xname=None, title=None, alpha=0.05,
percentile=False):
percentile=False, get_table=False):
"""
RSR fit summary.
"""

# check inputs for summary
self._check_summary_inputs(yname, xname, title, alpha, percentile)
self._check_summary_inputs(yname, xname, title, alpha, percentile,
get_table)

# get the preamble
# info abour RSR
Expand Down Expand Up @@ -263,8 +264,8 @@ def summary(self, yname=None, xname=None, title=None, alpha=0.05,
'=' * 80,
sep='\n')

# return None
return None
# return table or None
return summary_table if self.get_table else None


#%% non-user in-class functions
Expand Down Expand Up @@ -430,7 +431,8 @@ def _check_conf_int_inputs(self, alpha, percentile):


# function for summary inputs checks
def _check_summary_inputs(self, yname, xname, title, alpha, percentile):
def _check_summary_inputs(self, yname, xname, title, alpha, percentile,
get_table):
"""Input checks for the .summary() function."""

# check name for y
Expand Down Expand Up @@ -501,7 +503,16 @@ def _check_summary_inputs(self, yname, xname, title, alpha, percentile):
else:
# raise value error
raise ValueError("percentile must be of type boolean"
", got %s" % percentile)
", got %s" % type(percentile))

# check whether to return table or not
if isinstance(get_table, bool):
# assign to self
self.get_table = get_table
else:
# raise value error
raise ValueError("get_table must be of type boolean"
", got %s" % type(get_table))


# define BaseAnnealResults class
Expand Down Expand Up @@ -655,14 +666,15 @@ def _output_checks(self):
# function to plot annealing sensitivity
def plot(self,
xname=None, title=None, alpha=0.05, percentile=False, color=None,
path=None, figsize=None, ylim=None, xlabel=None, dpi=None):
path=None, figsize=None, ylim=None, xlabel=None, dpi=None,
fname=None):
"""
RSR Annealing Sensitivity Plot.
"""

# check inputs for plot
self._check_plot_inputs(
xname, title, color, path, figsize, ylim, xlabel, dpi)
xname, title, color, path, figsize, ylim, xlabel, dpi, fname)

# set resolution
plt.rcParams['figure.dpi'] = self.dpi
Expand Down Expand Up @@ -729,8 +741,12 @@ def plot(self,
# save plot
if not self.path is None:
# save plot
fig.savefig(self.path + '/' + var_name + '_annealing.png',
bbox_inches='tight')
if not self.fname is None:
fig.savefig(self.path + '/' + self.fname,
bbox_inches='tight')
else:
fig.savefig(self.path + '/' + var_name + '_annealing.png',
bbox_inches='tight')

# save figure
figures[var_name] = (fig, ax)
Expand Down Expand Up @@ -805,7 +821,7 @@ def _check_conf_int_inputs(self, alpha, percentile):

# fucntion to check inputs for plot function
def _check_plot_inputs(self, xname, title, color, path, figsize, ylim,
xlabel, dpi):
xlabel, dpi, fname):
"""Input checks for the .plot() function."""

# check xname
Expand Down Expand Up @@ -852,6 +868,19 @@ def _check_plot_inputs(self, xname, title, color, path, figsize, ylim,
raise ValueError("path must be a string"
", got %s" % type(path))

# check fname for plot
if fname is None:
# set default as None
self.fname = None
# if supplied check if its valid
elif isinstance(fname, str):
# set value to user supplied
self.fname = fname
else:
# raise value error
raise ValueError("fname must be a string"
", got %s" % type(fname))

# check path for the plot
if figsize is None:
# set default as 10,5
Expand Down Expand Up @@ -1085,16 +1114,17 @@ def _output_checks(self):


# %% in-class functions
# function to plot annealing sensitivity
def plot(self,yname=None, xname=None, title=None, cmap=None, path=None,
figsize=None, s=None, ylim=None, xlabel=None, dpi=None):
# function to plot reliability scores
def plot(self, yname=None, xname=None, title=None, cmap=None, path=None,
figsize=None, s=None, ylim=None, xlabel=None, dpi=None,
fname=None):
"""
RSR Reliability Scores Plot.
"""

# check inputs for plot
self._check_plot_inputs(yname, xname, title, cmap, path, figsize, s,
ylim, xlabel, dpi)
ylim, xlabel, dpi, fname)

# set resolution
plt.rcParams['figure.dpi'] = self.dpi
Expand Down Expand Up @@ -1142,8 +1172,12 @@ def plot(self,yname=None, xname=None, title=None, cmap=None, path=None,
# save plot
if not self.path is None:
# save plot
fig.savefig(self.path + '/' + var_name + '_scores.png',
bbox_inches='tight')
if not self.fname is None:
fig.savefig(self.path + '/' + self.fname,
bbox_inches='tight')
else:
fig.savefig(self.path + '/' + var_name + '_scores.png',
bbox_inches='tight')

# save figure
figures[var_name] = (fig, ax, plot)
Expand All @@ -1163,7 +1197,7 @@ def plot(self,yname=None, xname=None, title=None, cmap=None, path=None,

# check inputs for score plot
def _check_plot_inputs(self, yname, xname, title, cmap, path, figsize, s,
ylim, xlabel, dpi):
ylim, xlabel, dpi, fname):
"""Input checks for the .plot() function."""

# check name for y
Expand Down Expand Up @@ -1223,6 +1257,19 @@ def _check_plot_inputs(self, yname, xname, title, cmap, path, figsize, s,
raise ValueError("path must be a string"
", got %s" % type(path))

# check fname for plot
if fname is None:
# set default as None
self.fname = None
# if supplied check if its valid
elif isinstance(fname, str):
# set value to user supplied
self.fname = fname
else:
# raise value error
raise ValueError("fname must be a string"
", got %s" % type(fname))

# check path for the plot
if figsize is None:
# set default as 10,5
Expand Down

0 comments on commit 154ce32

Please sign in to comment.