diff --git a/discretisedfield/field.py b/discretisedfield/field.py
index 046476b12..7210e29a9 100644
--- a/discretisedfield/field.py
+++ b/discretisedfield/field.py
@@ -331,16 +331,16 @@ def update_field_values(self, value):
>>> mesh = df.Mesh(p1=p1, p2=p2, cell=cell,\
subregions={'s1': sub1, 's2': sub2})
>>> field = df.Field(mesh, nvdim=1, value={'s1': 1, 's2': 1})
- >>> (field.array == 1).all()
+ >>> (field.array == 1).all().item()
True
>>> field = df.Field(mesh, nvdim=1, value={'s1': 1})
Traceback (most recent call last):
...
KeyError: ...
>>> field = df.Field(mesh, nvdim=1, value={'s1': 2, 'default': 1})
- >>> (field.array == 1).all()
+ >>> (field.array == 1).all().item()
False
- >>> (field.array == 0).any()
+ >>> (field.array == 0).any().item()
False
>>> mesh = df.Mesh(p1=p1, p2=p2, cell=cell, subregions={'s': sub1})
>>> field = df.Field(mesh, nvdim=1, value={'s': 1})
@@ -348,7 +348,7 @@ def update_field_values(self, value):
...
KeyError: ...
>>> field = df.Field(mesh, nvdim=1, value={'default': 1})
- >>> (field.array == 1).all()
+ >>> (field.array == 1).all().item()
True
.. seealso:: :py:func:`~discretisedfield.Field.array`
diff --git a/discretisedfield/html/templates/mesh.jinja2 b/discretisedfield/html/templates/mesh.jinja2
index 29c6ed6b5..7e6907ce4 100644
--- a/discretisedfield/html/templates/mesh.jinja2
+++ b/discretisedfield/html/templates/mesh.jinja2
@@ -2,7 +2,7 @@
{%- set region = mesh.region %}
- {% include 'region.jinja2' %}
- - n = {{ mesh.n|list }}
+ - n = {{ mesh.n.tolist() }}
{%- if mesh.bc %}
- bc = {{ mesh.bc }}
{%- endif %}
diff --git a/discretisedfield/html/templates/region.jinja2 b/discretisedfield/html/templates/region.jinja2
index 3d16ff4ea..43b183bb9 100644
--- a/discretisedfield/html/templates/region.jinja2
+++ b/discretisedfield/html/templates/region.jinja2
@@ -1,7 +1,7 @@
Region{%if region_name is defined %} {{ region_name }}{% endif %}
- - pmin = {{ region.pmin|list }}
- - pmax = {{ region.pmax|list }}
+ - pmin = {{ region.pmin.tolist() }}
+ - pmax = {{ region.pmax.tolist() }}
- dims = {{ region.dims|list }}
- units = {{ region.units|list }}
diff --git a/discretisedfield/io/hdf5.py b/discretisedfield/io/hdf5.py
index c2776c7d2..235dbcd03 100644
--- a/discretisedfield/io/hdf5.py
+++ b/discretisedfield/io/hdf5.py
@@ -73,7 +73,9 @@ class _FieldIO_HDF5:
def _to_hdf5(self, filename):
"""Save a single field in a new hdf5 file."""
- utc_now = datetime.datetime.utcnow().isoformat(timespec="seconds")
+ utc_now = datetime.datetime.now(datetime.timezone.utc).isoformat(
+ timespec="seconds"
+ )
with h5py.File(filename, "w") as f:
f.attrs["ubermag-hdf5-file-version"] = "0.1"
f.attrs["discretisedfield.__version__"] = df.__version__
diff --git a/discretisedfield/line.py b/discretisedfield/line.py
index 968035768..ce28022ff 100644
--- a/discretisedfield/line.py
+++ b/discretisedfield/line.py
@@ -263,7 +263,7 @@ def length(self):
4.0
"""
- return self.data["r"].iloc[-1]
+ return self.data["r"].iloc[-1].item()
def __repr__(self):
"""Representation string.
diff --git a/discretisedfield/mesh.py b/discretisedfield/mesh.py
index c17f0909e..39dceb800 100644
--- a/discretisedfield/mesh.py
+++ b/discretisedfield/mesh.py
@@ -850,7 +850,8 @@ def point2index(self, point, /):
# If index is rounded to the out-of-range values.
index = np.clip(index, 0, self.n - 1)
- return tuple(index)
+ # conversion to list is required to convert the datatypes to Python builtins
+ return tuple(index.tolist())
def region2slices(self, region):
"""Slices of indices that correspond to cells contained in the region.
@@ -1501,7 +1502,7 @@ def __getattr__(self, attr):
)
if len(attr) > 1 and attr[0] == "d":
with contextlib.suppress(ValueError):
- return self.cell[self.region._dim2index(attr[1:])]
+ return self.cell.tolist()[self.region._dim2index(attr[1:])]
raise AttributeError(f"Object has no attribute {attr}.")
def __dir__(self):
@@ -1544,7 +1545,7 @@ def dV(self):
8.0
"""
- return np.prod(self.cell)
+ return np.prod(self.cell).item()
def scale(self, factor, reference_point=None, inplace=False):
"""Scale the underlying region and all subregions.
@@ -2051,7 +2052,8 @@ def slider(self, axis, /, *, multiplier=None, description=None, **kwargs):
)
values = np.arange(slider_min, slider_max + 1e-20, slider_step)
- labels = np.around(values / multiplier, decimals=3)
+ labels = np.around(values / multiplier, decimals=3).tolist()
+ values = values.tolist()
options = list(zip(labels, values))
# Select middle element for slider value
diff --git a/discretisedfield/plotting/hv.py b/discretisedfield/plotting/hv.py
index 0c6d3d0ca..1820c381e 100644
--- a/discretisedfield/plotting/hv.py
+++ b/discretisedfield/plotting/hv.py
@@ -733,8 +733,11 @@ def _resample(array, kdims, n):
if n is None:
return array
+ # .item() is required to convert xarray to Python built-in type;
+ # without this conversion linspace will fail because it would try to create a
+ # a new xarray but no dimensions are provided.
vals = {
- dim: np.linspace(array[dim].min(), array[dim].max(), ni)
+ dim: np.linspace(array[dim].min().item(), array[dim].max().item(), ni)
for dim, ni in zip(kdims, n)
}
resampled = array.sel(**vals, method="nearest")
diff --git a/discretisedfield/region.py b/discretisedfield/region.py
index f343c2459..80a302c92 100644
--- a/discretisedfield/region.py
+++ b/discretisedfield/region.py
@@ -460,7 +460,7 @@ def volume(self):
100
"""
- return np.prod(self.edges)
+ return np.prod(self.edges).item()
def __repr__(self):
r"""Representation string.
diff --git a/discretisedfield/tools/tools.py b/discretisedfield/tools/tools.py
index 74e42d509..f2a31fa23 100644
--- a/discretisedfield/tools/tools.py
+++ b/discretisedfield/tools/tools.py
@@ -657,10 +657,11 @@ def count_bps(field, /, direction):
bp_count = bp_number[1:] - bp_number[:-1]
results = {}
- results["bp_number"] = abs(bp_count).sum()
- results["bp_number_hh"] = abs(bp_count[bp_count < 0].sum())
- results["bp_number_tt"] = bp_count[bp_count > 0].sum()
+ results["bp_number"] = abs(bp_count).sum().item()
+ results["bp_number_hh"] = abs(bp_count[bp_count < 0].sum()).item()
+ results["bp_number_tt"] = bp_count[bp_count > 0].sum().item()
+ bp_number = bp_number.tolist()
# pattern = list([, ])
pattern = [[bp_number[0], 1]]
for q_val in bp_number[1:]:
diff --git a/docs/field-definition.ipynb b/docs/field-definition.ipynb
index 6a477df26..a8a6d09d0 100644
--- a/docs/field-definition.ipynb
+++ b/docs/field-definition.ipynb
@@ -17,6 +17,8 @@
"metadata": {},
"outputs": [],
"source": [
+ "import numpy as np\n",
+ "\n",
"import discretisedfield as df\n",
"\n",
"p1 = (-50, -50, -50)\n",
@@ -170,7 +172,7 @@
}
],
"source": [
- "(field.array == 1.23).all()"
+ "np.array_equiv(field.array, 1.23)"
]
},
{
@@ -666,7 +668,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.10.12"
+ "version": "3.12.4"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
diff --git a/docs/mesh-basics.ipynb b/docs/mesh-basics.ipynb
index 4fa4ef012..e19c20131 100644
--- a/docs/mesh-basics.ipynb
+++ b/docs/mesh-basics.ipynb
@@ -361,7 +361,7 @@
{
"data": {
"text/plain": [
- ""
+ ""
]
},
"execution_count": 14,
@@ -555,7 +555,7 @@
{
"data": {
"text/plain": [
- "[1e-08, 3.0000000000000004e-08, 5e-08, 7e-08, 9e-08]"
+ "array([1.e-08, 3.e-08, 5.e-08, 7.e-08, 9.e-08])"
]
},
"execution_count": 20,
@@ -564,7 +564,7 @@
}
],
"source": [
- "list(mesh.cells.x)"
+ "mesh.cells.x"
]
},
{
@@ -575,7 +575,7 @@
{
"data": {
"text/plain": [
- "[1.25e-08, 3.75e-08]"
+ "array([1.25e-08, 3.75e-08])"
]
},
"execution_count": 21,
@@ -584,7 +584,7 @@
}
],
"source": [
- "list(mesh.cells.y)"
+ "mesh.cells.y"
]
},
{
@@ -602,7 +602,7 @@
{
"data": {
"text/plain": [
- "[0.0, 2e-08, 4e-08, 6.000000000000001e-08, 8e-08, 1e-07]"
+ "array([0.e+00, 2.e-08, 4.e-08, 6.e-08, 8.e-08, 1.e-07])"
]
},
"execution_count": 22,
@@ -611,7 +611,7 @@
}
],
"source": [
- "list(mesh.vertices.x)"
+ "mesh.vertices.x"
]
},
{
@@ -622,7 +622,7 @@
{
"data": {
"text/plain": [
- "[0.0, 2.5e-08, 5e-08]"
+ "array([0.0e+00, 2.5e-08, 5.0e-08])"
]
},
"execution_count": 23,
@@ -631,7 +631,7 @@
}
],
"source": [
- "list(mesh.vertices.y)"
+ "mesh.vertices.y"
]
},
{
@@ -1128,7 +1128,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.8.17"
+ "version": "3.12.4"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {