Coverage for install/scipp/core/util.py: 0%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-17 01:51 +0000

1# SPDX-License-Identifier: BSD-3-Clause 

2# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 

3# @author Simon Heybrock 

4from collections.abc import Mapping 

5from typing import TypeVar 

6 

7from .cpp_classes import DataArray, Dataset, Variable 

8from .like import empty_like 

9 

10_T = TypeVar('_T', Variable, DataArray, Dataset) 

11 

12 

13def _copy_dict_for_overwrite(mapping: Mapping[str, Variable]) -> dict[str, Variable]: 

14 return {name: copy_for_overwrite(var) for name, var in mapping.items()} 

15 

16 

17def copy_for_overwrite(obj: _T) -> _T: 

18 """ 

19 Copy a Scipp object for overwriting. 

20 

21 Unlike :py:func:`scipp.empty_like` this does not preserve (and share) coord, 

22 mask, and attr values. Instead, those values are not initialized, just like the 

23 data values. 

24 """ 

25 if isinstance(obj, Variable): 

26 return empty_like(obj) 

27 if isinstance(obj, DataArray): 

28 return DataArray( 

29 copy_for_overwrite(obj.data), 

30 coords=_copy_dict_for_overwrite(obj.coords), 

31 masks=_copy_dict_for_overwrite(obj.masks), 

32 attrs=_copy_dict_for_overwrite(obj.attrs), 

33 ) 

34 ds = Dataset(coords=_copy_dict_for_overwrite(obj.coords)) 

35 for name, da in obj.items(): 

36 ds[name] = DataArray( 

37 copy_for_overwrite(da.data), 

38 masks=_copy_dict_for_overwrite(da.masks), 

39 attrs=_copy_dict_for_overwrite(da.attrs), 

40 ) 

41 return ds 

42 

43 

44class VisibleDeprecationWarning(UserWarning): 

45 """Visible deprecation warning. 

46 

47 By default, Python and in particular Jupyter will not show deprecation 

48 warnings, so this class can be used when a very visible warning is helpful. 

49 """ 

50 

51 

52VisibleDeprecationWarning.__module__ = 'scipp'