<jupyter_start><jupyter_text>Set options<jupyter_code># Set this to true if you wish to save a new trasformed dataset
save_results = True<jupyter_output><empty_output><jupyter_text>Setup plotting<jupyter_code>phase_ticks = np.arange(-180, 181, 45)
freq_limit_low = 5.1
freq_limit_high = 7.8
ticks_pi = np.linspace(-np.pi, np.pi, 9)
ticks_pi_deg = ticks_pi * 180/np.pi
face_colors = {
    'orange': [0.8906, 0.4609, 0.4062],
    'gray': [0.6523, 0.6484, 0.6484],
    'blue': [0.5156, 0.5977, 0.8789]
}
edge_colors = {
    'orange': [0.9961, 0, 0],
    'gray': [0.4805, 0.4766, 0.4766],
    'blue': [0, 0, 0.9961]
}

edge_colors_arr = [ec for ec in edge_colors.values()]
face_colors_arr = [fc for fc in face_colors.values()]

cycler = (plt.cycler(mec=edge_colors_arr)+
          plt.cycler(mfc=face_colors_arr)+
          plt.cycler(color=edge_colors_arr))
plt.rcParams.update({
    'figure.figsize': (12, 8),
    'font.size': 20,
    'lines.markeredgewidth': 2,
    'lines.markersize': 9,
    'lines.marker': 'o',
    'lines.linestyle': '',
    'lines.linewidth': 3,
    'axes.prop_cycle': cycler
    })<jupyter_output><empty_output><jupyter_text>Load and prepare dataHere we load the corresponding data. The data gets normalized to a domain between 0 and 1 for each image individually. We also set the path to the rephased images, since they are needed for the following parts in the paper.Use this to rephase the _56 files.<jupyter_code>source_path = 'data/single_cut_56.h5'
dest_path = 'data/single_cut_rephased.h5'
images, data = hd.load_dataset(source_path,
                               ['micromotion_phase', 'freq'])
mmphase = data['micromotion_phase']
images = hd.normalize_single_images(images)
images = hd.prepare_images_tensorflow(images)<jupyter_output><empty_output><jupyter_text>Load the trained modelsTo train the models run 3b_train_micromotion_VAE.py<jupyter_code>encoder = tf.keras.models.load_model('networks/micromotion_rephasing/IIIb_VAE_micromotion_encoder.h5')
decoder = tf.keras.models.load_model('networks/micromotion_rephasing/IIIb_VAE_micromotion_decoder.h5')
latent_dim = int(encoder.layers[-1].units / 2)
encoder.summary()
decoder.summary()
model = CVAE(latent_dim, encoder, decoder)<jupyter_output><empty_output><jupyter_text>Transform data Create a batched dataset to respect GPU memory. If you do not use a GPU you noramlly have free choice.<jupyter_code>dataset = tf.data.Dataset.from_tensor_slices((images, mmphase)).batch(200)
def rephase_images(phase_shift):
    rephased_images = np.empty((0, 56, 56))
    for b_images, b_mmphase in dataset:
        b_rephased_images = model.dephase(b_images, -b_mmphase + phase_shift).numpy()
        b_rephased_images = np.squeeze(b_rephased_images)
        rephased_images = np.append(rephased_images, b_rephased_images, 0)
    return rephased_images<jupyter_output><empty_output><jupyter_text>Actual rephasingHere the data gets rephased to micromotion phase of 0.<jupyter_code>rephased_images = np.empty((0, 56, 56))
for b_images, b_mmphase in dataset:
    b_rephased_images = model.dephase(b_images, -b_mmphase).numpy()
    b_rephased_images = np.squeeze(b_rephased_images)
    rephased_images = np.append(rephased_images, b_rephased_images, 0)<jupyter_output><empty_output><jupyter_text>Save rephased dataset<jupyter_code>if save_results:
    with h5py.File(dest_path, 'w') as dest_file:
        with h5py.File(source_path, 'r') as source_file:
            source_file.copy('parameter', dest_file)
            dest_file['images'] = rephased_images<jupyter_output><empty_output><jupyter_text>Plot samples<jupyter_code>pick_freq = 7.4
selection_map = np.where((data['freq'] == pick_freq))[0]

displayimages_x = np.squeeze(images[selection_map])
original_mm_phase_display = data['micromotion_phase'][selection_map]
displayimages_y = rephased_images[selection_map]

fig, ax = plt.subplots(nrows=2, ncols=5, figsize=(2*5, 3.5))
for i in range(5):
    ax[0,i].imshow(np.squeeze(displayimages_x[i]))
    ax[1,i].imshow(np.squeeze(displayimages_y[i]))
plt.show()

for i in range(5):
    plt.imshow(np.squeeze(displayimages_x[i]))
    plt.axis('off')
    plt.clim(np.min(displayimages_x), np.max(displayimages_x))
    plt.colorbar()
    plt.clf()
    
    plt.imshow(np.squeeze(displayimages_y[i]))
    plt.axis('off')
    plt.clim(np.min(displayimages_y), np.max(displayimages_y))
    plt.colorbar()
    plt.clf()
pick_freq = 5.8
selection_map = np.where((data['freq'] == pick_freq))[0]

displayimages_x = np.squeeze(images[selection_map])
original_mm_phase_display = data['micromotion_phase'][selection_map]
displayimages_y = rephased_images[selection_map]

fig, ax = plt.subplots(nrows=2, ncols=5, figsize=(2*5, 3.5))
for i in range(5):
    ax[0,i].imshow(np.squeeze(displayimages_x[i]))
    ax[1,i].imshow(np.squeeze(displayimages_y[i]))
plt.show()

for i in range(5):
    plt.imshow(np.squeeze(displayimages_x[i]))
    plt.axis('off')
    plt.clim(np.min(displayimages_x), np.max(displayimages_x))
    plt.colorbar()
    plt.clf()
    
    plt.imshow(np.squeeze(displayimages_y[i]))
    plt.axis('off')
    plt.clim(np.min(displayimages_y), np.max(displayimages_y))
    plt.colorbar()
    plt.clf()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13420496058450299, 'fraction_numerical': 0.029801961161315133, 'mean_word_length': 5.12, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '448802', 'license': 'MIT', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1983, 'n_words': 345, 'path': '3b_micromotion_removal_validation.ipynb', 'repo_name': 'nkaeming/unsupervised-machine-learning-of-topological-phase-transitions-from-experimental-data', 'size': '5201'}