-
Notifications
You must be signed in to change notification settings - Fork 6
/
Etc.jl
174 lines (148 loc) · 5.34 KB
/
Etc.jl
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
## --- Digitize image data
"""
```julia
digitize_plotmarkers(img, marker_color, xlims, ylims; atol=0.16)
```
Calculate approximate `x` (horizontal) and `y` (vertical) positions and
position uncertainties for distinct colored markers in an image.
### Examples
```julia
img = load("xyscatter.png") # using FileIO, ImageIO
C = eltype(img)
(x,dx,y,dy) = digitize_plotmarkers(img, C(0,0.604,0.976,1), [0,10], [0,10])
```
"""
function digitize_plotmarkers(img, marker_color, xlims, ylims; atol=0.16)
# Test for approximate equality in color to marker
t = isapprox.(marker_color,img,atol=atol)
# Figure out our image dimensions
xrows = size(t,2)
yrows = size(t,1)
# Allocate index arrays
imin = Array{Float64}(undef,round(Int,xrows/2))
imax = Array{Float64}(undef,round(Int,xrows/2))
jmin = Array{Float64}(undef,round(Int,xrows/2))
jmax = Array{Float64}(undef,round(Int,xrows/2))
# Fill index arrays
found = false
markernumber = 0
for j ∈ 1:xrows
tⱼ = view(t,:,j)
if any(tⱼ)
list = findall(tⱼ)
if ~found
markernumber += 1
imin[markernumber] = minimum(list)
imax[markernumber] = maximum(list)
jmin[markernumber] = j
jmax[markernumber] = j
else
imin[markernumber] = min(imin[markernumber],minimum(list))
imax[markernumber] = max(imax[markernumber],maximum(list))
jmax[markernumber] = j
end
found = true
else
found = false
end
end
# Return only the filled indices
imin = imin[1:markernumber]
imax = imax[1:markernumber]
jmin = jmin[1:markernumber]
jmax = jmax[1:markernumber]
# Calculate x and y positions from indices
Δy = last(ylims) - first(ylims)
y = last(ylims) .- (imin+imax)/2 * Δy / yrows
dy = (imax-imin)/2 * Δy / yrows
Δx = last(xlims) - first(xlims)
x = (jmin+jmax)/2 * Δx / xrows .+ first(xlims)
dx = (jmax-jmin)/2 * Δx / xrows
return x, dx, y, dy
end
export digitize_plotmarkers
"""
```julia
digitize_plotline(img, line_color, xlims, ylims; atol=0.16)
```
Calculate approximate `x` (horizontal) and `y` (vertical) positions for
a colored line in an image
### Examples
```julia
img = load("xysin.png") # using FileIO, ImageIO
C = eltype(img)
(x,y) = digitize_plotline(img, C(0,0.604,0.976,1), [0,2pi], [-1.1,1.1])
```
"""
function digitize_plotline(img, line_color, xlims, ylims; atol=0.16)
# Test for approximate equality in color to marker
t = isapprox.(line_color,img,atol=atol)
# Figure out our image dimensions
xrows = size(t,2)
yrows = size(t,1)
# Calculate x for each column
x = cntr(range(first(xlims), last(xlims), length=xrows+1))
# y as a function of i-position in image
# (note: images are typically flipped)
Δy = last(ylims) - first(ylims)
yᵢ(i) = last(ylims) - i * Δy / yrows
# Calculate y, defaulting to NaN if no matches
y = fill(NaN, xrows)
for j = 1:xrows
tⱼ = view(t,:,j)
y[j] = yᵢ(findmeanindex(tⱼ))
end
return x, y
end
export digitize_plotline
function findmeanindex(x)
@assert isa(firstindex(x), Int)
μ = 0
n = 0
@inbounds for i ∈ eachindex(x)
if x[i] == true
μ += i
n += 1
end
end
return μ/n
end
## --- Retain deprecated functions with matlab-like syntax, to avoid breakages in user scripts that may depend on them
if ~ @isdefined linspace
"""
```julia
linspace(l::Number,u::Number,n::Number)
```
Returns a linearly spaced array with `n` points between the starting
bound `l` and ending bound `u`
"""
function linspace(l::Number,u::Number,n::Number)
return range(l,stop=u,length=n)
end
export linspace
end
if ~ @isdefined contains
"""
```julia
contains(haystack, needle)
```
Converts both `haystack` and `needle` to strings (if not already strings)
and checks whether `string(haystack)` contains `string(needle)`.
"""
contains(haystack::AbstractString, needle::Union{AbstractString,Regex,AbstractChar}) = occursin(needle, haystack)
contains(haystack, needle) = occursin(string(needle), string(haystack))
export contains
end
if ~ @isdefined containsi
"""
```julia
containsi(haystack, needle)
```
Converts both `haystack` and `needle` to strings and checks whether
`string(haystack)` contains `string(needle)`, ignoring case.
"""
containsi(haystack::AbstractString, needle::Union{AbstractString,AbstractChar}) = occursin(lowercase(needle), lowercase(haystack))
containsi(haystack, needle) = occursin(lowercase(string(needle)), lowercase(string(haystack)))
export containsi
end
## --- End of File