diff --git a/.ipynb_checkpoints/ptychoScopy-checkpoint.ipynb b/.ipynb_checkpoints/ptychoScopy-checkpoint.ipynb index 268e78d..1456a12 100644 --- a/.ipynb_checkpoints/ptychoScopy-checkpoint.ipynb +++ b/.ipynb_checkpoints/ptychoScopy-checkpoint.ipynb @@ -2,19 +2,10 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 79, "id": "7425242d-3c91-4c1e-a424-08625a38ee7a", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\skoupy_r\\Anaconda3\\lib\\site-packages\\scipy\\__init__.py:155: UserWarning: A NumPy version >=1.18.5 and <1.25.0 is required for this version of SciPy (detected version 1.26.1\n", - " warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n" - ] - } - ], + "outputs": [], "source": [ "### Initial packages import ###################################################\n", "import numpy as np\n", @@ -25,7 +16,7 @@ "from pandas import read_excel\n", "from IPython.display import display\n", "from plotly.subplots import make_subplots\n", - "from ipywidgets import interactive_output, HBox, VBox, Layout, Label, Valid, ToggleButtons, RadioButtons, Dropdown, IntSlider, Checkbox, Button, ButtonStyle, GridBox\n", + "from ipywidgets import interactive_output, HBox, VBox, Layout, Label, Valid, ToggleButtons, RadioButtons, Dropdown, IntSlider, Checkbox, Button, ButtonStyle, GridBox,IntSlider\n", "\n", "# print(\"Numpy version \", np.__version__)\n", "# print(\"Pandas version \", pd.__version__)\n", @@ -300,31 +291,37 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 389, "id": "8055b802-cf83-4250-aea9-54e3e6b73db0", "metadata": {}, "outputs": [], "source": [ "### CONTROLS ##########################################\n", "\n", - "ali = dict(style = {'description_width': '60px','button_width': '33px'}, button_style='',disabled=False)\n", - "ali2 = dict(style = {'description_width': '60px','button_width': '90px'}, button_style='',disabled=False)\n", - "header = Button(description='STEM control panel',layout=Layout(width='auto', grid_area='header'), style=ButtonStyle(button_color='silver'))\n", + "ali = dict(style = {'description_width': '60px','button_width': '35px', 'font_weight': 'bold'}, button_style='',disabled = False)\n", + "ali2 = dict(style = {'description_width': '60px','button_width': '90px', 'font_weight': 'bold'}, button_style='',disabled = False)\n", "\n", - "beam_set = Button(description='Electron beam settings',layout=Layout(width='auto', grid_area='beam_set'), style=ButtonStyle(button_color='lightsteelblue')) \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "header = Button(description='STEM control panel',layout=Layout(width='auto', grid_area='header'), style=ButtonStyle(button_color='silver', font_weight= 'bold',))\n", + "\n", + "beam_set = Button(description='Electron beam settings',layout=Layout(width='auto', grid_area='beam_set'), style=ButtonStyle(button_color='lightsteelblue', font_weight= 'bold')) \n", "beam = ToggleButtons(options=opt.energies(), value=200,description='E (keV)', layout=Layout(width='auto', grid_area='beam_set1'), **ali)\n", "aperture = ToggleButtons(options=opt.apertures(), value='30um', description='Aperture', layout=Layout(width='auto', grid_area='beam_set2'), **ali)\n", "probe = ToggleButtons(options=opt.probes(), value='8C', description='Probe', layout=Layout(width='auto', grid_area='beam_set3'), **ali)\n", "defocus = IntSlider(description='Δf (nm)', value=0, min=0, max=1000, step=1, continuous_update=False, layout=Layout(width='auto', grid_area='beam_set4'), **ali)\n", "\n", - "scanning_set = Button(description='Scanning parameters',layout=Layout(width='auto', grid_area='scanning_set'),style=ButtonStyle(button_color='lightsteelblue'))\n", + "scanning_set = Button(description='Scanning parameters',layout=Layout(width='auto', grid_area='scanning_set'),style=ButtonStyle(button_color='lightsteelblue', font_weight= 'bold'))\n", "mag = ToggleButtons(options=opt.magnifications(),value=3,description='Mag. Mx', layout=Layout(width='auto', grid_area='scanning_set1'), **ali)\n", "matrix = ToggleButtons(options=opt.mappings(), value=2048, description='Matrix', layout=Layout(width='auto', grid_area='scanning_set2'), **ali)\n", "dwell_time = ToggleButtons(options=opt.dwelltimes(), value=10, description='t (μs)', layout=Layout(width='auto', grid_area='scanning_set3'), **ali)\n", "\n", "\n", "\n", - "camera_set = Button(description='Detection',layout=Layout(width='auto', grid_area='camera_set'),style=ButtonStyle(button_color='lightsteelblue'))\n", + "camera_set = Button(description='Detection',layout=Layout(width='auto', grid_area='camera_set'),style=ButtonStyle(button_color='lightsteelblue', font_weight= 'bold'))\n", "cl = ToggleButtons(options=opt.cameralengths(), value=12, description='CL (cm)', layout=Layout(width='auto', grid_area='cl_set1'), **ali)\n", "camera = ToggleButtons(options=opt.detectors(), description='Detector', layout=Layout(width='auto', grid_area='camera_set1'), **ali2)\n", "restriction = ToggleButtons(options=[('.',False), ('..',True)], value = False, description='PAAR',icons = ['times','check'], layout=Layout(width='auto', grid_area='camera_set2'), **ali)\n", @@ -348,7 +345,7 @@ "footer = Button(description='Footer',layout=Layout(width='auto', grid_area='footer'),style=ButtonStyle(button_color='silver'))\n", "\n", "\n", - "controls = GridBox(children=[header, beam_set, beam, aperture, probe, defocus, scanning_set, mag,matrix,dwell_time, cl,camera_set, camera, restriction, binning, beam_res, aperture_res,aperture_res2,fov_res, dwell_time_res,footer],\n", + "controls = GridBox(children=[header, beam_set, beam, aperture, probe, defocus, scanning_set, mag,matrix,dwell_time, cl,camera_set, camera, restriction, binning, beam_res, aperture_res,aperture_res2,fov_res, dwell_time_res],\n", " layout=Layout(\n", " width='22%',\n", " grid_template_rows='auto auto auto',\n", @@ -375,49 +372,56 @@ "\n", "# SMALL CONTROLS\n", "\n", - "descr = '50px'\n", - "butt = '30px'\n", + "descr = '80px'\n", + "butt = '60px'\n", "\n", - "header = Button(description='Graph controls',layout=Layout(width='auto', grid_area='header'),style=ButtonStyle(button_color='whitesmoke'))\n", + "header = Button(description='Graph controls',layout=Layout(width='auto', grid_area='header'),style=ButtonStyle(button_color='whitesmoke', font_weight= 'bold'))\n", "\n", - "method = ToggleButtons(options=[('SSB','direct'), ('Iter','iterative')], description='Method', tooltips=['SSB', 'PIE, MLc, DM'],\n", - " layout=Layout(width='auto', grid_area='methods'), style = {'description_width': descr,'button_width': butt}, button_style='',disabled=False,)\n", + "method = ToggleButtons(options=[('SSB','direct'), ('Iterative','iterative')], description='Method', tooltips=['SSB', 'PIE, MLc, DM'],\n", + " layout=Layout(width='auto', grid_area='methods'), style = {'description_width': descr,'button_width': butt, 'font_weight': 'bold'}, button_style='',disabled=False,)\n", "\n", - "graph_size = ToggleButtons(options=[('XXS', 50),('XS', 70),('S', 90), ('M', 100), ('L', 120)], value=100, description='Graphs size',\n", + "graph_size = IntSlider(value=100,min=10,max=200,step=5, description='Graphs size',\n", " layout=Layout(width='auto', grid_area='gr_size'), style = {'description_width': descr,'button_width': butt}, button_style='',disabled=False,)\n", "\n", "ctf_xaxis = RadioButtons(options=['α','mrad', 'A'], description='CTF x-axis:',\n", " layout=Layout(width='auto', grid_area='ctxaxis'), style = {'description_width': descr,'button_width': butt}, disabled=False,)\n", "\n", "cl_check_laa = Checkbox(value=True, description='Low anlge approximation', indent=False,\n", - " layout=Layout(width='auto', grid_area='check1'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Low angle approximation check - scattering angle < 10 deg\n", - "cl_check_pix = Checkbox(value=True, description='Recon. pixel < step size',indent=False,\n", - " layout=Layout(width='auto', grid_area='check2'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Reconstructed pixel size < scanning step size \n", + " layout=Layout(width='auto', grid_area='check1'), style = {'description_width': descr}, disabled=False,) # Low angle approximation check - scattering angle < 10 deg\n", + "cl_check_pix = Checkbox(value=True, description='Ptycho pixel < step size',indent=False,\n", + " layout=Layout(width='auto', grid_area='check2'), style = {'description_width': descr}, disabled=False,) # Reconstructed pixel size < scanning step size \n", "cl_check_def = Checkbox(value=True, description='Beam fits in probe window', indent=False,\n", - " layout=Layout(width='auto', grid_area='check3'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Is probe window big enough to accomodate defocused probe?\n", + " layout=Layout(width='auto', grid_area='check3'), style = {'description_width': descr}, disabled=False,) # Is probe window big enough to accomodate defocused probe?\n", "cl_check_1bf = Checkbox(value=True, description='Detector cover < 1α', indent=False,\n", - " layout=Layout(width='auto', grid_area='check4'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Detector cover is lower than 1 α\n", + " layout=Layout(width='auto', grid_area='check4'), style = {'description_width': descr}, disabled=False,) # Detector cover is lower than 1 α\n", "cl_check_nbf = Checkbox(value=True, description='Detector cover > 6α', indent=False,\n", - " layout=Layout(width='auto', grid_area='check5'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Detector cover is higher than 6 α\n", + " layout=Layout(width='auto', grid_area='check5'), style = {'description_width': descr}, disabled=False,) # Detector cover is higher than 6 α\n", "\n", - "footer = Button(description='Interactive results',layout=Layout(width='auto', grid_area='footer'),style=ButtonStyle(button_color='whitesmoke'))\n", "\n", - "small_controls = GridBox(children=[header, method, graph_size, ctf_xaxis, cl_check_laa,cl_check_pix,cl_check_def, cl_check_1bf, cl_check_nbf, footer],\n", + "chart = ToggleButtons(options=[('STEM','micr'), ('Charts','dedic'),('Both','both')],value = 'both', description='Show',\n", + " layout=Layout(width='auto', grid_area='charts'), style = {'description_width': descr,'button_width': butt, 'font_weight': 'bold'}, button_style='',disabled=False,)\n", + "\n", + "\n", + "footer = Button(description='Interactive results',layout=Layout(width='auto', grid_area='footer'),style=ButtonStyle(button_color='whitesmoke', font_weight= 'bold'))\n", + "\n", + "small_controls = GridBox(children=[header, method, chart, graph_size, ctf_xaxis, cl_check_laa,cl_check_pix,cl_check_def, cl_check_1bf, cl_check_nbf, footer],\n", " layout=Layout(\n", " width='100%',\n", " grid_template_rows='auto auto auto',\n", - " grid_template_columns='40% 20% 20% 20%',\n", + " grid_template_columns='50% 20% 30%',\n", " grid_template_areas='''\n", - " \"header header header header \"\n", - " \"methods ctxaxis check1 check2\"\n", - " \"gr_size ctxaxis check3 check5\"\n", - " \". ctxaxis check4 . \" \n", - " \"footer footer footer footer\"\n", + " \"header header header \"\n", + " \"methods ctxaxis check1 \"\n", + " \"charts ctxaxis check2 \"\n", + " \"gr_size ctxaxis check3 \"\n", + " \". . check4 \"\n", + " \". . check5 \"\n", + " \"footer footer footer\"\n", " ''')\n", " )\n", "\n", "\n", - "def ptycho_interact(beam, aperture, aperture_res, aperture_res2, probe, cl, matrix, defocus, mag, camera, binning, dwell_time, restriction, method, ctf_xaxis, cl_check_laa, cl_check_pix, cl_check_def, cl_check_1bf, cl_check_nbf, graph_size):\n", + "def ptycho_interact(beam, aperture, aperture_res, aperture_res2, probe, cl, matrix, defocus, mag, camera, binning, dwell_time, restriction, method, ctf_xaxis, cl_check_laa, cl_check_pix, cl_check_def, cl_check_1bf, cl_check_nbf, graph_size, chart):\n", " \n", " ### SINGLE SETTING PARAMETERS ###\n", " wavelength = pty.get_wavelength(beam)*1e12\n", @@ -464,59 +468,90 @@ " pctf = pty.get_ssb_ctf()\n", " apertury = opt.apertures()\n", " \n", + "\n", + "\n", + "\n", + "\n", + "\n", " \n", " ### SAMPLE PLANE ##########################################\n", " points = 5\n", " yyy = np.append(np.linspace(0,overlap,100), 200)\n", - " wid = np.linspace(0,points-1,points) * step_size_corr\n", + " wid = np.array([-2, -1, 0, 1, 2]) * step_size_corr\n", " \n", " fig5 = make_subplots(specs=[[{\"secondary_y\": True}]])\n", - " fig5.update_xaxes(title_text=\"Sample plane (nm)\", range=[0, points*step_size_corr], showgrid=False, zeroline=False)\n", - " fig5.update_yaxes(range=[0, points*step_size_corr],showgrid=False, showticklabels=False)\n", + " fig5.update_xaxes(title_text=\"Sample plane (nm)\", range=[-4*step_size_corr, 4*step_size_corr], showgrid=False, zeroline=False)\n", + " fig5.update_yaxes(range=[-4*step_size_corr, 4*step_size_corr],showgrid=False, showticklabels=False)\n", " for x in range(len(wid)):\n", " fig5.add_trace(go.Scatter(showlegend=False, x=wid, y=np.ones(len(wid))*wid[x], marker_color='blue'),secondary_y=False)\n", " for x in range(len(wid)): \n", " for y in range(len(wid)): \n", " fig5.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", opacity=0.1, fillcolor=\"#FF7F7F\",name=False, x0=wid[x]-beam_diameter/2, y0=wid[y]-beam_diameter/2, x1=wid[x]+beam_diameter/2, y1=wid[y]+beam_diameter/2, line_color=\"red\",secondary_y=False)\n", "\n", - " fig5.add_trace(go.Scatter(showlegend=True, x=[-1], y=[-1],marker_size=12, marker_color='blue', name='Step '+str(np.round(10*step_size,3))+' Å ('+str(np.round(10*step_size_corr,3))+')'))\n", + " fig5.add_annotation(x=-1*step_size_corr, y=-2*step_size_corr, ax=-2*step_size_corr, ay=-2*step_size_corr, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=-2*step_size_corr, y=-1*step_size_corr, ax=-2*step_size_corr, ay=-2*step_size_corr, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=-1.25*step_size_corr, y=-1.5*step_size_corr, text=str(np.round(10*step_size_corr,3))+' Å', showarrow=False, yshift=0)\n", + " \n", + " fig5.add_annotation(x=2*step_size_corr-0.75*beam_diameter/2, y=-2*step_size_corr-0.75*beam_diameter/2, ax=2*step_size_corr, ay=-2*step_size_corr,xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=2*step_size_corr+0.75*beam_diameter/2, y=-2*step_size_corr+0.75*beam_diameter/2, ax=2*step_size_corr, ay=-2*step_size_corr,xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=2.25*step_size_corr, y=-2*step_size_corr, text=str(np.round(10*beam_diameter,2))+' Å', showarrow=False, yshift=-15)\n", + " \n", + " \n", " if method == 'direct':\n", " xx = 2*wavelength/(np.sin(semi_angle_corr*omega/1000))/100\n", " fig5.add_trace(go.Scatter(showlegend=True, x=[-1], y=[-1],marker_size=12, marker_color='green', name='Rec step '+str(np.round(xx[-1]/3,2))+' Å')) \n", " \n", - " fig5.add_trace(go.Scatter(showlegend=True, x=[-1], y=[-1],marker_size=12, marker_color='#FF7F7F', name='Beam ⌀ '+ str(np.round(10*beam_diameter,2))+\" Å\"))\n", " fig5.update_layout(legend=dict(orientation=\"v\",yanchor=\"bottom\",y=0.85,xanchor=\"right\",x=0.9))\n", - " fig5.add_trace(go.Scatter(showlegend=False, x = points*step_size_corr*np.ones(len(yyy)), y=yyy, opacity=1,mode='markers',marker_symbol = \"triangle-down\",marker=dict(size=20, color=yyy, colorscale='rainbow_r', showscale=False)),secondary_y=True) \n", - " fig5.update_yaxes(title_text=\"Overlap %\", range=[0, 100],showgrid=False, showticklabels=True, tickvals = [0, 20, 40,50, 60,70, 80, 90, 100], secondary_y=True)\n", - " fig5.update_layout(title={'text': \"Sample\",'y':0.95, 'x':0.30,'xanchor': 'left','yanchor': 'top'})\n", + " fig5.add_trace(go.Scatter(showlegend=False, x = -4*step_size_corr*np.ones(len(yyy)), y=yyy, opacity=1,mode='markers',marker_symbol = \"triangle-down\",marker=dict(size=20, color=yyy, colorscale='rainbow_r', showscale=False)),secondary_y=True) \n", + " fig5.update_yaxes(range=[0, 100], showgrid=False, showticklabels=False, secondary_y=True)\n", + " fig5.update_yaxes(title_text=\"Overlap %\", showgrid=False, showticklabels=True, tickvals = [-4*step_size_corr, -2*step_size_corr, 0, 2*step_size_corr, 4*step_size_corr],ticktext = ['0', '25', '50', '75', '100'], secondary_y=False)\n", + " fig5.update_layout(title={'text': \"Sample\",'y': 0.975, 'x': 0.30,'xanchor': 'left','yanchor': 'top'})\n", " fig5.update_layout(width=4.5*graph_size, height=3.0*graph_size, margin =dict(l=1.3*graph_size, r=0.3*graph_size, t=0.1*graph_size, b=0))\n", "\n", " \n", + "\n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " ### DETECTOR PLANE ##########################################\n", - " fig6 = go.Figure() \n", - " fig6.update_xaxes(title_text=\"Pixels\", range=[1, num_pixels], zeroline=False)\n", - " fig6.update_yaxes(range=[1, num_pixels], showticklabels=False,)\n", - " for x in np.linspace(1,int(np.round(covered_alfas)),int(np.round(covered_alfas))):\n", - " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", x0=num_pixels/2-x*beam_diameter_pix, y0=num_pixels/2-x*beam_diameter_pix, x1=num_pixels/2+x*beam_diameter_pix, y1=num_pixels/2+x*beam_diameter_pix,line_color=\"gray\")\n", - " fig6.add_trace(go.Scatter(showlegend=False,x=[num_pixels/2],y=[num_pixels/2-x*beam_diameter_pix],mode=\"lines+text\", text=str(int(2*x))+\"α\",textposition=\"top center\"))\n", + " fig6 = make_subplots(specs=[[{\"secondary_y\": True}]])\n", " \n", - " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", fillcolor=\"#FF7F7F\", x0=num_pixels/2-beam_diameter_pix/2, y0=num_pixels/2-beam_diameter_pix/2, x1=num_pixels/2+beam_diameter_pix/2, y1=num_pixels/2+beam_diameter_pix/2,line_color=\"#FF7F7F\")\n", - " fig6.add_annotation(x=num_pixels/2-beam_diameter_pix/2, y=num_pixels/2, ax=num_pixels/2+beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=3,arrowsize=1,arrowwidth=1,arrowcolor='black')\n", - " fig6.add_annotation(x=num_pixels/2+beam_diameter_pix/2,y=num_pixels/2, ax=num_pixels/2-beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y', text='', showarrow=True,arrowhead=3,arrowsize=1,arrowwidth=1,arrowcolor='black')\n", + " for x in np.linspace(1,int(np.round(covered_alfas)),int(np.round(covered_alfas))):\n", + " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", x0=num_pixels/2-x*beam_diameter_pix, y0=num_pixels/2-x*beam_diameter_pix, x1=num_pixels/2+x*beam_diameter_pix, y1=num_pixels/2+x*beam_diameter_pix, line=dict(color=\"gray\",width=1,dash=\"dot\",),secondary_y=False)\n", + " fig6.add_trace(go.Scatter(showlegend=False,x=[num_pixels/2],y=[num_pixels/2+x*beam_diameter_pix],mode=\"lines+text\", text=str(int(2*x))+\"α\",textposition=\"top center\"),secondary_y=False)\n", + " \n", + " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\",opacity=0.5, fillcolor=\"#FF7F7F\", x0=num_pixels/2-beam_diameter_pix/2, y0=num_pixels/2-beam_diameter_pix/2, x1=num_pixels/2+beam_diameter_pix/2, y1=num_pixels/2+beam_diameter_pix/2,line_color=\"#FF7F7F\",secondary_y=False)\n", + " fig6.add_annotation(x=num_pixels/2-beam_diameter_pix/2, y=num_pixels/2, ax=num_pixels/2+beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig6.add_annotation(x=num_pixels/2+beam_diameter_pix/2,y=num_pixels/2, ax=num_pixels/2-beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y', text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", " fig6.add_annotation(x=num_pixels/2, y=num_pixels/2, text=str(np.round(beam_diameter_pix,1))+\" pix\", showarrow=False, yshift=10)\n", - " fig6.add_trace(go.Scatter(x=[1,1.5,1,1.5], y=[1,1,1.5,1.5], name='Pix ∠ ' +str(np.round(pixel_angle,2))+' mrad'))\n", - " fig6.add_trace(go.Scatter(x=[1,1.5,1,1.5], y=[1,1,1.5,1.5], name='Pixel size ' +str(np.round(size_pixel,2))+' um'))\n", - " fig6.update_layout(legend=dict(orientation=\"v\",yanchor=\"bottom\",y=0.80,xanchor=\"right\",x=1))\n", - " fig6.update_layout(title={'text': \"Detector\",'y':0.95, 'x':0.11,'xanchor': 'left','yanchor': 'top'})\n", - " fig6.update_layout(xaxis = dict(tickmode = 'array', tickvals = [1, num_pixels/4, num_pixels/2, 3*num_pixels/4, num_pixels])) \n", - " fig6.update_layout(width=3.5*graph_size, height=3*graph_size, margin = dict(l=0.35*graph_size, r=0.65*graph_size, t=0.3*graph_size, b=0))\n", + " fig6.add_annotation(x=num_pixels/2, y=num_pixels/2, text=\"1 pix = \"+str(np.round(pixel_angle,2))+\" mrad\", showarrow=False, yshift=-10)\n", + " fig6.add_annotation(x=num_pixels/2, y=num_pixels/10, text=\"Real pixel size = \"+str(np.round(size_pixel,2))+\" μm\", showarrow=False, yshift=0)\n", + " fig6.add_trace(go.Scatter(showlegend=True,x=[-1], y=[-1], marker_color='red', name = \"BF disk\"),secondary_y=False)\n", + " \n", + " fig6.add_trace(go.Scatter(showlegend=False,x=[-1], y=[-1]),secondary_y=True)\n", + " \n", + " fig6.update_layout(legend=dict(orientation=\"h\",yanchor=\"bottom\",y=1.00,xanchor=\"right\",x=0.9))\n", + " fig6.update_layout(title={'text': \"Detector\",'y':0.975, 'x':0.11,'xanchor': 'left','yanchor': 'top'})\n", + " \n", + " fig6.update_xaxes(title_text=\"Pixels\", range=[1, num_pixels], zeroline=False, tickvals = [1, num_pixels/4, num_pixels/2, 3*num_pixels/4, num_pixels]) \n", + " fig6.update_yaxes(range=[1, num_pixels], showticklabels=False, showgrid=False, secondary_y=False)\n", + " fig6.update_yaxes(title_text=\"Angular cover (mrad)\", range=[1, num_pixels], showticklabels=True, tickvals = [1, num_pixels/4, num_pixels/2, 3*num_pixels/4, num_pixels], ticktext = ['', np.round(detector_cover/4,1), np.round(detector_cover/2,1), np.round(3*detector_cover/4,1), np.round(detector_cover,1)], secondary_y=True) \n", + " \n", + " fig6.update_layout(width=3.5*graph_size, height=3*graph_size, margin = dict(l=0.35*graph_size, r=0.48*graph_size, t=0.3*graph_size, b=0))\n", "\n", " \n", " \n", " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " focusdepth = wavelength/(semi_angle_corr/1000)**2/1000\n", " ssb_cl, kolik = pty.cl4ssb(detector_cover_a_all)\n", " ### MICROSCOPE SCHEME ##########################################\n", @@ -654,20 +689,27 @@ " fig4.update_layout(xaxis = dict(tickmode = 'array', tickvals = opt.cameralengths()))\n", " fig4.update_layout(width=8.0*graph_size, height=2.25*graph_size, margin =dict(l=1.3*graph_size, r=0.25*graph_size, t=0.3*graph_size, b=0.3*graph_size))\n", "\n", - " ### SHOWING ###\n", - " right_column = VBox([go.FigureWidget(fig),HBox([go.FigureWidget(fig5),go.FigureWidget(fig6)]),go.FigureWidget(fig4)])\n", - " left_column = VBox([go.FigureWidget(fig8)])\n", - " \n", - "\n", + " \n", " \n", + " ### SHOWING ###\n", + " if chart == 'micr':\n", + " left_column = VBox([go.FigureWidget(fig8)])\n", + " total = HBox([left_column]) \n", " \n", - " total = HBox([left_column, right_column])\n", - " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px')\n", - " \n", + " elif chart == 'dedic':\n", + " right_column = VBox([go.FigureWidget(fig) ,HBox([go.FigureWidget(fig5),go.FigureWidget(fig6)]),go.FigureWidget(fig4)])\n", + " total = HBox([right_column])\n", + " \n", + " else: \n", + " right_column = VBox([go.FigureWidget(fig) ,HBox([go.FigureWidget(fig5),go.FigureWidget(fig6)]),go.FigureWidget(fig4)])\n", + " left_column = VBox([go.FigureWidget(fig8)])\n", + " total = HBox([left_column, right_column])\n", + " \n", + " \n", + " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px') \n", " display(total)\n", - " \n", - " \n", - " \n", + " \n", + "\n", " \n", " \n", " if method == 'iterative': \n", @@ -781,7 +823,7 @@ " \n", " dictionary[i] = ToggleButtons(options=data_tuple, button_style=button_style, icons = icons, **align6)\n", " \n", - " legend = ToggleButtons(options=['Ptycho pix. (Å)','Det. cover (mrad)','Det. cover (α)', 'Max. def. (nm)','Selected by CL ','Nominal CL (cm)',], \n", + " legend = ToggleButtons(options=['Ptycho pixel (Å)','Det. cover (mrad)','Det. cover (α)', 'Max. def. (nm)','Selected by CL ','Nominal CL (cm)',], \n", " tooltips=['Dependent on: detector, beam energy, camera length',\n", " 'Dependent on: detector, camera length',\n", " 'Dependent on: detector, camera length, probe semi-angle',\n", @@ -796,22 +838,32 @@ " \n", " \n", " \n", + " \n", " ### SHOWING ###\n", - " sample_overlap = HBox([go.FigureWidget(fig5), go.FigureWidget(fig7), go.FigureWidget(fig6)]) \n", - " right_column = VBox([HBox([go.FigureWidget(fig),checks]), sample_overlap, cltab]) \n", - " left_column = VBox([go.FigureWidget(fig8)]) \n", - " \n", - " \n", - " total = HBox([left_column, right_column])\n", - " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px')\n", - " \n", + " if chart == 'micr':\n", + " left_column = VBox([go.FigureWidget(fig8)])\n", + " total = HBox([left_column]) \n", + " \n", + " elif chart == 'dedic':\n", + " sample_overlap = HBox([go.FigureWidget(fig5), go.FigureWidget(fig7), go.FigureWidget(fig6)]) \n", + " right_column = VBox([HBox([go.FigureWidget(fig),checks]), sample_overlap, cltab]) \n", + " total = HBox([right_column])\n", + " \n", + " else: \n", + " sample_overlap = HBox([go.FigureWidget(fig5), go.FigureWidget(fig7), go.FigureWidget(fig6)]) \n", + " right_column = VBox([HBox([go.FigureWidget(fig),checks]), sample_overlap, cltab]) \n", + " left_column = VBox([go.FigureWidget(fig8)]) \n", + " total = HBox([left_column, right_column]) \n", + " \n", + " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px') \n", " display(total)\n", + "\n", " \n", " return \n", " \n", "gui = interactive_output(ptycho_interact, {\"beam\": beam, \"aperture\": aperture,\"aperture_res\": aperture_res,\"aperture_res2\": aperture_res2, \"probe\": probe, \"cl\": cl, \"matrix\": matrix, \"defocus\": defocus, \"mag\": mag, \"camera\": camera,\n", " \"binning\": binning, \"dwell_time\": dwell_time, \"restriction\": restriction, \"method\": method, \"ctf_xaxis\": ctf_xaxis,\n", - " \"cl_check_laa\":cl_check_laa, \"cl_check_pix\": cl_check_pix, \"cl_check_def\": cl_check_def, \"cl_check_1bf\": cl_check_1bf, \"cl_check_nbf\": cl_check_nbf, \"graph_size\": graph_size}) \n", + " \"cl_check_laa\":cl_check_laa, \"cl_check_pix\": cl_check_pix, \"cl_check_def\": cl_check_def, \"cl_check_1bf\": cl_check_1bf, \"cl_check_nbf\": cl_check_nbf, \"graph_size\": graph_size,\"chart\": chart}) \n", "\n" ] }, @@ -845,14 +897,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 390, "id": "7937f054-fcd0-4e67-a20f-7696f5903a94", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "47642ef0d8b94bceb11f878293997c33", + "model_id": "6f84c5e6c29142a9bb9db08cd48aefbd", "version_major": 2, "version_minor": 0 }, @@ -865,6 +917,7 @@ } ], "source": [ + "\n", "display(HBox([controls, VBox([small_controls, gui]) ]))" ] }, @@ -888,6 +941,139 @@ "# import ipywidgets\n", "# print(\"Ipywidgets version \", ipywidgets.__version__)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33727cb4-c3d7-4134-be89-f10b9922fbda", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "1cb358ba-42c4-420a-a0a6-0de425e670a6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ipywidgets version 7.6.5\n" + ] + } + ], + "source": [ + "import ipywidgets\n", + "print(\"Ipywidgets version \", ipywidgets.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a52ff861-8243-4d39-b7da-1ee8fb0ee494", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61970cec-8678-4edf-99f6-4f832d2aad8d", + "metadata": {}, + "outputs": [], + "source": [ + "https://ipywidgets.readthedocs.io/en/latest/user_install.html" + ] + }, + { + "cell_type": "code", + "execution_count": 386, + "id": "a4628def-4c43-456e-a93b-95952e2a3dea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 386, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import defaultdict\n", + "from IPython.display import HTML\n", + "import ipywidgets\n", + "from pprint import pprint\n", + "\n", + "reverse_lut = defaultdict(set)\n", + "styles = set()\n", + "for export_name in dir(ipywidgets.widgets):\n", + " export = getattr(ipywidgets.widgets, export_name)\n", + " try:\n", + " if issubclass(export, ipywidgets.Widget) and 'style' in export.class_trait_names():\n", + " reverse_lut[export.style.klass.__name__].add(export.__name__)\n", + " styles.add(export.style.klass)\n", + " except TypeError:\n", + " pass\n", + "\n", + "html = '\"\n", + "HTML(html)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "54c1add7-f5ec-43f0-a447-e22dd03926a7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "
AttributeToggleButtonsStyleButtonStyleDescriptionStyleProgressStyleSliderStyle
description_width
button_width
font_weight
bar_color
handle_color
button_color
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "attributes = defaultdict(set)\n", + "base_traits = set(ipywidgets.Style.class_trait_names())\n", + "\n", + "for s in styles:\n", + " for t in s.class_trait_names():\n", + " if not t.startswith(\"_\") and t not in base_traits:\n", + " attributes[s.__name__].add(t)\n", + "all_attributes = set().union(*attributes.values())\n", + "\n", + "html = '\\n'\n", + "html = f\"{html}{ ''.join(f'' for s in attributes.keys()) }\"\n", + "for a in all_attributes:\n", + " html = f\"\"\"{html}{ ''.join(f'' for attribs in attributes.values()) }\"\"\"\n", + "html += \"
Attribute{s}
{a}{\"✓\" if a in attribs else \"\"}
\"\n", + "HTML(html)" + ] } ], "metadata": { diff --git a/calibrations.xlsx b/calibrations.xlsx index db7ef42..10025df 100644 Binary files a/calibrations.xlsx and b/calibrations.xlsx differ diff --git a/ptychoScopy.ipynb b/ptychoScopy.ipynb index 268e78d..1456a12 100644 --- a/ptychoScopy.ipynb +++ b/ptychoScopy.ipynb @@ -2,19 +2,10 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 79, "id": "7425242d-3c91-4c1e-a424-08625a38ee7a", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\skoupy_r\\Anaconda3\\lib\\site-packages\\scipy\\__init__.py:155: UserWarning: A NumPy version >=1.18.5 and <1.25.0 is required for this version of SciPy (detected version 1.26.1\n", - " warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n" - ] - } - ], + "outputs": [], "source": [ "### Initial packages import ###################################################\n", "import numpy as np\n", @@ -25,7 +16,7 @@ "from pandas import read_excel\n", "from IPython.display import display\n", "from plotly.subplots import make_subplots\n", - "from ipywidgets import interactive_output, HBox, VBox, Layout, Label, Valid, ToggleButtons, RadioButtons, Dropdown, IntSlider, Checkbox, Button, ButtonStyle, GridBox\n", + "from ipywidgets import interactive_output, HBox, VBox, Layout, Label, Valid, ToggleButtons, RadioButtons, Dropdown, IntSlider, Checkbox, Button, ButtonStyle, GridBox,IntSlider\n", "\n", "# print(\"Numpy version \", np.__version__)\n", "# print(\"Pandas version \", pd.__version__)\n", @@ -300,31 +291,37 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 389, "id": "8055b802-cf83-4250-aea9-54e3e6b73db0", "metadata": {}, "outputs": [], "source": [ "### CONTROLS ##########################################\n", "\n", - "ali = dict(style = {'description_width': '60px','button_width': '33px'}, button_style='',disabled=False)\n", - "ali2 = dict(style = {'description_width': '60px','button_width': '90px'}, button_style='',disabled=False)\n", - "header = Button(description='STEM control panel',layout=Layout(width='auto', grid_area='header'), style=ButtonStyle(button_color='silver'))\n", + "ali = dict(style = {'description_width': '60px','button_width': '35px', 'font_weight': 'bold'}, button_style='',disabled = False)\n", + "ali2 = dict(style = {'description_width': '60px','button_width': '90px', 'font_weight': 'bold'}, button_style='',disabled = False)\n", "\n", - "beam_set = Button(description='Electron beam settings',layout=Layout(width='auto', grid_area='beam_set'), style=ButtonStyle(button_color='lightsteelblue')) \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "header = Button(description='STEM control panel',layout=Layout(width='auto', grid_area='header'), style=ButtonStyle(button_color='silver', font_weight= 'bold',))\n", + "\n", + "beam_set = Button(description='Electron beam settings',layout=Layout(width='auto', grid_area='beam_set'), style=ButtonStyle(button_color='lightsteelblue', font_weight= 'bold')) \n", "beam = ToggleButtons(options=opt.energies(), value=200,description='E (keV)', layout=Layout(width='auto', grid_area='beam_set1'), **ali)\n", "aperture = ToggleButtons(options=opt.apertures(), value='30um', description='Aperture', layout=Layout(width='auto', grid_area='beam_set2'), **ali)\n", "probe = ToggleButtons(options=opt.probes(), value='8C', description='Probe', layout=Layout(width='auto', grid_area='beam_set3'), **ali)\n", "defocus = IntSlider(description='Δf (nm)', value=0, min=0, max=1000, step=1, continuous_update=False, layout=Layout(width='auto', grid_area='beam_set4'), **ali)\n", "\n", - "scanning_set = Button(description='Scanning parameters',layout=Layout(width='auto', grid_area='scanning_set'),style=ButtonStyle(button_color='lightsteelblue'))\n", + "scanning_set = Button(description='Scanning parameters',layout=Layout(width='auto', grid_area='scanning_set'),style=ButtonStyle(button_color='lightsteelblue', font_weight= 'bold'))\n", "mag = ToggleButtons(options=opt.magnifications(),value=3,description='Mag. Mx', layout=Layout(width='auto', grid_area='scanning_set1'), **ali)\n", "matrix = ToggleButtons(options=opt.mappings(), value=2048, description='Matrix', layout=Layout(width='auto', grid_area='scanning_set2'), **ali)\n", "dwell_time = ToggleButtons(options=opt.dwelltimes(), value=10, description='t (μs)', layout=Layout(width='auto', grid_area='scanning_set3'), **ali)\n", "\n", "\n", "\n", - "camera_set = Button(description='Detection',layout=Layout(width='auto', grid_area='camera_set'),style=ButtonStyle(button_color='lightsteelblue'))\n", + "camera_set = Button(description='Detection',layout=Layout(width='auto', grid_area='camera_set'),style=ButtonStyle(button_color='lightsteelblue', font_weight= 'bold'))\n", "cl = ToggleButtons(options=opt.cameralengths(), value=12, description='CL (cm)', layout=Layout(width='auto', grid_area='cl_set1'), **ali)\n", "camera = ToggleButtons(options=opt.detectors(), description='Detector', layout=Layout(width='auto', grid_area='camera_set1'), **ali2)\n", "restriction = ToggleButtons(options=[('.',False), ('..',True)], value = False, description='PAAR',icons = ['times','check'], layout=Layout(width='auto', grid_area='camera_set2'), **ali)\n", @@ -348,7 +345,7 @@ "footer = Button(description='Footer',layout=Layout(width='auto', grid_area='footer'),style=ButtonStyle(button_color='silver'))\n", "\n", "\n", - "controls = GridBox(children=[header, beam_set, beam, aperture, probe, defocus, scanning_set, mag,matrix,dwell_time, cl,camera_set, camera, restriction, binning, beam_res, aperture_res,aperture_res2,fov_res, dwell_time_res,footer],\n", + "controls = GridBox(children=[header, beam_set, beam, aperture, probe, defocus, scanning_set, mag,matrix,dwell_time, cl,camera_set, camera, restriction, binning, beam_res, aperture_res,aperture_res2,fov_res, dwell_time_res],\n", " layout=Layout(\n", " width='22%',\n", " grid_template_rows='auto auto auto',\n", @@ -375,49 +372,56 @@ "\n", "# SMALL CONTROLS\n", "\n", - "descr = '50px'\n", - "butt = '30px'\n", + "descr = '80px'\n", + "butt = '60px'\n", "\n", - "header = Button(description='Graph controls',layout=Layout(width='auto', grid_area='header'),style=ButtonStyle(button_color='whitesmoke'))\n", + "header = Button(description='Graph controls',layout=Layout(width='auto', grid_area='header'),style=ButtonStyle(button_color='whitesmoke', font_weight= 'bold'))\n", "\n", - "method = ToggleButtons(options=[('SSB','direct'), ('Iter','iterative')], description='Method', tooltips=['SSB', 'PIE, MLc, DM'],\n", - " layout=Layout(width='auto', grid_area='methods'), style = {'description_width': descr,'button_width': butt}, button_style='',disabled=False,)\n", + "method = ToggleButtons(options=[('SSB','direct'), ('Iterative','iterative')], description='Method', tooltips=['SSB', 'PIE, MLc, DM'],\n", + " layout=Layout(width='auto', grid_area='methods'), style = {'description_width': descr,'button_width': butt, 'font_weight': 'bold'}, button_style='',disabled=False,)\n", "\n", - "graph_size = ToggleButtons(options=[('XXS', 50),('XS', 70),('S', 90), ('M', 100), ('L', 120)], value=100, description='Graphs size',\n", + "graph_size = IntSlider(value=100,min=10,max=200,step=5, description='Graphs size',\n", " layout=Layout(width='auto', grid_area='gr_size'), style = {'description_width': descr,'button_width': butt}, button_style='',disabled=False,)\n", "\n", "ctf_xaxis = RadioButtons(options=['α','mrad', 'A'], description='CTF x-axis:',\n", " layout=Layout(width='auto', grid_area='ctxaxis'), style = {'description_width': descr,'button_width': butt}, disabled=False,)\n", "\n", "cl_check_laa = Checkbox(value=True, description='Low anlge approximation', indent=False,\n", - " layout=Layout(width='auto', grid_area='check1'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Low angle approximation check - scattering angle < 10 deg\n", - "cl_check_pix = Checkbox(value=True, description='Recon. pixel < step size',indent=False,\n", - " layout=Layout(width='auto', grid_area='check2'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Reconstructed pixel size < scanning step size \n", + " layout=Layout(width='auto', grid_area='check1'), style = {'description_width': descr}, disabled=False,) # Low angle approximation check - scattering angle < 10 deg\n", + "cl_check_pix = Checkbox(value=True, description='Ptycho pixel < step size',indent=False,\n", + " layout=Layout(width='auto', grid_area='check2'), style = {'description_width': descr}, disabled=False,) # Reconstructed pixel size < scanning step size \n", "cl_check_def = Checkbox(value=True, description='Beam fits in probe window', indent=False,\n", - " layout=Layout(width='auto', grid_area='check3'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Is probe window big enough to accomodate defocused probe?\n", + " layout=Layout(width='auto', grid_area='check3'), style = {'description_width': descr}, disabled=False,) # Is probe window big enough to accomodate defocused probe?\n", "cl_check_1bf = Checkbox(value=True, description='Detector cover < 1α', indent=False,\n", - " layout=Layout(width='auto', grid_area='check4'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Detector cover is lower than 1 α\n", + " layout=Layout(width='auto', grid_area='check4'), style = {'description_width': descr}, disabled=False,) # Detector cover is lower than 1 α\n", "cl_check_nbf = Checkbox(value=True, description='Detector cover > 6α', indent=False,\n", - " layout=Layout(width='auto', grid_area='check5'), style = {'description_width': descr,'button_width': butt}, disabled=False,) # Detector cover is higher than 6 α\n", + " layout=Layout(width='auto', grid_area='check5'), style = {'description_width': descr}, disabled=False,) # Detector cover is higher than 6 α\n", "\n", - "footer = Button(description='Interactive results',layout=Layout(width='auto', grid_area='footer'),style=ButtonStyle(button_color='whitesmoke'))\n", "\n", - "small_controls = GridBox(children=[header, method, graph_size, ctf_xaxis, cl_check_laa,cl_check_pix,cl_check_def, cl_check_1bf, cl_check_nbf, footer],\n", + "chart = ToggleButtons(options=[('STEM','micr'), ('Charts','dedic'),('Both','both')],value = 'both', description='Show',\n", + " layout=Layout(width='auto', grid_area='charts'), style = {'description_width': descr,'button_width': butt, 'font_weight': 'bold'}, button_style='',disabled=False,)\n", + "\n", + "\n", + "footer = Button(description='Interactive results',layout=Layout(width='auto', grid_area='footer'),style=ButtonStyle(button_color='whitesmoke', font_weight= 'bold'))\n", + "\n", + "small_controls = GridBox(children=[header, method, chart, graph_size, ctf_xaxis, cl_check_laa,cl_check_pix,cl_check_def, cl_check_1bf, cl_check_nbf, footer],\n", " layout=Layout(\n", " width='100%',\n", " grid_template_rows='auto auto auto',\n", - " grid_template_columns='40% 20% 20% 20%',\n", + " grid_template_columns='50% 20% 30%',\n", " grid_template_areas='''\n", - " \"header header header header \"\n", - " \"methods ctxaxis check1 check2\"\n", - " \"gr_size ctxaxis check3 check5\"\n", - " \". ctxaxis check4 . \" \n", - " \"footer footer footer footer\"\n", + " \"header header header \"\n", + " \"methods ctxaxis check1 \"\n", + " \"charts ctxaxis check2 \"\n", + " \"gr_size ctxaxis check3 \"\n", + " \". . check4 \"\n", + " \". . check5 \"\n", + " \"footer footer footer\"\n", " ''')\n", " )\n", "\n", "\n", - "def ptycho_interact(beam, aperture, aperture_res, aperture_res2, probe, cl, matrix, defocus, mag, camera, binning, dwell_time, restriction, method, ctf_xaxis, cl_check_laa, cl_check_pix, cl_check_def, cl_check_1bf, cl_check_nbf, graph_size):\n", + "def ptycho_interact(beam, aperture, aperture_res, aperture_res2, probe, cl, matrix, defocus, mag, camera, binning, dwell_time, restriction, method, ctf_xaxis, cl_check_laa, cl_check_pix, cl_check_def, cl_check_1bf, cl_check_nbf, graph_size, chart):\n", " \n", " ### SINGLE SETTING PARAMETERS ###\n", " wavelength = pty.get_wavelength(beam)*1e12\n", @@ -464,59 +468,90 @@ " pctf = pty.get_ssb_ctf()\n", " apertury = opt.apertures()\n", " \n", + "\n", + "\n", + "\n", + "\n", + "\n", " \n", " ### SAMPLE PLANE ##########################################\n", " points = 5\n", " yyy = np.append(np.linspace(0,overlap,100), 200)\n", - " wid = np.linspace(0,points-1,points) * step_size_corr\n", + " wid = np.array([-2, -1, 0, 1, 2]) * step_size_corr\n", " \n", " fig5 = make_subplots(specs=[[{\"secondary_y\": True}]])\n", - " fig5.update_xaxes(title_text=\"Sample plane (nm)\", range=[0, points*step_size_corr], showgrid=False, zeroline=False)\n", - " fig5.update_yaxes(range=[0, points*step_size_corr],showgrid=False, showticklabels=False)\n", + " fig5.update_xaxes(title_text=\"Sample plane (nm)\", range=[-4*step_size_corr, 4*step_size_corr], showgrid=False, zeroline=False)\n", + " fig5.update_yaxes(range=[-4*step_size_corr, 4*step_size_corr],showgrid=False, showticklabels=False)\n", " for x in range(len(wid)):\n", " fig5.add_trace(go.Scatter(showlegend=False, x=wid, y=np.ones(len(wid))*wid[x], marker_color='blue'),secondary_y=False)\n", " for x in range(len(wid)): \n", " for y in range(len(wid)): \n", " fig5.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", opacity=0.1, fillcolor=\"#FF7F7F\",name=False, x0=wid[x]-beam_diameter/2, y0=wid[y]-beam_diameter/2, x1=wid[x]+beam_diameter/2, y1=wid[y]+beam_diameter/2, line_color=\"red\",secondary_y=False)\n", "\n", - " fig5.add_trace(go.Scatter(showlegend=True, x=[-1], y=[-1],marker_size=12, marker_color='blue', name='Step '+str(np.round(10*step_size,3))+' Å ('+str(np.round(10*step_size_corr,3))+')'))\n", + " fig5.add_annotation(x=-1*step_size_corr, y=-2*step_size_corr, ax=-2*step_size_corr, ay=-2*step_size_corr, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=-2*step_size_corr, y=-1*step_size_corr, ax=-2*step_size_corr, ay=-2*step_size_corr, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=-1.25*step_size_corr, y=-1.5*step_size_corr, text=str(np.round(10*step_size_corr,3))+' Å', showarrow=False, yshift=0)\n", + " \n", + " fig5.add_annotation(x=2*step_size_corr-0.75*beam_diameter/2, y=-2*step_size_corr-0.75*beam_diameter/2, ax=2*step_size_corr, ay=-2*step_size_corr,xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=2*step_size_corr+0.75*beam_diameter/2, y=-2*step_size_corr+0.75*beam_diameter/2, ax=2*step_size_corr, ay=-2*step_size_corr,xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig5.add_annotation(x=2.25*step_size_corr, y=-2*step_size_corr, text=str(np.round(10*beam_diameter,2))+' Å', showarrow=False, yshift=-15)\n", + " \n", + " \n", " if method == 'direct':\n", " xx = 2*wavelength/(np.sin(semi_angle_corr*omega/1000))/100\n", " fig5.add_trace(go.Scatter(showlegend=True, x=[-1], y=[-1],marker_size=12, marker_color='green', name='Rec step '+str(np.round(xx[-1]/3,2))+' Å')) \n", " \n", - " fig5.add_trace(go.Scatter(showlegend=True, x=[-1], y=[-1],marker_size=12, marker_color='#FF7F7F', name='Beam ⌀ '+ str(np.round(10*beam_diameter,2))+\" Å\"))\n", " fig5.update_layout(legend=dict(orientation=\"v\",yanchor=\"bottom\",y=0.85,xanchor=\"right\",x=0.9))\n", - " fig5.add_trace(go.Scatter(showlegend=False, x = points*step_size_corr*np.ones(len(yyy)), y=yyy, opacity=1,mode='markers',marker_symbol = \"triangle-down\",marker=dict(size=20, color=yyy, colorscale='rainbow_r', showscale=False)),secondary_y=True) \n", - " fig5.update_yaxes(title_text=\"Overlap %\", range=[0, 100],showgrid=False, showticklabels=True, tickvals = [0, 20, 40,50, 60,70, 80, 90, 100], secondary_y=True)\n", - " fig5.update_layout(title={'text': \"Sample\",'y':0.95, 'x':0.30,'xanchor': 'left','yanchor': 'top'})\n", + " fig5.add_trace(go.Scatter(showlegend=False, x = -4*step_size_corr*np.ones(len(yyy)), y=yyy, opacity=1,mode='markers',marker_symbol = \"triangle-down\",marker=dict(size=20, color=yyy, colorscale='rainbow_r', showscale=False)),secondary_y=True) \n", + " fig5.update_yaxes(range=[0, 100], showgrid=False, showticklabels=False, secondary_y=True)\n", + " fig5.update_yaxes(title_text=\"Overlap %\", showgrid=False, showticklabels=True, tickvals = [-4*step_size_corr, -2*step_size_corr, 0, 2*step_size_corr, 4*step_size_corr],ticktext = ['0', '25', '50', '75', '100'], secondary_y=False)\n", + " fig5.update_layout(title={'text': \"Sample\",'y': 0.975, 'x': 0.30,'xanchor': 'left','yanchor': 'top'})\n", " fig5.update_layout(width=4.5*graph_size, height=3.0*graph_size, margin =dict(l=1.3*graph_size, r=0.3*graph_size, t=0.1*graph_size, b=0))\n", "\n", " \n", + "\n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " ### DETECTOR PLANE ##########################################\n", - " fig6 = go.Figure() \n", - " fig6.update_xaxes(title_text=\"Pixels\", range=[1, num_pixels], zeroline=False)\n", - " fig6.update_yaxes(range=[1, num_pixels], showticklabels=False,)\n", - " for x in np.linspace(1,int(np.round(covered_alfas)),int(np.round(covered_alfas))):\n", - " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", x0=num_pixels/2-x*beam_diameter_pix, y0=num_pixels/2-x*beam_diameter_pix, x1=num_pixels/2+x*beam_diameter_pix, y1=num_pixels/2+x*beam_diameter_pix,line_color=\"gray\")\n", - " fig6.add_trace(go.Scatter(showlegend=False,x=[num_pixels/2],y=[num_pixels/2-x*beam_diameter_pix],mode=\"lines+text\", text=str(int(2*x))+\"α\",textposition=\"top center\"))\n", + " fig6 = make_subplots(specs=[[{\"secondary_y\": True}]])\n", " \n", - " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", fillcolor=\"#FF7F7F\", x0=num_pixels/2-beam_diameter_pix/2, y0=num_pixels/2-beam_diameter_pix/2, x1=num_pixels/2+beam_diameter_pix/2, y1=num_pixels/2+beam_diameter_pix/2,line_color=\"#FF7F7F\")\n", - " fig6.add_annotation(x=num_pixels/2-beam_diameter_pix/2, y=num_pixels/2, ax=num_pixels/2+beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=3,arrowsize=1,arrowwidth=1,arrowcolor='black')\n", - " fig6.add_annotation(x=num_pixels/2+beam_diameter_pix/2,y=num_pixels/2, ax=num_pixels/2-beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y', text='', showarrow=True,arrowhead=3,arrowsize=1,arrowwidth=1,arrowcolor='black')\n", + " for x in np.linspace(1,int(np.round(covered_alfas)),int(np.round(covered_alfas))):\n", + " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\", x0=num_pixels/2-x*beam_diameter_pix, y0=num_pixels/2-x*beam_diameter_pix, x1=num_pixels/2+x*beam_diameter_pix, y1=num_pixels/2+x*beam_diameter_pix, line=dict(color=\"gray\",width=1,dash=\"dot\",),secondary_y=False)\n", + " fig6.add_trace(go.Scatter(showlegend=False,x=[num_pixels/2],y=[num_pixels/2+x*beam_diameter_pix],mode=\"lines+text\", text=str(int(2*x))+\"α\",textposition=\"top center\"),secondary_y=False)\n", + " \n", + " fig6.add_shape(type=\"circle\",xref=\"x\", yref=\"y\",opacity=0.5, fillcolor=\"#FF7F7F\", x0=num_pixels/2-beam_diameter_pix/2, y0=num_pixels/2-beam_diameter_pix/2, x1=num_pixels/2+beam_diameter_pix/2, y1=num_pixels/2+beam_diameter_pix/2,line_color=\"#FF7F7F\",secondary_y=False)\n", + " fig6.add_annotation(x=num_pixels/2-beam_diameter_pix/2, y=num_pixels/2, ax=num_pixels/2+beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y',text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", + " fig6.add_annotation(x=num_pixels/2+beam_diameter_pix/2,y=num_pixels/2, ax=num_pixels/2-beam_diameter_pix/2, ay=num_pixels/2, xref='x',yref='y',axref='x',ayref='y', text='', showarrow=True,arrowhead=2,arrowsize=2,arrowwidth=1,arrowcolor='black')\n", " fig6.add_annotation(x=num_pixels/2, y=num_pixels/2, text=str(np.round(beam_diameter_pix,1))+\" pix\", showarrow=False, yshift=10)\n", - " fig6.add_trace(go.Scatter(x=[1,1.5,1,1.5], y=[1,1,1.5,1.5], name='Pix ∠ ' +str(np.round(pixel_angle,2))+' mrad'))\n", - " fig6.add_trace(go.Scatter(x=[1,1.5,1,1.5], y=[1,1,1.5,1.5], name='Pixel size ' +str(np.round(size_pixel,2))+' um'))\n", - " fig6.update_layout(legend=dict(orientation=\"v\",yanchor=\"bottom\",y=0.80,xanchor=\"right\",x=1))\n", - " fig6.update_layout(title={'text': \"Detector\",'y':0.95, 'x':0.11,'xanchor': 'left','yanchor': 'top'})\n", - " fig6.update_layout(xaxis = dict(tickmode = 'array', tickvals = [1, num_pixels/4, num_pixels/2, 3*num_pixels/4, num_pixels])) \n", - " fig6.update_layout(width=3.5*graph_size, height=3*graph_size, margin = dict(l=0.35*graph_size, r=0.65*graph_size, t=0.3*graph_size, b=0))\n", + " fig6.add_annotation(x=num_pixels/2, y=num_pixels/2, text=\"1 pix = \"+str(np.round(pixel_angle,2))+\" mrad\", showarrow=False, yshift=-10)\n", + " fig6.add_annotation(x=num_pixels/2, y=num_pixels/10, text=\"Real pixel size = \"+str(np.round(size_pixel,2))+\" μm\", showarrow=False, yshift=0)\n", + " fig6.add_trace(go.Scatter(showlegend=True,x=[-1], y=[-1], marker_color='red', name = \"BF disk\"),secondary_y=False)\n", + " \n", + " fig6.add_trace(go.Scatter(showlegend=False,x=[-1], y=[-1]),secondary_y=True)\n", + " \n", + " fig6.update_layout(legend=dict(orientation=\"h\",yanchor=\"bottom\",y=1.00,xanchor=\"right\",x=0.9))\n", + " fig6.update_layout(title={'text': \"Detector\",'y':0.975, 'x':0.11,'xanchor': 'left','yanchor': 'top'})\n", + " \n", + " fig6.update_xaxes(title_text=\"Pixels\", range=[1, num_pixels], zeroline=False, tickvals = [1, num_pixels/4, num_pixels/2, 3*num_pixels/4, num_pixels]) \n", + " fig6.update_yaxes(range=[1, num_pixels], showticklabels=False, showgrid=False, secondary_y=False)\n", + " fig6.update_yaxes(title_text=\"Angular cover (mrad)\", range=[1, num_pixels], showticklabels=True, tickvals = [1, num_pixels/4, num_pixels/2, 3*num_pixels/4, num_pixels], ticktext = ['', np.round(detector_cover/4,1), np.round(detector_cover/2,1), np.round(3*detector_cover/4,1), np.round(detector_cover,1)], secondary_y=True) \n", + " \n", + " fig6.update_layout(width=3.5*graph_size, height=3*graph_size, margin = dict(l=0.35*graph_size, r=0.48*graph_size, t=0.3*graph_size, b=0))\n", "\n", " \n", " \n", " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " focusdepth = wavelength/(semi_angle_corr/1000)**2/1000\n", " ssb_cl, kolik = pty.cl4ssb(detector_cover_a_all)\n", " ### MICROSCOPE SCHEME ##########################################\n", @@ -654,20 +689,27 @@ " fig4.update_layout(xaxis = dict(tickmode = 'array', tickvals = opt.cameralengths()))\n", " fig4.update_layout(width=8.0*graph_size, height=2.25*graph_size, margin =dict(l=1.3*graph_size, r=0.25*graph_size, t=0.3*graph_size, b=0.3*graph_size))\n", "\n", - " ### SHOWING ###\n", - " right_column = VBox([go.FigureWidget(fig),HBox([go.FigureWidget(fig5),go.FigureWidget(fig6)]),go.FigureWidget(fig4)])\n", - " left_column = VBox([go.FigureWidget(fig8)])\n", - " \n", - "\n", + " \n", " \n", + " ### SHOWING ###\n", + " if chart == 'micr':\n", + " left_column = VBox([go.FigureWidget(fig8)])\n", + " total = HBox([left_column]) \n", " \n", - " total = HBox([left_column, right_column])\n", - " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px')\n", - " \n", + " elif chart == 'dedic':\n", + " right_column = VBox([go.FigureWidget(fig) ,HBox([go.FigureWidget(fig5),go.FigureWidget(fig6)]),go.FigureWidget(fig4)])\n", + " total = HBox([right_column])\n", + " \n", + " else: \n", + " right_column = VBox([go.FigureWidget(fig) ,HBox([go.FigureWidget(fig5),go.FigureWidget(fig6)]),go.FigureWidget(fig4)])\n", + " left_column = VBox([go.FigureWidget(fig8)])\n", + " total = HBox([left_column, right_column])\n", + " \n", + " \n", + " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px') \n", " display(total)\n", - " \n", - " \n", - " \n", + " \n", + "\n", " \n", " \n", " if method == 'iterative': \n", @@ -781,7 +823,7 @@ " \n", " dictionary[i] = ToggleButtons(options=data_tuple, button_style=button_style, icons = icons, **align6)\n", " \n", - " legend = ToggleButtons(options=['Ptycho pix. (Å)','Det. cover (mrad)','Det. cover (α)', 'Max. def. (nm)','Selected by CL ','Nominal CL (cm)',], \n", + " legend = ToggleButtons(options=['Ptycho pixel (Å)','Det. cover (mrad)','Det. cover (α)', 'Max. def. (nm)','Selected by CL ','Nominal CL (cm)',], \n", " tooltips=['Dependent on: detector, beam energy, camera length',\n", " 'Dependent on: detector, camera length',\n", " 'Dependent on: detector, camera length, probe semi-angle',\n", @@ -796,22 +838,32 @@ " \n", " \n", " \n", + " \n", " ### SHOWING ###\n", - " sample_overlap = HBox([go.FigureWidget(fig5), go.FigureWidget(fig7), go.FigureWidget(fig6)]) \n", - " right_column = VBox([HBox([go.FigureWidget(fig),checks]), sample_overlap, cltab]) \n", - " left_column = VBox([go.FigureWidget(fig8)]) \n", - " \n", - " \n", - " total = HBox([left_column, right_column])\n", - " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px')\n", - " \n", + " if chart == 'micr':\n", + " left_column = VBox([go.FigureWidget(fig8)])\n", + " total = HBox([left_column]) \n", + " \n", + " elif chart == 'dedic':\n", + " sample_overlap = HBox([go.FigureWidget(fig5), go.FigureWidget(fig7), go.FigureWidget(fig6)]) \n", + " right_column = VBox([HBox([go.FigureWidget(fig),checks]), sample_overlap, cltab]) \n", + " total = HBox([right_column])\n", + " \n", + " else: \n", + " sample_overlap = HBox([go.FigureWidget(fig5), go.FigureWidget(fig7), go.FigureWidget(fig6)]) \n", + " right_column = VBox([HBox([go.FigureWidget(fig),checks]), sample_overlap, cltab]) \n", + " left_column = VBox([go.FigureWidget(fig8)]) \n", + " total = HBox([left_column, right_column]) \n", + " \n", + " total.layout = Layout(border='solid 0px gray',margin='10px 10px 10px 40px', padding='10px 10px 10px 10px') \n", " display(total)\n", + "\n", " \n", " return \n", " \n", "gui = interactive_output(ptycho_interact, {\"beam\": beam, \"aperture\": aperture,\"aperture_res\": aperture_res,\"aperture_res2\": aperture_res2, \"probe\": probe, \"cl\": cl, \"matrix\": matrix, \"defocus\": defocus, \"mag\": mag, \"camera\": camera,\n", " \"binning\": binning, \"dwell_time\": dwell_time, \"restriction\": restriction, \"method\": method, \"ctf_xaxis\": ctf_xaxis,\n", - " \"cl_check_laa\":cl_check_laa, \"cl_check_pix\": cl_check_pix, \"cl_check_def\": cl_check_def, \"cl_check_1bf\": cl_check_1bf, \"cl_check_nbf\": cl_check_nbf, \"graph_size\": graph_size}) \n", + " \"cl_check_laa\":cl_check_laa, \"cl_check_pix\": cl_check_pix, \"cl_check_def\": cl_check_def, \"cl_check_1bf\": cl_check_1bf, \"cl_check_nbf\": cl_check_nbf, \"graph_size\": graph_size,\"chart\": chart}) \n", "\n" ] }, @@ -845,14 +897,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 390, "id": "7937f054-fcd0-4e67-a20f-7696f5903a94", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "47642ef0d8b94bceb11f878293997c33", + "model_id": "6f84c5e6c29142a9bb9db08cd48aefbd", "version_major": 2, "version_minor": 0 }, @@ -865,6 +917,7 @@ } ], "source": [ + "\n", "display(HBox([controls, VBox([small_controls, gui]) ]))" ] }, @@ -888,6 +941,139 @@ "# import ipywidgets\n", "# print(\"Ipywidgets version \", ipywidgets.__version__)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33727cb4-c3d7-4134-be89-f10b9922fbda", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "1cb358ba-42c4-420a-a0a6-0de425e670a6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ipywidgets version 7.6.5\n" + ] + } + ], + "source": [ + "import ipywidgets\n", + "print(\"Ipywidgets version \", ipywidgets.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a52ff861-8243-4d39-b7da-1ee8fb0ee494", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61970cec-8678-4edf-99f6-4f832d2aad8d", + "metadata": {}, + "outputs": [], + "source": [ + "https://ipywidgets.readthedocs.io/en/latest/user_install.html" + ] + }, + { + "cell_type": "code", + "execution_count": 386, + "id": "a4628def-4c43-456e-a93b-95952e2a3dea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 386, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import defaultdict\n", + "from IPython.display import HTML\n", + "import ipywidgets\n", + "from pprint import pprint\n", + "\n", + "reverse_lut = defaultdict(set)\n", + "styles = set()\n", + "for export_name in dir(ipywidgets.widgets):\n", + " export = getattr(ipywidgets.widgets, export_name)\n", + " try:\n", + " if issubclass(export, ipywidgets.Widget) and 'style' in export.class_trait_names():\n", + " reverse_lut[export.style.klass.__name__].add(export.__name__)\n", + " styles.add(export.style.klass)\n", + " except TypeError:\n", + " pass\n", + "\n", + "html = '
    '\n", + "for style, widgets in reverse_lut.items():\n", + " html = f\"{html}\\n
  • {style}: {', '.join(sorted(widgets))}
  • \"\n", + "html += \"
\"\n", + "HTML(html)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "54c1add7-f5ec-43f0-a447-e22dd03926a7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "
AttributeToggleButtonsStyleButtonStyleDescriptionStyleProgressStyleSliderStyle
description_width
button_width
font_weight
bar_color
handle_color
button_color
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "attributes = defaultdict(set)\n", + "base_traits = set(ipywidgets.Style.class_trait_names())\n", + "\n", + "for s in styles:\n", + " for t in s.class_trait_names():\n", + " if not t.startswith(\"_\") and t not in base_traits:\n", + " attributes[s.__name__].add(t)\n", + "all_attributes = set().union(*attributes.values())\n", + "\n", + "html = '\\n'\n", + "html = f\"{html}{ ''.join(f'' for s in attributes.keys()) }\"\n", + "for a in all_attributes:\n", + " html = f\"\"\"{html}{ ''.join(f'' for attribs in attributes.values()) }\"\"\"\n", + "html += \"
Attribute{s}
{a}{\"✓\" if a in attribs else \"\"}
\"\n", + "HTML(html)" + ] } ], "metadata": {