Skip to content

Commit

Permalink
update LHI and LR
Browse files Browse the repository at this point in the history
  • Loading branch information
babaq committed Jun 28, 2024
1 parent 3723bde commit 775169f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
12 changes: 8 additions & 4 deletions src/Base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ end
- ky: Frequency in cycle/unit_y
- phase: Phase of a cycle in [0, 1] scale
- isnorm: scale `cas` in [-√2, √2] to [-1, 1]
!!! note
``cas(...;kx, ky, phase) = cas(...;-kx, -ky, π/2 - phase)``
"""
function cas(x,y;kx=1,ky=1, phase=0, isnorm::Bool=true)
r = sum(sincos(2π * (kx * x + ky * y + phase)))
Expand All @@ -164,7 +167,7 @@ cas2sin(phase) = phase + 0.125
function cas2sin(kx,ky,phase)
θ = atan(ky,kx) - π/2
f = sqrt(kx*kx + ky*ky)
return (θ=θ,f=f,phase=phase + 0.125)
return (;θ,f,phase=phase + 0.125)
end

"""
Expand All @@ -182,7 +185,7 @@ function sin2cas(θ,f,phase)
sinθ,cosθ = sincos+ π/2)
kx = cosθ*f
ky = sinθ*f
return (kx=kx,ky=ky,phase=phase - 0.125)
return (;kx,ky,phase=phase - 0.125)
end

"`Gabor` function"
Expand Down Expand Up @@ -542,8 +545,9 @@ Properties of Spatial Frequency Tuning
- Prefered Spatial Frequency with Peak Response
- Half Width at Half Peak-to-Trough
- Frequency Passing Type {A:All Pass, H:High Pass, L:Low Pass, B:Band Pass}
- Bandwidth ``log2(H_{cut}/L_{cut})``
- Ratio Bandwidth ``log2(H_{cut}/L_{cut})``
- Frequency Passwidth at Half Peak-to-Trough constrained by `low/high` frequency limits
- Quality Factor defined as ``PreferedFrequency / (H_{cut} - L_{cut})``, also the reciprocal of Fractional Bandwidth
1. x: sf in cycle/degree
2. y: responses
Expand All @@ -566,7 +570,7 @@ function sftuningfeature(x,y;low=minimum(x),high=maximum(x))
pw = pt == 'L' ? maxx-low+hw[2] : high-maxx+hw[1]
end

(;psf=maxx,sfhw=hw,sfpt=pt,sfbw=bw,sfpw=pw)
(;psf=maxx,sfhw=hw,sfpt=pt,sfbw=bw,sfpw=pw,sfqf=maxx/sum(hw))
end

"""
Expand Down
45 changes: 29 additions & 16 deletions src/Base/Image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,27 @@ Complex sumation of angles and corresponding image responses
1. image response for each angle
2. angles in radius
- nsd: median ± nsd*sd for clamping
- rsign: increasing/decreasing(+/-) response
- mnorm: whether clampscale magnitude map
- nsd: median ± nsd*sd for clamping(default=3)
- rsign: increasing/decreasing(+/-) response(default=-1)
- n: scale factor for a circle (default=1 for direction)
- mnorm: :m(default) - clampscale magnitude map, :r - mean resultant length(1-circ_var)
- filter: filter for image response(default=dogfilter)
return complex map, angle map[0,2π) and magnitude map([0,1] if mnorm)
return complex map, angle map([0,2π)/n) and magnitude map([0,1] if mnorm)
"""
function complexmap(rs,as;nsd=3,rsign=(-1 for _ in eachindex(rs)),mnorm=true,filter=dogfilter)
cmap = mapreduce((r,a,s)->clampscale(sign(s)*filter(r),nsd)*exp(im*a),.+,rs,as,rsign)
amap = mod2pi.(angle.(cmap))
function complexmap(rs,as;nsd=3,rsign=(-1 for _ in eachindex(rs)),n=1,mnorm=:m,filter=dogfilter)
ws = map((r,s)->clampscale(sign(s)*filter(r),nsd),rs,rsign)
cmap = mapreduce((w,a)->w*cis(n*a),.+,ws,as)
amap = mod2pi.(angle.(cmap))/n
mmap = abs.(cmap)
mnorm && (mmap = clampscale(mmap,nsd))
if mnorm==:m
mmap = clampscale(mmap,nsd)
elseif mnorm==:r
mmap = mmap./reduce(.+,ws)
end
(;cmap,amap,mmap)
end
complexmap(rs::AbstractArray{T,3},as;nsd=3,rsign=(-1 for _ in 1:size(rs,3)),mnorm=true,filter=dogfilter) where T = complexmap(eachslice(rs,dims=3),as;nsd,rsign,mnorm,filter)
complexmap(rs::AbstractArray{T,3},as;nsd=3,rsign=(-1 for _ in 1:size(rs,3)),n=1,mnorm=:m,filter=dogfilter) where T = complexmap(eachslice(rs,dims=3),as;nsd,rsign,n,mnorm,filter)

gaussianfilter(x::AbstractMatrix=5,l=6round(Int,σ)+1,border="replicate") = imfilter(x,KernelFactors.gaussian((σ,σ),(l,l)),border)
dogfilter(x::AbstractMatrix;hσ=0.5,lσ=25,l=6round(Int,max(hσ,lσ))+1,border="replicate") = imfilter(x,Kernel.DoG((hσ,hσ),(lσ,lσ),(l,l)),border)
Expand All @@ -227,23 +234,29 @@ ahe(x::AbstractMatrix;nsd=3,nbins=256,nblock=20,clip=0.1) = adjust_histogram(cla
Local Homogeneity Index
```math
LHI(𝐱)=\\frac{1}{2πσ²}|∫ exp(\\frac{-\\| 𝐱-𝐲 \\|²}{2σ²}) exp(i2θ_𝐲)d𝐲|
LHI(𝐱)=\\frac{1}{2πσ²}|∫ exp(\\frac{-\\| 𝐱-𝐲 \\|²}{2σ²}) exp(inθ_𝐲)d𝐲|
```
1. amap: angle map in radius
2. center: center coordinates of the local region
- σ: spatial scale of local region (default=6 pixels)
- n: scale factor for a circle (default=2 for orientation)
- n: scale factor for a circle (default=1 for direction)
return: the index and the local region of angle map
return: the index and the roi
Nauhaus, I., Benucci, A., Carandini, M. & Ringach, D. L. Neuronal Selectivity and Local Map Structure in Visual Cortex. Neuron 57, 673-679 (2008).
"""
function localhomoindex(amap,center;σ=6,n=2)
roi = map(c->round.(Int,c.+(-3σ,3σ)),center)
t = [exp(-0.5((d1-center[1])^2 + (d2-center[2])^2) / σ^2) * exp(im*n*amap[d1,d2]) for d1 in range(roi[1]...), d2 in range(roi[2]...)]
abs(sum(t)) / (2π*σ^2), amap[range(roi[1]...),range(roi[2]...)]
function localhomoindex(amap,center;σ=6,n=1)
roi = map(c->range(round.(Int,c.+(-3σ,3σ))...),center)
t = [exp(-0.5((d1-center[1])^2 + (d2-center[2])^2) / σ^2) * exp(im*n*amap[d1,d2]) for d1 in roi[1], d2 in roi[2]]
(;lhi=abs(sum(t)) / (2π*σ^2), roi)
end

function localaverage(mmap,center;σ=6)
roi = map(c->range(round.(Int,c.+(-3σ,3σ))...),center)
t = [exp(-0.5((d1-center[1])^2 + (d2-center[2])^2) / σ^2) for d1 in roi[1], d2 in roi[2]]
(;la=sum(t.*mmap[roi...]) / sum(t), roi)
end

function angleabs(cmap)
Expand Down
8 changes: 8 additions & 0 deletions test/imagetest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ hgs = map(i -> begin
minv,maxv = extrema(reduce((i,j)->i.+j,hgs)/length(hs))
@test minvmaxv0.5


ss=cas2sin(0.3,-0.2,0.2)
Gray.(grating= ss.θ, sf = ss.f, phase = ss.phase, size = (5, 5), ppd = 30))

ss=cas2sin(-0.3,0.2,0.05)
Gray.(grating= ss.θ, sf = ss.f, phase = ss.phase, size = (5, 5), ppd = 30))


hs = hartleysubspace(kbegin=0.2,kend=6.6,dk=0.2,addhalfcycle=true)
hgs = map(i -> begin
ss = cas2sin(i...)
Expand Down

0 comments on commit 775169f

Please sign in to comment.