Skip to content

Commit

Permalink
pep8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgostick committed Jun 7, 2024
1 parent d9ccb36 commit 55c136d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/porespy/metrics/_meshtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def region_volumes(regions, mode='marching_cubes', voxel_size=(1, 1, 1)):
msg = "Computing region volumes".ljust(60)
voxel_size = np.array(voxel_size, dtype=float, ndmin=1)
if np.size(voxel_size) == 1:
voxel_size = np.array([voxel_size for i in range(im.ndim)]).flatten()
voxel_size = np.array([voxel_size for i in range(regions.ndim)]).flatten()

Check warning on line 63 in src/porespy/metrics/_meshtools.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/metrics/_meshtools.py#L63

Added line #L63 was not covered by tests
for i, s in enumerate(tqdm(slices, desc=msg, **settings.tqdm)):
region = regions[s] == (i + 1)
if mode == 'marching_cubes':
Expand Down
77 changes: 39 additions & 38 deletions src/porespy/networks/_getnet_para.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ def wait():

def calculate_throat_perimeter(vx, sub_dt, voxel_size):
# Directions used to evaluate geom properties
dirs = [
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(-1, 0, 0),
(0, -1, 0),
(0, 0, -1),
]
voxel_size = np.array(voxel_size)
voxel_areas = np.array([
voxel_size[1] * voxel_size[2],
voxel_size[2] * voxel_size[0],
voxel_size[0] * voxel_size[1],
])
# dirs = [
# (1, 0, 0),
# (0, 1, 0),
# (0, 0, 1),
# (-1, 0, 0),
# (0, -1, 0),
# (0, 0, -1),
# ]
# voxel_size = np.array(voxel_size)
# voxel_areas = np.array([
# voxel_size[1] * voxel_size[2],
# voxel_size[2] * voxel_size[0],
# voxel_size[0] * voxel_size[1],
# ])

dx = voxel_size[0]*(max(vx[0]) - min(vx[0]))
dy = voxel_size[1]*(max(vx[1]) - min(vx[1]))
Expand Down Expand Up @@ -552,14 +552,10 @@ def _jit_regions_to_network_parallel(
worker_target = np.zeros((threads-1,), dtype=np.uint32)

Check warning on line 552 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L551-L552

Added lines #L551 - L552 were not covered by tests

for self_id in prange(threads):

if (self_id == (threads - 1)):

current_pore = 1

while True:
wait()

for worker_id in range(len(worker_status)):
if worker_status[worker_id] == IDLE:
if current_pore <= Np:
Expand All @@ -569,17 +565,23 @@ def _jit_regions_to_network_parallel(
else:
worker_status[worker_id] = FINISHED
if worker_status[worker_id] == DONE:

for throat_i in range(len(partial_t_conns_0[worker_id])):
t_conns_0.append(partial_t_conns_0[worker_id][throat_i])
t_conns_1.append(partial_t_conns_1[worker_id][throat_i])
t_dia_inscribed.append(partial_t_dia_inscribed[worker_id][throat_i])
t_perimeter.append(partial_t_perimeter[worker_id][throat_i])
t_area.append(partial_t_area[worker_id][throat_i])
t_coords_0.append(partial_t_coords_0[worker_id][throat_i])
t_coords_1.append(partial_t_coords_1[worker_id][throat_i])
t_coords_2.append(partial_t_coords_2[worker_id][throat_i])

t_conns_0.append(

Check warning on line 569 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L566-L569

Added lines #L566 - L569 were not covered by tests
partial_t_conns_0[worker_id][throat_i])
t_conns_1.append(

Check warning on line 571 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L571

Added line #L571 was not covered by tests
partial_t_conns_1[worker_id][throat_i])
t_dia_inscribed.append(

Check warning on line 573 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L573

Added line #L573 was not covered by tests
partial_t_dia_inscribed[worker_id][throat_i])
t_perimeter.append(

Check warning on line 575 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L575

Added line #L575 was not covered by tests
partial_t_perimeter[worker_id][throat_i])
t_area.append(

Check warning on line 577 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L577

Added line #L577 was not covered by tests
partial_t_area[worker_id][throat_i])
t_coords_0.append(

Check warning on line 579 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L579

Added line #L579 was not covered by tests
partial_t_coords_0[worker_id][throat_i])
t_coords_1.append(

Check warning on line 581 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L581

Added line #L581 was not covered by tests
partial_t_coords_1[worker_id][throat_i])
t_coords_2.append(

Check warning on line 583 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L583

Added line #L583 was not covered by tests
partial_t_coords_2[worker_id][throat_i])
if current_pore <= Np:
worker_target[worker_id] = current_pore
current_pore += 1
Expand All @@ -588,16 +590,11 @@ def _jit_regions_to_network_parallel(
worker_status[worker_id] = FINISHED
if (np.equal(worker_status, FINISHED).all()):
break

Check warning on line 592 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L590-L592

Added lines #L590 - L592 were not covered by tests

else:

while True:

wait()
status = worker_status[self_id]

if status == ASSIGNED:

partial_t_conns_0[self_id] = List.empty_list(np.uint64)
partial_t_conns_1[self_id] = List.empty_list(np.uint64)
partial_t_dia_inscribed[self_id] = List.empty_list(np.float64)
Expand Down Expand Up @@ -654,20 +651,24 @@ def _jit_regions_to_network_parallel(
if j > pore:
partial_t_conns_0[self_id].append(pore)
partial_t_conns_1[self_id].append(j)
partial_t_dia_inscribed[self_id].append(inscribed_diameter[j])
partial_t_dia_inscribed[self_id].append(

Check warning on line 654 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L650-L654

Added lines #L650 - L654 were not covered by tests
inscribed_diameter[j])
partial_t_perimeter[self_id].append(perimeters[j])
partial_t_area[self_id].append(areas[j])
partial_t_coords_0[self_id].append(centers[j][0] + s_offset[0])
partial_t_coords_1[self_id].append(centers[j][1] + s_offset[1])
partial_t_coords_2[self_id].append(centers[j][2] + s_offset[2])
partial_t_coords_0[self_id].append(centers[j][0] +

Check warning on line 658 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L656-L658

Added lines #L656 - L658 were not covered by tests
s_offset[0])
partial_t_coords_1[self_id].append(centers[j][1] +

Check warning on line 660 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L660

Added line #L660 was not covered by tests
s_offset[1])
partial_t_coords_2[self_id].append(centers[j][2] +

Check warning on line 662 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L662

Added line #L662 was not covered by tests
s_offset[2])

worker_status[self_id] = DONE

Check warning on line 665 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L665

Added line #L665 was not covered by tests

elif status == FINISHED:
break

Check warning on line 668 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L667-L668

Added lines #L667 - L668 were not covered by tests

# Clean up values
Nt = len(t_conns_0) # Get number of throats
# Nt = len(t_conns_0) # Get number of throats

if len(t_conns_0) == 0:
return None

Check warning on line 674 in src/porespy/networks/_getnet_para.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/networks/_getnet_para.py#L673-L674

Added lines #L673 - L674 were not covered by tests
Expand Down
56 changes: 30 additions & 26 deletions src/porespy/tools/_marching_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
import math
import itertools
from pathlib import Path

from numba import njit
import numpy as np
from skimage import measure
from scipy import spatial


MC_TEMPLATES_FILENAME = 'marching_cubes_templates.dat'
THIS_FOLDER = Path(__file__).absolute().parent
MC_TEMPLATES_PATH = os.path.join(THIS_FOLDER, MC_TEMPLATES_FILENAME)


def face_orientation(v0, v1, v2):
'''
Return outward perpendicular vector distance of face along the z axis
Expand All @@ -28,36 +29,37 @@ def face_orientation(v0, v1, v2):
else:
return 0

Check warning on line 30 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L30

Added line #L30 was not covered by tests


def area_of_triangle(p0, p1, p2):
'''
As per Herons formula
'''
lines = list(itertools.combinations((p0,p1,p2),2))
distances = [(spatial.distance.euclidean(i[0],i[1])) for i in lines]
lines = list(itertools.combinations((p0, p1, p2), 2))
distances = [(spatial.distance.euclidean(i[0], i[1])) for i in lines]
s = sum(distances)/2
product_of_diferences = np.prod([(s-i) for i in distances])
area = math.sqrt(s*product_of_diferences)
return area

Check warning on line 42 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L37-L42

Added lines #L37 - L42 were not covered by tests


def mc_templates_generator(override = False):
def mc_templates_generator(override=False):
'''
Generates a marching cubes template list file, if one is not available
'''
if os.path.isfile(MC_TEMPLATES_PATH) and not override:
return
summation_to_coordinate = {}
for i in [(x,y,z) for x in range(2) for y in range(2) for z in range(2)]:
for i in [(x, y, z) for x in range(2) for y in range(2) for z in range(2)]:
summation_to_coordinate[2 ** (i[0] + 2*i[1] + 4*i[2])] = i

Check warning on line 53 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L49-L53

Added lines #L49 - L53 were not covered by tests

templates_triangles = []
for _ in range(256):
templates_triangles.append( [[],[]] )
templates_triangles.append([[], []])

Check warning on line 57 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L55-L57

Added lines #L55 - L57 were not covered by tests

for i in range(1,255):
array = np.zeros((2,2,2))
for i in range(1, 255):
array = np.zeros((2, 2, 2))
index = i
for j in range(7,-1,-1):
for j in range(7, -1, -1):
e = 2**j
if index >= e:
index -= e
Expand All @@ -66,7 +68,7 @@ def mc_templates_generator(override = False):
templates_triangles[i][0] = verts
templates_triangles[i][1] = faces

Check warning on line 69 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L59-L69

Added lines #L59 - L69 were not covered by tests

with open(MC_TEMPLATES_PATH, mode = 'w') as file:
with open(MC_TEMPLATES_PATH, mode='w') as file:
for i in range(256):
verts, faces = templates_triangles[i]
file.write(f'{i};')
Expand All @@ -78,7 +80,7 @@ def mc_templates_generator(override = False):
file.write('\n')

Check warning on line 80 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L71-L80

Added lines #L71 - L80 were not covered by tests


def create_mc_template_list(spacing = (1,1,1)):
def create_mc_template_list(spacing=(1, 1, 1)):
'''
Return area and volume lists for the marching cubes templates
Reads the templates file
Expand All @@ -90,8 +92,8 @@ def create_mc_template_list(spacing = (1,1,1)):
areas = {}
volumes = {}
triangles = {}
vertices_on_top = set((16,32,64,128))
with open(MC_TEMPLATES_PATH, mode = 'r') as file:
vertices_on_top = set((16, 32, 64, 128))
with open(MC_TEMPLATES_PATH, mode='r') as file:
for line in file:
index, verts, faces = line.split(';')
index = int(index)
Expand All @@ -108,7 +110,7 @@ def create_mc_template_list(spacing = (1,1,1)):

occupied_vertices = set()
sub_index = index
for i in range(7,-1,-1):
for i in range(7, -1, -1):
e = 2 ** i
if sub_index >= e:
occupied_vertices.add(e)
Expand All @@ -120,7 +122,7 @@ def create_mc_template_list(spacing = (1,1,1)):
basic_volume = 1/8
elif total_vertices_on_top == 2:
if ((16 in occupied_vertices and 128 in occupied_vertices) or

Check warning on line 124 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L111-L124

Added lines #L111 - L124 were not covered by tests
(32 in occupied_vertices and 64 in occupied_vertices)):
(32 in occupied_vertices and 64 in occupied_vertices)):
basic_volume = 1/4

Check warning on line 126 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L126

Added line #L126 was not covered by tests
else:
basic_volume = 1/2
Expand All @@ -131,10 +133,10 @@ def create_mc_template_list(spacing = (1,1,1)):

for f in faces:
v0, v1, v2 = [verts[i] for i in f]
v0_proj, v1_proj, v2_proj = [(i[0],i[1],0) for i in (v0,v1,v2)]
mean_z = sum([i[2] for i in (v0,v1,v2)])/3
v0_proj, v1_proj, v2_proj = [(i[0], i[1], 0) for i in (v0, v1, v2)]
mean_z = sum([i[2] for i in (v0, v1, v2)])/3
proj_area = area_of_triangle(v0_proj, v1_proj, v2_proj)
direction = face_orientation(v0,v1,v2)
direction = face_orientation(v0, v1, v2)
basic_volume += mean_z * proj_area * direction

Check warning on line 140 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L134-L140

Added lines #L134 - L140 were not covered by tests

for i in range(len(verts)):
Expand Down Expand Up @@ -175,14 +177,15 @@ def calculate_area_and_volume(
area = 0.0
volume = 0.0

Check warning on line 178 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L176-L178

Added lines #L176 - L178 were not covered by tests

if debug: print("Shape: ", w, h, d)
if debug:
print("Shape: ", w, h, d)

Check warning on line 181 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L180-L181

Added lines #L180 - L181 were not covered by tests

for x in range(w - 1):
for y in range(h - 1):
for z in range(d - 1):
if debug: print(x, y, z)
sub_array = img[x : x + 2, y : y + 2, z : z + 2]

if debug:
print(x, y, z)
sub_array = img[x:x + 2, y:y + 2, z:z + 2]
if target_label not in sub_array:
continue

Check warning on line 190 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L183-L190

Added lines #L183 - L190 were not covered by tests

Expand Down Expand Up @@ -221,6 +224,7 @@ def marching_cubes_area_and_volume(

return area, volume

Check warning on line 225 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L225

Added line #L225 was not covered by tests


@njit
def jit_marching_cubes_area_and_volume(
img,
Expand All @@ -232,10 +236,10 @@ def jit_marching_cubes_area_and_volume(
):

vertex_index_array = np.array(

Check warning on line 238 in src/porespy/tools/_marching_cubes.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_cubes.py#L238

Added line #L238 was not covered by tests
[[[ 1, 16],
[ 4, 64]],
[[ 2, 32],
[ 8, 128]]],
[[[1, 16],
[4, 64]],
[[2, 32],
[8, 128]]],
dtype=np.int32
)

Expand Down
21 changes: 13 additions & 8 deletions src/porespy/tools/_marching_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
1/4,
)


@njit
def jit_marching_squares_perimeter_and_area(
img,
Expand All @@ -82,17 +83,21 @@ def jit_marching_squares_perimeter_and_area(
for x in range(max_x - 1):
for y in range(max_y - 1):
template = 0
if img[x , y ] == target_label: template += 1
if img[x + 1, y ] == target_label: template += 2
if img[x , y + 1] == target_label: template += 4
if img[x + 1, y + 1] == target_label: template += 8
if img[x, y] == target_label:
template += 1
if img[x + 1, y] == target_label:
template += 2
if img[x, y + 1] == target_label:
template += 4
if img[x + 1, y + 1] == target_label:
template += 8
if overlap:
total_perimeter += perimeters[TEMPLATE_PERIMETER[template]] * TEMPLATE_REPETITION[template]
total_area += complete_area * TEMPLATE_AREA[template] * TEMPLATE_REPETITION[template]
total_perimeter += perimeters[TEMPLATE_PERIMETER[template]] \

Check warning on line 95 in src/porespy/tools/_marching_squares.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_squares.py#L80-L95

Added lines #L80 - L95 were not covered by tests
* TEMPLATE_REPETITION[template]
total_area += complete_area * TEMPLATE_AREA[template] \

Check warning on line 97 in src/porespy/tools/_marching_squares.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_squares.py#L97

Added line #L97 was not covered by tests
* TEMPLATE_REPETITION[template]
else:
total_perimeter += perimeters[TEMPLATE_PERIMETER[template]]
total_area += complete_area * TEMPLATE_AREA[template]

Check warning on line 101 in src/porespy/tools/_marching_squares.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_squares.py#L100-L101

Added lines #L100 - L101 were not covered by tests

return total_perimeter/2, total_area

Check warning on line 103 in src/porespy/tools/_marching_squares.py

View check run for this annotation

Codecov / codecov/patch

src/porespy/tools/_marching_squares.py#L103

Added line #L103 was not covered by tests


0 comments on commit 55c136d

Please sign in to comment.