Skip to content

Commit 3ff4271

Browse files
committed
Bug fix to MetaData, improved Julia README.
1 parent e831908 commit 3ff4271

2 files changed

Lines changed: 57 additions & 20 deletions

File tree

src/julia/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Just type `Pkg.add("JSON")` at the Julia prompt.
1919

2020
## Minimal reader / writer
2121

22-
The minimal WCON reader is in `Minimal.jl`. Just `include("Minimal.jl")` and then use
22+
The minimal WCON reader is in `Minimal.jl`. If you want to build a lightweight WCON reader/writer
23+
for your own data, this may be a good place to start. To use it, just `include("Minimal.jl")` and then
24+
enter
2325

2426
```julia
2527
TrackerCommonsMinimal.read_wcon("my/path/to/my_data.wcon")
@@ -32,3 +34,36 @@ If you have a `WormDataSet`, you can write it out with
3234
```julia
3335
TrackerCommonsMinimal.write_wcon("my/path/to/new_file.wcon")
3436
```
37+
38+
## Full-featured reader / writer
39+
40+
### Data I/O
41+
42+
TODO: write examples.
43+
44+
### Data Types
45+
46+
Worm data, including a single ID, plus a time series and x- and y-coordinate values, is
47+
represented in the `CommonWorm` type.
48+
49+
TODO: finish section
50+
51+
#### A note about Data Frames
52+
53+
Julia contains a _Data Frame_ type, motivated by a similar type in R, that consists of
54+
tabular data with named column headers. Data frames are commonly used in data analysis
55+
and statistics, so it would seem that they would be a natural fit for worm tracking data.
56+
57+
However, tracking data is not necessarily a convenient rectangular shape: worms may be
58+
tracked for different periods of time, and within one worm, some data may be static (which side is ventral),
59+
some may be a scalar for each time point (centroid), and other data may be vector valued (x coordinates of
60+
spine). Forcing the data into tabular form could thus be inefficient and require the user to
61+
deal with frequent missing data.
62+
63+
Thus, the Tracker Commons implementation imports the data in custom structures. Users are
64+
encouraged to convert this to Data Frames in those cases where it suits their analysis.
65+
66+
### Unit Conversions
67+
68+
TODO: finish section
69+

src/julia/src/MetaData.jl

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Base.==
2+
import Base.isequal
23

34
type Laboratory
45
pi :: AbstractString
@@ -139,6 +140,7 @@ function convert_for_json(m :: MetaData)
139140
if length(m.media) > 0 d["media"] = m.media end
140141
if length(m.sex) > 0 d["sex"] = m.sex end
141142
if length(m.stage) > 0 d["stage"] = m.stage end
143+
if !isnan(m.age) d["age"] = m.age end
142144
if length(m.protocol) > 0 d["protocol"] = m.protocol end
143145
nzsw = filter(x -> length(x) > 0, map(x -> convert_for_json(x), m.software))
144146
if length(nzsw) > 0
@@ -161,12 +163,12 @@ end
161163

162164
function error_if_not_string(a :: Any, err :: AbstractString, msg :: AbstractString)
163165
result :: AbstractString = err
164-
if !(typeof(a) <: AbstractString) result = error_accum(err, msg) end
166+
if !isa(a, AbstractString) result = error_accum(err, msg) end
165167
return result
166168
end
167169

168170
function empty_if_not_string(a :: Any)
169-
if typeof(a) <: AbstractString convert(AbstractString, a) else "" end
171+
if isa(a, AbstractString) convert(AbstractString, a) else "" end
170172
end
171173

172174
function parsed_json_to_laboratory(m :: Dict{AbstractString, Any})
@@ -196,9 +198,9 @@ function parsed_json_to_arena(m :: Dict{AbstractString, Any})
196198
err = error_if_not_string(kind, err, string("Arena ", keys[1], " should be a string"))
197199
diam = make_dbl_array(get(m, keys[2], Array{Float64,1}()))
198200
if length(diam) == 1
199-
if !(typeof(diam[1]) <: Number) err = error_accum(err, string("Arena ", keys[2], " should be numeric")) end
201+
if !isa(diam[1],Number) err = error_accum(err, string("Arena ", keys[2], " should be numeric")) end
200202
elseif length(diam) == 2
201-
if (!diam[1] <: Number && diam[2] <: Number) err = error_accum(err, string("Arena ", keys[2], " should have only numeric entries")) end
203+
if !(isa(diam[1], Number) && isa(diam[2], Number)) err = error_accum(err, string("Arena ", keys[2], " should have only numeric entries")) end
202204
elseif length(diam) > 2
203205
err = error_accum(err, string("Arena ", keys[2], " size should have at most two dimensions"))
204206
end
@@ -227,14 +229,14 @@ function parsed_json_to_software(m :: Dict{AbstractString, Any})
227229
err = error_if_not_string(name, err, string("Software ", keys[2], " should be a string"))
228230
fid = get(m, "featureID", "")
229231
featureID :: Set{AbstractString} = Set{AbstractString}()
230-
if (typeof(fid) <: AbstractString)
232+
if isa(fid, AbstractString)
231233
str = convert(AbstractString, fid)
232234
if length(str) > 0 featureID = Set(str) end
233-
elseif typeof(fid) <: Array
235+
elseif isa(fid, Array)
234236
sid = convert(Array, fid)
235237
allstring = true
236238
for s in sid
237-
if !(typeof(s) <: AbstractString) allstring = false end
239+
if !isa(s, AbstractString) allstring = false end
238240
end
239241
if !allstring err = error_accum(err, "Software featureIDs should all be strings")
240242
else
@@ -283,7 +285,7 @@ function parsed_json_to_metadata(d :: Dict{AbstractString, Any})
283285
if onestring[i]
284286
s = get(d, keys[i], "")
285287
strings[i] = empty_if_not_string(s)
286-
if !(typeof(s) <: AbstractString)
288+
if !isa(s, AbstractString)
287289
err = error_accum(err, string("MetaData ", keys[i], " should be a string"))
288290
end
289291
end
@@ -292,9 +294,9 @@ function parsed_json_to_metadata(d :: Dict{AbstractString, Any})
292294
for i in 1:length(keys)
293295
if vecstring[i]
294296
s = get(d, keys[i], Array{AbstractString,1}())
295-
if typeof(s) <: AbstractString
297+
if isa(s, AbstractString)
296298
vstrings[i] = Array(convert(AbstractString, s))
297-
elseif typeof(s) <: Array{AbstractString,1}
299+
elseif isa(s, Array{AbstractString,1})
298300
vstrings[i] = convert(Array{AbstractString,1}, s)
299301
else
300302
err = error_accum(err, string("MetaData ", keys[i], " should be a string or array of strings"))
@@ -305,7 +307,7 @@ function parsed_json_to_metadata(d :: Dict{AbstractString, Any})
305307
for i in 1:length(keys)
306308
if onenumber[i]
307309
n = get(d, keys[i], NaN)
308-
if typeof(n) <: Number
310+
if isa(n, Number)
309311
numbers[i] = convert(Float64, n)
310312
else
311313
err = error_accum(err, string("MetaData", keys[i], " should be a numeric value"))
@@ -315,10 +317,10 @@ function parsed_json_to_metadata(d :: Dict{AbstractString, Any})
315317
laboratory =
316318
if haskey(d, "lab")
317319
l = d["lab"]
318-
if typeof(l) <: Dict{AbstractString, Any}
320+
if isa(l, Dict{AbstractString, Any})
319321
ld = convert(Dict{AbstractString, Any}, l)
320322
lab = parsed_json_to_laboratory(ld)
321-
if typeof(lab) <: AbstractString
323+
if isa(lab, AbstractString)
322324
err = error_accum(err, convert(AbstractString, lab))
323325
Nullable{Laboratory}()
324326
else
@@ -333,10 +335,10 @@ function parsed_json_to_metadata(d :: Dict{AbstractString, Any})
333335
arena =
334336
if haskey(d, "arena")
335337
a = d["arena"]
336-
if typeof(a) <: Dict{AbstractString, Any}
338+
if isa(a, Dict{AbstractString, Any})
337339
ad = convert(Dict{AbstractString, Any}, a)
338340
arn = parsed_json_to_arena(ad)
339-
if typeof(arn) <: AbstractString
341+
if isa(arn, AbstractString)
340342
err = error_accum(err, convert(AbstractString, arn))
341343
Nullable{Arena}()
342344
else
@@ -350,18 +352,18 @@ function parsed_json_to_metadata(d :: Dict{AbstractString, Any})
350352
softwares =
351353
if haskey(d, "software")
352354
s = d["software"]
353-
if typeof(s) <: Dict{AbstractString, Any}
355+
if isa(s, Dict{AbstractString, Any})
354356
s = Array(s)
355357
end
356-
if typeof(s) <: Array
358+
if isa(s, Array)
357359
sa = convert(Array, s)
358360
softs = fill(empty_software(), length(sa))
359361
for i in 1:length(sa)
360362
si = sa[i]
361-
if typeof(si) <: Dict{AbstractString, Any}
363+
if isa(si, Dict{AbstractString, Any})
362364
sid = convert(Dict{AbstractString, Any}, si)
363365
soft = parsed_json_to_software(sid)
364-
if typeof(soft) <: AbstractString
366+
if isa(soft, AbstractString)
365367
err = error_accum(err, convert(AbstractString, soft))
366368
else
367369
softs[i] = convert(Software, soft)

0 commit comments

Comments
 (0)