Skip to content

Commit 97021f0

Browse files
committed
- [ENH] Added check when pushing to LE
- [ENH] Better handling of data_vars (the keys of a dataset) for structuredData
1 parent 005b439 commit 97021f0

File tree

6 files changed

+42
-22
lines changed

6 files changed

+42
-22
lines changed

subsurface/interfaces/liquid_earth/rest_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def add_new_project(self, project_name, extent, project_id=None):
8787

8888
def add_data_to_project(self, project_id: str, data_name: str, data_type: DataTypes,
8989
header: json, body: bytearray):
90-
90+
# Check if _ is in data_name
91+
if "_" in project_id:
92+
raise ValueError("project_id should not contain _")
93+
9194
self._post_update_meta_data(project_id, data_name, data_type)
9295
self._put_file_in_project(project_id, data_name + ".le", data_type, body)
9396
self._put_file_in_project(project_id, data_name + ".json", data_type, json.dumps(header))

subsurface/structs/base_structures/structured_data.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from dataclasses import dataclass
2-
from typing import Dict, List
2+
from typing import Dict, List, Tuple
33

44
import numpy as np
55
import xarray as xr
66

7-
87
__all__ = ['StructuredData', ]
98

109

11-
@dataclass(frozen=True)
10+
@dataclass(frozen=False)
1211
class StructuredData:
1312
data: xr.Dataset
14-
data_array_name: str = "data_array"
13+
_data_array_name: str = "data_array"
1514

1615
"""Primary structure definition for structured data
1716
@@ -31,6 +30,17 @@ class StructuredData:
3130
data (xarray.Dataset)
3231
"""
3332

33+
@property
34+
def data_array_name(self):
35+
data_var_list = list(self.data.data_vars.keys())
36+
if self._data_array_name not in data_var_list:
37+
raise ValueError("data_array_name not found in data_vars: {}".format(data_var_list))
38+
return self._data_array_name
39+
40+
@data_array_name.setter
41+
def data_array_name(self, data_array_name: str):
42+
self._data_array_name = data_array_name
43+
3444
@classmethod
3545
def from_numpy(cls, array: np.ndarray, coords: dict = None, data_array_name: str = "data_array",
3646
dim_names: List[str] = None):
@@ -58,20 +68,27 @@ def values(self):
5868
return self.data[self.data_array_name].values
5969

6070
@property
61-
def default_dataset(self):
71+
def default_data_array(self):
6272
return self.data[self.data_array_name]
6373

64-
def to_binary(self, order='F'):
65-
bytearray_le = self._to_bytearray(order)
66-
header = self._set_binary_header()
74+
def default_data_array_to_binary(self, order='F'):
75+
bytearray_le = self._to_bytearray(self.default_data_array, order)
76+
header = self._set_binary_header(self.default_data_array)
6777

6878
return bytearray_le, header
69-
70-
def _set_binary_header(self):
71-
header = {"data_shape": self.values.shape}
79+
80+
def to_binary(self, data_array: xr.DataArray, order: str = 'F') -> Tuple[bytes, Dict]:
81+
bytearray_le = self._to_bytearray(data_array, order)
82+
header = self._set_binary_header(data_array)
83+
return bytearray_le, header
84+
85+
@staticmethod
86+
def _set_binary_header(data_array: xr.DataArray) -> Dict:
87+
header = {"data_shape": data_array.shape}
7288
return header
7389

74-
def _to_bytearray(self, order):
75-
data = self.values.astype('float32').tobytes(order)
90+
@staticmethod
91+
def _to_bytearray(data_array: xr.DataArray, order: str) -> bytes:
92+
data = data_array.values.astype('float32').tobytes(order)
7693
bytearray_le = data
7794
return bytearray_le

subsurface/writer/to_binary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def base_structs_to_binary_file(path, base_struct, order='F'):
5-
bytearray_le, header = base_struct.to_binary(order=order)
5+
bytearray_le, header = base_struct.default_data_array_to_binary(order=order)
66
with open(path+'.json', 'w') as outfile:
77
json.dump(header, outfile)
88

tests/test_interfaces/test_liquid_earth.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_upload_data_to_new_project(self, liquid_earth_client, data_path):
3535

3636
unstruc = self.test_dataset2(data_path)
3737

38-
body, header = unstruc.to_binary()
38+
body, header = unstruc.default_data_array_to_binary()
3939

4040
liquid_earth_client.add_data_to_project(
4141
project_id="museum",
@@ -62,7 +62,7 @@ def test_add_new_project(self, liquid_earth_client):
6262
def test_add_data_to_project(self, liquid_earth_client, data_path):
6363
us = read_unstruct(data_path + '/interpolator_meshes.nc')
6464
print(us.extent)
65-
body, header = us.to_binary()
65+
body, header = us.default_data_array_to_binary()
6666

6767
liquid_earth_client.add_data_to_project(
6868
project_id="52f88baa-c84c-4082-bbba-869ef3819004",
@@ -112,7 +112,7 @@ def test_dataset2(self, data_path):
112112
def test_put_file_in_project(self, liquid_earth_client, data_path):
113113
us = read_unstruct(data_path + '/interpolator_meshes.nc')
114114
print(us.extent)
115-
body, header = us.to_binary()
115+
body, header = us.default_data_array_to_binary()
116116

117117
liquid_earth_client._put_file_in_project(
118118
project_id="52f88baa-c84c-4082-bbba-869ef3819004",

tests/test_interfaces/test_to_binary.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def wells(data_path):
3030

3131

3232
def test_wells_to_binary(wells):
33-
bytearray_le, header = wells.to_binary()
33+
bytearray_le, header = wells.default_data_array_to_binary()
3434
print(header)
3535

3636
with open('well_f.json', 'w') as outfile:
@@ -50,7 +50,7 @@ def test_profile_to_binary(data_path):
5050

5151
cross = imageio.imread(data_path + '/profiles/Profil1_cropped.png')
5252
struct = StructuredData.from_numpy(np.array(cross))
53-
texture_binary, texture_header = struct.to_binary()
53+
texture_binary, texture_header = struct.default_data_array_to_binary()
5454

5555
origin = [traces.loc[0, 'geometry'].xy[0][0],
5656
traces.loc[0, 'geometry'].xy[1][0],
@@ -78,7 +78,7 @@ def test_profile_to_binary(data_path):
7878
import pandas as pd
7979

8080
unstruct = UnstructuredData.from_array(v, e, vertex_attr=pd.DataFrame(uv, columns=['u', 'v']))
81-
mesh_binary, mesh_header = unstruct.to_binary()
81+
mesh_binary, mesh_header = unstruct.default_data_array_to_binary()
8282

8383
with open('mesh_uv.json', 'w') as outfile:
8484
import json

tests/test_io/test_segy_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_images() -> List[str]:
5151
def test_converted_to_structured_data(get_structured_data):
5252
for x in get_structured_data:
5353
assert isinstance(x, StructuredData)
54-
x.default_dataset.plot()
54+
x.default_data_array.plot()
5555
plt.show(block=False)
5656

5757

0 commit comments

Comments
 (0)