Skip to content

Existing unique_names when using copy on large arrays of classes with multiple parameters #259

Open

Description

@henrikjacobsenfys

Here's a minimal example that often gives the error, but usually not on the first call to copy.

If the class creates only one parameter, then I cannot reproduce the error.

Interestingly, if I don't use scipp then I cannot reproduce the error

from easyscience.variable import Parameter
from easyscience.base_classes.model_base import ModelBase
import scipp as sc
import numpy as np

class A(ModelBase):
    def __init__(self, par1, par2,par3):
        super().__init__()
        A._par1=Parameter(name='name1',value=par1)
        A._par2=Parameter(name='name2',value=par2)
        A._par3=Parameter(name='name3',value=par3)

    @property
    def par1(self):
        return self._par1
    
    @par1.setter
    def par1(self,value):
        self._par1.value=value

    @property
    def par2(self):
        return self._par2
    
    @par2.setter
    def par2(self,value):
        self._par2.value=value

    @property
    def par3(self):
        return self._par3
    
    @par3.setter
    def par3(self,value):
        self._par3.value=value


# The serializer checks if @module starts with easy and raises if not.
    def to_dict(self,skip):
        dict=super().to_dict(skip=skip)
        dict['@module']='easy'+dict['@module']
        return dict


coords = {'Q': sc.array(dims=['Q'], values=[1, 2, 3,4,5,6,7,8,9,10], unit='K'), 
              'temperature': sc.array(dims=['temperature'], values=[10, 20, 30, 40, 50])}


shape = tuple(
    coord.sizes[dim]
    for dim, coord in coords.items()
)

values = np.empty(
    shape,
    dtype=object,
)

for idx in np.ndindex(shape):
    values[idx] = A(par1=1, par2=2, par3=3)

sizes = {
    dim: coord.sizes[dim]
    for dim, coord in coords.items()
}

array = sc.array(dims=["flat"], values=values.flatten()).fold(
    dim="flat", sizes=sizes
)

data_array = sc.DataArray(data=array, coords=coords)


data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()
data_array.copy()

This works, even though it creates more parameters:


shape=(20,15)
values = np.empty(
    shape,
    dtype=object,
)

for idx in np.ndindex(shape):
    values[idx] = A(par1=1, par2=2, par3=3)

values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()
values.copy()

Activity

  1. added
    [scope] bug
    [maintainer] help wanted
    [priority] high
    [area] serialization
    on Jun 11, 2026
  2. damskii9992 commented on Jun 11, 2026

    @damskii9992
    Contributor

    Considering that it works when you simply run values.copy I'm assuming this is actually a Scipp issue rather than an easyscience one. Or rather, the way that Scipp's copy method works doesn't work with out easyscience objects.

    But putting easyscience models into a scipp.DataArray is also quite a crazy use-case, so I'm not really surprised that you're breaking stuff.

  3. henrikjacobsenfys commented on Jun 11, 2026

    @henrikjacobsenfys
    MemberAuthor

    Considering that it works when you simply run values.copy I'm assuming this is actually a Scipp issue rather than an easyscience one. Or rather, the way that Scipp's copy method works doesn't work with out easyscience objects.

    But putting easyscience models into a scipp.DataArray is also quite a crazy use-case, so I'm not really surprised that you're breaking stuff.

    It's handy for slicing and indexing :)

    Also, it it works sometimes, so I'm sure it is a bug somewhere in our code

    Edit: also, scipp clearly calls copy on the elements in the array, so it's using our copy method. The bug is ours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [area] serialization[maintainer] help wanted[priority] high[scope] bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions