Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Changes - from version >= 1.x
* [enhancement] from now on it is not possible to select neighbors using `triangle` method (in directional variogram cases)
* [docs] updated missing DOI in Indicator Variogram
* [enhancement] representative point in `Blocks` is sampled from the largest `Polygon` when `MultiPolygon` is passed
* [enhancement] `Blocks` object might be altered during CRS transformation or new object might be created (`inplace` parameter and copying mechanism)

2025-07-17
----------
Expand Down
51 changes: 37 additions & 14 deletions src/pyinterpolate/core/data_models/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Package doesn't read data files, data must be loaded into DataFrame and
then passed into the Blocks object.
"""
import copy
from typing import Union, Hashable, Dict
from numpy.typing import ArrayLike

Expand Down Expand Up @@ -477,8 +478,7 @@ def select_distances_between_blocks(self,
except AttributeError:
return df.loc[block_id, other_blocks]

# TODO manage copying and inplace transformations
def transform_crs(self, target_crs):
def transform_crs(self, target_crs, inplace=True):
"""Function transforms Blocks CRS

Parameters
Expand All @@ -488,23 +488,46 @@ def transform_crs(self, target_crs):
:meth:`pyproj.CRS.from_user_input()
<pyproj.crs.CRS.from_user_input>`,
such as an authority string (eg "EPSG:4326") or a WKT string.

inplace : bool, default = True
When set to `True` then transform object's instance on the fly,
otherwise return modified object and do leave the old instance
unchanged.
"""
# Transform core dataset
self.ds.to_crs(target_crs, inplace=True)
if inplace:
self.ds.to_crs(target_crs, inplace=True)

# representative points
self._get_representative_points()
self._points_to_floats()
# representative points
self._get_representative_points()
self._points_to_floats()

# distances
self.distances = self.calculate_distances_between_rep_points(
update=False
)
# distances
self.distances = self.calculate_distances_between_rep_points(
update=False
)

# angles
self.angles = self.calculate_angles_between_rep_points(
update=False
)
# angles
self.angles = self.calculate_angles_between_rep_points(
update=False
)
return None
else:
new_object = copy.deepcopy(self)

new_object.ds.to_crs(target_crs, inplace=True)
new_object._get_representative_points()
new_object._points_to_floats()

new_object.distances = new_object.calculate_distances_between_rep_points(
update=False
)

new_object.angles = new_object.calculate_angles_between_rep_points(
update=False
)

return new_object

def _delete(self, block_index: Union[str, Hashable]):
"""
Expand Down
41 changes: 39 additions & 2 deletions tests/test_core/test_blocks_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_select_distances_between_blocks_method():
assert isinstance(dist, np.ndarray)


def test_transform_crs():
def test_transform_inplace_crs():
block = Blocks(
**CANCER_DATA,
angles_between_representative_points=True
Expand All @@ -142,7 +142,7 @@ def test_transform_crs():

ks = list(sample_base_angles.keys())[0]

block.transform_crs('EPSG:2180')
block.transform_crs('EPSG:2180', inplace=True)

sample_transformed = block.ds.iloc[0]
sample_transformed_dists = block.distances.copy(deep=True).iloc[0, 1]
Expand All @@ -156,6 +156,43 @@ def test_transform_crs():
assert sb_angles[1] != st_angles[1]


def test_transform_crs():
block = Blocks(
**CANCER_DATA,
angles_between_representative_points=True
)

sample_base_pre = block.ds.iloc[0]
sample_base_dists_pre = block.distances.copy(deep=True).iloc[0, 1]
sample_base_angles_pre = deepcopy(block.angles)

ks_pre = list(sample_base_angles_pre.keys())[0]
sb_angles_pre = sample_base_angles_pre[ks_pre]

new_block = block.transform_crs('EPSG:2180',
inplace=False)

sample_base_post = block.ds.iloc[0]
sample_base_dists_post = block.distances.copy(deep=True).iloc[0, 1]
sample_base_angles_post = deepcopy(block.angles)

sb_angles_post = sample_base_angles_post[ks_pre]

assert sample_base_pre[block._lon_col_name] == sample_base_post[block._lon_col_name]
assert sample_base_dists_post == sample_base_dists_pre
assert sb_angles_pre[1] == sb_angles_post[1]

sample_transformed = new_block.ds.iloc[0]
sample_transformed_dists = new_block.distances.copy(deep=True).iloc[0, 1]
sample_transformed_angles = deepcopy(new_block.angles)

st_angles = sample_transformed_angles[ks_pre]

assert sample_base_pre[block._lon_col_name] != sample_transformed[block._lon_col_name]
assert sample_transformed_dists != sample_base_dists_pre
assert sb_angles_pre[1] != st_angles[1]


def test_block_index_outputs():
block = Blocks(**CANCER_DATA)

Expand Down
2 changes: 1 addition & 1 deletion tutorials/data/regularized_variogram.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"experimental_variogram": null, "nugget": 0.0, "sill": 176.68706240332543, "rang": 87733.33333333334, "variogram_model_type": "spherical", "direction": null, "spatial_dependence": null, "spatial_index": null, "yhat": null, "errors": null}
{"experimental_variogram": null, "nugget": 0.0, "sill": 176.942139054805, "rang": 57866.66666666667, "variogram_model_type": "linear", "direction": null, "spatial_dependence": null, "spatial_index": null, "yhat": null, "errors": null}
126 changes: 62 additions & 64 deletions tutorials/functional/4-1-semivariogram-regularization.ipynb

Large diffs are not rendered by default.

286 changes: 143 additions & 143 deletions tutorials/functional/4-2-poisson-kriging-centroid-based.ipynb

Large diffs are not rendered by default.

270 changes: 135 additions & 135 deletions tutorials/functional/4-3-poisson-kriging-area-to-area.ipynb

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading