def angle_last_dim(a, b): ''' Calculate the angle between two nd-arrays (array of vectors) along the last dimension without anything further: 1->0°, 0.9->23°, 0.7->45°, 0->90° np.arccos -> returns degree in pi (90°: 0.5*pi) return: one dimension less then input ''' if len(a.shape) == 4: return torch.abs(einsum('abcd,abcd->abc', a, b) / (torch.norm(a, 2., -1) * torch.norm(b, 2, -1) + 1e-7)) else: return torch.abs(einsum('abcde,abcde->abcd', a, b) / (torch.norm(a, 2., -1) * torch.norm(b, 2, -1) + 1e-7))
def angle_last_dim(a, b): ''' Calculate the angle between two nd-arrays (array of vectors) along the last dimension. Returns dot product without applying arccos -> higher value = lower angle dot product <-> degree conversion: 1->0°, 0.9->23°, 0.7->45°, 0->90° By using np.arccos you could return degree in pi (90°: 0.5*pi) return: one dimension less than input ''' from tractseg.libs.pytorch_einsum import einsum if len(a.shape) == 4: return torch.abs(einsum('abcd,abcd->abc', a, b) / (torch.norm(a, 2., -1) * torch.norm(b, 2, -1) + 1e-7)) else: return torch.abs(einsum('abcde,abcde->abcd', a, b) / (torch.norm(a, 2., -1) * torch.norm(b, 2, -1) + 1e-7))
def angle_second_dim(a, b): ''' Not working ! RuntimeError: invalid argument 2: input is not contiguous (and Calculate the angle between two nd-arrays (array of vectors) along the second dimension without anything further: 1->0°, 0.9->23°, 0.7->45°, 0->90° np.arccos -> returns degree in pi (90°: 0.5*pi) return: one dimension less then input ''' from tractseg.libs.pytorch_einsum import einsum return torch.abs( einsum('abcd,abcd->acd', a, b) / (torch.norm(a, 2., 1) * torch.norm(b, 2, 1) + 1e-7))