This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdjointGridSample(torch.autograd.Function): | |
@staticmethod | |
def forward( | |
ctx: torch.autograd.function.FunctionCtx, | |
y: torch.Tensor, | |
grid: torch.Tensor, | |
xshape: Sequence[int], | |
interpolation_mode: Literal['bilinear', 'nearest', 'bicubic'] = 'bilinear', | |
padding_mode: Literal['zeros', 'border', 'reflection'] = 'zeros', | |
align_corners: bool = True, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from scipy.spatial.transform import Rotation | |
from typing import Callable, Literal | |
import einops | |
import itertools | |
from torch import Tensor | |
class MatrixMultiplication(torch.autograd.Function): | |
"""Helper to do a matrix multiplication if we know the adjoint of the matrix""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dataclasses | |
from typing import Type, Callable, Any | |
def _rapply(obj:Any, functions_per_types: dict[Type | list[Type], Callable]): | |
""" Apply callables to all fields of a dataclass and keys of a dictionary, recursively. | |
Which callable is used is determined by the type. By default, the function will recurse into dataclasses.fields and dictionaries, respectively, returning new dataclass instances or dictionaries, respectively. This can be disabled by, for example, adding {dict:lambda x:x} to functions_per_types, as these take precedence. | |
Example: | |
>>> _rapply({'a':1, 'string':'not changed'}, {int:lambda x:x+1}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from pathlib import Path | |
from datetime import datetime | |
import pypulseq as pp | |
### SETTINGS ### | |
timestamp = datetime.now().strftime("%y%m%d-%H%M%S") | |
filename = Path(f"profile_{timestamp}.seq") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
import torch | |
import numpy as np | |
def _filter_separable(x, kernels, axis): | |
"""Apply the separable filter kernels to the tensor x along the axes axis. | |
Does zero-padding to keep the output the same size as the input. | |
Parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
from typing import Sequence | |
import torch | |
import numpy as np | |
# fzimmermann89, felix.zimmermann@ptb.de, 2024 | |
def sliding_window(x:torch.Tensor, window_shape:int|Sequence[int], axis:None|int|Sequence[int]|=None): | |
"""Sliding window into the tensor x. | |
Returns a view into the tensor x that represents a sliding window. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torch import nn | |
from typing import Tuple, Callable | |
class LUT(nn.Module): | |
def __init__(self, f: Callable, dx: float, xrange: Tuple[float, float], mode: str = "linear"): | |
""" | |
LUT of values of a function | |
f: function to use, does not need to be differentiable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
### Requested pixel2q and q2pixel #### | |
def pixel2q(pixel, E_ev, detz_m, pixelsize_m=75e-6): | |
""" | |
returns q in reciprocal nm | |
E_ev: photon Energy in eV | |
detz_m: detector distance in m | |
pixelsize_m: detector pixelsize in m |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
NewerOlder