Home

Awesome

HierarPy [in progress]

Tools for calculating dominance hiearchies in Python. Working on including:

Usage:

First, import packages:

import pandas as pd
import hierarpy as hp

Load an example dataframe (provided in hierarpy/test_dfs)

df = pd.read_csv('test_dfs/df1.csv')

This dataframe looks like the following:

datetimewinnerlosersex_winnersex_loser
02016-09-08 12:19:41AGmm
12016-09-08 12:24:35ACmm
22016-09-08 14:43:32BCmm
32016-09-08 15:26:44CBmm
42016-09-08 17:08:47CRmm

Processing interactions into a matrix:

We can tabulate this dataframe using hierarpy's matrix_from_dataframe function:

mat = hp.matrix_from_dataframe(df, Winner = 'winner', Loser = 'loser')

Resulting in the following matrix of interactions:

winnerABCDEFGHIJKLMNOPQRSTUVWXYZ
A021111060001200521001000000
B00201100001100100000001000
C23010030011011020113301110
D00100000000000000110000000
E00200000102000010100000101
F00010000000001000000100000
G01310200000001300020111061
H10000000000001000000000000
I11000000000003000021000010
J00200100001006000000000010
K10000000000000310110000110
L00200020000003500100000000
M00000000001001000200000000
N74520350313211630111031002
O00220000101002010203000010
P00000010000101200100000000
Q00100100000001100000000000
R00100000000000000000000000
S00000000000000000000000000
T00100000000000100000000020
U00100000000000000300000010
V00000000000000100000000000
W00000000000000000001000020
X00000000000000000000100000
Y00000000000000000000000000
Z00000000000000000000000000

Getting David's ranks and scores

We can get David's ranks and scores from such a matrix using the function david_ranks:

david = hp.david_ranks(mat)

And david will now be a dataframe with scores and ranks for each individual:

indD_scoreDavids_rank
1N67.51
2A53.24542
3Q35.39293
4J29.95154
5H28.30575
6E25.16676
7L24.02627
8G16.05958
9I15.7899
10B13.209510
11M3.9633711
12C0.34102612
13V-4.7904813
14P-5.5071414
15O-7.4404815
16F-8.716
17K-12.266717
18Z-15.373818
19U-16.829519
20X-17.246220
21W-24.2221
22D-28.440522
23T-28.607123
24S-39.8724
25R-50.186625
26Y-53.472326

Getting ADAGIO graph and ranks

We can get the ADAGIO graph's nodes and edges from a matrix using the function run_ADAGIO. This function can take the arguments preprocess_data (Boolean, see paper for details), and plot (Also Boolean, whether or not to plot the resulting graph).

nodes, edges = hp.run_ADAGIO(mat, preprocess_data = False ,plot=True)

This results in the following plot:

ADAGIO graph

From these nodes and edges, we can convert to rankings using the function rank_from_graph. We can use the argument method to choose "bottom-up" or "top-down" rankings (see paper for details):

indadagio_rank
12M1
23X1
22W1
4E1
16Q1
7H1
8I1
9J1
11L1
13N2
21V3
15P3
0A3
10K3
6G4
2C4
14O5
5F5
20U5
1B5
25Z5
17R6
19T6
3D6
18S7
24Y7

More to come :)