Skip to content

Commit

Permalink
Rename dims to shape throughout dlite (#774)
Browse files Browse the repository at this point in the history
Rename dims to shape throughout dlite
  • Loading branch information
alfredoisg authored Feb 14, 2024
1 parent 8e2ebd0 commit 70c22b4
Show file tree
Hide file tree
Showing 70 changed files with 596 additions and 575 deletions.
28 changes: 14 additions & 14 deletions bindings/fortran/dlite.f90
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ function dlite_instance_create_from_id(metaid, ndim, dims, id) result(instance)
! copy metaid in a C string
call f_c_string(metaid, metaid_c)

! copy dims in a C pointer (size_t*)
! copy shape in a C pointer (size_t*)
allocate(dims1(ndim))
do i=1, ndim
dims1(i) = dims(i)
Expand Down Expand Up @@ -530,32 +530,32 @@ function dlite_instance_get_property_by_index(instance, i) result(ptr)
ptr = dlite_instance_get_property_by_index_c(instance%cinst, i_c)
end function dlite_instance_get_property_by_index

subroutine dlite_instance_get_property_dims_by_index(instance, i, dims, reverse)
subroutine dlite_instance_get_property_dims_by_index(instance, i, shape, reverse)
class(DLiteInstance), intent(in) :: instance
integer, intent(in) :: i
integer(kind=c_size_t) :: i_c
type(c_ptr) :: ptr
integer(c_size_t), pointer :: dims_p(:)
integer(c_int) :: ndim_c
integer(8) :: ndim, j
integer(8), allocatable, intent(out) :: dims(:)
integer(8), allocatable, intent(out) :: shape(:)
logical, optional :: reverse
i_c = i
ndim_c = dlite_instance_get_property_ndims_by_index_c(instance%cinst, i_c)
ndim = ndim_c
ptr = dlite_instance_get_property_dims_by_index_c(instance%cinst, i_c)
call c_f_pointer(ptr, dims_p, [ndim])
allocate(dims(ndim))
allocate( shape(ndim))
if (present(reverse)) then
if (reverse .and. (ndim .gt. 1)) then
do j = 1, ndim
dims(j) = dims_p(ndim - j + 1)
shape(j) = dims_p(ndim - j + 1)
end do
else
dims = dims_p
shape = dims_p
endif
else
dims = dims_p
shape = dims_p
endif
call free_c(ptr)
end subroutine dlite_instance_get_property_dims_by_index
Expand Down Expand Up @@ -726,16 +726,16 @@ subroutine set_property_array_string(inst, index, prop)
character(*), dimension(*), intent(in) :: prop(:)
type(c_ptr) :: cptr
character(kind=c_char), pointer :: prop_p(:,:)
integer(8), dimension(*), allocatable :: dims(:), dims2(:)
integer(8), dimension(*), allocatable :: shape(:), shape2(:)
integer(8) :: i

call dlite_instance_get_property_dims_by_index(inst, index, dims)
allocate(dims2(2))
dims2(1) = len(prop(1))+1
dims2(2) = dims(1)
call dlite_instance_get_property_dims_by_index(inst, index, shape)
allocate(shape2(2))
shape2(1) = len(prop(1))+1
shape2(2) = shape(1)
cptr = inst%get_property_by_index(index)
call c_f_pointer(cptr, prop_p, dims2)
do i = 1, dims(1)
call c_f_pointer(cptr, prop_p, shape2)
do i = 1, shape(1)
call f_c_string(prop(i), prop_p(:,i))
end do

Expand Down
4 changes: 2 additions & 2 deletions bindings/fortran/tests/Person.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{
"name": "skills",
"type": "string10",
"dims": [
"shape": [
"N"
],
"description": "List of skills."
Expand All @@ -38,7 +38,7 @@
"name": "temperature",
"type": "float",
"unit": "degC",
"dims": [
"shape": [
"M"
],
"description": "Temperature on the body."
Expand Down
2 changes: 1 addition & 1 deletion bindings/fortran/tests/Scan3D.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"name": "points",
"type": "float",
"unit": "mm",
"dims": ["P", "N"],
"shape": ["P", "N"],
"description": "Point cloud."
}
]
Expand Down
18 changes: 9 additions & 9 deletions bindings/python/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def add_property(
self.properties[name] = dlite.Property(
name=name,
type=type,
dims=shape,
shape=shape,
unit=unit,
description=description,
)
Expand Down Expand Up @@ -130,9 +130,9 @@ def validate(self):
def get(self):
"""Returns a DLite Metadata created from the datamodel."""
self.validate()
dims = [len(self.dimensions), len(self.properties)]
if "nrelations" in self.schema:
dims.append(len(self.relations))
shape = [len(self.dimensions), len(self.properties)]
if 'nrelations' in self.schema:
shape.append(len(self.relations))

# Hmm, there seems to be a bug when instantiating from schema.
# The returned metadata seems not to be initialised, i.e.
Expand All @@ -144,11 +144,11 @@ def get(self):
f"Currently only entity schema is supported"
)

# meta = self.schema(dims, id=self.uri)
# meta.description = self.description
# meta['dimensions'] = list(self.dimensions.values())
# meta['properties'] = list(self.properties.values())
# if 'relations' in meta:
#meta = self.schema(shape, id=self.uri)
#meta.description = self.description
#meta['dimensions'] = list(self.dimensions.values())
#meta['properties'] = list(self.properties.values())
#if 'relations' in meta:
# meta['relations'] = self.relations
# return meta

Expand Down
22 changes: 12 additions & 10 deletions bindings/python/dlite-entity-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,21 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I
doc='Whether this is a meta-metadata instance.')

@classmethod
def from_metaid(cls, metaid, dims, id=None):
"""Create a new instance of metadata `metaid`. `dims` must be a
def from_metaid(cls, metaid, dimensions, id=None):
"""Create a new instance of metadata `metaid`. `dimensions` must be a
sequence with the size of each dimension. All values initialized
to zero. If `id` is None, a random UUID is generated. Otherwise
the UUID is derived from `id`.
"""
if isinstance(dims, dict):
if isinstance(dimensions, dict):
meta = get_instance(metaid)
dims = [dims[dim.name] for dim in meta.properties['dimensions']]
dimensions = [dimensions[dim.name]
for dim in meta.properties['dimensions']]
# Allow metaid to be an Instance
if isinstance(metaid, Instance):
metaid = metaid.uri
return Instance(
metaid=metaid, dims=dims, id=id,
metaid=metaid, dims=dimensions, id=id,
dimensions=(), properties=() # arrays must not be None
)

Expand Down Expand Up @@ -499,20 +500,21 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I
)

@classmethod
def create_from_metaid(cls, metaid, dims, id=None):
"""Create a new instance of metadata `metaid`. `dims` must be a
def create_from_metaid(cls, metaid, dimensions, id=None):
"""Create a new instance of metadata `metaid`. `dimensions` must be a
sequence with the size of each dimension. All values initialized
to zero. If `id` is None, a random UUID is generated. Otherwise
the UUID is derived from `id`.
"""
warnings.warn(
"create_from_metaid() is deprecated, use from_metaid() instead.",
DeprecationWarning, stacklevel=2)
if isinstance(dims, dict):
if isinstance(dimensions, dict):
meta = get_instance(metaid)
dims = [dims[dim.name] for dim in meta.properties['dimensions']]
dimensions = [dimensions[dim.name]
for dim in meta.properties['dimensions']]
return Instance(
metaid=metaid, dims=dims, id=id,
metaid=metaid, dims=dimensions, id=id,
dimensions=(), properties=() # arrays must not be None
)

Expand Down
33 changes: 18 additions & 15 deletions bindings/python/dlite-entity.i
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
DLiteProperty *
dlite_swig_create_property(const char *name, enum _DLiteType type,
size_t size, const char *ref,
obj_t *dims, const char *unit,
obj_t *shape, const char *unit,
const char *description)
{
DLiteProperty *p = calloc(1, sizeof(DLiteProperty));
p->name = strdup(name);
p->type = type;
p->size = size;
if (ref && *ref) p->ref = strdup(ref);
if (dims && dims != DLiteSwigNone) {
p->ndims = (int)PySequence_Length(dims);
if (!(p->dims = dlite_swig_copy_array(1, &p->ndims, dliteStringPtr,
sizeof(char *), dims))) {
if (shape && shape != DLiteSwigNone) {
p->ndims = (int)PySequence_Length(shape);
if (!(p->shape = dlite_swig_copy_array(1, &p->ndims, dliteStringPtr,
sizeof(char *), shape))) {
free(p->name);
free(p);
return NULL;
}
} else {
p->ndims = 0;
p->dims = NULL;
p->shape = NULL;
}
if (unit) p->unit = strdup(unit);
if (description) p->description = strdup(description);
Expand Down Expand Up @@ -139,18 +139,18 @@ struct _DLiteProperty {

%extend _DLiteProperty {
_DLiteProperty(const char *name, const char *type, const char *ref=NULL,
obj_t *dims=NULL, const char *unit=NULL,
obj_t *shape=NULL, const char *unit=NULL,
const char *description=NULL) {
DLiteType dtype;
size_t size;
if (dlite_type_set_dtype_and_size(type, &dtype, &size)) return NULL;
return dlite_swig_create_property(name, dtype, size, ref, dims, unit,
return dlite_swig_create_property(name, dtype, size, ref, shape, unit,
description);
}
~_DLiteProperty() {
free($self->name);
if ($self->ref) free($self->ref);
if ($self->dims) free_str_array($self->dims, $self->ndims);
if ($self->shape) free_str_array($self->shape, $self->ndims);
if ($self->unit) free($self->unit);
if ($self->description) free($self->description);
free($self);
Expand All @@ -165,7 +165,7 @@ struct _DLiteProperty {
}
obj_t *get_shape(void) {
return dlite_swig_get_array(NULL, 1, &$self->ndims,
dliteStringPtr, sizeof(char *), $self->dims);
dliteStringPtr, sizeof(char *), $self->shape);
}
void set_shape(obj_t *arr) {
int i, n = dlite_swig_length(arr);
Expand All @@ -174,10 +174,10 @@ struct _DLiteProperty {
FAIL("allocation failure");
if (dlite_swig_set_array(&new, 1, &n, dliteStringPtr, sizeof(char *), arr))
FAIL("cannot set new shape");
for (i=0; i < $self->ndims; i++) free($self->dims[i]);
free($self->dims);
for (i=0; i < $self->ndims; i++) free($self->shape[i]);
free($self->shape);
$self->ndims = n;
$self->dims = new;
$self->shape = new;
return;
fail:
if (new) {
Expand Down Expand Up @@ -246,10 +246,13 @@ struct _Triple {
%feature("docstring", "\
Returns a new instance.

Instance(metaid=None, dims=None, id=None, url=None, storage=None, driver=None, location=None, options=None, dimensions=None, properties=None, description=None)
Instance(metaid=None, dims=None, id=None, url=None, storage=None, driver=None,
location=None, options=None, dimensions=None, properties=None,
description=None)

Is called from one of the following class methods defined in dlite.py:

- from_metaid(cls, metaid, dims, id=None)
- from_metaid(cls, metaid, dimensions, id=None)
- from_url(cls, url, metaid=None)
- from_storage(cls, storage, id=None, metaid=None)
- from_location(cls, driver, location, options=None, id=None)
Expand Down
Loading

0 comments on commit 70c22b4

Please sign in to comment.