In: Computer Science
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote.
Implement a class to perform windowing of a Hounsfield value
Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been done for you. You must carefully study the API of the class to determine the precise signatures of the constructors and methods that you need to implement. Furthermore, you need to thoroughly understand the concept of windowing to determine what fields are required in your class.
Remember to run the JUnit tester each time you complete a constructor or method, and to carefully study the result of the tests to help you through the development process.
API doc: https://drive.google.com/open?id=1GKm_m74PwFBZIC__JmnWnGi6cFzmGuul
/**
* A class that represents a windowed view of Hounsfield units. A
Hounsfield
* window is defined by two values: (1) the window level, and (2)
the window
* width. The window level is the Hounsfield unit value that the
window is
* centered on. The window width is the range of Hounsfield unit
values that the
* window is focused on.
*
*
* A window has a lower and upper bound. The lower bound is defined
as the
* window level minus half the window width:
*
*
* lo = level - (width / 2)
*
*
* The upper bound is defined as the window level plus half the
window width:
*
*
* hi = level + (width / 2)
*
*
* Hounsfield units are mapped by the window to a real number in the
range of
* {@code 0} to {@code 1}. A Hounsfield unit with a value less than
lo is mapped
* to the value {@code 0}. A Hounsfield unit with a value greater
than hi is
* mapped to the value {@code 1}. A Hounsfield unit with a value v
between lo
* and hi is mapped to the value:
*
*
* (v - lo) / width
*
*
*/
public class HounsfieldWindow {
private int level;
private int width;
HounsfieldWindow(int level, int width) {
setLevel(level);
this.width = width;
}
public HounsfieldWindow() {
this(0, 400);
}
public int getLevel() {
return level;
}
public int setLevel(int level) {
if (level < Hounsfield.MIN_VALUE
|| level > Hounsfield.MAX_VALUE) {
throw new
IllegalArgumentException();
}
int data = this.level;
this.level = level;
return data;
}
public int getWidth() {
return width;
}
public int setWidth(int width) {
if(width < 1) {
throw new
IllegalArgumentException();
}
int data = this.width;
this.width = width;
return data;
}
public double getLowerBound() {
return level - (width / 2);
}
public double getUpperBound() {
return level + (width / 2);
}
public double map(Hounsfield h) {
// TODO Auto-generated method
stub
int hounsfieldUnitVal =
h.get();
System.out.println("got " + hounsfieldUnitVal);
if(hounsfieldUnitVal < getLowerBound()) {
return 0;
}
if(hounsfieldUnitVal > getUpperBound()) {
return 1;
}
return (hounsfieldUnitVal - getLowerBound()) /
(double)width;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Tests that fails:
@Test
public void test04_ctorThrowsOnBadWidth() {
final int[] BAD_WIDTHS = {-10000,
-10, 0};
for (int width : BAD_WIDTHS)
{
try {
new HounsfieldWindow(0, width);
fail(String.format("new HounsfieldWindow(0, %s)
should throw an exception", width));
}
catch
(IllegalArgumentException x) {
// ok
}
}
}
@Test
public void test10_map() {
assumeTrue("test requires a correct
implementation of HounsfieldWindow(int, int)", IS_CTOR_OK);
// uses windows of width 1
// easy to test because map should
always return 0.5 for
// Hounsfield values inside the
window
final int width = 1;
for (int level =
Hounsfield.MIN_VALUE; level <= Hounsfield.MAX_VALUE; level++)
{
// make a window
to call map on
HounsfieldWindow
w = new HounsfieldWindow(level, width);
// the actual
value returned by map
double got =
w.map(new Hounsfield(level));
// make an
error message in case the test fails
String error =
String.format(
"map(%s) failed to return the
correct value for a window with level %s, width %s", level,
level,
width);
// assert
that 0.5 equals got
assertEquals(error, 0.5, got, 1e-9);
}
}
@Test
public void test11_map() {
assumeTrue("test requires a correct
implementation of HounsfieldWindow(int, int)", IS_CTOR_OK);
// tests Hounsfield units in
windows of various widths and levels
final int[] WIDTH = {2, 3, 4, 5,
10, 25, 50, 75, 100, 255};
final int[] LEVEL = {-800, -1, 1,
750};
for (int level : LEVEL) {
for (int width :
WIDTH) {
// make a window to call map on
HounsfieldWindow w = new HounsfieldWindow(level,
width);
// expected values map should return
double[] EXP =
HounsfieldWindowTest.mapValues(width);
for (int i = 0; i < EXP.length; i++) {
// Hounsfield unit to
map
Hounsfield h = new
Hounsfield(level - width / 2 + i);
// the expected return value
of map(h)
double exp = EXP[i];
// the actual value returned
by map(h)
double got = w.map(h);
// make an error message in
case the test fails
String error =
String.format(
"map(%s) failed to return the correct value for
a window with level %s, width %s", h.get(), level,
width);
// assert that exp equals
got
assertEquals(error, exp, got,
1e-9);
}
}
}
}
Relevant files(Hounsfield.java and Hounsfieldtest.java): https://drive.google.com/open?id=1HRUH5bika5xeLO7G_kP2LeMxNeXejRgl
You have not provide the Hounsfield class. Because of that I
could not run the code. However I have gone through the class and
fixed the code which may be causing the junits to fail.
I have fixed the 2 arg constructor to call setWidth() and also
using 2.0 in getLowerBound() and getUpperBound(). Rest of the code
seems to be correct.
Can you please check with the updated code given below and let me know through comments if you still see some issues. In case of issues, please provide the Hounsfield class in your google drive and post a comment. I will reply back to you. If the answer helped, please do rate it. Thank you.
/**
* A class that represents a windowed view of Hounsfield units. A
Hounsfield
* window is defined by two values: (1) the window level, and (2)
the window
* width. The window level is the Hounsfield unit value that the
window is
* centered on. The window width is the range of Hounsfield unit
values that the
* window is focused on.
*
*
* A window has a lower and upper bound. The lower bound is defined
as the
* window level minus half the window width:
*
*
* lo = level - (width / 2)
*
*
* The upper bound is defined as the window level plus half the
window width:
*
*
* hi = level + (width / 2)
*
*
* Hounsfield units are mapped by the window to a real number in the
range of
* {@code 0} to {@code 1}. A Hounsfield unit with a value less than
lo is mapped
* to the value {@code 0}. A Hounsfield unit with a value greater
than hi is
* mapped to the value {@code 1}. A Hounsfield unit with a value v
between lo
* and hi is mapped to the value:
*
*
* (v - lo) / width
*
*
*/
public class HounsfieldWindow {
private int level;
private int width;
HounsfieldWindow(int level, int width) {
setLevel(level);
setWidth(width);
}
public HounsfieldWindow() {
this(0, 400);
}
public int getLevel() {
return level;
}
public int setLevel(int level) {
if (level < Hounsfield.MIN_VALUE
|| level > Hounsfield.MAX_VALUE) {
throw new
IllegalArgumentException();
}
int data = this.level;
this.level = level;
return data;
}
public int getWidth() {
return width;
}
public int setWidth(int width) {
if(width < 1) {
throw new
IllegalArgumentException();
}
int data = this.width;
this.width = width;
return data;
}
public double getLowerBound() {
return level - (width / 2.0);
}
public double getUpperBound() {
return level + (width /
2.0));
}
public double map(Hounsfield h) {
// TODO Auto-generated method
stub
int hounsfieldUnitVal =
h.get();
System.out.println("got " +
hounsfieldUnitVal);
if(hounsfieldUnitVal <
getLowerBound()) {
return 0;
}
if(hounsfieldUnitVal >
getUpperBound()) {
return 1;
}
return (hounsfieldUnitVal -
getLowerBound()) / width;
}
}