-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
108 lines (80 loc) · 2.52 KB
/
Copy pathutils.py
File metadata and controls
108 lines (80 loc) · 2.52 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
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
from config import read_config
import pytz
import datetime
import numpy as np
import pandas as pd
def set_folder_structure():
config = read_config()
pathway = config['DATA_PATHWAY']
if pathway == 'legacy':
folder_structure = '*\\*\\*\\Miniscope\\'
elif pathway == 'bonsai':
folder_structure = '*'
else:
raise ValueError('Wrong pathway!')
return folder_structure
def get_datetime():
tz = pytz.timezone('Europe/Moscow')
now = datetime.datetime.now(tz)
dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
return dt_string
def _plain_bfs(adj, source):
'''
adapted from networkx.algorithms.components.connected._plain_bfs
Args:
adj:
source:
Returns:
'''
n = adj.shape[0]
seen = {source}
nextlevel = [source]
while nextlevel:
thislevel = nextlevel
nextlevel = []
for v in thislevel:
for w in get_neighbors_from_adj(adj, v):
if w not in seen:
seen.add(w)
nextlevel.append(w)
if len(seen) == n:
return seen
return seen
def get_neighbors_from_adj(a, node):
inds = a[[node], :].nonzero()[1]
return inds
def get_ccs_from_adj(adj):
seen = set()
for v in range(adj.shape[0]):
if v not in seen:
c = _plain_bfs(adj, v)
seen.update(c)
yield c
def calculate_polygon_area(coordinates):
# Filter out points with NaN coordinates
valid_coordinates = [point for point in coordinates if not (np.isnan(point[0]) or np.isnan(point[1]))]
# Check if we have enough valid points to form a polygon
if len(valid_coordinates) < 3:
return 0 # Not enough points to form a polygon
area = 0
# Number of vertices
n = len(valid_coordinates)
# Calculate area using the Shoelace formula
for i in range(n):
j = (i + 1) % n
area += valid_coordinates[i][0] * valid_coordinates[j][1]
area -= valid_coordinates[j][0] * valid_coordinates[i][1]
# Take absolute value and divide by 2
area = abs(area) / 2
return area
def calculate_perimeter(contour):
dots_dist = []
contour_mask = np.array(pd.Series(contour[:, 0] * contour[:, 1]).notna())
contour = contour[contour_mask]
dot_num = len(contour) - 1
while dot_num >= 0:
dist = np.linalg.norm(contour[dot_num] - contour[dot_num-1])
dots_dist.append(dist)
dot_num -= 1
perimeter = np.sum(dots_dist)
return perimeter, dots_dist