def wt_plot(self, pdf):
"""
Create a plot of the linear fit of the wild type variant.
*pdf* is an open PdfPages instance.
Only created for selections that use WLS or OLS scoring and have a wild type specified.
Uses :py:func:`~plots.fit_axes` for the plotting.
"""
logging.info("Creating wild type fit plot", extra={'oname' : self.name})
# get the data and calculate log ratios
if "variants" in self.labels:
wt_label = "variants"
elif "identifiers" in self.labels:
wt_label = "identifiers"
data = self.store.select("/main/{}/counts".format(wt_label), where='index = "{}"'.format(WILD_TYPE_VARIANT)).ix[0]
sums = self.store['/main/{}/counts'.format(wt_label)].sum(axis="index") # sum of complete cases (N')
yvalues = np.log(data + 0.5) - np.log(sums + 0.5)
xvalues = [tp / float(max(self.timepoints)) for tp in self.timepoints]
# fit the line
X = sm.add_constant(xvalues) # fit intercept
if self.scoring_method == "WLS":
W = 1 / (1 / (data + 0.5) + 1 / (sums + 0.5))
fit = sm.WLS(yvalues, X, weights=W).fit()
elif self.scoring_method == "OLS":
fit = sm.OLS(yvalues, X).fit()
else:
raise ValueError('Invalid regression scoring method "{}" [{}]'.format(self.scoring_method, self.name))
intercept, slope = fit.params
slope_se = fit.bse['x1']
# make the plot
fig, ax = plt.subplots()
fig.set_tight_layout(True)
fit_axes(ax, xvalues, yvalues, slope, intercept, xlabels=self.timepoints)
fit_axes_text(ax, cornertext="Slope {:3.2f}\nSE {:.1f}".format(slope, slope_se))
ax.set_title("Wild Type Shape\n{}".format(self.name))
ax.set_ylabel("Log Ratio (Complete Cases)")
pdf.savefig(fig)
plt.close(fig)
评论列表
文章目录