def test_is_a_grid(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) assert point_list.is_a_grid( resolution=sample_logs_mock['resolution']) is False regular_point_list = point_list.grid_point_list( resolution=sample_logs_mock['resolution']) assert regular_point_list.is_a_grid( resolution=sample_logs_mock['resolution']) is True
def test_coordinates_along_direction(self): xyz = [[0, 1], [2, 3], [4, 5]] point_list = PointList(xyz) for r, index, label in zip(xyz, range(3), ('vx', 'vy', 'vz')): assert point_list.coordinates_along_direction( index) == pytest.approx(r) assert point_list.coordinates_along_direction( label) == pytest.approx(r)
def test_has_overlapping_points(self): xyz = [[0.0, 1.0, 2.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] assert PointList(xyz).has_overlapping_points( resolution=DEFAULT_POINT_RESOLUTION) is False xyz = [[0.0, 1.0, 2.0, 2.009], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]] assert PointList(xyz).has_overlapping_points( resolution=DEFAULT_POINT_RESOLUTION) is True
def test_fuse_with(self): xyz1 = [[0.0, 1.0, 2.0, 3.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]] xyz2 = [[1.009, 0.995, 2.0, 3.005, 4.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]] point_list = PointList(xyz1) common = point_list.fuse_with(PointList(xyz2), resolution=DEFAULT_POINT_RESOLUTION) assert common.vx == pytest.approx([0.0, 1.0, 2.0, 3.0, 4.0])
def test_intersection(self): xyz1 = [[0.0, 1.0, 2.0, 3.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]] xyz2 = [[1.009, 0.995, 2.0, 3.005, 4.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]] common = PointList(xyz1).intersection( PointList(xyz2), resolution=DEFAULT_POINT_RESOLUTION) assert common.vx == pytest.approx( [1.0, 2.0, 3.0, 1.009, 0.995, 2.0, 3.005])
def test_aggregate(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']).aggregate( PointList(sample_logs_mock['xyz'])) np.testing.assert_equal( point_list.vx, sample_logs_mock['xyz'][0] + sample_logs_mock['xyz'][0]) np.testing.assert_equal( point_list.vy, sample_logs_mock['xyz'][1] + sample_logs_mock['xyz'][1]) np.testing.assert_equal( point_list.vz, sample_logs_mock['xyz'][2] + sample_logs_mock['xyz'][2])
def test_linspace(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) extents = sample_logs_mock['extents'] vx, vy, vz = point_list.linspace( resolution=sample_logs_mock['resolution']) assert [vx[0], vx[-1], (vx[-1] - vx[0]) / (len(vx) - 1)] == pytest.approx(extents[0]) assert len(vx) == 14 assert [vy[0], vy[-1], (vy[-1] - vy[0]) / (len(vy) - 1)] == pytest.approx(extents[1]) assert len(vy) == 4 # many values of point_list.vy are repeated assert [vz[0], vz[-1], (vz[-1] - vz[0]) / (len(vz) - 1)] == pytest.approx(extents[2]) assert len(vz) == 4 # many values of point_list.vz are repeated
def test_grid_point_list(self): r""" The regular grid spanned by the three orthonormal vectors is the unit cube, with eight points The order of the coordinates must follow this nested loop structure: for vx in ...: for vy in ...: for vz in ...: """ # Passing the orthonormal vectors along each direction as three points point_list = PointList([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) other_list = point_list.grid_point_list( resolution=DEFAULT_POINT_RESOLUTION) cube_coordinates = [[0., 0., 0.], [0., 0., 1.], [0., 1., 0.], [0., 1., 1.], [1., 0., 0.], [1., 0., 1.], [1., 1., 0.], [1., 1., 1.]] assert np.allclose(cube_coordinates, other_list.coordinates)
def wrapped_function(peaks_data): r""" Constructor of `StrainField` objects Parameters ---------- peaks_data: dict Returns ------- ~pyrs.dataobjects.fields.StrainField Examples -------- """ # Required arguments for required in ('subruns', 'wavelength', 'peak_profile', 'background_type', 'd_spacing', 'error_fraction', 'vx', 'vy', 'vz'): assert required in peaks_data for key in ('subruns', 'd_reference', 'd_spacing', 'fit_costs', 'vx', 'vy', 'vz'): if isinstance(peaks_data[key], (list, tuple)): peaks_data[key] = np.array(peaks_data[key]) for key in peaks_data['native']: peaks_data['native'][key] = _coerce_to_ndarray(peaks_data['native'][key]) # Default values for optional arguments of the PeakCollection constructor runnumber = peaks_data.get('runnumber', -1) peak_collection = PeakCollection(peaks_data['peak_tag'], peaks_data['peak_profile'], peaks_data['background_type'], peaks_data['wavelength'], runnumber=runnumber) # Back-calculate the peak centers from supplied lattice spacings centers = 2 * np.rad2deg(np.arcsin(peaks_data['wavelength'] / (2 * peaks_data['d_spacing']))) # Enter the native parameters in the peak collection subruns_count = len(peaks_data['subruns']) peaks_data['native'].update({'PeakCentre': centers}) dtype = get_parameter_dtype(peaks_data['peak_profile'], peaks_data['background_type']) parameters_value = np.zeros(subruns_count, dtype=dtype) parameters_error = np.zeros(subruns_count, dtype=dtype) for parameter_name in peaks_data['native']: values = peaks_data['native'][parameter_name] parameters_value[parameter_name] = values parameters_error[parameter_name] = peaks_data['error_fraction'] * values # set_peak_fitting_values ensures all required fitting parameters are being passed peak_collection.set_peak_fitting_values(peaks_data['subruns'], parameters_value, parameters_error, peaks_data['fit_costs']) # set the reference lattice spacing peak_collection.set_d_reference(1.0, 0.1) # Point list point_list = PointList([peaks_data['vx'], peaks_data['vy'], peaks_data['vz']]) return StrainFieldSingle(point_list=point_list, peak_collection=peak_collection)
def test_cluster(self): xyz = [[0.0, 1.0, 2.0, 3.0, 1.009, 0.995, 2.0, 3.005, 4.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] clusters = PointList(xyz).cluster(resolution=DEFAULT_POINT_RESOLUTION) for cluster, comparison in zip(clusters, [[1, 4, 5], [2, 6], [3, 7], [0], [8]]): assert cluster == pytest.approx(comparison)
def test_tolist(self, sample_logs_mock): for input_source in (sample_logs_mock['xyz'], sample_logs_mock['logs'], np.array(sample_logs_mock['xyz'])): list_of_list = PointList.tolist(input_source) assert isinstance(list_of_list, tuple) for i in range(3): assert isinstance(list_of_list[i], np.ndarray) np.testing.assert_equal(list_of_list[i], sample_logs_mock['xyz'][i])
def test_aggregate_point_list(sample_logs_mock): point_list = aggregate_point_lists(*[ PointList(sample_logs_mock['logs']) for _ in range(3) ]) # three lists list_x, list_y, list_z = sample_logs_mock['xyz'][0], sample_logs_mock[ 'xyz'][1], sample_logs_mock['xyz'][2] assert list(point_list.vx) == pytest.approx(list_x + list_x + list_x) assert list(point_list.vy) == pytest.approx(list_y + list_y + list_y) assert list(point_list.vz) == pytest.approx(list_z + list_z + list_z)
def test_getattr(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) assert list(point_list.vx) == pytest.approx(sample_logs_mock['xyz'][0]) assert list(point_list.vy) == pytest.approx(sample_logs_mock['xyz'][1]) try: _ = point_list.dummy # noqa F841 assert False, 'Should not have been able to access attribute' except AttributeError: pass # this is the correct behavior
def test_init(self, sample_logs_mock): for input_source_type in ('logs', 'xyz'): input_source = sample_logs_mock[input_source_type] point_list = PointList(input_source) # dereference attributes 'vx', 'vy', 'vz' np.testing.assert_equal(point_list.vx, sample_logs_mock['xyz'][0]) np.testing.assert_equal(point_list.vy, sample_logs_mock['xyz'][1]) np.testing.assert_equal(point_list.vz, sample_logs_mock['xyz'][2]) # dereference by item index xyz = np.array(sample_logs_mock['xyz']) assert point_list[5] == pytest.approx(list(xyz[:, 5]))
def test_coordinates_irreducible(self): xyz = [ [0.0, 1.000, 1.001, 2.0, 3.0, 4.0, 0.0, 1.0, 2.0, 3.0, 4.0], # x [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0], # y [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0] ] # z point_list = PointList(xyz) # Volume scan coordinates_irreducible = point_list.coordinates_irreducible( resolution=DEFAULT_POINT_RESOLUTION) assert coordinates_irreducible[0] == pytest.approx( [0.0, 0.0, 0.0]) # check first point assert coordinates_irreducible[-1] == pytest.approx( [4.0, 1.0, 1.0]) # check last point assert coordinates_irreducible.shape[ -1] == 3 # xyz if a volume scan, thus three dimensions assert coordinates_irreducible == pytest.approx( np.array(xyz).transpose()) # Surface scan xyz[2] = [0] * 11 point_list = PointList(xyz) coordinates_irreducible = point_list.coordinates_irreducible( resolution=DEFAULT_POINT_RESOLUTION) assert coordinates_irreducible[0] == pytest.approx( [0.0, 0.0]) # check first point assert coordinates_irreducible[-1] == pytest.approx( [4.0, 1.0]) # check last point assert coordinates_irreducible.shape[ -1] == 2 # xyz if a surface scan, thus two dimensions assert coordinates_irreducible == pytest.approx( np.array(xyz[0:2]).transpose()) # Linear scan xyz[1] = [0] * 11 point_list = PointList(xyz) coordinates_irreducible = point_list.coordinates_irreducible( resolution=DEFAULT_POINT_RESOLUTION) assert coordinates_irreducible[0] == pytest.approx( [0.0]) # check first point assert coordinates_irreducible[-1] == pytest.approx( [4.0]) # check last point assert coordinates_irreducible.shape[ -1] == 1 # xyz if a linear scan, thus one dimensions assert coordinates_irreducible == pytest.approx( np.array(xyz[0]).reshape((11, 1)))
def test_is_contained_in(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) subset = np.array( sample_logs_mock['xyz'])[:, 0: 5] # pick only the first five coordinates other_list = PointList(subset) assert other_list.is_contained_in(point_list) is True assert point_list.is_contained_in(other_list) is False
def test_linear_scan_vector(self): # vx and vy are only one point, within resolution xyz = [[0, 0, 0.003, 0.004, 0], [0, 1, 2, 3, 4], [1, 1.001, 1.002, 1.003, 1.009]] point_list = PointList(xyz) assert point_list.linear_scan_vector( resolution=DEFAULT_POINT_RESOLUTION) == pytest.approx([0, 1, 0]) xyz[0][ 0] = 0.011 # this point is beyond resolution with the rest of the other vx points point_list = PointList(xyz) assert point_list.linear_scan_vector( resolution=DEFAULT_POINT_RESOLUTION) is None
def test_is_equal_within_resolution(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) # perturb the positions of point_list within resolution original = np.array(sample_logs_mock['xyz']) perturbation = (sample_logs_mock['resolution'] / 3.0) * (np.random.random(original.shape) - 0.5) other_list = PointList(original + perturbation) assert point_list.is_equal_within_resolution( other_list, resolution=sample_logs_mock['resolution']) is True # perturb the very first coordinate too much perturbation[0][0] += 2 * sample_logs_mock['resolution'] other_list = PointList(original + perturbation) assert point_list.is_equal_within_resolution( other_list, resolution=sample_logs_mock['resolution']) is False
def strain_instantiator(name, values, errors, x, y, z): return StrainField(name, peak_collection=PeakCollectionLite(name, strain=values, strain_error=errors), point_list=PointList([x, y, z]))
def test_get_indices(self): x = [0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5] # x y = [1.0, 1.0, 1.5, 1.5, 1.0, 1.0, 1.5, 1.5] # y z = [2.0, 2.0, 2.0, 2.0, 2.5, 2.5, 2.5, 2.5] # z point_list1 = PointList([x, y, z]) # changes a single x-value in the last 4 points of point_list1 x = [0.0, 0.5, 0.0, 0.5, 1.0, 1.5, 1.0, 1.5] # x y = [1.0, 1.0, 1.5, 1.5, 1.0, 1.0, 1.5, 1.5] # y z = [2.0, 2.0, 2.0, 2.0, 2.5, 2.5, 2.5, 2.5] # z point_list2 = PointList([x, y, z]) # reverse order of point_list1 x = [0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0] y = [1.5, 1.5, 1.0, 1.0, 1.5, 1.5, 1.0, 1.0] z = [2.5, 2.5, 2.5, 2.5, 2.0, 2.0, 2.0, 2.0] point_list3 = PointList([x, y, z]) # last three sample points of point_list1 x = [0.5, 0.0, 0.5] # x y = [1.0, 1.5, 1.5] # y z = [2.5, 2.5, 2.5] # z point_list4 = PointList([x, y, z]) # indices with self are running numbers for point_list in (point_list1, point_list2, point_list3, point_list4): np.testing.assert_equal(point_list.get_indices(point_list), np.arange(len(point_list))) # reversing values gets decreasing numbers for forward, reverse in zip((point_list1, point_list3), (point_list3, point_list1)): np.testing.assert_equal(forward.get_indices(reverse), [7, 6, 5, 4, 3, 2, 1, 0]) # partial matching tests np.testing.assert_equal(point_list1.get_indices(point_list2), [0, 1, 2, 3, -1, -1, -1, -1]) np.testing.assert_equal(point_list3.get_indices(point_list2), [-1, -1, -1, -1, 3, 2, 1, 0]) np.testing.assert_equal(point_list4.get_indices(point_list1), [5, 6, 7]) np.testing.assert_equal(point_list4.get_indices(point_list2), [-1, -1, -1]) np.testing.assert_equal(point_list4.get_indices(point_list3), [2, 1, 0])
def test_sorted_indices(self): # The two point lists contain a different number of sample points point_list = PointList([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) point_list_other = PointList([[2.0, 0.005], [0.0, 0.0], [0.0, 0.0]]) with pytest.raises(AssertionError) as exception_info: point_list.sorted_indices(point_list=point_list_other, resolution=DEFAULT_POINT_RESOLUTION) assert 'point lists do not contain same number of sample points' in str( exception_info.value) # Corner case: one of the point lists contains overlapping points point_list = PointList([[0.0, 0.009, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) # three points, two overlap with pytest.raises(ValueError) as exception_info: point_list.sorted_indices(point_list=point_list, resolution=DEFAULT_POINT_RESOLUTION) assert 'point lists contains overlapping points' in str( exception_info.value) # The two point lists are not equal within resolution point_list = PointList([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) point_list_other = PointList([[0.0, 1.1, 2.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) with pytest.raises(ValueError) as exception_info: point_list.sorted_indices(point_list=point_list_other, resolution=DEFAULT_POINT_RESOLUTION) assert 'point lists are not the same, within resolution' in str( exception_info.value) # Compare against itself point_list = PointList([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) indices = point_list.sorted_indices( point_list=point_list, resolution=DEFAULT_POINT_RESOLUTION) np.testing.assert_equal(indices, np.array([0, 1, 2])) # General case point_list = PointList([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) point_list_other = PointList([[2.0, 0.005, 1.009], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) indices = point_list.sorted_indices( point_list=point_list_other, resolution=DEFAULT_POINT_RESOLUTION) np.testing.assert_equal(indices, np.array([1, 2, 0]))
def test_coordinates(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) np.testing.assert_allclose( point_list.coordinates, np.array(sample_logs_mock['xyz']).transpose())
def test_extents(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) for i, extent in enumerate( point_list.extents(resolution=DEFAULT_POINT_RESOLUTION)): assert list(extent) == pytest.approx( sample_logs_mock['extents'][i])
def test_mgrid(self, sample_logs_mock): point_list = PointList(sample_logs_mock['logs']) grid_x, grid_y, grid_z = point_list.mgrid( resolution=sample_logs_mock['resolution']) assert grid_x[0][0] == pytest.approx([-0.6, -0.6, -0.6, -0.6]) assert grid_x[13][-1] == pytest.approx([0.7, 0.7, 0.7, 0.7]) assert grid_y[0][0] == pytest.approx([0.495, 0.495, 0.495, 0.495]) assert grid_y[13][-1] == pytest.approx([0.601, 0.601, 0.601, 0.601]) assert grid_z[0][0] == pytest.approx([1.498, 1.565, 1.633, 1.700], abs=0.001) assert grid_z[13][-1] == pytest.approx([1.498, 1.565, 1.633, 1.700], abs=0.001) # Test irreducibility option # volume scan: all three directions have more than unique point xyz = [ [0.0, 1.000, 1.001, 2.0, 3.0, 4.0, 0.0, 1.0, 2.0, 3.0, 4.0], # x [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0], # y [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0] ] # z point_list = PointList(xyz) grid = point_list.mgrid(resolution=sample_logs_mock['resolution'], irreducible=True) assert grid.shape == (3, 5, 2, 2) # Surface scan on the {vx, vy} plane xyz[2] = [0] * 11 point_list = PointList(xyz) grid = point_list.mgrid(resolution=sample_logs_mock['resolution'], irreducible=True) assert grid.shape == (2, 5, 2) # Linear scan on the vx axis xyz[1] = [0] * 11 point_list = PointList(xyz) grid = point_list.mgrid(resolution=sample_logs_mock['resolution'], irreducible=True) assert grid.shape == (1, 5)