Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
MetaPhys/utils.py at master · xliucs/MetaPhys · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
xliucs
MetaPhys
Repository navigation
Code
Issues
5
(5)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
MetaPhys
/
utils.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
119 lines (96 loc) · 3.78 KB
master
Breadcrumbs
MetaPhys
/
utils.py
Copy path
Top
File metadata and controls
Code
Blame
119 lines (96 loc) · 3.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from
collections
import
OrderedDict
import
h5py
import
numpy
as
np
import
torch
from
scipy
.
sparse
import
spdiags
def
compute_accuracy
(
logits
,
targets
):
"""Compute the accuracy"""
with
torch
.
no_grad
():
_
,
predictions
=
torch
.
max
(
logits
,
dim
=
1
)
accuracy
=
torch
.
mean
(
predictions
.
eq
(
targets
).
float
())
return
accuracy
.
item
()
def
tensors_to_device
(
tensors
,
device
=
torch
.
device
(
'cpu'
)):
"""Place a collection of tensors in a specific device"""
if
isinstance
(
tensors
,
torch
.
Tensor
):
return
tensors
.
to
(
device
=
device
)
elif
isinstance
(
tensors
, (
list
,
tuple
)):
return
type
(
tensors
)(
tensors_to_device
(
tensor
,
device
=
device
)
for
tensor
in
tensors
)
elif
isinstance
(
tensors
, (
dict
,
OrderedDict
)):
return
type
(
tensors
)([(
name
,
tensors_to_device
(
tensor
,
device
=
device
))
for
(
name
,
tensor
)
in
tensors
.
items
()])
else
:
raise
NotImplementedError
()
class
ToTensor1D
(
object
):
"""Convert a `numpy.ndarray` to tensor. Unlike `ToTensor` from torchvision,
this converts numpy arrays regardless of the number of dimensions.
Converts automatically the array to `float32`.
"""
def
__call__
(
self
,
array
):
return
torch
.
from_numpy
(
array
.
astype
(
'float32'
))
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
'()'
def
read_txt
(
filelists
,
data_dir
):
all_path
=
[]
for
file_path
in
filelists
:
with
open
(
file_path
,
'r'
)
as
f
:
test_paths
=
f
.
read
().
split
(
'
\n
'
)
test_paths
=
[
data_dir
+
'/'
+
path
for
path
in
test_paths
]
all_path
.
append
(
test_paths
)
return
all_path
def
_read_list
(
file_path
,
data_dir
):
with
open
(
file_path
,
'r'
)
as
f
:
test_paths
=
f
.
read
().
split
(
'
\n
'
)
test_paths
=
[
data_dir
+
path
for
path
in
test_paths
]
return
test_paths
def
read_from_single_txt
(
train_path
,
data_dir
):
all_train_paths
=
_read_list
(
train_path
,
data_dir
)
return
all_train_paths
def
read_from_txt
(
train_path
,
test_path
,
data_dir
):
all_train_paths
=
_read_list
(
train_path
,
data_dir
)
all_test_paths
=
_read_list
(
test_path
,
data_dir
)
return
all_train_paths
,
all_test_paths
def
get_nframe_video
(
path
,
dataset
):
temp_f1
=
h5py
.
File
(
path
,
'r'
)
# This might be changed
temp_dysub
=
np
.
array
(
temp_f1
[
"dysub"
])
nframe_per_video
=
temp_dysub
.
shape
[
0
]
# It was 36000, but in this dataset it should be 8999
return
nframe_per_video
def
detrend
(
signal
,
Lambda
):
"""detrend(signal, Lambda) -> filtered_signal
This function applies a detrending filter.
This code is based on the following article "An advanced detrending method with application
to HRV analysis". Tarvainen et al., IEEE Trans on Biomedical Engineering, 2002.
*Parameters*
``signal`` (1d numpy array):
The signal where you want to remove the trend.
``Lambda`` (int):
The smoothing parameter.
*Returns*
``filtered_signal`` (1d numpy array):
The detrended signal.
"""
signal_length
=
signal
.
shape
[
0
]
# observation matrix
H
=
np
.
identity
(
signal_length
)
# second-order difference matrix
ones
=
np
.
ones
(
signal_length
)
minus_twos
=
-
2
*
np
.
ones
(
signal_length
)
diags_data
=
np
.
array
([
ones
,
minus_twos
,
ones
])
diags_index
=
np
.
array
([
0
,
1
,
2
])
D
=
spdiags
(
diags_data
,
diags_index
, (
signal_length
-
2
),
signal_length
).
toarray
()
filtered_signal
=
np
.
dot
((
H
-
np
.
linalg
.
inv
(
H
+
(
Lambda
**
2
)
*
np
.
dot
(
D
.
T
,
D
))),
signal
)
return
filtered_signal
def
mag2db
(
mag
):
"""Convert a magnitude to decibels (dB)
If A is magnitude,
db = 20 * log10(A)
Parameters
----------
mag : float or ndarray
input magnitude or array of magnitudes
Returns
-------
db : float or ndarray
corresponding values in decibels
"""
return
20.
*
np
.
log10
(
mag
)
You can’t perform that action at this time.