Skip to content

Commit

Permalink
Added optical class with import functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Curran committed Sep 29, 2024
1 parent bb80dbf commit 97ab620
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 1 deletion.
126 changes: 126 additions & 0 deletions defdap/dev-Optical.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "a167425e-fc5d-4f0d-ad30-96ab225104a9",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'numba'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[3], line 7\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfile_readers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m OpticalDataLoader, MatplotlibLoader\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ebsd\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01moptical\u001b[39;00m \n",
"File \u001b[1;32mC:\\\\Users\\\\mbgm5pc3\\\\dev-optical\\\\DefDAP\\defdap\\ebsd.py:28\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mquat\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Quat\n\u001b[0;32m 27\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m base\n\u001b[1;32m---> 28\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_accelerated\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m flood_fill\n\u001b[0;32m 30\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m defaults\n\u001b[0;32m 31\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdefdap\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mplotting\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m MapPlot\n",
"File \u001b[1;32mC:\\\\Users\\\\mbgm5pc3\\\\dev-optical\\\\DefDAP\\defdap\\_accelerated.py:1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnumba\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m njit\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;129m@njit\u001b[39m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfind_first\u001b[39m(arr):\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'numba'"
]
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.image as mpimg\n",
"#%matplotlib qt5\n",
"from defdap.file_readers import OpticalDataLoader, MatplotlibLoader\n",
"import defdap\n",
"from defdap import ebsd\n",
"import optical "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "23fafbcf-4cb1-4540-b85e-10906c14cfe0",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(r'C:\\\\Users\\\\mbgm5pc3\\\\dev-optical\\\\DefDAP')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ae24a9b-6842-4540-b36b-3795c1d85f05",
"metadata": {},
"outputs": [],
"source": [
"fname = \"./test_data/F5-pol-Cy50.png\"\n",
"Opt = optical.Map(fname)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bcb125c-55be-4745-a22c-3578cef3f7ee",
"metadata": {},
"outputs": [],
"source": [
"Opt.set_scale (2.105) #um/pixel"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4387f479-785f-4639-b778-b60148f849cb",
"metadata": {},
"outputs": [],
"source": [
"#Opt.set_crop (left = 10, right = 10, top = 10, bottom = 10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de922a4a-44ff-45a9-8f1d-2fb99297b698",
"metadata": {},
"outputs": [],
"source": [
"Opt.plot_optical_image()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0eaeccb-5e58-428e-b448-903ed9bb1942",
"metadata": {},
"outputs": [],
"source": [
"EbsdMap = ebsd.Map(path+'../EBSD_data/post_deformed/f-5-test-region')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a0c8059-c551-40b6-8091-cbaacbd798e7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
33 changes: 33 additions & 0 deletions defdap/file_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from abc import ABC, abstractmethod
import pathlib
import re
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

from typing import TextIO, Dict, List, Callable, Any, Type, Optional

Expand All @@ -27,6 +29,32 @@
from defdap.utils import Datastore



class OpticalDataLoader(ABC):
def __init__(self, file_path):
"""Initialize with the file path of the image."""
self.file_path = file_path

@abstractmethod
def load_image(self):
"""Abstract method to load the image. Must be implemented by subclass."""
pass

class MatplotlibLoader(OpticalDataLoader):
def load_image(self):
"""Loads an image using matplotlib and displays it."""
try:
# Use matplotlib's imread to load the image
self.image = mpimg.imread(self.file_path)
print(f"Image loaded from {self.file_path}")

return self.image
except Exception as e:
print(f"Failed to load image: {e}")
return None



class EBSDDataLoader(ABC):
"""Class containing methods for loading and checking EBSD data
Expand Down Expand Up @@ -741,6 +769,7 @@ def load_davis_image_data(file_name: pathlib.Path) -> np.ndarray:
return np.array(data)



class OpenPivLoader(DICDataLoader):
def load(self, file_name: pathlib.Path) -> None:
""" Load from Open PIV .txt file.
Expand Down Expand Up @@ -791,13 +820,16 @@ def load(self, file_name: pathlib.Path) -> None:
self.check_data()



def read_until_string(
file: TextIO,
term_string: str,
comment_char: str = '*',
line_process: Optional[Callable[[str], Any]] = None,
exact: bool = False
) -> List[Any]:


"""Read lines in a file until a line starting with the `termString`
is encountered. The file position is returned before the line starting
with the `termString` when found. Comment and empty lines are ignored.
Expand Down Expand Up @@ -837,3 +869,4 @@ def read_until_string(
line = line_process(line)
lines.append(line)
return lines

2 changes: 1 addition & 1 deletion defdap/hrdic.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def load_data(self, file_name, data_type=None):

# write final status
yield (f"Loaded {self.format} {self.version} data "
f"(dimensions: {self.xdim} x {self.xdim} pixels, "
f"(dimensions: {self.xdim} x {self.ydim} pixels, "
f"sub-window size: {self.binning} x {self.binning} pixels)")

def load_corr_val_data(self, file_name, data_type=None):
Expand Down
Loading

0 comments on commit 97ab620

Please sign in to comment.