Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MCC Flow
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Köllőd Csaba
MCC Flow
Commits
1122a88e
Commit
1122a88e
authored
2 years ago
by
Köllőd Csaba
Browse files
Options
Downloads
Patches
Plain Diff
Release: file converter
parent
9c76d60c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mcc-flow/merge_records.py
+43
-20
43 additions, 20 deletions
mcc-flow/merge_records.py
with
43 additions
and
20 deletions
mcc-flow/merge_records.py
+
43
−
20
View file @
1122a88e
...
@@ -6,8 +6,12 @@ from struct import unpack
...
@@ -6,8 +6,12 @@ from struct import unpack
import
mne
import
mne
import
numpy
as
np
import
numpy
as
np
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
from
pyedflib
import
EdfWriter
,
FILETYPE_
B
DFPLUS
from
pyedflib
import
EdfWriter
,
FILETYPE_
E
DFPLUS
from
scipy
import
signal
from
scipy
import
signal
from
scipy.interpolate
import
interp1d
TASK_SPLIT_TIME
=
30
# (sec)
EXPERIMENT_SPLIT_TIME
=
10
# (min)
HEADER_FS
=
1
HEADER_FS
=
1
EEG_EMG_FS
=
250
EEG_EMG_FS
=
250
...
@@ -87,7 +91,7 @@ class FlowPacket:
...
@@ -87,7 +91,7 @@ class FlowPacket:
self
.
_plot
(
self
.
eeg
,
'
EEG
'
)
self
.
_plot
(
self
.
eeg
,
'
EEG
'
)
self
.
_plot
(
self
.
eeg_giro
,
'
EEG Gyroscope
'
)
self
.
_plot
(
self
.
eeg_giro
,
'
EEG Gyroscope
'
)
self
.
_plot
(
self
.
eeg_acc
,
'
EEG Accelerometer
'
)
self
.
_plot
(
self
.
eeg_acc
,
'
EEG Accelerometer
'
)
#
self._plot(self.emg, 'EMG')
self
.
_plot
(
self
.
emg
,
'
EMG
'
)
# self._plot(self.spo2, 'SPO2')
# self._plot(self.spo2, 'SPO2')
plt
.
show
()
plt
.
show
()
...
@@ -96,14 +100,14 @@ class FlowPacket:
...
@@ -96,14 +100,14 @@ class FlowPacket:
self
.
emg
,
self
.
emg_giro
,
self
.
emg_acc
,
self
.
emg
,
self
.
emg_giro
,
self
.
emg_acc
,
self
.
flow
,
self
.
hr
,
self
.
spo2
,
self
.
gsr
]
self
.
flow
,
self
.
hr
,
self
.
spo2
,
self
.
gsr
]
with
EdfWriter
(
filename
,
sum
(
pkt
.
chs
for
pkt
in
to_save
),
FILETYPE_
B
DFPLUS
)
as
edf
:
with
EdfWriter
(
filename
,
sum
(
pkt
.
chs
for
pkt
in
to_save
),
FILETYPE_
E
DFPLUS
)
as
edf
:
data_buffer
=
[]
data_buffer
=
[]
sig
=
0
sig
=
0
for
pkt
in
to_save
:
for
pkt
in
to_save
:
for
i
,
ch_dat
in
enumerate
(
pkt
.
data
):
for
i
,
ch_dat
in
enumerate
(
pkt
.
data
):
# print(sig)
# print(sig)
edf
.
setPhysicalMaximum
(
sig
,
3000
)
edf
.
setPhysicalMaximum
(
sig
,
max
(
ch_dat
.
max
(),
1
)
)
edf
.
setPhysicalMinimum
(
sig
,
-
3000
)
edf
.
setPhysicalMinimum
(
sig
,
min
(
ch_dat
.
min
(),
-
1
)
)
# edf.setDigitalMaximum(sig, 2**7)
# edf.setDigitalMaximum(sig, 2**7)
# edf.setDigitalMinimum(sig, -2**7)
# edf.setDigitalMinimum(sig, -2**7)
edf
.
setPhysicalDimension
(
sig
,
pkt
.
unit
)
edf
.
setPhysicalDimension
(
sig
,
pkt
.
unit
)
...
@@ -114,7 +118,8 @@ class FlowPacket:
...
@@ -114,7 +118,8 @@ class FlowPacket:
edf
.
writeSamples
(
data_buffer
)
edf
.
writeSamples
(
data_buffer
)
def
to_mne
(
self
,
filename
):
def
to_mne
(
self
,
filename
):
to_save
=
[
self
.
eeg
,
self
.
eeg_giro
,
self
.
eeg_acc
,
to_save
=
[
self
.
eeg
,
self
.
eeg_giro
,
self
.
eeg_acc
,
self
.
emg
,
self
.
emg_giro
,
self
.
emg_acc
,
self
.
emg
,
self
.
emg_giro
,
self
.
emg_acc
,
self
.
flow
,
self
.
hr
,
self
.
spo2
,
self
.
gsr
]
self
.
flow
,
self
.
hr
,
self
.
spo2
,
self
.
gsr
]
...
@@ -126,14 +131,21 @@ class FlowPacket:
...
@@ -126,14 +131,21 @@ class FlowPacket:
data_buffer
.
extend
(
pkt
.
data
*
1e-6
)
data_buffer
.
extend
(
pkt
.
data
*
1e-6
)
ch_types
.
extend
([
pkt
.
type
]
*
pkt
.
chs
)
ch_types
.
extend
([
pkt
.
type
]
*
pkt
.
chs
)
else
:
else
:
# https://docs.scipy.org/doc/scipy-1.9.3/reference/generated/scipy.interpolate.interp1d.html#scipy.interpolate.interp1d
if
pkt
.
fs
<
EEG_EMG_FS
:
dat
=
np
.
array
([
signal
.
resample
(
t
,
self
.
eeg
.
data
.
shape
[
-
1
])
for
t
in
pkt
.
data
])
interp
=
interp1d
(
np
.
arange
(
pkt
.
data
.
shape
[
-
1
]),
pkt
.
data
,
kind
=
'
linear
'
,
axis
=-
1
)
dat
=
interp
(
np
.
linspace
(
0
,
pkt
.
data
.
shape
[
-
1
]
-
1
,
self
.
eeg
.
data
.
shape
[
-
1
]))
# # Spline interpolation
# t0 = np.arange(pkt.data.shape[-1])
# tn = np.linspace(0, pkt.data.shape[-1] - 1, self.eeg.data.shape[-1])
# dat = np.array([splev(tn, splrep(t0, d)) for d in pkt.data])
else
:
dat
=
np
.
array
([
signal
.
resample
(
t
,
self
.
eeg
.
data
.
shape
[
-
1
])
for
t
in
pkt
.
data
])
data_buffer
.
extend
(
dat
)
data_buffer
.
extend
(
dat
)
ch_types
.
extend
([
'
misc
'
]
*
pkt
.
chs
)
ch_types
.
extend
([
'
misc
'
]
*
pkt
.
chs
)
ch_names
.
extend
([
f
'
{
pkt
.
type
}{
i
}
'
for
i
in
range
(
pkt
.
chs
)])
ch_names
.
extend
([
f
'
{
pkt
.
type
}{
i
}
'
for
i
in
range
(
pkt
.
chs
)])
info
=
mne
.
create_info
(
ch_names
,
EEG_EMG_FS
,
ch_types
)
info
=
mne
.
create_info
(
ch_names
,
EEG_EMG_FS
,
ch_types
)
raw
=
mne
.
io
.
RawArray
(
np
.
array
(
data_buffer
),
info
)
raw
=
mne
.
io
.
RawArray
(
np
.
array
(
data_buffer
),
info
)
raw
.
save
(
filename
)
raw
.
save
(
filename
,
overwrite
=
True
)
def
_read_block_data
(
file
,
shape
,
format_
,
format_size
):
def
_read_block_data
(
file
,
shape
,
format_
,
format_size
):
...
@@ -203,9 +215,24 @@ def read_dat_file(file):
...
@@ -203,9 +215,24 @@ def read_dat_file(file):
return
packet_holder
return
packet_holder
def
read_data
(
path
):
def
process_data_files_in_dir
(
path
,
out_file_type
=
'
edf
'
):
path
=
Path
(
path
)
path
=
Path
(
path
)
files
=
list
(
path
.
rglob
(
'
*.dat
'
))
files
=
list
(
sorted
(
path
.
rglob
(
'
*.dat
'
)))
def
save
():
save_path
=
path
.
joinpath
(
f
'
exp
{
exp
:
03
}
'
)
if
out_file_type
in
[
'
edf
'
,
'
fif
'
,
'
mne
'
]:
save_path
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
base_file
=
str
(
save_path
.
joinpath
(
f
'
exp
{
exp
:
03
}
-rec
{
rec
:
03
}
'
))
if
out_file_type
==
'
edf
'
:
data
.
to_edf
(
base_file
+
'
.edf
'
)
else
:
data
.
to_mne
(
base_file
+
'
_raw.fif
'
)
else
:
if
out_file_type
!=
'
plot
'
:
print
(
f
'
Supported out_file_types are [edf, fif], got
{
out_file_type
}
instead.
\n
'
f
'
Files are not saved, but displayed with matplotlib.
'
)
data
.
plot
()
with
open
(
files
[
0
],
'
rb
'
)
as
f
:
with
open
(
files
[
0
],
'
rb
'
)
as
f
:
data
=
read_dat_file
(
f
)
data
=
read_dat_file
(
f
)
...
@@ -215,23 +242,19 @@ def read_data(path):
...
@@ -215,23 +242,19 @@ def read_data(path):
for
i
in
range
(
1
,
len
(
files
)):
for
i
in
range
(
1
,
len
(
files
)):
with
open
(
files
[
i
],
'
rb
'
)
as
f
:
with
open
(
files
[
i
],
'
rb
'
)
as
f
:
dat
=
read_dat_file
(
f
)
dat
=
read_dat_file
(
f
)
if
dat
.
date
-
data
.
date
<
timedelta
(
seconds
=
data
.
duration
+
10
):
if
dat
.
date
-
data
.
date
<
timedelta
(
seconds
=
data
.
duration
+
TASK_SPLIT_TIME
):
data
+=
dat
data
+=
dat
else
:
else
:
# data.plot()
save
()
# data.to_edf(f'exp{exp:03}-rec{rec:03}.bdf')
if
dat
.
date
-
data
.
date
<
timedelta
(
minutes
=
EXPERIMENT_SPLIT_TIME
):
data
.
to_mne
(
f
'
exp
{
exp
:
03
}
-rec
{
rec
:
03
}
_raw.fif
'
)
if
dat
.
date
-
data
.
date
<
timedelta
(
minutes
=
10
):
rec
+=
1
rec
+=
1
else
:
else
:
rec
=
1
rec
=
1
exp
+=
1
exp
+=
1
data
=
dat
data
=
dat
# data.plot()
save
()
# data.to_edf(f'exp{exp:03}-rec{rec:03}.bdf')
data
.
to_mne
(
f
'
exp
{
exp
:
03
}
-rec
{
rec
:
03
}
_raw.fif
'
)
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
path
=
r
'
D:\Users\Csabi\OneDrive - Pázmány Péter Katolikus Egyetem\MCC Flow\database
'
path
=
r
'
D:\Users\Csabi\OneDrive - Pázmány Péter Katolikus Egyetem\MCC Flow\database
'
read_data
(
path
)
process_data_files_in_dir
(
path
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment