COWS exampleΒΆ

This Jupyter Notebook is available here and the Python .py file here.

import cows
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipykernel_250/1034075976.py in <module>
      1 import cows
      2 import numpy as np
----> 3 import matplotlib.pyplot as plt
      4 get_ipython().run_line_magic('matplotlib', 'inline')

ModuleNotFoundError: No module named 'matplotlib'
def thick_slice(data, z, dz, operator='and'):
    '''Returns a slice of the data at z
        with thickness dz.
    '''
    zmin = int(z-np.round(dz/2))
    zmax = int(z+np.round(dz/2))
    if operator == 'and':
        return np.any(data[zmin:zmax], axis=0).astype(int)
    if operator == 'sum':
        return np.sum(data[zmin:zmax], axis=0)
    else:
        raise ValueError('Invalid operator: {}'.format(operator))
    
# Load the test data. It consists of a 64x64x64 cube of V-web data, a cosmic web classifier.
# In the context of the V-web, the values are:
# 0 - voids
# 1 - sheets
# 2 - filaments
# 3 - knots
data = np.load('../tests/test_data.npy')
ncells = data.shape[0]
# Plot a slice of the V-web centred of a big knot
z = 11 # depth of slice
plt.imshow(data[z], origin='lower')
plt.show()
_images/54c8591f4050d8eccc9c02fc0c8dc1be25ac8c43080c9eff897014d4a6ccce03.png
# Process the V-web data to generate the COWS input data.
# Set the filaments and knots to foreground value (1)
cows_input = np.zeros(data.shape)
cows_input[data==2] = 1 # filaments
cows_input[data==3] = 1 # knots

# Plot the same slice of input data
plt.imshow(cows_input[z], cmap='binary', origin='lower')
plt.show()
_images/004c959d2a1ae70ce470b5b374729e80321a618f675ee2619683b2a14862dcfb.png
# Start the COWS method
# First, get the medial axis, the skeleton of the input data.
# The V-web data is a chuck from a periodic simulation, so periodic=False. If it was
# the full periodic simulation box, periodic=True.
skeleton = cows.skeletonize(cows_input, periodic=False)

# Plot a slice of the skeleton with some thickness, dz
dz = 10
plt.imshow(thick_slice(skeleton,z,dz,operator='and'), cmap='binary', origin='lower')
plt.show()
_images/cc9b64243158fe8aee6c311384b4844bb225ff26238be5d8f4d371cc6e325af5.png
# Remove cells that are knots in the V-web
skeleton[data==3] = 0

# Separate the skeleton into filaments by removing cavities (blobs) and junctions
filaments = cows.separate_skeleton(skeleton, periodic=False)

# Plot a slice of the skeleton with some thickness, dz
plt.imshow(thick_slice(filaments,z,dz,operator='and'), cmap='binary', origin='lower')
plt.show()
_images/97b8e0ba3da45689700e0e0cec3a3444418e599e1dcba1ec3a43164a73165447.png
# Generate a filament catalogue
catalogue = cows.gen_catalogue(filaments)

# The filament catalogue is formatted as follows:
# filament ID, filament length, X-, Y-, Z-position (on the grid), X-, Y-, Z-direction
print(catalogue[0])
[ 1. 33. 61. 16. 50.  0.  1.  0.]