Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nshmp-lib
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
Service Desk
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
ghsc
National Seismic Hazard Model Project
nshmp-lib
Commits
8caf8fa7
Commit
8caf8fa7
authored
3 years ago
by
Powers, Peter M.
Browse files
Options
Downloads
Patches
Plain Diff
additional sequences class tests
parent
fb630a5a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!197
Data tests
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/gov/usgs/earthquake/nshmp/data/Sequences.java
+11
-25
11 additions, 25 deletions
src/main/java/gov/usgs/earthquake/nshmp/data/Sequences.java
src/test/java/gov/usgs/earthquake/nshmp/data/SequencesTests.java
+84
-0
84 additions, 0 deletions
...t/java/gov/usgs/earthquake/nshmp/data/SequencesTests.java
with
95 additions
and
25 deletions
src/main/java/gov/usgs/earthquake/nshmp/data/Sequences.java
+
11
−
25
View file @
8caf8fa7
...
@@ -20,6 +20,10 @@ public class Sequences {
...
@@ -20,6 +20,10 @@ public class Sequences {
private
Sequences
()
{}
private
Sequences
()
{}
/**
* Check that the supplied arrays are not empty, are the same size, and that
* the supplied xs increase monotonically.
*/
static
void
validateArrays
(
double
[]
xs
,
double
[]
ys
)
{
static
void
validateArrays
(
double
[]
xs
,
double
[]
ys
)
{
checkArgument
(
xs
.
length
>
0
,
"x-values may not be empty"
);
checkArgument
(
xs
.
length
>
0
,
"x-values may not be empty"
);
checkArgument
(
xs
.
length
==
ys
.
length
,
"x- and y-values are different sizes"
);
checkArgument
(
xs
.
length
==
ys
.
length
,
"x- and y-values are different sizes"
);
...
@@ -30,9 +34,10 @@ public class Sequences {
...
@@ -30,9 +34,10 @@ public class Sequences {
/**
/**
* Trim points from the start or end of a sequence for which y-values are
* Trim points from the start or end of a sequence for which y-values are
* zero. If no such leading or trailing points exist, sequence con
a
tins only a
* zero. If no such leading or trailing points exist, sequence cont
a
ins only a
* single point, or all y-values are zero, method returns the supplied
* single point, or all y-values are zero, method returns the supplied
* sequence.
* sequence. If supplied sequence is mutable, returned sequence is mutable;
* otherwise returned sequence is immutable.
*/
*/
static
XySequence
trim
(
ArrayXySequence
xy
)
{
static
XySequence
trim
(
ArrayXySequence
xy
)
{
if
(
xy
.
size
()
==
1
||
xy
.
isClear
())
{
if
(
xy
.
size
()
==
1
||
xy
.
isClear
())
{
...
@@ -93,7 +98,7 @@ public class Sequences {
...
@@ -93,7 +98,7 @@ public class Sequences {
return
((
int
)
Maths
.
round
(((
max
-
min
)
/
delta
),
6
))
+
1
;
return
((
int
)
Maths
.
round
(((
max
-
min
)
/
delta
),
6
))
+
1
;
}
}
/*
/*
*
* Ensure validity of sequence discretization parameters. Confirms that for a
* Ensure validity of sequence discretization parameters. Confirms that for a
* specified range [min..max] and Δ that (1) min, max, and Δ are finite, (2)
* specified range [min..max] and Δ that (1) min, max, and Δ are finite, (2)
* max > min, and (3) Δ > 0. Returns the supplied Δ for use inline.
* max > min, and (3) Δ > 0. Returns the supplied Δ for use inline.
...
@@ -107,8 +112,9 @@ public class Sequences {
...
@@ -107,8 +112,9 @@ public class Sequences {
return
Δ
;
return
Δ
;
}
}
// TODO docs; consider moving to cumulate method in data/sequence package;
/**
// not necessarily MFD specific
* Create a new XySequence with reverse cumulative values.
*/
public
static
XySequence
toCumulative
(
XySequence
incremental
)
{
public
static
XySequence
toCumulative
(
XySequence
incremental
)
{
MutableXySequence
cumulative
=
MutableXySequence
.
copyOf
(
incremental
);
MutableXySequence
cumulative
=
MutableXySequence
.
copyOf
(
incremental
);
double
sum
=
0.0
;
double
sum
=
0.0
;
...
@@ -216,32 +222,12 @@ public class Sequences {
...
@@ -216,32 +222,12 @@ public class Sequences {
:
sequence
;
:
sequence
;
}
}
/*
* TODO clean
*
* Method rounds (max-min)/Δ to 1e-6 before casting to an integer to
* determine the number of elements in the sequence. If (max-min)/Δ is
* reasonably close to an integer but max is off by some double-precision
* error (e.g. 6.7999999999), then max of 6.8 will be the upper end of the
* sequence.
*/
// private static double[] buildSequence(double min, double max, double Δ) {
// checkSequenceParameters(min, max, Δ);
// int size = ((int) Maths.round(((max - min) / Δ), 6)) + 1;
// checkArgument(Range.openClosed(0, 100000).contains(size), "sequence
// size");
// return IntStream.range(0, size)
// .mapToDouble(i -> min + Δ * i)
// .toArray();
// }
private
static
double
[]
buildSequence
(
double
min
,
double
Δ
,
int
size
)
{
private
static
double
[]
buildSequence
(
double
min
,
double
Δ
,
int
size
)
{
checkArgument
(
Range
.
openClosed
(
0
,
100000
).
contains
(
size
),
"sequence size"
);
checkArgument
(
Range
.
openClosed
(
0
,
100000
).
contains
(
size
),
"sequence size"
);
return
IntStream
.
range
(
0
,
size
)
return
IntStream
.
range
(
0
,
size
)
.
mapToDouble
(
i
->
min
+
Δ
*
i
)
.
mapToDouble
(
i
->
min
+
Δ
*
i
)
.
toArray
();
.
toArray
();
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
src/test/java/gov/usgs/earthquake/nshmp/data/SequencesTests.java
+
84
−
0
View file @
8caf8fa7
package
gov.usgs.earthquake.nshmp.data
;
package
gov.usgs.earthquake.nshmp.data
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertArrayEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertArrayEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertDoesNotThrow
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertSame
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Optional
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
...
@@ -12,6 +15,76 @@ import com.google.common.primitives.Doubles;
...
@@ -12,6 +15,76 @@ import com.google.common.primitives.Doubles;
class
SequencesTests
{
class
SequencesTests
{
@Test
final
void
validateArraysTests
()
{
// empty xs array
assertThrows
(
IllegalArgumentException
.
class
,
()
->
{
Sequences
.
validateArrays
(
new
double
[
0
],
new
double
[
0
]);
});
// xs and ys different sizes
assertThrows
(
IllegalArgumentException
.
class
,
()
->
{
Sequences
.
validateArrays
(
new
double
[
2
],
new
double
[
3
]);
});
// xs not monotonic
assertThrows
(
IllegalArgumentException
.
class
,
()
->
{
Sequences
.
validateArrays
(
new
double
[]
{
1
,
0
},
new
double
[
2
]);
});
// xs not monotonic
assertDoesNotThrow
(()
->
{
Sequences
.
validateArrays
(
new
double
[]
{
0
,
1
},
new
double
[
2
]);
});
// single values makes it through monotonic test
assertDoesNotThrow
(()
->
{
Sequences
.
validateArrays
(
new
double
[
1
],
new
double
[
1
]);
});
}
@Test
final
void
trimTests
()
{
double
[]
xs1
=
new
double
[
1
];
double
[]
ys1
=
new
double
[
1
];
double
[]
xs3
=
new
double
[]
{
1
,
2
,
3
};
double
[]
ys3zero
=
new
double
[
3
];
double
[]
ys3ones
=
new
double
[]
{
1
,
1
,
1
};
// size and clear check
ArrayXySequence
xy1
=
(
ArrayXySequence
)
XySequence
.
create
(
xs1
,
ys1
);
assertSame
(
xy1
,
Sequences
.
trim
(
xy1
));
ArrayXySequence
xy3a
=
(
ArrayXySequence
)
XySequence
.
create
(
xs3
,
ys3zero
);
assertSame
(
xy3a
,
Sequences
.
trim
(
xy3a
));
// start and end y values > 0
ArrayXySequence
xy3b
=
(
ArrayXySequence
)
XySequence
.
create
(
xs3
,
ys3ones
);
assertSame
(
xy3b
,
Sequences
.
trim
(
xy3b
));
double
[]
xs4toTrim
=
new
double
[]
{
1
,
2
,
3
,
4
};
double
[]
ys4toTrim
=
new
double
[]
{
1
,
1
,
1
,
0
};
double
[]
xs4expect
=
new
double
[]
{
1
,
2
,
3
};
double
[]
ys4expect
=
new
double
[]
{
1
,
1
,
1
};
// extra test that gets upper end y = 0
ArrayXySequence
xy4toTrim
=
(
ArrayXySequence
)
XySequence
.
create
(
xs4toTrim
,
ys4toTrim
);
ArrayXySequence
xy4expect
=
(
ArrayXySequence
)
XySequence
.
create
(
xs4expect
,
ys4expect
);
assertEquals
(
xy4expect
,
Sequences
.
trim
(
xy4toTrim
));
double
[]
xs5toTrim
=
new
double
[]
{
1
,
2
,
3
,
4
,
5
};
double
[]
ys5toTrim
=
new
double
[]
{
0
,
1
,
1
,
1
,
0
};
double
[]
xs5expect
=
new
double
[]
{
2
,
3
,
4
};
double
[]
ys5expect
=
new
double
[]
{
1
,
1
,
1
};
// immutable trimmed
ArrayXySequence
xy5toTrim
=
(
ArrayXySequence
)
XySequence
.
create
(
xs5toTrim
,
ys5toTrim
);
ArrayXySequence
xy5expect
=
(
ArrayXySequence
)
XySequence
.
create
(
xs5expect
,
ys5expect
);
assertEquals
(
xy5expect
,
Sequences
.
trim
(
xy5toTrim
));
// mutable trimmed
MutableArrayXySequence
xy5toTrimMutable
=
(
MutableArrayXySequence
)
MutableXySequence
.
create
(
xs5toTrim
,
Optional
.
of
(
ys5toTrim
));
assertEquals
(
xy5expect
,
Sequences
.
trim
(
xy5toTrimMutable
));
}
@Test
@Test
final
void
nonZeroIndexTests
()
{
final
void
nonZeroIndexTests
()
{
double
[]
values
=
new
double
[]
{
0.0
,
0.0
,
1.0
,
1.0
,
0.0
,
0.0
};
double
[]
values
=
new
double
[]
{
0.0
,
0.0
,
1.0
,
1.0
,
0.0
,
0.0
};
...
@@ -25,6 +98,17 @@ class SequencesTests {
...
@@ -25,6 +98,17 @@ class SequencesTests {
assertEquals
(-
1
,
Sequences
.
lastNonZeroIndex
(
zeros
),
0.0
);
assertEquals
(-
1
,
Sequences
.
lastNonZeroIndex
(
zeros
),
0.0
);
}
}
@Test
final
void
toCumulativeTests
()
{
double
[]
xs
=
new
double
[]
{
1
,
2
,
3
,
4
,
5
};
double
[]
ys
=
new
double
[]
{
1
,
1
,
1
,
1
,
1
};
double
[]
ysExpect
=
new
double
[]
{
5
,
4
,
3
,
2
,
1
};
XySequence
xyActual
=
Sequences
.
toCumulative
(
XySequence
.
create
(
xs
,
ys
));
XySequence
xyExpect
=
XySequence
.
create
(
xs
,
ysExpect
);
assertEquals
(
xyExpect
,
xyActual
);
}
@Test
@Test
final
void
arrayBuilderTests
()
{
final
void
arrayBuilderTests
()
{
...
...
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