Skip to content

Commit

Permalink
catch inputs that don't exit in the mp database
Browse files Browse the repository at this point in the history
  • Loading branch information
tcpekin committed Nov 4, 2022
1 parent 1e66e07 commit 2aa4df9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -209,7 +209,7 @@ Current configuration notes
## TODO

- [x] HTTPS - via docker?
- [ ] Logging/analytics
- [x] Logging/analytics
- [ ] `nginx` static files update
- [ ] Simulation **g** vectors
- [ ] Simulation table of most common zone axes
Expand Down
41 changes: 28 additions & 13 deletions blog.py
Expand Up @@ -20,7 +20,14 @@
logger.info("Log is working.")

import matplotlib.pyplot as plt
from flask import Flask, Response, render_template, render_template_string, request, send_from_directory
from flask import (
Flask,
Response,
render_template,
render_template_string,
request,
send_from_directory,
)
from flask.helpers import redirect, url_for
from flask_flatpages import FlatPages, pygments_style_defs
from flask_flatpages.utils import pygmented_markdown
Expand All @@ -41,19 +48,15 @@

load_dotenv()

from figs import create_dp_figure, create_structure_figure
from figs import create_dp_figure, create_structure_figure, get_mp_structure


DEBUG = bool(os.environ.get("FLASK_DEBUG", False))
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = ".md"
FLATPAGES_ROOT = "content"
FLATPAGES_MARKDOWN_EXTENSIONS = ["codehilite", "fenced_code"]
FLATPAGES_EXTENSION_CONFIGS = {
'codehilite': {
'linenums': 'True'
}
}
FLATPAGES_EXTENSION_CONFIGS = {"codehilite": {"linenums": "True"}}
POST_DIR = "posts"


Expand Down Expand Up @@ -102,6 +105,7 @@ def about():
def dp_sim(structure=None, zone_axis=None):
logger.info(f"{request.remote_addr} - {request.full_path} - {request.referrer}")
print(zone_axis)
message = None
if request.args.get("structure") is not None and structure is None:
try:
structure = request.args.get("structure")
Expand All @@ -122,20 +126,31 @@ def dp_sim(structure=None, zone_axis=None):
else:
zone_axis = [1, 1, 1]
if structure is not None:
success = True
try:
get_mp_structure(structure=structure)
success = True
except Exception as e:
message = str(e)
success = False
else:
success = False

h, k, l = zone_axis
return render_template(
"dp_sim.html", success=success, structure=structure, h=h, k=k, l=l
"dp_sim.html",
success=success,
structure=structure,
h=h,
k=k,
l=l,
message=message,
)


@app.route("/dp_sim/img/<structure>_<h>_<k>_<l>_structure_plot.png")
def plot_structure_png(structure=None, h=None, k=None, l=None):
fname = f'assets/img/dps/{structure}_{h}_{k}_{l}_structure_plot.svg'
folder = 'static/'
fname = f"assets/img/dps/{structure}_{h}_{k}_{l}_structure_plot.svg"
folder = "static/"
try:
if os.path.isfile(folder + fname):
with open(folder + fname) as f:
Expand All @@ -156,8 +171,8 @@ def plot_structure_png(structure=None, h=None, k=None, l=None):

@app.route("/dp_sim/img/<structure>_<h>_<k>_<l>_dp_plot.png")
def plot_dp_png(structure=None, h=None, k=None, l=None):
fname = f'assets/img/dps/{structure}_{h}_{k}_{l}_dp_plot.svg'
folder = 'static/'
fname = f"assets/img/dps/{structure}_{h}_{k}_{l}_dp_plot.svg"
folder = "static/"
try:
if os.path.isfile(folder + fname):
with open(folder + fname) as f:
Expand Down
26 changes: 16 additions & 10 deletions figs.py
Expand Up @@ -12,41 +12,47 @@
# from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

MP_API_KEY = os.environ.get('MP_API_KEY')
MP_API_KEY = os.environ.get("MP_API_KEY")

# surpress plt.show warnings because using non-interactive backend - can change py4DSTEM to not automatically plt.show things too?
warnings.filterwarnings("ignore", category=UserWarning)

def get_mp_structure(structure='mp-81'):

def get_mp_structure(structure="mp-81"):
"""
This will try to get the Materials Project structure and save it, if it does not already exist.
This will try to get the Materials Project structure and save it, if it does not already exist.
"""
structure_path = f'static/assets/data/structures/{structure}.json'
try:
with open(structure_path, 'r') as f:
structure_path = f"static/assets/data/structures/{structure}.json"

try:
with open(structure_path, "r") as f:
mp_structure = pymatgen.core.Structure.from_dict(json.load(f))
except:
with MPRester(MP_API_KEY) as mpr:
mp_structure = mpr.get_structure_by_material_id(structure)
with open(structure_path, 'w') as f:
with open(structure_path, "w") as f:
json.dump(mp_structure.as_dict(), f)

return mp_structure


def create_structure_figure(structure="mp-81", zone_axis=[1, 1, 1]):
fig = Figure(dpi=200)
axis = fig.add_subplot(1, 1, 1)
structure = get_mp_structure(structure)
crystal = py4DSTEM.process.diffraction.Crystal.from_pymatgen_structure(
structure=structure,
)
fig, ax = crystal.plot_structure(returnfig=True, zone_axis_lattice=zone_axis, perspective_axes=True)
fig, ax = crystal.plot_structure(
returnfig=True, zone_axis_lattice=zone_axis, perspective_axes=True
)
ax.set_position([0, 0, 1, 1])
return fig


def create_dp_figure(structure="mp-81", zone_axis=[1, 1, 1], accelerating_voltage=200e3):
def create_dp_figure(
structure="mp-81", zone_axis=[1, 1, 1], accelerating_voltage=200e3
):
fig = Figure(dpi=100)
ax = fig.add_subplot(1, 1, 1)
structure = get_mp_structure(structure)
Expand Down
10 changes: 7 additions & 3 deletions templates/dp_sim.html
Expand Up @@ -21,8 +21,8 @@
<option value="mp-22862" label="interpenetrating FCC (NaCl)"></option>
</datalist>

<input type="text" name="zone_axis" placeholder="1,1,1 (h,k,l)" {% if structure
is not none %} value="{{'{},{},{}'.format(h,k,l)}}" {% endif %}/>
<input type="text" name="zone_axis" placeholder="1,1,1 (h,k,l)" {% if
structure is not none %} value="{{'{},{},{}'.format(h,k,l)}}" {% endif %} />

<button class="pure-button pure-button-primary">Submit</button>
</form>
Expand All @@ -48,7 +48,11 @@
alt="Crystal Structure" />
</div>
</div>
{% else %} {% endif %}
{% else %}

<p>{{message}}</p>

{% endif %}

<p>
See the
Expand Down

0 comments on commit 2aa4df9

Please sign in to comment.