Skip to content

Commit

Permalink
Fix more merge conflicts and add competent subset extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
BC-Chang committed Dec 4, 2024
1 parent 52e9717 commit 75cbf6b
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 89 deletions.
7 changes: 0 additions & 7 deletions dpm_tools/io/_io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ def convert_filetype(filepath: pathlib.Path, convert_to: str, **kwargs) -> None:
image=original_image, filetype=convert_to)


<< << << < HEAD
== == == =


def download_file(url: str, save_path: str = ".") -> None:
"""
Download a file from the provided URL and save it to the specified save path.
Expand All @@ -207,6 +203,3 @@ def download_file(url: str, save_path: str = ".") -> None:
wget.download(url, out=save_path)

return


>>>>>> > 3dfbfc1db602ad1d542da0ed78e2787f6f89c60b
14 changes: 9 additions & 5 deletions dpm_tools/io/_read_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def _read_raw(filepath: pathlib.Path, **kwargs) -> np.ndarray:
metadata = kwargs['meta']

assert all(key in metadata for key in ['nz', 'ny', 'nx', 'bits', 'signed', 'byte_order']), \
f"Image metadata dictionary must " \
f"contain {{'nz', 'ny', 'nx', 'bits', 'signed', 'byte_order'} - metadata.keys()}"
f"Image metadata dictionary must contain {
['nz', 'ny', 'nx', 'bits', 'signed', 'byte_order']} - found {list(metadata.keys())}"

bits = metadata['bits']
signed = metadata['signed']
Expand Down Expand Up @@ -93,6 +93,7 @@ def _read_raw(filepath: pathlib.Path, **kwargs) -> np.ndarray:

# TODO: Review this function


def _read_nc(filepath: pathlib.Path, **kwargs) -> np.ndarray:
"""
A utility function to read in netcdf files
Expand Down Expand Up @@ -261,7 +262,8 @@ def __post_init__(self):

# If vector is populated
if self.vector is not None:
self.magnitude = np.sqrt(self.vector[0]**2 + self.vector[1]**2 + self.vector[2]**2)
self.magnitude = np.sqrt(
self.vector[0]**2 + self.vector[1]**2 + self.vector[2]**2)

self.nz, self.nx, self.ny = self.scalar.shape
self.shape = (self.nz, self.nx, self.ny)
Expand All @@ -285,9 +287,11 @@ def _read_data_from_files(self):
A utility method to read images in from a list of filepaths
"""

images = [read_image(fp, meta=self.meta[i]) if fp else None for i, fp in enumerate(self.filepaths)]
images = [read_image(fp, meta=self.meta[i])
if fp else None for i, fp in enumerate(self.filepaths)]

assert len(images) == 1 or len(images) == 3 or len(images) == 4, f"You must provide a scalar, a vector, or both"
assert len(images) == 1 or len(images) == 3 or len(
images) == 4, f"You must provide a scalar, a vector, or both"

if len(images) == 1:
self.scalar = images[0]
Expand Down
15 changes: 9 additions & 6 deletions dpm_tools/metrics/_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ def histogram_statistics(*images: np.ndarray, plot_histogram: bool = False, nbin
Get the statistics of multiple images.
Parameters:
*images: A list of 2D or 3D images.
plot_histograms: Plot the image histograms. Defaults to False.
bins: Number of histogram bins. Defaults to 256.
nbins: Number of histogram bins. Defaults to 256.
legend_elem: A list of strings to use as legend labels. Defaults to an empty list.
Returns:
dict: A dictionary of image metadata and histogram statistics.
Expand All @@ -226,25 +228,26 @@ def histogram_statistics(*images: np.ndarray, plot_histogram: bool = False, nbin
for image in images:
stats = _hist_stats(image, nbins)

print('-'*35)
print('-' * 35)
print('Image statistics:')
print(f'\tShape: {stats["shape"]}')
print(f'\tData type: {stats["dtype"]}')
print(f'\tMin: {stats["min"]}, Max: {
stats["max"]}, Mean: {stats["mean"]}\n')

img_stats.append(_hist_stats(image, nbins))
img_list.append(image.flatten())

if plot_histogram:
assert len(legend_elem) == len(
images), 'Number of legend elements must match the number of images'
fig, ax = plt.subplots()
ax.hist(img_list, bins=nbins, density=True,
histtype='step', fill=False)
for img, label in zip(images, legend_elem):
ax.hist(img.flatten(), bins=nbins, density=True,
histtype='step', fill=False, label=label)
ax.set_xlabel('Image Value', fontsize=16)
ax.set_ylabel('Probability Density', fontsize=16)
ax.grid(True)
ax.legend(legend_elem)
ax.legend()
# ax.legend(handles, labels)

return img_stats
28 changes: 2 additions & 26 deletions dpm_tools/visualization/_plot_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,38 +161,19 @@ def plot_heterogeneity_curve(radii: np.ndarray, variances: np.ndarray, relative_
relative_radii: If True, the plotted radii are relative to the first window size. Otherwise, the absolute radii are shown.
Returns:
<<<<<<< HEAD
fig, ax: Matplotlib figure and axes object.
"""
if fig is None and ax is None:
fig1, ax1 = plt.subplots()
else:
assert fig is not None and ax is not None, "Both fig and ax must be provided."
fig1, ax1 = fig, ax
# plt.figure()
if relative_radii:
ax1.plot(variances, markersize=6, **kwargs)
ax1.set_xlabel("Relative Radius")
else:
ax1.plot(radii, variances, markersize=6,
**kwargs)


== == == =
fig, ax: Matplotlib figure and axes objects.
"""

if fig is None or ax is None:
fig1, ax1 = plt.subplots()
else:
fig1, ax1 = fig, ax

if relative_radii:
ax1.plot(variances, **kwargs)
ax1.set_xlabel("Relative Radius")
else:
ax1.plot(radii, variances, **kwargs)
>>>>>>> 3dfbfc1db602ad1d542da0ed78e2787f6f89c60b
ax1.set_xlabel("Absolute Radius")

x = np.linspace(-2, 17, len(variances))
Expand All @@ -210,9 +191,4 @@ def plot_heterogeneity_curve(radii: np.ndarray, variances: np.ndarray, relative_

ax1.set_ylabel("Porosity Variance")

<<<<<<< HEAD
# plt.legend()

=======
>>>>>>> 3dfbfc1db602ad1d542da0ed78e2787f6f89c60b
return fig1, ax1
Loading

0 comments on commit 75cbf6b

Please sign in to comment.