This is an exploration of the ability to get element-wise averages for Pandas DataFrames.
In our case, we know the row/column combinations do not match across matrices we want to average.
This pd.Panel looked promising: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Panel.html when I saw it here: http://stackoverflow.com/questions/29438585/pandas-elsement-wise-average-and-stdandard-deviation-across-multiple-dataframes
In [1]:
import pandas as pd
For these dataframes to make sense, you may consult this photo:
In [2]:
df1 = pd.DataFrame({'dog':[0,1,1], 'cat':[0,0,0], 'rat':[3,4,5]})
df1.index = df1.columns
df1
Out[2]:
In [3]:
df2 = pd.DataFrame({'dog':[0, 0, 0], 'cat':[0, 1, 1], 'rat':[3,4,5]})
df2.index = df2.columns
df2
Out[3]:
In [4]:
df3 = pd.DataFrame({'dog':[1, 1, 1], 'cat':[0, -1, -1], 'rat':[0,0,0]})
df3.index = df3.columns
df3
Out[4]:
What does this do? {n: df for n, df in enumerate([df1, df2, df3])}
In [5]:
{n: df for n, df in enumerate([df1, df2, df3])}
Out[5]:
It just makes a dictionary with keys 0, 1, 2, and dataframes df1, df2, df3.
In [6]:
p = pd.Panel(data={n: df for n, df in enumerate([df1, df2, df3])})
In [7]:
p
Out[7]:
In [8]:
p.mean(axis=0)
Out[8]:
In [9]:
p.std(axis=0)
Out[9]:
In [10]:
df4 = df1.copy()
In [11]:
df4 = pd.DataFrame({'dog':[0,1,1,9], 'cat':[0,0,0,9], 'rat':[3,4,5,9], 'zebra':[9,9,9,9]})
df4.index = df4.columns
df4
Out[11]:
In [12]:
p2 = pd.Panel(data={n: df for n, df in enumerate([df4, df2, df3])})
In [13]:
p2.mean(axis=0)
Out[13]:
In [14]:
p2.std(axis=0)
Out[14]:
Hmm.... it looks like it drops species that aren't common across. Are we cool with it?
In [15]:
import network_construction as net
aggregate_adjacency_matrices()
works:
In [16]:
net.aggregate_adjacency_matrices([df1, df2, df3, df4])
Out[16]:
Make this toy data have the same tuple-keys format to test summarize_replicate_adjacency_matrices()
In [17]:
mock_data = {('High', 1): df1, ('High', 2): df2, ('High', 3):df3, ('High', 4):df4,
('Low', 1): df1, ('Low', 2): df2, ('Low', 3):df3, ('Low', 4):df4}
In [18]:
net.summarize_replicate_adjacency_matrices(mock_data)
Out[18]:
In [ ]: