Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions deeptrack/optical/optics.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,17 +677,10 @@ def get_voxel_size(
props = self._normalize(
resolution=resolution, magnification=magnification
)
res = props["resolution"]
mag = props["magnification"]
if TORCH_AVAILABLE and (
torch.is_tensor(res) or torch.is_tensor(mag)
):
res = res if torch.is_tensor(res) else torch.tensor(res, dtype=torch.float64)
mag = mag if torch.is_tensor(mag) else torch.tensor(mag, dtype=torch.float64)
v = res / mag
return torch.stack([v, v, v])
return (
xp.ones((3,), dtype=xp.float64) * res / mag
xp.ones((3,), dtype=xp.float64)
* props["resolution"]
/ props["magnification"]
)

def get_pixel_size(
Expand Down
9 changes: 0 additions & 9 deletions deeptrack/optical/scatterers.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,6 @@ def _asarray_vector(value, dtype=None):
if isinstance(value, (list, tuple)) and any(
apc.is_array_api_obj(element) for element in value
):
if TORCH_AVAILABLE and any(torch.is_tensor(e) for e in value):
elements = []
for e in value:
if not torch.is_tensor(e):
e = torch.tensor(e, dtype=dtype)
elif dtype is not None:
e = e.to(dtype=dtype)
elements.append(e.reshape(()))
return torch.stack(elements)
return xp.stack(
[xp.reshape(_asarray(element, dtype), ()) for element in value]
)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_scatterers.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,12 @@ def test_mie_sphere_brightfield_sums_multiple_torch_fields(self):
return_field=True,
)

# image = microscope(sample).resolve()
image = microscope(sample).resolve()
print(radius_1.requires_grad) # True — set by us
print(image.requires_grad) # Is the graph connected?
loss = torch.abs(image).sum()
loss.backward()

self.assertIsInstance(image, torch.Tensor)
self.assertEqual(image.shape, (32, 32, 1))
Expand Down Expand Up @@ -774,7 +779,11 @@ def test_mie_sphere_brightfield_autodiff_learnable_parameters(self):

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
print(parameter.requires_grad) # True here
image = microscope(sample).resolve()
print(parameter.requires_grad) # Still True, but now detached from computation graph

# image = microscope(sample).resolve()

tensor_warning = (
"Converting a tensor with requires_grad=True to a scalar"
Expand Down
Loading