Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CryoGrid.jl
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CryoGrid
CryoGrid.jl
Commits
57609a42
Commit
57609a42
authored
3 years ago
by
Brian Groenke
Browse files
Options
Downloads
Patches
Plain Diff
Add more utility functions and tests
parent
39184908
No related branches found
No related tags found
1 merge request
!70
Add piecewise linear parameter mapping
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Utils/Utils.jl
+13
-5
13 additions, 5 deletions
src/Utils/Utils.jl
test/Utils/runtests.jl
+38
-2
38 additions, 2 deletions
test/Utils/runtests.jl
with
51 additions
and
7 deletions
src/Utils/Utils.jl
+
13
−
5
View file @
57609a42
...
...
@@ -14,12 +14,12 @@ import CryoGrid
import
ExprTools
import
ForwardDiff
export
@xu_str
,
@Float_str
,
@Real_str
,
@Number_str
,
@UFloat_str
,
@UT_str
,
@setscalar
,
@threaded
export
@xu_str
,
@Float_str
,
@Real_str
,
@Number_str
,
@UFloat_str
,
@UT_str
,
@setscalar
,
@threaded
,
@sym_str
include
(
"macros.jl"
)
export
DistUnit
,
DistQuantity
,
TempUnit
,
TempQuantity
,
TimeUnit
,
TimeQuantity
export
dustrip
,
duconvert
,
applyunit
export
structiterate
,
getscalar
,
tuplejoin
,
convert_tspan
export
structiterate
,
getscalar
,
tuplejoin
,
convert_t
,
convert_tspan
export
Params
# Convenience constants for units
...
...
@@ -84,17 +84,25 @@ Base.iterate(p::Params, state) = structiterate(p,state)
getscalar
(
x
::
Number
)
=
x
getscalar
(
x
::
Number
,
i
)
=
x
getscalar
(
a
::
AbstractArray
)
=
a
[
1
]
getscalar
(
a
::
AbstractArray
)
=
begin
@assert
length
(
a
)
==
1
;
a
[
1
]
end
getscalar
(
a
::
AbstractArray
,
i
)
=
a
[
i
]
"""
convert_t(t::DateTime)
convert_t(t::Float64)
Convenience method for converting between `Dates.DateTime` and solver time.
"""
convert_t
(
t
::
DateTime
)
=
Dates
.
datetime2epochms
(
t
)
/
1000
convert_t
(
t
::
Float64
)
=
Dates
.
epochms2datetime
(
1000
t
)
"""
convert_tspan(tspan::Tuple{DateTime,DateTime})
convert_tspan(tspan::Tuple{Float64,Float64})
Convenience method for converting between `Dates.DateTime` and solver time.
"""
convert_tspan
(
tspan
::
NTuple
{
2
,
DateTime
})
=
Dates
.
datetime2epochms
.
(
tspan
)
./
1000.0
convert_tspan
(
tspan
::
NTuple
{
2
,
Float64
})
=
Dates
.
epochms2datetime
.
(
tspan
.*
1000.0
)
convert_tspan
(
tspan
::
NTuple
{
2
,
DateTime
})
=
convert_t
.
(
tspan
)
convert_tspan
(
tspan
::
NTuple
{
2
,
Float64
})
=
convert_t
.
(
tspan
)
"""
argnames(f, choosefn=first)
...
...
This diff is collapsed.
Click to expand it.
test/Utils/runtests.jl
+
38
−
2
View file @
57609a42
using
CryoGrid
.
Utils
:
ffill!
using
CryoGrid
using
Dates
using
Test
struct
TestCallable
end
(
::
TestCallable
)(
x
,
y
,
z
)
=
nothing
@testset
"Utils"
begin
@testset
"argnames"
begin
f
(
x
)
=
nothing
g
(
x
,
y
)
=
nothing
h
(
x
,
y
,
args
...
)
=
nothing
@test
Utils
.
argnames
(
f
)
==
[
:
x
,]
@test
Utils
.
argnames
(
g
)
==
[
:
x
,
:
y
]
@test
Utils
.
argnames
(
h
)
==
[
:
x
,
:
y
,
:
args
]
@test
Utils
.
argnames
(
TestCallable
())
==
[
:
x
,
:
y
,
:
z
]
end
@testset
"convert time"
begin
epoch_date
=
Dates
.
epochms2datetime
(
0.0
)
# solver time is seconds since epoch, so we check that the conversion function does it correctly
@test
convert_t
(
epoch_date
+
Hour
(
1
))
≈
Dates
.
value
(
convert
(
Second
,
Hour
(
1
)))
@test
convert_t
(
convert_t
(
epoch_date
+
Hour
(
1
)))
==
epoch_date
+
Hour
(
1
)
@test
all
(
convert_tspan
((
epoch_date
,
epoch_date
+
Hour
(
1
)))
.
≈
(
0.0
,
Dates
.
value
(
convert
(
Second
,
Hour
(
1
)))))
@test
all
(
convert_tspan
(
convert_tspan
((
epoch_date
,
epoch_date
+
Hour
(
1
))))
.==
(
epoch_date
,
epoch_date
+
Hour
(
1
)))
end
@testset
"ffill!"
begin
X
=
[
missing
,
1.0
,
1.0
,
missing
,
missing
,
missing
,
2.0
,
missing
,
3.0
,
missing
]
ffill!
(
X
)
Utils
.
ffill!
(
X
)
@test
ismissing
(
X
[
1
])
@test
X
[
2
:
end
]
==
[
1.0
,
1.0
,
1.0
,
1.0
,
1.0
,
2.0
,
2.0
,
3.0
,
3.0
]
end
@testset
"getscalar"
begin
@test
getscalar
([
1.0
])
==
1.0
@test
getscalar
(
1.0
)
==
1.0
@test
getscalar
([
1.0
,
2.0
],
2
)
==
2.0
@test_throws
AssertionError
getscalar
([
1.0
,
2.0
])
end
@testset
"tuplejoin"
begin
@test
tuplejoin
(())
==
()
@test
tuplejoin
((),(
1
,))
==
(
1
,)
@test
tuplejoin
(
1
,
2
,
3
)
==
(
1
,
2
,
3
)
@test
tuplejoin
((
1
,),(
2
,),(
3
,))
==
(
1
,
2
,
3
)
@test
tuplejoin
((
1
,
2
,
3
),(
4
,
5
))
==
(
1
,
2
,
3
,
4
,
5
)
# should not flatten nested tuples
@test
tuplejoin
((
1
,
2
,(
3
,)),(
4
,))
==
(
1
,
2
,(
3
,),
4
)
end
end
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