-
Notifications
You must be signed in to change notification settings - Fork 2
/
clustering_points_v1.py
executable file
·134 lines (106 loc) · 4.54 KB
/
clustering_points_v1.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#/usr/bin/env python3
"""
Author : Travis Simmons, Emmanuel Gonzalez
Date : 2020-10-30
Purpose: Plant clustering for a full growing season using agglomerative clustering
"""
import argparse
import os
import sys
import numpy as np
import pandas as pd
import sklearn
import glob
from sklearn.cluster import AgglomerativeClustering
# --------------------------------------------------
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='Plant clustering',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Working 1/24/2021
# parser.add_argument('csv_list',
# nargs='+',
# metavar='csv_list',
# help='Directory containing CSV files to match')
# Added 1/24/2021
parser.add_argument('csv_list',
metavar='csv_list',
type = str,
help='Directory containing CSV files to match')
parser.add_argument('-o',
'--outdir',
help='Output directory',
metavar='outdir',
type=str,
default='pointmatching_out')
parser.add_argument('-f',
'--filename',
help='Output filename',
metavar='filename',
type=str,
default='agglomerative_plant_clustering')
return parser.parse_args()
# --------------------------------------------------
def main():
"""Cluster points"""
args = get_args()
df_list = []
if not os.path.isdir(args.outdir):
os.makedirs(args.outdir)
# Working 1/24/2021
# for csv in args.csv_list:
# df = pd.read_csv(csv, engine='python')
# df_list.append(df)
# Added 1/24/2021
identifications = glob.glob(os.path.join(args.csv_list,'*.csv'))
for csv in identifications:
df = pd.read_csv(csv, engine='python')
df_list.append(df)
# ----------------------------------------------------------------
whole = pd.concat(df_list)
# Creates a list of all unique genotypes in day 2 that we can itterate over.
geno_list = whole.genotype.unique().tolist()
# Green towers border is our buffer group and will not be included in analysis
if 'Green_Towers_BORDER' in geno_list:
geno_list.remove('Green_Towers_BORDER')
# Run clustering algorithm and add matching column: plant_name
model = sklearn.cluster.AgglomerativeClustering(n_clusters=None, affinity='euclidean', memory=None, connectivity=None, compute_full_tree='auto', linkage='average', distance_threshold= .0000006)
matched_df = pd.DataFrame(columns=['date',
'treatment',
'plot',
'genotype',
'lon',
'lat',
'min_x',
'max_x',
'min_y',
'max_y',
'nw_lat',
'nw_lon',
'se_lat',
'se_lon',
'bounding_area_m2'])
# Doing the prediction by genotype so it doesn't get overwhelmed
for geno in geno_list:
sub_df = whole.set_index('genotype').loc[geno]
# An agglomerative clustering model is fitted for each genotype
try:
cords = list(zip(sub_df['lon'], sub_df['lat']))
clustering = model.fit_predict(cords)
geno_clustered = sub_df.assign(plant_name = clustering)
matched_df = pd.concat([matched_df,geno_clustered])
except:
pass
# Assigning the match names to the plants and exporting
matched_df = matched_df.reset_index()
matched_df['genotype'] = matched_df['index']
del matched_df['index']
names = list(zip(matched_df['genotype'], matched_df['plant_name']))
names_format = [i[0] + '_' + str(int(i[1])) for i in names]
matched_df = matched_df.assign(plant_name = names_format)
out_path = os.path.join(args.outdir, args.filename + '.csv')
matched_df.to_csv(out_path)
# --------------------------------------------------
if __name__ == '__main__':
main()