-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update to v1.0.0
- Loading branch information
Showing
46 changed files
with
793 additions
and
243 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+206 KB
Examples/0Great/18feb14h_grid1_00014gr_00007sq_v02_00005hl_00005en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+196 KB
Examples/0Great/19may13c_grid4e_8_00084gr_00025sq_v02_00009hl_00014en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+194 KB
Examples/1Decent/18apr26h_grid4e_00123gr_00009sq_00022hl_00010en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+212 KB
Examples/1Decent/19aug16d_Grid2c_00026gr_00004sq_v02_00004hl_00017en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+221 KB
Examples/1Decent/19aug21a_Grid2c_00026gr_00002sq_v02_00002hl_00011en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+211 KB
Examples/1Decent/19jan24d_grid1_00024gr_00001sq_v01_00003hl_00006en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+214 KB
Examples/1Decent/19jun17a_grid4e_8_00054gr_00017sq_00007hl_00003en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+195 KB
...Crack_Breaking_Drifting/19jun18a_grid4e_9_00106gr_00005sq_00011hl_00002en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+151 KB
...ack_Breaking_Drifting/19oct11b_grid2Tilt2_00013gr_00031sq_00005hl_00005en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+174 KB
...ack_Breaking_Drifting/19oct11b_grid2Tilt2_00014gr_00021sq_00004hl_00003en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+205 KB
...Breaking_Drifting/20feb12b_hexFK__00028gr_00028sq_v01_00002hl_v01_00004en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+204 KB
...ack_Breaking_Drifting/20feb19c_grid8a_00074gr_00022sq_v02_00005hl_00007en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+214 KB
Examples/3Empty_no_ice/18dec17a_grid3_00085gr_00105sq_00003hl_00011en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+227 KB
Examples/3Empty_no_ice/18sep26a_grid4e_4_00084gr_00030sq_v02_00002hl_00006en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+131 KB
Examples/3Empty_no_ice/19apr30a_grid1_00056gr_00011sq_00003hl_00003en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+132 KB
Examples/3Empty_no_ice/19aug17a_grid5_00090gr_00048sq_00002hl_00012en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+230 KB
Examples/3Empty_no_ice/19jun18a_grid4e_9_00107gr_00017sq_00009hl_00004en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+199 KB
Examples/4Crystalline_ice/18jul13c_gr1b_00013gr_00001sq_v01_00003hl_00010en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+181 KB
Examples/4Crystalline_ice/19aug14a_grid5_00086gr_00036sq_00005hl_00002en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+198 KB
Examples/4Crystalline_ice/19aug17a_grid5_00055gr_00037sq_00007hl_00002en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+206 KB
...es/4Crystalline_ice/19may13c_grid4e_8_00084gr_00028sq_v02_00003hl_00014en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+209 KB
...ples/4Crystalline_ice/19sep04c_enc_1B_00110gr_00007sq_v01_00002hl_00017en-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+221 KB
...particles_but_vitreous_ice/18dec17a_grid3_00119gr_00088sq_00007hl_00009en-a.png
Oops, something went wrong.
Binary file added
BIN
+204 KB
...icles_but_vitreous_ice/18jan09b_grid2_00006gr_00004sq_v02_00011hl_00005en-a.png
Oops, something went wrong.
Binary file added
BIN
+207 KB
...es_but_vitreous_ice/18sep28b_grid4e_5_00111gr_00025sq_v02_00008hl_00003en-a.png
Oops, something went wrong.
Binary file added
BIN
+221 KB
...icles_but_vitreous_ice/19apr12a_grid2_00012gr_00010sq_v02_00003hl_00013en-a.png
Oops, something went wrong.
Binary file added
BIN
+218 KB
...cles_but_vitreous_ice/19nov08e_grid8a_00085gr_00002sq_v01_00003hl_00010en-a.png
Oops, something went wrong.
Oops, something went wrong.
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
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
from tensorflow.keras import backend as K | ||
|
||
|
||
def normalize(x): | ||
x /= 127.5 | ||
x -= 1. | ||
return x | ||
|
||
|
||
def fft2(x): | ||
return np.fft.fftshift(np.fft.fft2(np.fft.fftshift(x))) | ||
|
||
def fft2_tf(x): | ||
x = tf.cast(x, tf.complex64) | ||
return tf.signal.fftshift(tf.signal.fft2d(tf.signal.fftshift(x))) | ||
|
||
def power_spectrum(x): | ||
return np.abs(fft2(x))**2 | ||
|
||
def power_spectrum_tf(x): | ||
return tf.math.pow(tf.math.abs(fft2_tf(x)), 2) | ||
|
||
|
||
def radial_avg(img): | ||
|
||
x0 = img.shape[1] // 2 | ||
y0 = img.shape[0] // 2 | ||
|
||
x,y = np.meshgrid(np.arange(img.shape[1]), np.arange(img.shape[0])) | ||
R = np.sqrt((x-x0)**2 + (y-y0)**2) | ||
|
||
# calculate the mean | ||
f = lambda r : img[(R >= r-.5) & (R < r+.5)].mean() | ||
img_r = min(img.shape[1], img.shape[0]) // 2 | ||
r = np.linspace(1, img_r, num=img_r) | ||
mean = np.vectorize(f)(r) | ||
|
||
return r, mean | ||
|
||
def radial_avg_tf(img): | ||
|
||
x0 = tf.math.divide(img.shape[2], 2) | ||
x0 = tf.cast(x0, tf.float32) | ||
y0 = tf.math.divide(img.shape[1], 2) | ||
y0 = tf.cast(y0, tf.float32) | ||
|
||
x,y = tf.meshgrid(tf.range(img.shape[2]), tf.range(img.shape[1])) | ||
x = tf.cast(x, tf.float32) | ||
y = tf.cast(y, tf.float32) | ||
R = tf.math.sqrt(tf.math.square(x-x0) + tf.math.square(y-y0)) | ||
# R = K.tile(K.expand_dims(R, axis=0),[tf.shape(img)[0], 1, 1]) | ||
|
||
# calculate the mean | ||
# f = lambda r : tf.reduce_mean(img[(R >= r-.5) & (R < r+.5)]) | ||
f = lambda r : tf.reduce_mean(tf.boolean_mask(img, (R >= r-.5) & (R < r+.5), axis=1), axis=1) | ||
|
||
img_r = tf.math.floordiv(tf.math.minimum(img.shape[2], img.shape[1]), 2) | ||
img_r = tf.cast(img_r, tf.float32) | ||
r = tf.linspace(1.0, img_r, num=tf.cast(img_r, tf.int32)) | ||
mean = tf.map_fn(f, r) | ||
|
||
mean = tf.transpose(tf.squeeze(mean)) | ||
|
||
return r, mean | ||
|
||
|
||
def radavg_logps(x, normalize=False): | ||
|
||
if normalize: | ||
x = normalize(x) | ||
ps_x = power_spectrum(x) | ||
r, mean = radial_avg(np.log(ps_x)) | ||
|
||
return mean | ||
|
||
def radavg_logps_tf(x): | ||
''' | ||
No normalization. | ||
''' | ||
ps_x = power_spectrum_tf(x) | ||
r, mean = radial_avg_tf(tf.math.log(ps_x)) | ||
# mean = K.expand_dims(mean, axis=0) | ||
|
||
return mean | ||
|
||
def radavg_logps_sigmoid_tf(x): | ||
return tf.math.sigmoid(radavg_logps_tf(x)) | ||
|
||
|
||
|
||
# import cv2 | ||
# import matplotlib.pyplot as plt | ||
# x = cv2.imread(r'C:\Users\Mutania\Desktop\micrograph.png', cv2.IMREAD_GRAYSCALE).astype('float64') | ||
# x = normalize(x) | ||
# fft_x = fft2(x) | ||
# ps_x = power_spectrum(x) | ||
# plt.imshow(np.log(ps_x), cmap='gray') | ||
|
||
# r, mean = radial_avg(np.log(ps_x)) | ||
# plt.plot(r, mean) | ||
|
||
# x = tf.convert_to_tensor(x) | ||
# mean = radavg_logps_tf(x) | ||
|
||
|
||
|
||
# import cv2, os | ||
# import utils | ||
|
||
# allimg_path = r'C:\Users\Mutania\Desktop\MicAssess_v1.0_data\micassess_train_k2\data' | ||
# all_mean = [] | ||
|
||
# i = 0 | ||
# for f in os.listdir(allimg_path): | ||
# fname = os.path.join(allimg_path, f) | ||
# x = cv2.imread(fname, cv2.IMREAD_GRAYSCALE).astype('float64') | ||
# x = utils.preprocess(x) | ||
# mean = radavg_logps(x, normalize=False) | ||
# all_mean.append(mean) | ||
# i += 1 | ||
# if i % 500 == 0: | ||
# print(i) | ||
# if i == 2000: | ||
# break | ||
|
||
# avg_mean = np.mean(all_mean, axis=0) | ||
# std_mean = np.std(all_mean, axis=0) | ||
# np.save('radavg_mean.npy', avg_mean) | ||
# np.save('radavg_std.npy', std_mean) | ||
|
||
# max_mean = np.max(all_mean, axis=0) | ||
# min_mean = np.min(all_mean, axis=0) | ||
# np.save('radavg_max.npy', max_mean) | ||
# np.save('radavg_min.npy', min_mean) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
Read mrc files, use FFT to downsample them to smaller png files. | ||
The output png files will have height as 494 px and h/w ratio will be kept. | ||
''' | ||
|
||
import mrcfile | ||
import os | ||
import glob | ||
import numpy as np | ||
import argparse | ||
from PIL import Image | ||
import multiprocessing as mp | ||
from pathlib import Path | ||
from . import star | ||
|
||
def setupParserOptions(): | ||
ap = argparse.ArgumentParser() | ||
ap.add_argument('-i', '--input', help="Provide the path to the micrographs.star file.") | ||
ap.add_argument('-o', '--output', help='Provide the path to the output directory.') | ||
ap.add_argument('--threads', type=int, default=None, | ||
help='Number of threads for conversion. Default is None, using mp.cpu_count(). If get memory error, set it to a reasonable number.') | ||
args = vars(ap.parse_args()) | ||
return args | ||
|
||
def downsample(img, height=494): | ||
''' | ||
Downsample 2d array using fourier transform. | ||
factor is the downsample factor. | ||
''' | ||
m,n = img.shape[-2:] | ||
ds_factor = m/height | ||
width = round(n/ds_factor/2)*2 | ||
F = np.fft.rfft2(img) | ||
A = F[...,0:height//2,0:width//2+1] | ||
B = F[...,-height//2:,0:width//2+1] | ||
F = np.concatenate([A, B], axis=0) | ||
f = np.fft.irfft2(F, s=(height, width)) | ||
return f | ||
|
||
def scale_image(img, height=494): | ||
newImg = downsample(img, height) | ||
newImg = ((newImg - newImg.min()) / ((newImg.max() - newImg.min()) + 1e-7) * 255) | ||
newImg = Image.fromarray(newImg).convert('L') | ||
return newImg | ||
|
||
def save_image(mrc_name, outdir, height=494): | ||
# print(mrc_name) | ||
try: | ||
micrograph = mrcfile.open(mrc_name, permissive=True).data | ||
micrograph = micrograph.reshape((micrograph.shape[-2], micrograph.shape[-1])) | ||
newImg = scale_image(micrograph, height) | ||
newImg.save(os.path.join(outdir, os.path.splitext(os.path.basename(mrc_name))[0] + '.png')) | ||
except ValueError: | ||
print('An error occured when trying to save ', mrc_name) | ||
pass | ||
|
||
def mrc2png(args): | ||
# os.chdir(args['output']) | ||
mic_list = star.star2miclist(args['input']) | ||
Path(os.path.join(args['output'], 'png', 'data')).mkdir(parents=True, exist_ok=True) | ||
threads = mp.cpu_count() if args['threads'] is None else args['threads'] | ||
with mp.Pool(threads) as pool: | ||
print('Converting in %d parallel threads....' %threads) | ||
pool.starmap(save_image, ((mrc_name, os.path.join(args['output'], 'png', 'data')) for mrc_name in mic_list)) | ||
print('Conversion finished.') | ||
|
||
if __name__ == '__main__': | ||
args = setupParserOptions() | ||
mrc2png(args) |
Oops, something went wrong.