-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_permanova.py
More file actions
executable file
·63 lines (47 loc) · 1.62 KB
/
stats_permanova.py
File metadata and controls
executable file
·63 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 24 12:50:40 2020
INFORMATION
===========
This script performs a PERMANOVA test between all UMAP coordinates between
national and international visitors. First, it calculates a distance matrix
between the coordinate points on which it performs the PERMANOVA with 999
permutations.
USAGE
=====
Run this script by typing:
python stats_permanova.py -i input.pkl -o output.csv
@author: waeiski
"""
import pandas as pd
from scipy.spatial import distance
import skbio as sk
import argparse
# define arguments
ap = argparse.ArgumentParser()
ap.add_argument('-i','--input',required=True,
help='path to input pickle')
ap.add_argument('-o','--output',required=True,
help='path to output csv file')
args = vars(ap.parse_args())
# get input and output files
infile = args['input']
outfile = args['output']
# read pickle in
print("[INFO] - Reading data in...")
df = pd.read_pickle('scripts/natparks/top20parks_resnext101_datetimes.pkl')
# get coordinates
df_array = df[["umap-2d-one", "umap-2d-two"]].to_numpy()
# calculate distance matrix
print("[INFO] - Calculating distance matrix...")
distmat = distance.cdist(df_array, df_array)
# convert to scikit-bio distance matrix
dm = sk.stats.distance.DistanceMatrix(distmat, df['pid'].values.tolist())
# get permanova distance (999 permutations as default)
print("[INFO] - Running PERMANOVA...")
perm = sk.stats.distance.permanova(distance_matrix=dm, grouping=df['locstr'])
# save output
print("[INFO] - Saving results to csv...")
perm.to_csv(outfile, sep=',', encoding='utf-8')
print("[INFO] - ... done!")