-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauxiliary.py
147 lines (118 loc) · 3.97 KB
/
auxiliary.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
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
"""
@author: Raymond F. Pauszek III, Ph.D. (2020)
smtirf >> auxiliary
"""
import numpy as np
import json
from datetime import datetime
from collections import OrderedDict
from . import SMJsonEncoder
class SMTraceID():
"""
lightweight class implementing unique Trace identifier
coded by hex representation of recording time
and index of spot within movie
"""
def __init__(self, s):
self.string = s
@staticmethod
def time2hex(dt):
return hex(int(dt.timestamp()))[2:] # trim 0x
@classmethod
def from_datetime(cls, dt, trcIndex=None):
""" instantiate from datetime object """
hxdt = SMTraceID.time2hex(dt)
try:
return cls(f"{hxdt}:{trcIndex:04d}")
except (TypeError, ValueError):
return cls(f"{hxdt}:XXXX")
def new_trace(self, trcIndex):
return self.__class__(f"{self.movID}:{trcIndex:04d}")
def __str__(self):
return self.string
@property
def movID(self):
mov, _ = self.string.split(":")
return mov
@property
def trcIndex(self):
_, trc = self.string.split(":")
return trc
@property
def isMovieID(self):
return self.trcIndex == "XXXX"
def strftime(self, fmt="%Y-%m-%d %H:%M:%S"):
dt = datetime.fromtimestamp(int(self.movID, base=16))
return dt.strftime(fmt)
class SMMovie():
def __init__(self, movID, img, info):
self._id = movID
if img.ndim != 2:
raise ValueError("image must be flat (not RGB channels)")
self.img = img
self.info = info
def __str__(self):
s = f"{self.__class__.__name__}\t{self._id}\n"
for key, item in self.info.items():
s += f"-> {key}\n\t{item}\n"
s += "\n"
return s
class SMMovieList(OrderedDict):
def __init__(self):
super().__init__(self)
@classmethod
def load(cls, images, movInfo):
movies = cls()
for item in movInfo:
movies.append(item["id"], images[:,:,item["position"]], item["contents"])
return movies
def append(self, key, img, info):
self[key] = SMMovie(key, img, info)
def add_movie(self, key, mov):
self[key] = mov
def __getitem__(self, key):
try:
return super().__getitem__(key)
except KeyError: # lookup by index
keys = [k for k in self.keys()]
return self[keys[key]]
def _as_image_stack(self):
""" return stack of images (ndarray) """
images = [mov.img for j, (key, mov) in enumerate(self.items())]
return np.stack(images, axis=2)
def _as_json(self):
""" json-serialized list of info dicts """
return json.dumps([{"id": str(mov._id), "position": j, "contents": mov.info}
for j, (key, mov) in enumerate(self.items())], cls=SMJsonEncoder)
class SMSpotCoordinate():
def __init__(self, coords):
self._coords = coords
def _as_dict(self):
return self._coords
# ==============================================================================
# functions
# ==============================================================================
def where(condition):
"""
Finds contiguous True regions of the boolean array "condition".
Returns a 2D array where
column 1 is the start index of the region
column 2 is the end index.
goo.gl/eExJV3
"""
# Find the indices of changes in "condition"
d = np.diff(condition)
idx, = d.nonzero()
# We need to start things after the change in "condition". Therefore,
# we'll shift the index by 1 to the right.
idx += 1
if condition[0]:
# If the start of condition is True prepend a 0
idx = np.r_[0, idx]
if condition[-1]:
# If the end of condition is True, append the length of the array
idx = np.r_[idx, condition.size]
# Reshape the result into two columns
idx.shape = (-1,2)
return idx