Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Multi Scale Causality
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FROGE Ewen
Multi Scale Causality
Commits
178efe68
Commit
178efe68
authored
9 months ago
by
FROGE Ewen
Browse files
Options
Downloads
Patches
Plain Diff
Replace Main.py
parent
ccd27a30
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
TE_filters/Main.py
+136
-32
136 additions, 32 deletions
TE_filters/Main.py
with
136 additions
and
32 deletions
TE_filters/Main.py
+
136
−
32
View file @
178efe68
...
...
@@ -3,7 +3,7 @@
import
numpy
as
np
from
scipy.ndimage
import
gaussian_filter1d
from
scipy.signal
import
convolve
from
MFsynthesis
import
synthmrw
from
MFsynthesis
import
synthmrw
,
synthMultiFractalField
import
entropy.entropy
as
entropy
# Assuming your TE library is imported
import
entropy.tools
as
tools
import
matplotlib.pyplot
as
plt
...
...
@@ -44,7 +44,11 @@ fbm_signal,_ = synthmrw(dim=1, N=N, H=1/3, lambda2=0) # Placeholder for FBM
fbm_signal
=
fbm_signal
[:,
0
]
fgn_signal
=
np
.
diff
(
fbm_signal
,
axis
=
0
)
scales
=
[
10
,
20
]
# Define your scales
FBM_regu
=
synthMultiFractalField
(
dim
=
1
,
N
=
N
,
H
=
1
/
3
,
lambda2
=
0
,
regparam
=
[
1
,
2980
])
FBM_regu
=
FBM_regu
[:,
0
]
scales
=
[
20
,
30
]
# Define your scales
positions
=
[
'
causal
'
,
'
symmetric
'
,
'
anticausal
'
]
signals
=
[
'
fgn_signal
'
,
'
fbm_signal
'
,
'
Modane
'
]
max_lag_factor
=
2
# We will vary lag up to 10 times the largest scale
...
...
@@ -58,15 +62,15 @@ max_lag_factor = 2 # We will vary lag up to 10 times the largest scale
results
=
{}
def
gaussian_kernel
(
scale
,
position
):
size
=
2
*
scale
+
1
size
=
10
*
scale
+
1
x
=
np
.
linspace
(
-
(
size
//
2
),
size
//
2
,
size
)
kernel
=
np
.
exp
(
-
0.5
*
(
x
/
scale
)
**
2
)
if
position
==
'
causal
'
:
kernel
[:
s
cale
]
=
0
kernel
[:
s
ize
//
2
]
=
0
elif
position
==
'
anticausal
'
:
kernel
[
s
cale
+
1
:]
=
0
kernel
[
s
ize
//
2
+
1
:]
=
0
elif
position
==
'
symmetric
'
:
pass
...
...
@@ -75,15 +79,15 @@ def gaussian_kernel(scale, position):
return
kernel
def
gaussian_HP_kernel
(
scale
,
position
):
size
=
2
*
scale
+
1
size
=
10
*
scale
+
1
x
=
np
.
linspace
(
-
(
size
//
2
),
size
//
2
,
size
)
kernel
=
np
.
exp
(
-
0.5
*
(
x
/
scale
)
**
2
)
if
position
==
'
causal
'
:
kernel
[:
s
cale
]
=
0
kernel
[:
s
ize
//
2
]
=
0
elif
position
==
'
anticausal
'
:
kernel
[
s
cale
+
1
:]
=
0
kernel
[
s
ize
//
2
+
1
:]
=
0
elif
position
==
'
symmetric
'
:
pass
...
...
@@ -91,21 +95,22 @@ def gaussian_HP_kernel(scale, position):
kernel
/=
kernel
.
sum
()
delta
=
np
.
zeros
(
size
)
delta
[
s
cale
]
=
1
delta
[
s
ize
//
2
]
=
1
kernel
=
delta
-
kernel
return
kernel
def
box_HP_kernel
(
scale
,
position
):
size
=
2
*
scale
+
1
kernel
=
np
.
ones
(
size
)
size
=
10
*
scale
+
1
x
=
np
.
linspace
(
-
(
size
//
2
),
size
//
2
,
size
)
kernel
=
np
.
zeros
(
size
)
kernel
[
np
.
where
(
np
.
abs
(
x
)
<=
scale
)]
=
1
if
position
==
'
causal
'
:
kernel
[:
s
cale
]
=
0
kernel
[:
s
ize
//
2
]
=
0
elif
position
==
'
anticausal
'
:
kernel
[
s
cale
+
1
:]
=
0
kernel
[
s
ize
//
2
+
1
:]
=
0
elif
position
==
'
symmetric
'
:
pass
...
...
@@ -113,7 +118,7 @@ def box_HP_kernel(scale, position):
kernel
/=
kernel
.
sum
()
delta
=
np
.
zeros
(
size
)
delta
[
s
cale
]
=
1
delta
[
s
ize
//
2
]
=
1
kernel
=
delta
-
kernel
return
kernel
...
...
@@ -121,7 +126,7 @@ def box_HP_kernel(scale, position):
def
gaussian_BP_kernel
(
scale_high
,
position
,
scale_low
=
None
):
if
scale_low
==
None
:
scale_low
=
2
*
scale_high
size
=
2
*
scale_low
+
1
size
=
10
*
scale_low
+
1
x
=
np
.
linspace
(
-
(
size
//
2
),
size
//
2
,
size
)
kernel_low
=
np
.
exp
(
-
0.5
*
(
x
/
scale_low
)
**
2
)
kernel_high
=
np
.
exp
(
-
0.5
*
(
x
/
scale_high
)
**
2
)
...
...
@@ -139,13 +144,13 @@ def gaussian_BP_kernel(scale_high,position,scale_low=None):
kernel_low
/=
kernel_low
.
sum
()
kernel_high
/=
kernel_high
.
sum
()
kernel
=
kernel_
low
-
kernel_
high
kernel
=
kernel_
high
-
kernel_
low
return
kernel
def
box_BP_kernel
(
scale_high
,
position
,
scale_low
=
None
):
if
scale_low
==
None
:
scale_low
=
2
*
scale_high
size
=
2
*
scale_low
+
1
size
=
10
*
scale_low
+
1
x
=
np
.
linspace
(
-
(
size
//
2
),
size
//
2
,
size
)
kernel_low
=
np
.
zeros
(
size
)
kernel_low
[
np
.
where
(
np
.
abs
(
x
)
<=
scale_low
)]
=
1
...
...
@@ -165,7 +170,7 @@ def box_BP_kernel(scale_high,position,scale_low=None ):
kernel_low
/=
kernel_low
.
sum
()
kernel_high
/=
kernel_high
.
sum
()
kernel
=
kernel
_low
-
kernel_high
kernel
=
kernel
=
kernel_high
-
kernel_low
return
kernel
...
...
@@ -191,8 +196,8 @@ def increment_kernel(scale,position):
def
gabor_kernel
(
scale
,
position
,
omega
=
None
):
if
omega
==
None
:
omega
=
1
/
scale
size
=
2
*
scale
+
1
omega
=
5
/
scale
size
=
10
*
scale
+
1
x
=
np
.
linspace
(
-
(
size
//
2
),
size
//
2
,
size
)
...
...
@@ -209,14 +214,15 @@ def gabor_kernel(scale, position,omega=None):
return
kernel
def
box_kernel
(
scale
,
position
):
size
=
2
*
scale
+
1
kernel
=
np
.
ones
(
size
)
size
=
10
*
scale
+
1
x
=
np
.
linspace
(
-
(
size
//
2
),
size
//
2
,
size
)
kernel
=
np
.
zeros
(
size
)
kernel
[
np
.
where
(
np
.
abs
(
x
)
<=
scale
)]
=
1
if
position
==
'
causal
'
:
kernel
[:
s
cale
]
=
0
kernel
[:
s
ize
//
2
]
=
0
elif
position
==
'
anticausal
'
:
kernel
[
s
cale
+
1
:]
=
0
kernel
[
s
ize
//
2
+
1
:]
=
0
elif
position
==
'
symmetric
'
:
pass
...
...
@@ -226,12 +232,12 @@ def box_kernel(scale, position):
kernel_functions
=
{
#
'increment': increment_kernel,
#
'gaussian': gaussian_kernel,
#
'gabor': gabor_kernel,
#
'box': box_kernel,
#
'box_HP': box_HP_kernel,
#
'gaussian_HP': gaussian_HP_kernel,
'
increment
'
:
increment_kernel
,
'
gaussian
'
:
gaussian_kernel
,
'
gabor
'
:
gabor_kernel
,
'
box
'
:
box_kernel
,
'
box_HP
'
:
box_HP_kernel
,
'
gaussian_HP
'
:
gaussian_HP_kernel
,
'
box_BP
'
:
box_BP_kernel
,
'
gaussian_BP
'
:
gaussian_BP_kernel
,
...
...
@@ -304,6 +310,10 @@ for kernel_type in kernel_types:
te_values
,
te_std
=
results
[
key
]
lags
=
range
(
1
,
len
(
te_values
)
+
1
)
ax
.
errorbar
(
lags
,
te_values
,
yerr
=
te_std
,
label
=
f
'
Scale1=
{
scale1
}
, Scale2=
{
scale2
}
'
,
capsize
=
5
)
ax
.
axvline
(
x
=
scale1
,
color
=
'
k
'
,
linestyle
=
'
:
'
)
ax
.
axvline
(
x
=
scale2
,
color
=
'
k
'
,
linestyle
=
'
:
'
)
scale_diff
=
abs
(
scale1
-
scale2
)
ax
.
axvline
(
x
=
scale_diff
,
color
=
'
k
'
,
linestyle
=
'
:
'
)
ax
.
legend
(
fontsize
=
16
)
...
...
@@ -364,4 +374,98 @@ plt.tight_layout(rect=[0, 0, 1, 0.95])
plt
.
savefig
(
'
/users2/local/e22froge/codes/TE_Filter/Kernels_BP.pdf
'
,
bbox_inches
=
'
tight
'
)
plt
.
show
()
# %% # %%
# Plotting Signal Spectrum with Vertical Lines for Scales using scipy.welch and Gabor BP frequencies (log-log scale)
from
scipy.signal
import
welch
fig
,
axes
=
plt
.
subplots
(
1
,
len
(
signals
),
figsize
=
(
20
,
10
))
for
i
,
sgn
in
enumerate
(
signals
):
signal
=
eval
(
sgn
)
ax
=
axes
[
i
]
# Compute the power spectral density using Welch's method
freqs
,
psd
=
welch
(
signal
,
fs
=
1.0
,
nperseg
=
2048
)
# fs=1.0 assumes normalized frequency
# Plot the PSD on a log-log scale
ax
.
plot
(
freqs
,
psd
,
label
=
f
'
{
sgn
.
capitalize
()
}
Spectrum
'
)
ax
.
set_xscale
(
'
log
'
)
ax
.
set_yscale
(
'
log
'
)
# Plot vertical lines corresponding to the scales
for
scale
in
scales
:
freq_scale
=
1
/
scale
# Frequency corresponding to each scale
ax
.
axvline
(
x
=
freq_scale
,
color
=
'
r
'
,
linestyle
=
'
--
'
,
label
=
f
'
Scale=
{
scale
}
'
)
# Plot a dashed line for the difference between the scales
scale_diff
=
abs
(
scales
[
1
]
-
scales
[
0
])
freq_scale_diff
=
1
/
scale_diff
ax
.
axvline
(
x
=
freq_scale_diff
,
color
=
'
b
'
,
linestyle
=
'
-.
'
,
label
=
f
'
Scale Difference=
{
scale_diff
}
'
)
# Add smaller dashed lines for Gabor modulation frequencies
for
scale
in
scales
:
omega_modulation
=
5
/
scale
# Modulation frequency used in Gabor filter
ax
.
axvline
(
x
=
omega_modulation
,
color
=
'
g
'
,
linestyle
=
'
:
'
,
label
=
f
'
Gabor Modulation for Scale=
{
scale
}
'
)
ax
.
set_title
(
f
'
Spectrum of
{
sgn
.
capitalize
()
}
'
,
fontsize
=
24
)
ax
.
set_xlabel
(
'
Frequency (log scale)
'
,
fontsize
=
20
)
ax
.
set_ylabel
(
'
Power Spectral Density (log scale)
'
,
fontsize
=
20
)
ax
.
legend
()
plt
.
tight_layout
(
rect
=
[
0
,
0
,
1
,
0.95
])
#plt.savefig('/users2/local/e22froge/codes/TE_Filter/Signal_Spectrum.pdf', bbox_inches='tight')
plt
.
show
()
# %%
# %%
# Importing scipy's welch method
from
scipy.signal
import
welch
# Plotting all Signal Spectrums on the same graph with Vertical Lines for Scales and Gabor Modulation
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
12
,
8
))
fig
.
suptitle
(
'
All Signal Spectrums with Vertical Lines for Scales and Gabor Modulation
'
,
fontsize
=
20
)
colors
=
[
'
b
'
,
'
g
'
,
'
r
'
,
'
y
'
]
# Define different colors for each signal
line_styles
=
[
'
-
'
,
'
--
'
,
'
-.
'
,
'
:
'
]
# Line styles for different signals
for
i
,
sgn
in
enumerate
(
signals
):
signal
=
eval
(
sgn
)
# Compute the power spectral density using Welch's method
freqs
,
psd
=
welch
(
signal
,
nperseg
=
2048
,
fs
=
1.0
)
if
sgn
==
'
FBM_regu
'
:
psd
=
10
**
5
*
psd
# Plot the power spectral density on the same axis
ax
.
plot
(
freqs
,
psd
,
color
=
colors
[
i
],
linestyle
=
line_styles
[
i
],
label
=
f
'
{
sgn
.
capitalize
()
}
'
)
ax
.
set_xscale
(
'
log
'
)
ax
.
set_yscale
(
'
log
'
)
# Plot vertical lines corresponding to the scales
for
scale
in
scales
:
freq_scale
=
1
/
scale
# Frequency corresponding to each scale
ax
.
axvline
(
x
=
freq_scale
,
color
=
'
r
'
,
linestyle
=
'
--
'
,
label
=
f
'
Scale=
{
scale
}
'
if
i
==
0
else
""
)
# Add label only for the first signal
# Plot a dashed line for the difference between the scales
scale_diff
=
abs
(
scales
[
1
]
-
scales
[
0
])
freq_scale_diff
=
1
/
scale_diff
ax
.
axvline
(
x
=
freq_scale_diff
,
color
=
'
b
'
,
linestyle
=
'
-.
'
,
label
=
f
'
Scale Difference=
{
scale_diff
}
'
if
i
==
0
else
""
)
# Add label only once
# Add smaller dashed lines for Gabor modulation frequencies
for
scale
in
scales
:
omega_modulation
=
5
/
scale
# Modulation frequency used in Gabor filter
ax
.
axvline
(
x
=
omega_modulation
,
color
=
'
g
'
,
linestyle
=
'
:
'
,
label
=
f
'
Gabor Modulation for Scale=
{
scale
}
'
if
i
==
0
else
""
)
# Set titles and labels
ax
.
set_title
(
'
Power Spectral Density of Signals
'
,
fontsize
=
16
)
ax
.
set_xlabel
(
'
Frequency
'
,
fontsize
=
14
)
ax
.
set_ylabel
(
'
Power Spectral Density
'
,
fontsize
=
14
)
# Display the legend
ax
.
legend
()
# Display the plot
plt
.
tight_layout
()
plt
.
savefig
(
'
/users2/local/e22froge/codes/TE_Filter/Signal_Spectrum_Superposed.pdf
'
,
bbox_inches
=
'
tight
'
)
plt
.
show
()
# %%
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