Skip to content
Snippets Groups Projects
Select Git revision
  • 86b9768864e99e6f604d424d08ef835d45a99ef7
  • master default protected
  • v2.0-dev protected
  • zikeliml/Task-96-dotExporterForAST
  • zikeliml/124-rework-tutorials
  • fma
  • fhennig/v2.0-deprecations
  • holzer-master-patch-46757
  • 66-absolute-access-is-probably-not-copied-correctly-after-_eval_subs
  • gpu_bufferfield_fix
  • hyteg
  • vectorization_sqrt_fix
  • target_dh_refactoring
  • const_fix
  • improved_comm
  • gpu_liveness_opts
  • release/1.3.7 protected
  • release/1.3.6 protected
  • release/2.0.dev0 protected
  • release/1.3.5 protected
  • release/1.3.4 protected
  • release/1.3.3 protected
  • release/1.3.2 protected
  • release/1.3.1 protected
  • release/1.3 protected
  • release/1.2 protected
  • release/1.1.1 protected
  • release/1.1 protected
  • release/1.0.1 protected
  • release/1.0 protected
  • release/0.4.4 protected
  • last/Kerncraft
  • last/OpenCL
  • last/LLVM
  • release/0.4.3 protected
  • release/0.4.2 protected
36 results

test_field_coordinates.py

Blame
  • test_field_coordinates.py 2.65 KiB
    # -*- coding: utf-8 -*-
    #
    # Copyright © 2019 Stephan Seitz <stephan.seitz@fau.de>
    #
    # Distributed under terms of the GPLv3 license.
    
    """
    
    """
    from os.path import dirname, join
    
    import numpy as np
    import sympy
    
    import pystencils
    from pystencils.interpolation_astnodes import LinearInterpolator
    
    try:
        import pyconrad.autoinit
    except Exception:
        import unittest.mock
        pyconrad = unittest.mock.MagicMock()
    
    LENNA_FILE = join(dirname(__file__), 'test_data', 'lenna.png')
    
    try:
        import skimage.io
        lenna = skimage.io.imread(LENNA_FILE, as_gray=True).astype(np.float32)
    except Exception:
        lenna = np.random.rand(20, 30)
    
    
    def test_rotate_center():
        x, y = pystencils.fields('x, y:  float32[2d]')
    
        # Rotate around center when setting coordindates origins to field centers
        x.set_coordinate_origin_to_field_center()
        y.set_coordinate_origin_to_field_center()
    
        rotation_angle = sympy.pi / 5
        transform_matrix = sympy.rot_axis3(rotation_angle)[:2, :2]
    
        # Generic matrix transform works like that (for rotation it would be more clever to use transform_matrix.T)
        inverse_matrix = transform_matrix.inv()
        input_coordinate = x.physical_to_index(inverse_matrix @ y.physical_coordinates)
    
        assignments = pystencils.AssignmentCollection({
            y.center(): LinearInterpolator(x).at(input_coordinate)
        })
    
        kernel = pystencils.create_kernel(assignments).compile()
        rotated = np.zeros_like(lenna)
    
        kernel(x=lenna, y=rotated)
    
        pyconrad.imshow(lenna, "lenna")
        pyconrad.imshow(rotated, "rotated")
    
        # If distance in input field is twice as close we will see a smaller image
        x.coordinate_transform /= 2
    
        input_coordinate = x.physical_to_index(inverse_matrix @ y.physical_coordinates)
    
        assignments = pystencils.AssignmentCollection({
            y.center(): LinearInterpolator(x).at(input_coordinate)
        })
    
        kernel = pystencils.create_kernel(assignments).compile()
        rotated = np.zeros_like(lenna)
    
        kernel(x=lenna, y=rotated)
    
        pyconrad.imshow(rotated, "rotated smaller")
    
        # Conversely, if output field has samples 3 times closer we will see a bigger image
        y.coordinate_transform /= 3
    
        input_coordinate = x.physical_to_index(inverse_matrix @ y.physical_coordinates)
    
        assignments = pystencils.AssignmentCollection({
            y.center(): LinearInterpolator(x).at(input_coordinate)
        })
    
        kernel = pystencils.create_kernel(assignments).compile()
        rotated = np.zeros_like(lenna)
    
        kernel(x=lenna, y=rotated)
    
        pyconrad.imshow(rotated, "rotated bigger")
    
        # coordinate_transform can be any matrix, also with symbols as entries
    
    
    def main():
        test_rotate_center()
    
    
    if __name__ == '__main__':
        main()