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

Jump Probability with Greyscale Images #24

Merged
merged 11 commits into from
Jun 15, 2023
Next Next commit
make a start on grey
  • Loading branch information
TomTranter committed Oct 8, 2019
commit 46d86d35c42dd47d53f1d3fbc2e27baeabfb615b
26 changes: 26 additions & 0 deletions grey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 8 17:07:13 2019

@author: Tom
"""

import porespy as ps
import pytrax as pt
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as spim
if __name__ == '__main__':
im = ps.generators.blobs(shape=[1000, 1000], porosity=0.7).astype(np.int)
dt = spim.distance_transform_edt(im)
grey = dt.copy()/dt.max()
# Number of time steps and walkers
num_t = 10000
num_w = 800
stride = 1

rw = pt.RandomWalk(grey, seed=False)
rw.run(num_t, num_w, same_start=False, stride=stride, num_proc=8)
# Plot mean square displacement
rw.plot_msd()
rw.plot_walk_2d()
31 changes: 28 additions & 3 deletions pytrax/__RandomWalk__.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ def _get_starts(self, same_start=False):
walkers = np.tile(w, (self.nw, 1))
return walkers

def get_probable_cancels(self, walkers):
r'''
If the image is grey scale and has been normalized with values ranging
between 0.0 and 1.0 where the fractional value in each pixel represents
a fraction of space that is void we can moderate the walker movements
so that walkers in higher void space fractions have a higher chance of
moving on.
'''
random_cancel = np.random.random(len(walkers))
if self.dim == 2:
greys = self.im[walkers[:, 0],
walkers[:, 1]]
elif self.dim == 3:
greys = self.im[walkers[:, 0],
walkers[:, 1],
walkers[:, 2]]
return greys < random_cancel

def _run_walk(self, walkers):
r'''
Run the walk in self contained way to enable parallel processing for
Expand Down Expand Up @@ -245,6 +263,12 @@ def _run_walk(self, walkers):
if np.any(wall_hit):
m[wall_hit] = 0
mr[wall_hit] = 0
# Check that the move out of the current voxel is ok based on the
# Grey scale of the image
cancels = self.get_probable_cancels(walkers)
if np.any(cancels):
m[cancels] = 0
mr[cancels] = 0
# Reflected velocity in real direction
wr += mr*real
walkers += m
Expand Down Expand Up @@ -435,9 +459,9 @@ def _build_big_image(self, num_copies=0):
num_copies: int
the number of times to copy the image along each axis
'''
big_im = self.im.copy()
big_im = self.im != self.solid_value
func = [np.vstack, np.hstack, np.dstack]
temp_im = self.im.copy()
temp_im = big_im.copy()
for ax in tqdm(range(self.dim), desc='building big image'):
flip_im = np.flip(temp_im, ax)
for c in range(num_copies):
Expand Down Expand Up @@ -541,7 +565,8 @@ def plot_walk_2d(self, w_id=None, data='t', check_solid=False):
solid = big_im == self.solid_value-2
solid = solid.astype(float)
solid[np.where(solid == 0)] = np.nan
porous = big_im == self.solid_value-1
# porous = big_im == self.solid_value-1
porous = big_im == -1
porous = porous.astype(float)
porous[np.where(porous == 0)] = np.nan
plt.imshow(big_im, cmap=cmap)
Expand Down