Example #1
1
class FractionTaxaBarStack(Graph):
    """Comparing all fractions across all pools in a barstack"""

    short_name = "fraction_taxa_barstack"

    def plot(self):
        self.frame = OrderedDict(
            (
                ("%s - %s" % (p, f), getattr(p.fractions, f).rdp.phyla)
                for f in ("low", "med", "big")
                for p in self.parent.pools
            )
        )
        self.frame = pandas.DataFrame(self.frame)
        self.frame = self.frame.fillna(0)
        self.frame = self.frame.transpose()
        self.frame = self.frame.apply(lambda x: 100 * x / x.sum(), axis=1)
        # Sort the table by sum #
        sums = self.frame.sum()
        sums.sort(ascending=False)
        self.frame = self.frame.reindex_axis(sums.keys(), axis=1)
        # Plot #
        fig = pyplot.figure()
        axes = self.frame.plot(kind="bar", stacked=True, color=cool_colors)
        fig = pyplot.gcf()
        # Other #
        axes.set_title("Species relative abundances per fraction per pool")
        axes.set_ylabel("Relative abundances in percent")
        axes.xaxis.grid(False)
        axes.yaxis.grid(False)
        axes.set_ylim([0, 100])
        # Put a legend below current axis
        axes.legend(loc="upper center", bbox_to_anchor=(0.5, -0.20), fancybox=True, shadow=True, ncol=5)
        # Save it #
        self.save_plot(fig, axes, width=24.0, height=14.0, bottom=0.30, top=0.97, left=0.04, right=0.98)
        self.frame.to_csv(self.csv_path)
        pyplot.close(fig)
Example #2
1
class FractionTaxaBarStack(Graph):
    """This is figure 3 of the paper"""

    short_name = "fraction_taxa_barstack"
    bottom = 0.4
    top = 0.95
    left = 0.1
    right = 0.95
    formats = ("pdf", "eps")

    def plot(self):
        # Make Frame #
        self.frame = OrderedDict(
            (
                ("%s - %s" % (p, f), getattr(p.fractions, f).rdp.phyla)
                for f in ("low", "med", "big")
                for p in self.parent.pools
            )
        )
        self.frame = pandas.DataFrame(self.frame)
        self.frame = self.frame.fillna(0)
        # Rename #
        new_names = {
            u"run001-pool01 - low": "2-step PCR low",
            u"run001-pool02 - low": "2-step PCR low",
            u"run001-pool03 - low": "2-step PCR low",
            u"run001-pool04 - low": "1-step PCR low",
            u"run002-pool01 - low": "New chem low",
            u"run001-pool01 - med": "2-step PCR med",
            u"run001-pool02 - med": "2-step PCR med",
            u"run001-pool03 - med": "2-step PCR med",
            u"run001-pool04 - med": "1-step PCR med",
            u"run002-pool01 - med": "New chem med",
            u"run001-pool01 - big": "2-step PCR high",
            u"run001-pool02 - big": "2-step PCR high",
            u"run001-pool03 - big": "2-step PCR high",
            u"run001-pool04 - big": "1-step PCR high",
            u"run002-pool01 - big": "New chem high",
        }
        self.frame.rename(columns=new_names, inplace=True)
        self.frame = self.frame.transpose()
        # Group low abundant into 'others' #
        low_abundance = self.frame.sum() < 30000
        other_count = self.frame.loc[:, low_abundance].sum(axis=1)
        self.frame = self.frame.loc[:, ~low_abundance]
        self.frame["Others"] = other_count
        # Normalize #
        self.frame = self.frame.apply(lambda x: 100 * x / x.sum(), axis=1)
        # Sort the table by sum #
        sums = self.frame.sum()
        sums.sort(ascending=False)
        self.frame = self.frame.reindex_axis(sums.keys(), axis=1)
        # Plot #
        fig = pyplot.figure()
        axes = self.frame.plot(kind="bar", stacked=True, color=cool_colors)
        fig = pyplot.gcf()
        # Other #
        axes.set_ylabel("Relative abundances in percent")
        axes.xaxis.grid(False)
        axes.yaxis.grid(False)
        axes.set_ylim([0, 100])
        # Put a legend below current axis
        axes.legend(
            loc="upper center", bbox_to_anchor=(0.5, -0.40), fancybox=True, shadow=True, ncol=5, prop={"size": 10}
        )
        # Font size #
        axes.tick_params(axis="x", which="major", labelsize=11)
        # Save it #
        self.save_plot(fig, axes)
        self.frame.to_csv(self.csv_path)
        pyplot.close(fig)
Example #3
1
    74.08,
    50.90,
    43.47,
    125.67,
    114.82,
]

for n in nd:
    fr.append(n)
fsm["francia"] = fr
fsm["svizzera"] = sv
fsm["pun"] = pun[:287]

fsm = pd.DataFrame.from_dict(fsm)

fsm.plot()  ### from this I'm very doubtful that flows actually correlate wth prices...
### except in the last week where something anomalous is definitely happening

############## analysis of correlations ###############
fsm = fsm.set_index(pun.index)
cors = []
for i in range(2, pun.shape[0], 1):
    cors.append(np.corrcoef(np.array(pun)[:i], np.array(fsm).ravel()[:i])[1, 0])

compl_cors = []
for i in range(2, pun.shape[0], 1):
    compl_cors.append(np.corrcoef(np.array(pun)[pun.shape[0] - i :], np.array(fsm).ravel()[pun.shape[0] - i :])[1, 0])

plt.figure()
plt.plot(np.array(cors))
plt.figure()
Example #4
1
zones.plot()
zones[zones.columns[[0, 4, 6]]].plot()
zones[zones.columns[[0, 4]]].plot(title="PUN vs FRAN")
zones[zones.columns[[0, 6]]].plot(title="PUN vs NORD")
zones[zones.columns[[6, 4]]].plot(title="NORD vs FRAN")

zones[zones.columns[0]].corr(zones[zones.columns[4]])
zones[zones.columns[0]].corr(zones[zones.columns[6]])
zones[zones.columns[4]].corr(zones[zones.columns[6]])

#### FRAN normalized:
nor_fran = (zones[zones.columns[4]] - zones[zones.columns[4]].mean()) / zones[zones.columns[4]].std()

plt.figure()
plt.plot(nor_fran)

zones[zones.columns[0]].corr(zones[zones.columns[4]])
zones[zones.columns[0]].corr(zones[zones.columns[6]])
zones[zones.columns[4]].corr(zones[zones.columns[6]])
zones[zones.columns[0]].corr(zones[zones.columns[11]])
zones[zones.columns[11]].corr(zones[zones.columns[6]])
zones[zones.columns[4]].corr(zones[zones.columns[11]])

nor_sviz = (zones[zones.columns[11]] - zones[zones.columns[11]].mean()) / zones[zones.columns[11]].std()
nor_nord = (zones[zones.columns[6]] - zones[zones.columns[6]].mean()) / zones[zones.columns[6]].std()
nor_pun = (zones[zones.columns[0]] - zones[zones.columns[0]].mean()) / zones[zones.columns[0]].std()

plt.figure()
nor_fran.ix[nor_fran.index.month == 9].plot()
plt.figure()
nor_sviz.ix[nor_sviz.index.month == 9].plot()
plt.figure()
nor_pun.ix[nor_pun.index.month == 9].plot()
plt.figure()
nor_nord.ix[nor_nord.index.month == 9].plot()

sep = OrderedDict()
sep["fran"] = nor_fran.ix[nor_fran.index.month == 9]
sep["sviz"] = nor_sviz.ix[nor_sviz.index.month == 9]
sep["pun"] = nor_pun.ix[nor_pun.index.month == 9]
sep["nord"] = nor_nord.ix[nor_nord.index.month == 9]

Sep = pd.DataFrame.from_dict(sep)

nnsep = OrderedDict()
nnsep["fran"] = zones[zones.columns[4]].ix[zones.index.month == 9]
nnsep["sviz"] = zones[zones.columns[11]].ix[zones.index.month == 9]
nnsep["pun"] = zones[zones.columns[0]].ix[zones.index.month == 9]
nnsep["nord"] = zones[zones.columns[6]].ix[zones.index.month == 9]

NSep = pd.DataFrame.from_dict(nnsep)

#######################################################################
days = OrderedDict()
days_of_week = ["Lun", "Mar", "Mer", "Gio", "Ven", "Sab", "Dom"]
cols = [12, 21, 24, 31]
nms = ["pun", "fran", "nord", "sviz"]
for i in cols:
    dm = []
    for d in days_of_week:
        dm.append(data[data.columns[i]].ix[data["Week Day"] == d].mean())
    days[nms[cols.index(i)]] = dm

days = pd.DataFrame.from_dict(days).set_index([days_of_week])
days.plot()