Skip to content

Commit

Permalink
added function for implicit water
Browse files Browse the repository at this point in the history
  • Loading branch information
Dingel321 committed Apr 24, 2023
1 parent e4f9529 commit e415e6b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions experiments/6wxb/image_params_mixed_training.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"N_PIXELS": 128,
"PIXEL_SIZE": 2.06,
"SIGMA": [4.0, 15.0],
"SIGMA": [0.5, 5.0],
"MODEL_FILE": "../data/protein_models/6wxb_mixed_models.npy",
"ROTATIONS": false,
"ROTATIONS": true,
"SHIFT": true,
"CTF": true,
"NOISE": true,
Expand Down
1 change: 1 addition & 0 deletions src/cryo_sbi/wpa_simulator/cryo_em_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cryo_sbi.wpa_simulator.padding import pad_image
from cryo_sbi.wpa_simulator.shift import apply_no_shift, apply_random_shift
from cryo_sbi.wpa_simulator.validate_image_config import check_params
from cryo_sbi.wpa_simulator.implicit_water import add_noise_field


class CryoEmSimulator:
Expand Down
47 changes: 47 additions & 0 deletions src/cryo_sbi/wpa_simulator/implicit_water.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import torch


def gen_noise_field(num_pixels, num_sin_func=10, max_intensity=1e-3):
"""Generate a noise field with a given number of sinusoidal functions.
Args:
num_pixels (int): Number of pixels in the noise field.
num_sin_func (int, optional): Number of sinusoidal functions. Defaults to 10.
max_intensity (float, optional): Maximum intensity of the noise field. Defaults to 1e-3.
Returns:
torch.Tensor: Noise field.
"""

x = torch.linspace(-100, 100, num_pixels)
y = torch.linspace(-100, 100, num_pixels)
xx, yy = torch.meshgrid(x, y)

b = 0.6 * (torch.rand((num_sin_func, 2)) - 0.5)
c = 2 * torch.pi * (torch.rand(num_sin_func, 2) - 0.5)

noise_field = torch.zeros_like(xx, dtype=torch.double)
for i in range(num_sin_func):
noise_field += torch.sin(b[i, 0] * xx + c[i, 0]) * torch.sin(
b[i, 1] * yy + c[i, 1]
)
noise_field = max_intensity * (noise_field / noise_field.max())
return noise_field


def add_noise_field(image, min_intensity):
"""Add a noise field to an image.
Args:
image (torch.Tensor): Image of shape (n_pixels, n_pixels) or (n_channels, n_pixels, n_pixels).
min_intensity (float): Minimum intensity of the image.
Returns:
torch.Tensor: Image with noise field.
"""

noise_field = gen_noise_field(image.shape[0], max_intensity=1e-12)
idx_replace = image < min_intensity
image[idx_replace] = noise_field[idx_replace]

return image

0 comments on commit e415e6b

Please sign in to comment.