mirror of
https://github.com/cdtools-developers/cdtools.git
synced 2026-11-29 00:17:48 +01:00
add to line_based_frc function to support standard 2D FRC computation for comparison
This commit is contained in:
1 parent
9264228c89
commit
505466b067
1 file changed
+169
-83
@@ -1528,7 +1528,9 @@ def calc_spectral_info(dataset, nbins=50):
|
||||
return sum_pattern, frc_bins[:-1], mean_spectrum
|
||||
|
||||
|
||||
def line_based_frc(image1, image2, axis=1, n_bins=None, thresholds={}, pixel_size=1.0, unit="pixels"):
|
||||
def line_based_frc(
|
||||
image1, image2, axis=1, n_bins=None, thresholds={}, pixel_size=1.0, unit="pixels"
|
||||
):
|
||||
"""
|
||||
Compute line-based FRC with multiple threshold methods. This works great for highly anisotropic
|
||||
images, such as those with vertical or horizontal stripes.
|
||||
@@ -1539,6 +1541,7 @@ def line_based_frc(image1, image2, axis=1, n_bins=None, thresholds={}, pixel_siz
|
||||
Input images (should be same size). Must be 2D arrays.
|
||||
axis: int
|
||||
axis along which to extract lines (1 for horizontal lines, 0 for vertical)
|
||||
if None, standard 2D FRC is computed
|
||||
n_bins: int
|
||||
number of frequency bins (if None, no binning is applied)
|
||||
thresholds: dict
|
||||
@@ -1564,8 +1567,12 @@ def line_based_frc(image1, image2, axis=1, n_bins=None, thresholds={}, pixel_siz
|
||||
if isinstance(image2, t.Tensor):
|
||||
image2 = image2.numpy()
|
||||
|
||||
assert isinstance(image1, np.ndarray), "image1 must be a numpy array or torch tensor"
|
||||
assert isinstance(image2, np.ndarray), "image2 must be a numpy array or torch tensor"
|
||||
assert isinstance(
|
||||
image1, np.ndarray
|
||||
), "image1 must be a numpy array or torch tensor"
|
||||
assert isinstance(
|
||||
image2, np.ndarray
|
||||
), "image2 must be a numpy array or torch tensor"
|
||||
|
||||
assert image1.shape == image2.shape, "Images must have same shape"
|
||||
|
||||
@@ -1580,98 +1587,177 @@ def line_based_frc(image1, image2, axis=1, n_bins=None, thresholds={}, pixel_siz
|
||||
lines1 = image1
|
||||
lines2 = image2
|
||||
n_freq = image1.shape[1] // 2
|
||||
|
||||
else:
|
||||
elif axis == 0:
|
||||
# Extract vertical lines (perpendicular to horizontal stripes)
|
||||
lines1 = image1.T
|
||||
lines2 = image2.T
|
||||
n_freq = image1.shape[0] // 2
|
||||
|
||||
# Compute 1D FFT for each line
|
||||
fft1 = np.fft.fft(lines1, axis=1)
|
||||
fft2 = np.fft.fft(lines2, axis=1)
|
||||
|
||||
# Only keep positive frequencies
|
||||
fft1 = fft1[:, :n_freq]
|
||||
fft2 = fft2[:, :n_freq]
|
||||
|
||||
# Get the raw frequency array
|
||||
raw_frequencies = np.fft.fftfreq(lines1.shape[1])[:n_freq]
|
||||
|
||||
if n_bins is None:
|
||||
# No binning - use original approach
|
||||
numerator = np.sum(fft1 * np.conj(fft2), axis=0)
|
||||
denominator = np.sqrt(np.sum(np.abs(fft1)**2, axis=0) * np.sum(np.abs(fft2)**2, axis=0))
|
||||
|
||||
frc_curve = np.abs(numerator) / denominator
|
||||
frequencies = raw_frequencies
|
||||
|
||||
elif axis is None:
|
||||
# we now assume axis=None, which means we compute standard 2D FRC
|
||||
lines1 = image1
|
||||
lines2 = image2
|
||||
n_freq = min(image1.shape) // 2 # Use half the smaller dimension
|
||||
else:
|
||||
# Binning approach - similar to 2D FRC
|
||||
max_freq = raw_frequencies[-1]
|
||||
freq_bins = np.linspace(0, max_freq, n_bins + 1)
|
||||
raise ValueError("Axis must be 0, 1, or None for line-based FRC")
|
||||
|
||||
# Compute binned FRC
|
||||
if axis is not None:
|
||||
# Compute 1D FFT for each line
|
||||
fft1 = np.fft.fft(lines1, axis=1)
|
||||
fft2 = np.fft.fft(lines2, axis=1)
|
||||
|
||||
# Only keep positive frequencies
|
||||
fft1 = fft1[:, :n_freq]
|
||||
fft2 = fft2[:, :n_freq]
|
||||
|
||||
# Get the raw frequency array
|
||||
raw_frequencies = np.fft.fftfreq(lines1.shape[1])[:n_freq]
|
||||
|
||||
if n_bins is None:
|
||||
# No binning - use original approach
|
||||
numerator = np.sum(fft1 * np.conj(fft2), axis=0)
|
||||
denominator = np.sqrt(
|
||||
np.sum(np.abs(fft1) ** 2, axis=0) * np.sum(np.abs(fft2) ** 2, axis=0)
|
||||
)
|
||||
|
||||
frc_curve = np.abs(numerator) / denominator
|
||||
frequencies = raw_frequencies
|
||||
|
||||
else:
|
||||
# Binning approach - similar to 2D FRC
|
||||
max_freq = raw_frequencies[-1]
|
||||
freq_bins = np.linspace(0, max_freq, n_bins + 1)
|
||||
|
||||
# Compute binned FRC
|
||||
frc_curve = []
|
||||
frequencies = []
|
||||
|
||||
for i in range(len(freq_bins) - 1):
|
||||
# Find frequency indices in this bin
|
||||
mask = (raw_frequencies >= freq_bins[i]) & (
|
||||
raw_frequencies < freq_bins[i + 1]
|
||||
)
|
||||
n_freq_components = np.sum(mask)
|
||||
|
||||
if n_freq_components > 0:
|
||||
# Extract FFT values in this frequency bin
|
||||
fft1_bin = fft1[:, mask]
|
||||
fft2_bin = fft2[:, mask]
|
||||
|
||||
# Compute FRC for this bin (sum over both lines and frequencies)
|
||||
# see van Heel, M. (2005). https://doi.org/10.1016/j.jsb.2005.05.009
|
||||
numerator = np.sum(fft1_bin * np.conj(fft2_bin))
|
||||
denominator = np.sqrt(
|
||||
np.sum(np.abs(fft1_bin) ** 2) * np.sum(np.abs(fft2_bin) ** 2)
|
||||
)
|
||||
|
||||
frc_value = np.abs(numerator) / denominator
|
||||
frc_curve.append(frc_value)
|
||||
frequencies.append((freq_bins[i] + freq_bins[i + 1]) / 2)
|
||||
|
||||
# Convert lists to numpy arrays
|
||||
frc_curve = np.array(frc_curve)
|
||||
frequencies = np.array(frequencies)
|
||||
|
||||
# Find resolution at each threshold using interpolation
|
||||
resolution_dict = {}
|
||||
for method, threshold_val in thresholds.items():
|
||||
# Find where FRC crosses the threshold
|
||||
above_threshold = frc_curve >= threshold_val
|
||||
|
||||
# Find all crossings (from above to below threshold)
|
||||
crossings = np.where(np.diff(above_threshold.astype(int)) == -1)[0]
|
||||
|
||||
if crossings.size > 0:
|
||||
# Use the first crossing (lowest frequency where FRC drops below threshold)
|
||||
idx = crossings[0]
|
||||
freq1 = frequencies[idx]
|
||||
freq2 = frequencies[idx + 1]
|
||||
frc1 = frc_curve[idx]
|
||||
frc2 = frc_curve[idx + 1]
|
||||
|
||||
# Linear interpolation: find frequency where FRC == threshold_val
|
||||
resolution_frequency = freq1 + (threshold_val - frc1) * (
|
||||
freq2 - freq1
|
||||
) / (frc2 - frc1)
|
||||
else:
|
||||
# No crossing found, check if all above or all below
|
||||
if np.all(above_threshold):
|
||||
# FRC never drops below threshold, use max frequency
|
||||
resolution_frequency = frequencies[-1]
|
||||
else:
|
||||
# FRC never reaches threshold
|
||||
resolution_frequency = 0
|
||||
|
||||
if resolution_frequency > 0:
|
||||
resolution_pixels = 1 / resolution_frequency
|
||||
resolution_physical = resolution_pixels * pixel_size
|
||||
else:
|
||||
resolution_physical = np.inf
|
||||
|
||||
resolution_dict[method] = resolution_physical
|
||||
|
||||
return frequencies, frc_curve, resolution_dict
|
||||
else:
|
||||
# Compute standard 2D FRC
|
||||
fft1 = np.fft.fft2(lines1)
|
||||
fft2 = np.fft.fft2(lines2)
|
||||
|
||||
# Shift zero frequency to center
|
||||
fft1 = np.fft.fftshift(fft1)
|
||||
fft2 = np.fft.fftshift(fft2)
|
||||
|
||||
# Compute radial frequencies
|
||||
shape = fft1.shape
|
||||
center = [int(s // 2) for s in shape]
|
||||
Y, X = np.ogrid[:shape[0], :shape[1]]
|
||||
r = np.sqrt((Y - center[0]) ** 2 + (X - center[1]) ** 2)
|
||||
r = r.astype(np.int32)
|
||||
|
||||
if n_bins is None:
|
||||
n_bins = min(shape) // 2
|
||||
|
||||
max_r = np.min(center)
|
||||
bin_edges = np.linspace(0, max_r, n_bins + 1)
|
||||
frc_curve = []
|
||||
frequencies = []
|
||||
|
||||
for i in range(len(freq_bins) - 1):
|
||||
# Find frequency indices in this bin
|
||||
mask = (raw_frequencies >= freq_bins[i]) & (raw_frequencies < freq_bins[i+1])
|
||||
n_freq_components = np.sum(mask)
|
||||
for i in range(n_bins):
|
||||
mask = (r >= bin_edges[i]) & (r < bin_edges[i + 1])
|
||||
if np.any(mask):
|
||||
num = np.sum(fft1[mask] * np.conj(fft2[mask]))
|
||||
denom = np.sqrt(np.sum(np.abs(fft1[mask]) ** 2) * np.sum(np.abs(fft2[mask]) ** 2))
|
||||
frc_val = np.abs(num) / denom if denom != 0 else 0
|
||||
frc_curve.append(frc_val)
|
||||
# Frequency in cycles per pixel
|
||||
freq = (bin_edges[i] + bin_edges[i + 1]) / 2 / max_r * 0.5 / pixel_size
|
||||
frequencies.append(freq)
|
||||
|
||||
if n_freq_components > 0:
|
||||
# Extract FFT values in this frequency bin
|
||||
fft1_bin = fft1[:, mask]
|
||||
fft2_bin = fft2[:, mask]
|
||||
|
||||
# Compute FRC for this bin (sum over both lines and frequencies)
|
||||
# see van Heel, M. (2005). https://doi.org/10.1016/j.jsb.2005.05.009
|
||||
numerator = np.sum(fft1_bin * np.conj(fft2_bin))
|
||||
denominator = np.sqrt(np.sum(np.abs(fft1_bin)**2) * np.sum(np.abs(fft2_bin)**2))
|
||||
|
||||
frc_value = np.abs(numerator) / denominator
|
||||
frc_curve.append(frc_value)
|
||||
frequencies.append((freq_bins[i] + freq_bins[i+1]) / 2)
|
||||
|
||||
# Convert lists to numpy arrays
|
||||
frc_curve = np.array(frc_curve)
|
||||
frequencies = np.array(frequencies)
|
||||
|
||||
# Find resolution at each threshold using interpolation
|
||||
resolution_dict = {}
|
||||
for method, threshold_val in thresholds.items():
|
||||
# Find where FRC crosses the threshold
|
||||
above_threshold = frc_curve >= threshold_val
|
||||
|
||||
# Find all crossings (from above to below threshold)
|
||||
crossings = np.where(np.diff(above_threshold.astype(int)) == -1)[0]
|
||||
|
||||
if crossings.size > 0:
|
||||
# Use the first crossing (lowest frequency where FRC drops below threshold)
|
||||
idx = crossings[0]
|
||||
freq1 = frequencies[idx]
|
||||
freq2 = frequencies[idx + 1]
|
||||
frc1 = frc_curve[idx]
|
||||
frc2 = frc_curve[idx + 1]
|
||||
|
||||
# Linear interpolation: find frequency where FRC == threshold_val
|
||||
resolution_frequency = freq1 + (threshold_val - frc1) * (freq2 - freq1) / (frc2 - frc1)
|
||||
else:
|
||||
# No crossing found, check if all above or all below
|
||||
if np.all(above_threshold):
|
||||
# FRC never drops below threshold, use max frequency
|
||||
resolution_frequency = frequencies[-1]
|
||||
# Find resolution at each threshold using interpolation
|
||||
resolution_dict = {}
|
||||
for method, threshold_val in thresholds.items():
|
||||
above_threshold = frc_curve >= threshold_val
|
||||
crossings = np.where(np.diff(above_threshold.astype(int)) == -1)[0]
|
||||
if crossings.size > 0:
|
||||
idx = crossings[0]
|
||||
freq1 = frequencies[idx]
|
||||
freq2 = frequencies[idx + 1]
|
||||
frc1 = frc_curve[idx]
|
||||
frc2 = frc_curve[idx + 1]
|
||||
# Linear interpolation
|
||||
resolution_frequency = freq1 + (threshold_val - frc1) * (freq2 - freq1) / (frc2 - frc1)
|
||||
else:
|
||||
# FRC never reaches threshold
|
||||
resolution_frequency = 0
|
||||
if np.all(above_threshold):
|
||||
resolution_frequency = frequencies[-1]
|
||||
else:
|
||||
resolution_frequency = 0
|
||||
if resolution_frequency > 0:
|
||||
resolution_pixels = 1 / resolution_frequency
|
||||
resolution_physical = resolution_pixels * pixel_size
|
||||
else:
|
||||
resolution_physical = np.inf
|
||||
resolution_dict[method] = resolution_physical
|
||||
|
||||
if resolution_frequency > 0:
|
||||
resolution_pixels = 1 / resolution_frequency
|
||||
resolution_physical = resolution_pixels * pixel_size
|
||||
else:
|
||||
resolution_physical = np.inf
|
||||
|
||||
resolution_dict[method] = resolution_physical
|
||||
|
||||
return frequencies, frc_curve, resolution_dict
|
||||
return frequencies, frc_curve, resolution_dict
|
||||
Reference in new issue
Block a user