Example #1
0
def test_set_get_user_transform_do_set_something():
    model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
    vtk_matrix = vtk.vtkMatrix4x4()
    vtk_matrix.Identity()
    vtk_matrix.SetElement(0, 0, 2)  # i.e. not identity
    model.set_user_matrix(vtk_matrix)
    result = model.get_user_matrix()
    assert result is not None
    assert result == vtk_matrix
Example #2
0
def test_get_set_visibility():
    input_file = 'tests/data/models/liver.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    assert (isinstance(model.get_visibility(), bool))
    assert (model.get_visibility())
    model.set_visibility(False)
    assert (not model.get_visibility())
    model.set_visibility(True)
    assert (model.get_visibility())
Example #3
0
def test_set_model_transform_is_used():
    model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
    vtk_matrix = vtk.vtkMatrix4x4()
    vtk_matrix.Identity()
    vtk_matrix.SetElement(0, 0, 2)  # i.e. not identity
    model.set_model_transform(vtk_matrix)
    result = model.get_model_transform()
    assert result is not None
    assert result.GetElement(0, 0) == vtk_matrix.GetElement(0, 0)
Example #4
0
def test_valid_set_texture_with_jpg_format(vtk_overlay_with_gradient_image):
    input_file = 'tests/data/models/liver.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    model.set_texture('tests/data/images/image0232.jpg')
    image, widget, _, app = vtk_overlay_with_gradient_image
    widget.add_vtk_actor(model.actor)
    widget.show()
    #app.exec_()

    return model
def test_set_texture_regression(vtk_overlay_with_gradient_image):

    in_github_ci = os.environ.get('CI')
    in_gitlab_ci = os.environ.get('GITLAB_CI')
    print("Gitlab_CI: " + str(in_gitlab_ci))
    print("Github CI: " + str(in_github_ci))

    if sys.platform == "darwin":
        pytest.skip("Test not working on Mac runner \
                    because the widget size is different")

    if in_github_ci and sys.platform.startswith("linux"):
        pytest.skip("Test not working on Linux runner \
                    because of unknown issue, see #60.")

    if in_github_ci and sys.platform.startswith("win"):
        pytest.skip("Skip on Windows on GitHub CI (use of MESA messes up \
                     result")

    input_file = 'tests/data/models/liver.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    model.set_texture('tests/data/images/image0232.png')
    image, widget, _, _, app = vtk_overlay_with_gradient_image
    widget.resize(400, 400)
    widget.add_vtk_actor(model.actor)

    widget.show()

    # Read the saved scene and compare it with the current scene.
    screenshot_filename = 'tests/data/images/set_texture_test.png'
    screenshot = cv2.imread(screenshot_filename)
    # OpenCV uses BGR while VTK uses RGB.
    screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB)

    current_scene = widget.convert_scene_to_numpy_array()

    tmp_dir = 'tests/output'
    if not os.path.isdir(tmp_dir):
        os.makedirs(tmp_dir)
    cv2.imwrite(os.path.join(tmp_dir, 'screenshot.png'), screenshot)
    cv2.imwrite(os.path.join(tmp_dir, 'current_scene.png'), current_scene)

    # As the rendered images in Ubuntu, Mac and Windows are different,
    # i.e., the pixel values are slightly different at the same location,
    # we add some threshold for comparison.
    # It checks if the number of values (in any channel)
    # that are different by more than 3 is less than 5 per cent
    # of the total number of pixels in the image.

    diff = abs(screenshot - current_scene)

    assert (np.sum(
        (diff > 3).astype(int)) / (screenshot.shape[0] * screenshot.shape[1]) *
            screenshot.shape[2]) < 0.05
Example #6
0
def test_extract_points_and_normals_as_numpy_array():
    input_file = 'tests/data/models/Prostate.vtk'
    model = VTKSurfaceModel(input_file, colors.red)
    number_of_points = model.get_number_of_points()
    points = model.get_points_as_numpy()
    assert isinstance(points, np.ndarray)
    assert points.shape[0] == number_of_points
    assert points.shape[1] == 3
    normals = model.get_normals_as_numpy()
    assert isinstance(normals, np.ndarray)
    assert normals.shape[0] == number_of_points
    assert normals.shape[1] == 3
Example #7
0
def test_set_texture_regression(vtk_overlay_with_gradient_image):

    in_github_ci = os.environ.get('CI')
    in_gitlab_ci = os.environ.get('GITLAB_CI')
    print("Gitlab_CI: " + str(in_gitlab_ci))
    print("Github CI: " + str(in_github_ci))

    if sys.platform == "darwin":
        pass
        #pytest.skip("Test not working on Mac runner \
        #           because the widget size is different")

    if in_github_ci and sys.platform.startswith("linux"):
        pass
    #pytest.skip("Test not working on Linux runner \
    #                because of unknown issue, see #60.")

    if in_github_ci and sys.platform.startswith("win"):
        pass
    #pytest.skip("Skip on Windows on GitHub CI (use of MESA messes up \
    #                 result")

    input_file = 'tests/data/models/liver.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    model.set_texture('tests/data/images/image0232.png')
    image, widget, _, app = vtk_overlay_with_gradient_image
    widget.resize(400, 400)
    widget.add_vtk_actor(model.actor)

    widget.show()

    # Read the saved scene and compare it with the current scene.
    screenshot_filename = 'tests/data/images/set_texture_test.png'
    screenshot = cv2.imread(screenshot_filename)
    # OpenCV uses BGR while VTK uses RGB.
    screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB)

    current_scene = widget.convert_scene_to_numpy_array()

    tmp_dir = 'tests/output'
    if not os.path.isdir(tmp_dir):
        os.makedirs(tmp_dir)
    cv2.imwrite(os.path.join(tmp_dir, 'screenshot.png'), screenshot)
    cv2.imwrite(os.path.join(tmp_dir, 'current_scene.png'), current_scene)

    # As the rendered images in Ubuntu, Mac and Windows are different, we'll
    # use the are similar function from sksurgery image

    assert are_similar(screenshot,
                       current_scene,
                       threshold=0.995,
                       metric=cv2.TM_CCOEFF_NORMED,
                       mean_threshold=0.005)
Example #8
0
def test_regression_test1():
    """A regression test with manually init"""
    parameters = [136.0, 151.0, -91.0, 8.0]
    model = VTKSurfaceModel("data/CT_Level_1.vtp", [1., 0., 0.])
    x_values = model.get_points_as_numpy()[:, 0]
    y_values = model.get_points_as_numpy()[:, 1]
    z_values = model.get_points_as_numpy()[:, 2]
    result = sphere_fitting.fit_sphere_least_squares(x_values, y_values,
                                                     z_values, parameters)
    numpy.testing.assert_approx_equal(result.x[0], 136.571, significant=3)
    numpy.testing.assert_approx_equal(result.x[1], 151.973, significant=3)
    numpy.testing.assert_approx_equal(result.x[2], -95.518, significant=3)
    numpy.testing.assert_approx_equal(result.x[3], 8.119, significant=3)
def run_demo(model_file_name, output="", configfile=False):
    """ Run the application """
    model = VTKSurfaceModel(model_file_name, [1., 0., 0.])
    x_values = model.get_points_as_numpy()[:, 0]
    y_values = model.get_points_as_numpy()[:, 1]
    z_values = model.get_points_as_numpy()[:, 2]

    initial_parameters = [
        mean(x_values),
        mean(y_values),
        mean(z_values),
        std(x_values)
    ]
    bounds = ((-inf, -inf, -inf, -inf), (inf, inf, inf, inf))

    if configfile:
        configurer = ConfigurationManager(configfile)
        configuration = configurer.get_copy()
        if "initial values" in configuration:
            initial_parameters = configuration.get("initial values")
        if "bounds" in configuration:
            bounds = configuration.get("bounds")
        if "fixed radius" in configuration:
            radius = configuration.get("fixed radius")
            bounds = ((-inf, -inf, -inf, radius - 1e-6), (inf, inf, inf,
                                                          radius + 1e-6))

    result = sphere_fitting.fit_sphere_least_squares(x_values,
                                                     y_values,
                                                     z_values,
                                                     initial_parameters,
                                                     bounds=bounds)

    print("Result is {}".format(result))

    if output != "":

        sphere = vtk.vtkSphereSource()
        sphere.SetCenter(result.x[0], result.x[1], result.x[2])
        sphere.SetRadius(result.x[3])
        sphere.SetThetaResolution(60)
        sphere.SetPhiResolution(60)

        writer = vtk.vtkXMLPolyDataWriter()
        writer.SetFileName(output)
        writer.SetInputData(sphere.GetOutput())
        sphere.Update()
        writer.Write()
Example #10
0
def test_valid_set_texture_with_png_format(vtk_overlay_with_gradient_image):
    input_file = 'tests/data/models/liver.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    model.set_texture('tests/data/images/image0232.png')
    image, widget, _, app = vtk_overlay_with_gradient_image
    widget.add_vtk_actor(model.actor)
    widget.show()

    # Save the scene to a file for parity check.
    # See test_set_texture_regression() below.
    # This line should be run again if the code is (purposefully) changed.
    #screenshot_filename = 'tests/data/images/set_texture_test.png'
    #widget.save_scene_to_file(screenshot_filename)
    #app.exec_()

    return model
Example #11
0
def make_source_and_target():
    """
    Helper to make a source and target.
    """
    filename = "data/liverphantom-iso=-130_cleaned2_mc_smooth2.stl"
    colour = [1.0, 1.0, 1.0]
    source_poly = VTKSurfaceModel(filename, colour)

    target_points = source_poly.source.GetPoints()

    target_vtk_poly = vtkPolyData()
    target_vtk_poly.SetPoints(target_points)
    return source_poly.source, target_vtk_poly
Example #12
0
def test_regression_test2():
    """A regression test with fixed radius"""
    model = VTKSurfaceModel("data/US_Sphere_2.vtp", [1., 0., 0.])
    x_values = model.get_points_as_numpy()[:, 0]
    y_values = model.get_points_as_numpy()[:, 1]
    z_values = model.get_points_as_numpy()[:, 2]
    parameters = [
        numpy.mean(x_values),
        numpy.mean(y_values),
        numpy.mean(z_values), 4.5
    ]
    radius = 4.5
    epsilon = 1e-6
    bounds = ((-numpy.inf, -numpy.inf, -numpy.inf, radius - epsilon),
              (numpy.inf, numpy.inf, numpy.inf, radius + epsilon))
    result = sphere_fitting.fit_sphere_least_squares(x_values,
                                                     y_values,
                                                     z_values,
                                                     parameters,
                                                     bounds=bounds)
    numpy.testing.assert_approx_equal(result.x[0], 86.474, significant=3)
    numpy.testing.assert_approx_equal(result.x[1], -97.243, significant=3)
    numpy.testing.assert_approx_equal(result.x[2], -208.528, significant=3)
    numpy.testing.assert_approx_equal(result.x[3], 4.500, significant=3)
Example #13
0
def test_flat_shaded_on_coloured_background(setup_vtk_overlay_window):
    #input_file = 'tests/data/models/liver.ply' # Don't use this one. It renders Grey, regardless of what colour you create it at.
    input_file = 'tests/data/liver/liver_sub.ply'
    model = VTKSurfaceModel(input_file, colors.white)
    widget, _, app = setup_vtk_overlay_window
    widget.add_vtk_actor(model.actor)
    model.set_no_shading(True)
    widget.background_renderer.SetBackground(0, 0, 1)
    widget.show()
    model.set_no_shading(False)
    widget.background_renderer.SetBackground(0, 1, 0)
    widget.show()
Example #14
0
def test_invalid_because_color_blue_not_float():
    with pytest.raises(TypeError):
        VTKSurfaceModel('tests/data/models/Prostate.vtk', (0.0, 0.0, 1))
Example #15
0
def test_invalid_because_color_green_too_low():
    with pytest.raises(ValueError):
        VTKSurfaceModel('tests/data/models/Prostate.vtk', (0.0, -1.0, 0.0))
Example #16
0
def test_its_valid_for_null_filename():
    model = VTKSurfaceModel(None, colors.red)
    assert model.source is not None
Example #17
0
def test_invalid_because_filename_invalid():
    with pytest.raises(TypeError):
        VTKSurfaceModel(9, colors.red)
Example #18
0
def test_invalid_because_visibility_not_bool():
    with pytest.raises(TypeError):
        VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0),
                        visibility=1.0)
Example #19
0
def test_invalid_because_model_file_format():
    input_file = 'tox.ini'
    with pytest.raises(ValueError):
        VTKSurfaceModel(input_file, colors.red)
Example #20
0
def test_invalid_because_pickable_not_bool():
    with pytest.raises(TypeError):
        VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0),
                        pickable=1.0)
Example #21
0
def test_ensure_setter_and_getter_set_something():
    model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
    assert model.get_name() == ""
    model.set_name("banana")
    assert model.get_name() == "banana"
Example #22
0
def test_valid_stl_results_in_vtkstlreader():
    input_file = 'tests/data/models/Fiducial.stl'
    model = VTKSurfaceModel(input_file, colors.red)
    assert isinstance(model.reader, vtk.vtkSTLReader)
Example #23
0
def test_invalid_set_texture_because_texture_filename_empty():
    input_file = 'tests/data/models/liver.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    with pytest.raises(ValueError):
        model.set_texture('')
Example #24
0
def test_invalid_set_texture_because_texture_file_format():
    input_file = 'tests/data/models/liver.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    texture_file = 'tox.ini'
    with pytest.raises(ValueError):
        model.set_texture(texture_file)
Example #25
0
def test_invalid_because_name_is_none():
    with pytest.raises(TypeError):
        model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
        model.set_name(None)
Example #26
0
def test_invalid_because_name_is_empty():
    with pytest.raises(ValueError):
        model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
        model.set_name("")
Example #27
0
def test_invalid_because_color_blue_too_high():
    with pytest.raises(ValueError):
        VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.1))
Example #28
0
def test_valid_ply_results_in_vtkplyreader():
    input_file = 'tests/data/models/Tumor.ply'
    model = VTKSurfaceModel(input_file, colors.red)
    assert isinstance(model.reader, vtk.vtkPLYReader)
Example #29
0
def test_invalid_because_opacity_not_float():
    with pytest.raises(TypeError):
        VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0),
                        opacity=1)
Example #30
0
def valid_vtk_model():
    input_file = 'tests/data/models/Prostate.vtk'
    model = VTKSurfaceModel(input_file, colors.red)
    return model