This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
97 lines (77 loc) · 2.68 KB
/
utils.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
from collections import Counter
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from pointpats import PoissonPointProcess, PoissonClusterPointProcess, Window
np.random.seed(0)
# the points generated function, credit for pointpats
def runif_in_circle(n, radius=1.0, center=(0., 0.), burn=2):
good = np.zeros((n, 2), float)
c = 0
r = radius
r2 = r * r
it = 0
while c < n:
x = np.random.uniform(-r, r, (burn * n, 1))
y = np.random.uniform(-r, r, (burn * n, 1))
ids = np.where(x * x + y * y <= r2)
candidates = np.hstack((x, y))[ids[0]]
nc = candidates.shape[0]
need = n - c
if nc > need: # more than we need
good[c:] = candidates[:need]
else: # use them all and keep going
good[c:c + nc] = candidates
c += nc
it += 1
return good + np.asarray(center)
def plot_points(points, types, ax=None, title=None):
data = pd.DataFrame(points, columns=['x', 'y'])
data['types'] = types
p = sns.scatterplot(data=data, x='x', y='y', hue='types', ax=ax)
p.legend(loc='upper right', ncol=1)
if ax is None:
plt.title(title)
else:
ax.title.set_text(title)
return p
def random_data(window, n):
window = Window(window)
rpp = PoissonPointProcess(window, 200, 1, conditioning=False, asPP=False)
return rpp.realizations[0]
def cluster_data(window, n, parents, d, types):
window = Window(window)
l, b, r, t = window.bbox
children = int(n / parents)
# get parent points
pxs = np.random.uniform(l, r, (int(n / children), 1))
pys = np.random.uniform(b, t, (int(n / children), 1))
cents = np.hstack((pxs, pys))
pnts = {tuple(center): runif_in_circle(children, d, center) for center in cents}
types_counts = Counter(types)
utypes = types_counts.keys()
type_mapper = {}
for k, v in pnts.items():
points_count = len(v)
new_arr = []
for i in utypes:
if len(new_arr) != points_count:
c = types_counts[i]
if c != 0:
current_len = points_count - len(new_arr)
if c >= current_len:
new_arr += [i for _ in range(current_len)]
types_counts[i] -= current_len
else:
new_arr += [i for _ in range(c)]
types_counts[i] = 0
else:
break
type_mapper[k] = new_arr
points = []
ordered_types = []
for k, v in pnts.items():
points += list(v)
ordered_types += type_mapper[k]
return points, ordered_types