Skip to content

Commit 7154ee0

Browse files
committed
[dev] inplace param added to crs transformation of Blocks object (+ declaration of new object's deepcopy)
1 parent 94cc285 commit 7154ee0

File tree

7 files changed

+451
-401
lines changed

7 files changed

+451
-401
lines changed

src/pyinterpolate/core/data_models/blocks.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Package doesn't read data files, data must be loaded into DataFrame and
1111
then passed into the Blocks object.
1212
"""
13+
import copy
1314
from typing import Union, Hashable, Dict
1415
from numpy.typing import ArrayLike
1516

@@ -477,7 +478,6 @@ def select_distances_between_blocks(self,
477478
except AttributeError:
478479
return df.loc[block_id, other_blocks]
479480

480-
# TODO manage copying and inplace transformations
481481
def transform_crs(self, target_crs, inplace=True):
482482
"""Function transforms Blocks CRS
483483
@@ -495,21 +495,39 @@ def transform_crs(self, target_crs, inplace=True):
495495
unchanged.
496496
"""
497497
# Transform core dataset
498-
self.ds.to_crs(target_crs, inplace=True)
498+
if inplace:
499+
self.ds.to_crs(target_crs, inplace=True)
499500

500-
# representative points
501-
self._get_representative_points()
502-
self._points_to_floats()
501+
# representative points
502+
self._get_representative_points()
503+
self._points_to_floats()
503504

504-
# distances
505-
self.distances = self.calculate_distances_between_rep_points(
506-
update=False
507-
)
505+
# distances
506+
self.distances = self.calculate_distances_between_rep_points(
507+
update=False
508+
)
508509

509-
# angles
510-
self.angles = self.calculate_angles_between_rep_points(
511-
update=False
512-
)
510+
# angles
511+
self.angles = self.calculate_angles_between_rep_points(
512+
update=False
513+
)
514+
return None
515+
else:
516+
new_object = copy.deepcopy(self)
517+
518+
new_object.ds.to_crs(target_crs, inplace=True)
519+
new_object._get_representative_points()
520+
new_object._points_to_floats()
521+
522+
new_object.distances = new_object.calculate_distances_between_rep_points(
523+
update=False
524+
)
525+
526+
new_object.angles = new_object.calculate_angles_between_rep_points(
527+
update=False
528+
)
529+
530+
return new_object
513531

514532
def _delete(self, block_index: Union[str, Hashable]):
515533
"""

tests/test_core/test_blocks_model.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def test_select_distances_between_blocks_method():
130130
assert isinstance(dist, np.ndarray)
131131

132132

133-
def test_transform_crs():
133+
def test_transform_inplace_crs():
134134
block = Blocks(
135135
**CANCER_DATA,
136136
angles_between_representative_points=True
@@ -142,7 +142,7 @@ def test_transform_crs():
142142

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

145-
block.transform_crs('EPSG:2180')
145+
block.transform_crs('EPSG:2180', inplace=True)
146146

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

158158

159+
def test_transform_crs():
160+
block = Blocks(
161+
**CANCER_DATA,
162+
angles_between_representative_points=True
163+
)
164+
165+
sample_base_pre = block.ds.iloc[0]
166+
sample_base_dists_pre = block.distances.copy(deep=True).iloc[0, 1]
167+
sample_base_angles_pre = deepcopy(block.angles)
168+
169+
ks_pre = list(sample_base_angles_pre.keys())[0]
170+
sb_angles_pre = sample_base_angles_pre[ks_pre]
171+
172+
new_block = block.transform_crs('EPSG:2180',
173+
inplace=False)
174+
175+
sample_base_post = block.ds.iloc[0]
176+
sample_base_dists_post = block.distances.copy(deep=True).iloc[0, 1]
177+
sample_base_angles_post = deepcopy(block.angles)
178+
179+
sb_angles_post = sample_base_angles_post[ks_pre]
180+
181+
assert sample_base_pre[block._lon_col_name] == sample_base_post[block._lon_col_name]
182+
assert sample_base_dists_post == sample_base_dists_pre
183+
assert sb_angles_pre[1] == sb_angles_post[1]
184+
185+
sample_transformed = new_block.ds.iloc[0]
186+
sample_transformed_dists = new_block.distances.copy(deep=True).iloc[0, 1]
187+
sample_transformed_angles = deepcopy(new_block.angles)
188+
189+
st_angles = sample_transformed_angles[ks_pre]
190+
191+
assert sample_base_pre[block._lon_col_name] != sample_transformed[block._lon_col_name]
192+
assert sample_transformed_dists != sample_base_dists_pre
193+
assert sb_angles_pre[1] != st_angles[1]
194+
195+
159196
def test_block_index_outputs():
160197
block = Blocks(**CANCER_DATA)
161198

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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}
1+
{"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}

tutorials/functional/4-1-semivariogram-regularization.ipynb

Lines changed: 62 additions & 64 deletions
Large diffs are not rendered by default.

tutorials/functional/4-2-poisson-kriging-centroid-based.ipynb

Lines changed: 143 additions & 143 deletions
Large diffs are not rendered by default.

tutorials/functional/4-3-poisson-kriging-area-to-area.ipynb

Lines changed: 135 additions & 135 deletions
Large diffs are not rendered by default.

tutorials/functional/4-4-poisson-kriging-area-to-point-smoothing.ipynb

Lines changed: 40 additions & 43 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)