aboutsummaryrefslogtreecommitdiff
path: root/data.py
blob: 8dd9319fc5b9f5ad2b36785b037ebbc49ccfcb28 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Data

from scipy.spatial import Voronoi, voronoi_plot_2d, KDTree, distance
import matplotlib.pyplot as plt

class Pit:
  def __init__(self, loc, depth, diam):
    self.loc = loc
    self.depth = depth
    self.diam = diam

  def __repr__(self):
    return '%s @ %.1fcm deep, %.1fcm wide' % (str(self.loc), self.depth, self.diam)

  def disp(self):
    return '%.1fcm dp\n %.1fcm wd' % (self.depth, self.diam)

  def __getitem__(self,ind):
    return self.loc[ind]

class Date:
  def __init__(self, year, month, day):
    self.year = year
    self.month = month
    self.day = day
    
  def __repr__(self):
    return '%d-%d-%d' % (self.year, self.month, self.day)

class Trial:
  def __init__(self, date, intro, dead, size, pits):
    self.date = date
    self.intro = intro
    self.dead = dead
    self.size = size
    self.pits = pits
    self.pitlocs = [pit.loc for pit in self.pits]

  def __repr__(self):
    return str(self.date) + ' ' + str(self.pits)

  def plot(self, save=False):
    vor = Voronoi([pit.loc for pit in self.pits])
    voronoi_plot_2d(vor)
    for pit in self.pits:
      plt.text(pit[0], pit[1], pit.disp(), ha='center', va='bottom', size='xx-small')
    plt.xlabel('%s (dimension %dx%dcm)' % (str(self.date), self.size[0], self.size[1]))
    if save:
      plt.savefig(str(self.date)+'.png')
    else:
      plt.show()

  def nearest_neighbor(self):
    tree = KDTree(self.pitlocs)
    sumnn = 0
    return [dists[1] for dists in tree.query(self.pitlocs,2)[0]]

trials = [
  Trial(Date(2019, 10, 16), 31, 6, [33,32], [
    Pit([4,25],1.3,4.2),
    Pit([3,13],1.4,3.7),
    Pit([10,25],1.1,3.0),
    Pit([18,18],1.8,2.3),
    Pit([30,14],2.2,3.1),
    Pit([29,11],1.4,2.5),
    Pit([27,10],1.2,2.1),
    Pit([29,6],2.4,3.9),
    Pit([26,5],1.8,3.6),
  ]),
  Trial(Date(2019, 10, 30), 27, 3, [24,24], [
    Pit([4,21],2.0,7.0),
    Pit([7,22],2.5,4.1),
    Pit([2,9],0.5,2.0),
    Pit([12,18],1.2,2.5),
    Pit([12,11],1.2,3.0),
    Pit([20,11],1.0,3.0),
    Pit([19,2],1.5,4.0),
  ]),
  Trial(Date(2019, 12, 3), 19, 3, [17, 16], [
    Pit([14,5],1.3,4.1),
    Pit([12,2],1.2,3.8),
    Pit([5,1],0.9,3.2),
    Pit([15,17],2.2,3.8),
    Pit([12,17],1.2,2.5),
    Pit([7,17],2.0,5.0),
    Pit([1,17],1.8,3.6),
  ]),
  Trial(Date(2019, 12, 5), 10, 0, [17, 16], [
    Pit([17,4],1.3,3.1),
    Pit([10,4],1.5,3.1),
    Pit([18,9],1.4,2.9),
  ]),
  Trial(Date(2019, 12, 19), 12, 4, [8,7], [
    Pit([4,7],.8,.9),
    Pit([3,5],.9,.8),
    Pit([8,2],2,3),
  ]),
  Trial(Date(2019, 12, 20), 5, 0, [8,7], [
    Pit([6,7],.8,.8),
    Pit([2,2],.8,.8),
    Pit([8,6],.8,.8),
    Pit([2,9],.8,.8),
  ]),
]

import sys
import math
import numpy as np
from scipy.stats import pearsonr

if len(sys.argv) < 2:
  print('You must provide at least one argument to choose the function of this program: `img` or `nei` or `depwid')
  quit()

arg = sys.argv[1]

if arg == 'img':
  for trial in trials:
    trial.plot(save=True)
elif arg == 'nei':
  x = []
  y = []
  for trial in trials:
    size = math.sqrt(trial.size[0]*trial.size[1])
    nei = trial.nearest_neighbor()
    #for n in range(len(nei)):
    #  nei[n] += n*.05
    #x += [math.sqrt(trial.size[0]*trial.size[1])]*len(nei)
    #y += nei
    x.append(size)
    y.append(sum(nei)/len(nei))
  print('p-value', pearsonr(x,y))
  plt.xlabel('Square root of Trial Area (cm)')
  plt.ylabel('Nearest Neighbor for Individual Pits (cm)')
  plt.plot(x, y, 'bo')
  plt.plot(x, np.poly1d(np.polyfit(x, y, 1))(x))
  plt.show()
elif arg == 'depwid':
  for trial in trials:
    size = math.sqrt(trial.size[0]*trial.size[1])
    for pit in trial.pits:
      plt.plot(size, pit.depth, 'bo')
      plt.plot(size, pit.diam, 'ro')
  plt.xlabel('Square root of Trial Area (cm)')
  plt.ylabel('Depth/Width of Antlion Pits (cm)')
  plt.show()
if arg == 'table':
  print('Dimensions (in)\tPit Depth (cm)\tPit Width (cm)\tNearest Neighbor (cm)')
  for trial in trials:
    size = trial.size
    nei = trial.nearest_neighbor()
    for pitind in range(len(trial.pits)):
      pit = trial.pits[pitind]
      print('\t'.join(['x'.join([str(el) for el in size]), str(pit.depth), str(pit.diam), str(nei[pitind])]))