Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/SOF-7438 feat: media creation nb #200

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Prev Previous commit
update: fix naming
  • Loading branch information
VsevolodX committed Jan 18, 2025
commit 854b7184695bb9196e5e8a2342ee0992b559f309
223 changes: 149 additions & 74 deletions other/media_generation/create_materials.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"metadata": {},
"cell_type": "code",
"source": [
"import json\n",
"import re\n",
"import os\n",
"from pymatgen.ext.matproj import MPRester\n",
"from mat3ra.made.tools.build.slab import SlabConfiguration, get_terminations, create_slab\n",
Expand All @@ -14,9 +16,17 @@
"\n",
"from mat3ra.standata.materials import Materials\n",
"from mat3ra.made.material import Material\n",
"from mat3ra.made.tools.modify import wrap_to_unit_cell\n",
"\n",
"materials = Materials.get_by_categories(\"3D\")\n",
"\n",
"# Load the symbols from the JSON file\n",
"with open('symbols_map.json', 'r') as file:\n",
" symbols = json.load(file)\n",
"\n",
"# Extract the \"OVER\" symbol\n",
"SLASH_SYMBOL = symbols[\"/\"]\n",
"\n",
"# Map lattice type to Miller Index\n",
"\n",
"lattice_to_miller = {\n",
Expand All @@ -26,91 +36,156 @@
" \"TRI\": (1, 1, 1)\n",
"}\n",
"\n",
"\n",
"def generate_interface(substrate_json, film_json):\n",
" print(f\"Creating interface: {substrate_json['name']} and {film_json['name']}\")\n",
"\n",
" substrate = Material(substrate_json)\n",
" film = Material(film_json)\n",
"\n",
"\n",
" substrate_miller_indices = lattice_to_miller[substrate.lattice.type] if substrate.lattice.type in lattice_to_miller else (0, 0, 1)\n",
" film_miller_indices = lattice_to_miller[film.lattice.type] if film.lattice.type in lattice_to_miller else (0, 0, 1)\n",
"\n",
" # Get material names before the \",\" -- the formula\n",
" substrate_name = re.match(r'[^,]*', substrate.name).group(0) + str(substrate_miller_indices).replace(\", \", \"\")\n",
" film_name = re.match(r'[^,]*', film.name).group(0) + str(film_miller_indices).replace(\", \", \"\")\n",
"\n",
" interface_name = f\"{film_name}{SLASH_SYMBOL}{substrate_name}\"\n",
" print(f\"Interface name: {interface_name}\")\n",
"\n",
"\n",
" # Define slab and interface parameters\n",
" film_params = {\n",
" \"miller_indices\": film_miller_indices,\n",
" \"thickness\": 3,\n",
" \"vacuum\": 15.0,\n",
" \"xy_supercell_matrix\": [[1, 0], [0, 1]],\n",
" \"use_conventional_cell\": True,\n",
" \"use_orthogonal_z\": True\n",
" }\n",
"\n",
" substrate_params = {\n",
" \"miller_indices\": substrate_miller_indices,\n",
" \"thickness\": 3,\n",
" \"vacuum\": 15.0,\n",
" \"xy_supercell_matrix\": [[1, 0], [0, 1]],\n",
" \"use_conventional_cell\": True,\n",
" \"use_orthogonal_z\": True\n",
" }\n",
"\n",
"\n",
" interface_params = {\n",
" \"distance_z\": 3.0,\n",
" \"vacuum\": 1.0,\n",
" \"max_area\": 150,\n",
" \"max_area_tol\": 0.10,\n",
" \"max_angle_tol\": 0.04,\n",
" \"max_length_tol\": 0.04\n",
" }\n",
"\n",
"\n",
" # Create slab configurations\n",
" substrate_slab_config = SlabConfiguration(bulk=substrate, **substrate_params)\n",
" film_slab_config = SlabConfiguration(bulk=film, **film_params)\n",
" try:\n",
" # Get terminations\n",
" substrate_terminations = get_terminations(substrate_slab_config)\n",
" film_terminations = get_terminations(film_slab_config)\n",
"\n",
" # Create slabs\n",
" # substrate_slabs = [create_slab(substrate_slab_config, t) for t in substrate_terminations]\n",
" # film_slabs = [create_slab(film_slab_config, t) for t in film_terminations]\n",
"\n",
" # Select termination pair (example: first pair)\n",
" termination_pair = (film_terminations[0], substrate_terminations[0])\n",
"\n",
" # Create interface configuration\n",
" interface_config = InterfaceConfiguration(\n",
" film_configuration=film_slab_config,\n",
" substrate_configuration=substrate_slab_config,\n",
" film_termination=termination_pair[0],\n",
" substrate_termination=termination_pair[1],\n",
" distance_z=interface_params[\"distance_z\"],\n",
" vacuum=interface_params[\"vacuum\"]\n",
" )\n",
"\n",
" # Set strain matching parameters\n",
" zsl_params = ZSLStrainMatchingParameters(\n",
" max_area=interface_params[\"max_area\"],\n",
" max_area_tol=interface_params[\"max_area_tol\"],\n",
" max_angle_tol=interface_params[\"max_angle_tol\"],\n",
" max_length_tol=interface_params[\"max_length_tol\"]\n",
" )\n",
"\n",
" # Generate interfaces\n",
" builder = ZSLStrainMatchingInterfaceBuilder(\n",
" build_parameters=ZSLStrainMatchingInterfaceBuilderParameters(strain_matching_parameters=zsl_params)\n",
" )\n",
" interfaces = builder.get_materials(configuration=interface_config)\n",
"\n",
" # Visualize and save the interfaces\n",
" interface = interfaces[0]\n",
" interface = wrap_to_unit_cell(interface)\n",
" visualize_materials(interface, repetitions=[1, 1, 1])\n",
"\n",
" interface.name = interface_name\n",
" set_materials(interface)\n",
" except Exception as e:\n",
" print(f\"Error creating interface between {substrate.name} and {film.name}: {e}\")\n"
],
"id": "f0c47155aa2a5343",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Generate interfaces between all pairs of materials",
"id": "2d27d1cce04b6bc4"
},
{
"metadata": {},
"cell_type": "code",
"source": [
"# Create interfaces\n",
"for i, substrate_json in enumerate(materials):\n",
" for j, film_json in enumerate(materials):\n",
" if i != j:\n",
" print(f\"Creating interface between {substrate_json['name']} and {film_json['name']}\")\n",
"\n",
" substrate = Material(substrate_json)\n",
" film = Material(film_json)\n",
" # Define slab and interface parameters\n",
" slab_params = {\n",
" \"miller_indices\": lattice_to_miller[substrate.lattice.type],\n",
" \"thickness\": 3,\n",
" \"vacuum\": 15.0,\n",
" \"xy_supercell_matrix\": [[1, 0], [0, 1]],\n",
" \"use_conventional_cell\": True,\n",
" \"use_orthogonal_z\": True\n",
" }\n",
"\n",
" interface_params = {\n",
" \"distance_z\": 3.0,\n",
" \"vacuum\": 0.0,\n",
" \"max_area\": 150,\n",
" \"max_area_tol\": 0.10,\n",
" \"max_angle_tol\": 0.04,\n",
" \"max_length_tol\": 0.04\n",
" }\n",
"\n",
"\n",
" # Create slab configurations\n",
" substrate_slab_config = SlabConfiguration(bulk=substrate, **slab_params)\n",
" film_slab_config = SlabConfiguration(bulk=film, **slab_params)\n",
" try:\n",
" # Get terminations\n",
" substrate_terminations = get_terminations(substrate_slab_config)\n",
" film_terminations = get_terminations(film_slab_config)\n",
"\n",
" # Create slabs\n",
" substrate_slabs = [create_slab(substrate_slab_config, t) for t in substrate_terminations]\n",
" film_slabs = [create_slab(film_slab_config, t) for t in film_terminations]\n",
"\n",
" # Select termination pair (example: first pair)\n",
" termination_pair = (film_terminations[0], substrate_terminations[0])\n",
"\n",
" # Create interface configuration\n",
" interface_config = InterfaceConfiguration(\n",
" film_configuration=film_slab_config,\n",
" substrate_configuration=substrate_slab_config,\n",
" film_termination=termination_pair[0],\n",
" substrate_termination=termination_pair[1],\n",
" distance_z=interface_params[\"distance_z\"],\n",
" vacuum=interface_params[\"vacuum\"]\n",
" )\n",
"\n",
" # Set strain matching parameters\n",
" zsl_params = ZSLStrainMatchingParameters(\n",
" max_area=interface_params[\"max_area\"],\n",
" max_area_tol=interface_params[\"max_area_tol\"],\n",
" max_angle_tol=interface_params[\"max_angle_tol\"],\n",
" max_length_tol=interface_params[\"max_length_tol\"]\n",
" )\n",
"\n",
" # Generate interfaces\n",
" builder = ZSLStrainMatchingInterfaceBuilder(\n",
" build_parameters=ZSLStrainMatchingInterfaceBuilderParameters(strain_matching_parameters=zsl_params)\n",
" )\n",
" interfaces = builder.get_materials(configuration=interface_config)\n",
"\n",
" # Visualize and save the interfaces\n",
" interface = interfaces[0]\n",
" visualize_materials(interface, repetitions=[1, 1, 1])\n",
" # set_materials(interface)\n",
" except Exception as e:\n",
" print(f\"Error creating interface between {substrate.name} and {film.name}: {e}\")"
"# for i, substrate_json in enumerate(materials):\n",
"# for j, film_json in enumerate(materials):\n",
"# if i != j:\n",
"# generate_interface(substrate_json, film_json)\n"
],
"id": "2774de5c47160056",
"id": "82f16d004d364af3",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Generate interfaces between random pairs of materials",
"id": "8c63d6310b33335c"
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"source": [
"import random\n",
"num_pairs = 10\n",
"\n",
"for _ in range(num_pairs):\n",
" substrate_json, film_json = random.sample(materials, 2)\n",
" generate_interface(substrate_json, film_json)"
],
"id": "3572ccacbb4fd25f",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"id": "36307e16f113f5e8",
"outputs": [],
"execution_count": null
}
],
"metadata": {
Expand Down
12 changes: 11 additions & 1 deletion other/media_generation/gif_processing_multiple.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
"metadata": {},
"cell_type": "code",
"source": [
"import json\n",
"\n",
"# GIFs generated by `record_gifs_with_wave.ipynb` are downloaded to the root of `api-examples`\n",
"# They need to be copied to input directory and pruned from duplications due to possible bugs.\n",
"current_dir = os.getcwd()\n",
Expand All @@ -164,14 +166,22 @@
"print(parent_dir)\n",
"copy_and_clean_gifs(parent_dir, INPUT_DIR)\n",
"\n",
"# Some symbols needed to be encoded and decoded\n",
"# Load the symbols from the JSON file\n",
"with open('symbols_map.json', 'r') as file:\n",
" symbols = json.load(file)\n",
"\n",
"# Extract the \"OVER\" symbol\n",
"SLASH_SYMBOL = symbols[\"/\"]\n",
"\n",
"# Create directories if they don't exist\n",
"for directory in [INPUT_DIR, OUTPUT_DIR, ASSETS_DIR]:\n",
" os.makedirs(directory, exist_ok=True)\n",
"\n",
"def create_text_overlays(filename):\n",
" \"\"\"Create text overlays using the GIF filename as text_1\"\"\"\n",
" # Clean up filename by removing extension and replacing underscores/hyphens with spaces\n",
" clean_name = os.path.splitext(filename)[0].replace('_', ' ').replace('-', ' ')\n",
" clean_name = os.path.splitext(filename)[0].replace(SLASH_SYMBOL, \"/\")\n",
"\n",
" return [\n",
" {\n",
Expand Down
6 changes: 4 additions & 2 deletions other/media_generation/record_gifs_with_wave.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"import time\n",
"\n",
"URL = \"https://deploy-preview-154--wave-js.netlify.app/\" # or \"http://localhost:3002/\"\n",
"JSON_FOLDER = \"input\""
"JSON_FOLDER = \"uploads\""
],
"outputs": [],
"execution_count": null
Expand Down Expand Up @@ -57,7 +57,8 @@
"for data in json_data:\n",
" materials_settings.append({\n",
" \"material_json\": data,\n",
" \"name\": file_names[json_data.index(data)]\n",
" \"name\": file_names[json_data.index(data)] # name of the file\n",
" # \"name\": data[\"name\"] # name of the material\n",
" })\n",
"\n",
"#\n",
Expand Down Expand Up @@ -301,6 +302,7 @@
" parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir, os.pardir))\n",
" gif_path = os.path.join(parent_dir, GIF_NAME)\n",
" print(f\"Waiting for gif to be generated in {gif_path}\")\n",
"\n",
" while not os.path.exists(gif_path):\n",
" time.sleep(1)\n",
" else:\n",
Expand Down
3 changes: 3 additions & 0 deletions other/media_generation/symbols_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"/": "OVER"
}
Loading