src/DSGRN/SaveDatabaseJSON.py
def cubical_complex_json(network):
    """Return json data for cubical complex."""
    # Get complex dimension
    dimension = network.size()
    # Construct a cubical complex using pychomp. A cubical complex in pychomp
    # does not contain the rightmost boundary, so make one extra layer of
    # cubes and ignore the last layer (called rightfringe in pychomp).
    cubical_complex = pychomp.CubicalComplex([x + 1 for x in network.domains()])
    # Get vertices coordinates and set a
    # mapping from coords to its index in
    # the list of coordinates.
    verts_coords = []
    coords2idx = {}
    # Get the coords of all cells of dimension 0.
    # The 0-dim cells in a cubical complex in pychomp
    # are indexed from 0 to n-1, where n is the number
    # of 0-dim cells. Hence the cell_index coincides
    # with the index of coords in the list verts_coords.
    for cell_index in cubical_complex(0):
        coords = cubical_complex.coordinates(cell_index)
        coords2idx[tuple(coords)] = cell_index
        verts_coords.append(coords)
    cells = [] # Get the cell complex data
    for cell_index in cubical_complex:
        # Ignore fringe cells
        if cubical_complex.rightfringe(cell_index):
            continue
        # Get this cell dimension
        cell_dim = cubical_complex.cell_dim(cell_index)
        # Get coords of the lower corner of the box
        coords_lower = cubical_complex.coordinates(cell_index)
        # Get index of vertex corresponding to these coords
        # Due to the way pychomp index the 0-dim cells we get
        # that idx_lower == cell_index (see coords2idx above).
        idx_lower = coords2idx[tuple(coords_lower)]
        # Get the shape of this cell (see pychomp)
        shape = cubical_complex.cell_shape(cell_index)
        # Add 1 to the appropriate entries to get coords of the upper corner
        coords_upper = [coords_lower[d] + (1 if shape & (1 << d) != 0 else 0) for d in range(dimension)]
        # Get index of vertex corresponding to these coords
        idx_upper = coords2idx[tuple(coords_upper)]
        # Get indices of coords that have extent
        ind_extent = [d for d in range(dimension) if shape & (1 << d) != 0]
        if cell_dim == 0:
            cell_verts = [idx_lower]
        elif cell_dim == 1:
            cell_verts = [idx_lower, idx_upper]
        elif cell_dim == 2:
            # Index of vertex 1
            idx1 = idx_lower
            # Coords and index of vertex 2
            coords_v2 = [coord for coord in coords_lower]
            coords_v2[ind_extent[0]] += 1
            idx2 = coords2idx[tuple(coords_v2)]
            # Index of vertex 3
            idx3 = idx_upper
            # Coords and index of vertex 4
            coords_v4 = [coord for coord in coords_lower]
            coords_v4[ind_extent[1]] += 1
            idx4 = coords2idx[tuple(coords_v4)]
            cell_verts = [idx1, idx2, idx3, idx4]
        elif cell_dim == 3: # cell_dim == dimension == 3
            # First get vertices of unit cube as cartesian product of {0, 1}
            u_verts = list(itertools.product((0, 1), repeat=cell_dim))
            # Add verts of unit cube to coords_lower to get verts of this cell
            cell_verts = []
            for u in u_verts:
                coords_vert = [sum(x) for x in zip(u, coords_lower)]
                idx_vert = coords2idx[tuple(coords_vert)]
                cell_verts.append(idx_vert)
        else: # Ignore cell if dim > 3
            continue
        cell = {"cell_dim" : cell_dim, "cell_index" : cell_index, "cell_verts" : cell_verts}
        cells.append(cell)
    complex_json_data = {"complex" : {"dimension" : dimension,
                                      "verts_coords" : verts_coords,
                                      "cells" : cells} }
    return complex_json_data
Return json data for cubical complex.
Return json data for cubical complex.
Return json data for cubical complex .
def cubical_complex_json(network):
    dimension = network.size()
    cubical_complex = pychomp.CubicalComplex([x + 1 for x in network.domains()])
    verts_coords = []
    coords2idx = {}
    for cell_index in cubical_complex(0):
        coords = cubical_complex.coordinates(cell_index)
        coords2idx[tuple(coords)] = cell_index
        verts_coords.append(coords)
    cells = [] 
    for cell_index in cubical_complex:
        if cubical_complex.rightfringe(cell_index):
            continue
        cell_dim = cubical_complex.cell_dim(cell_index)
        coords_lower = cubical_complex.coordinates(cell_index)
        idx_lower = coords2idx[tuple(coords_lower)]
        shape = cubical_complex.cell_shape(cell_index)
        coords_upper = [coords_lower[d] + (1 if shape & (1 << d) != 0 else 0) for d in range(dimension)]
        idx_upper = coords2idx[tuple(coords_upper)]
        ind_extent = [d for d in range(dimension) if shape & (1 << d) != 0]
        if cell_dim == 0:
            cell_verts = [idx_lower]
        elif cell_dim == 1:
            cell_verts = [idx_lower, idx_upper]
        elif cell_dim == 2:
            idx1 = idx_lower
            coords_v2 = [coord for coord in coords_lower]
            coords_v2[ind_extent[0]] += 1
            idx2 = coords2idx[tuple(coords_v2)]
            idx3 = idx_upper
            coords_v4 = [coord for coord in coords_lower]
            coords_v4[ind_extent[1]] += 1
            idx4 = coords2idx[tuple(coords_v4)]
            cell_verts = [idx1, idx2, idx3, idx4]
        elif cell_dim == 3: 
            u_verts = list(itertools.product((0, 1), repeat=cell_dim))
            cell_verts = []
            for u in u_verts:
                coords_vert = [sum(x) for x in zip(u, coords_lower)]
                idx_vert = coords2idx[tuple(coords_vert)]
                cell_verts.append(idx_vert)
        else: 
            continue
        cell = {"cell_dim" : cell_dim, "cell_index" : cell_index, "cell_verts" : cell_verts}
        cells.append(cell)
    complex_json_data = {"complex" : {"dimension" : dimension,
                                      "verts_coords" : verts_coords,
                                      "cells" : cells} }
    return complex_json_data
def cubical_complex_json ( network ) : dimension = network . size ( ) cubical_complex = pychomp . CubicalComplex ( [ x + 1 for x in network . domains ( ) ] ) verts_coords = [ ] coords2idx = { } for cell_index in cubical_complex ( 0 ) : coords = cubical_complex . coordinates ( cell_index ) coords2idx [ tuple ( coords ) ] = cell_index verts_coords . append ( coords ) cells = [ ] for cell_index in cubical_complex : if cubical_complex . rightfringe ( cell_index ) : continue cell_dim = cubical_complex . cell_dim ( cell_index ) coords_lower = cubical_complex . coordinates ( cell_index ) idx_lower = coords2idx [ tuple ( coords_lower ) ] shape = cubical_complex . cell_shape ( cell_index ) coords_upper = [ coords_lower [ d ] + ( 1 if shape & ( 1 << d ) != 0 else 0 ) for d in range ( dimension ) ] idx_upper = coords2idx [ tuple ( coords_upper ) ] ind_extent = [ d for d in range ( dimension ) if shape & ( 1 << d ) != 0 ] if cell_dim == 0 : cell_verts = [ idx_lower ] elif cell_dim == 1 : cell_verts = [ idx_lower , idx_upper ] elif cell_dim == 2 : idx1 = idx_lower coords_v2 = [ coord for coord in coords_lower ] coords_v2 [ ind_extent [ 0 ] ] += 1 idx2 = coords2idx [ tuple ( coords_v2 ) ] idx3 = idx_upper coords_v4 = [ coord for coord in coords_lower ] coords_v4 [ ind_extent [ 1 ] ] += 1 idx4 = coords2idx [ tuple ( coords_v4 ) ] cell_verts = [ idx1 , idx2 , idx3 , idx4 ] elif cell_dim == 3 : u_verts = list ( itertools . product ( ( 0 , 1 ) , repeat = cell_dim ) ) cell_verts = [ ] for u in u_verts : coords_vert = [ sum ( x ) for x in zip ( u , coords_lower ) ] idx_vert = coords2idx [ tuple ( coords_vert ) ] cell_verts . append ( idx_vert ) else : continue cell = { "cell_dim" : cell_dim , "cell_index" : cell_index , "cell_verts" : cell_verts } cells . append ( cell ) complex_json_data = { "complex" : { "dimension" : dimension , "verts_coords" : verts_coords , "cells" : cells } } return complex_json_data
Return json data for cubical complex.
Return json data for cubical complex .
"""Return json data for cubical complex.""" # Get complex dimension # Construct a cubical complex using pychomp. A cubical complex in pychomp # does not contain the rightmost boundary, so make one extra layer of # cubes and ignore the last layer (called rightfringe in pychomp). # Get vertices coordinates and set a # mapping from coords to its index in # the list of coordinates. # Get the coords of all cells of dimension 0. # The 0-dim cells in a cubical complex in pychomp # are indexed from 0 to n-1, where n is the number # of 0-dim cells. Hence the cell_index coincides # with the index of coords in the list verts_coords. # Get the cell complex data # Ignore fringe cells # Get this cell dimension # Get coords of the lower corner of the box # Get index of vertex corresponding to these coords # Due to the way pychomp index the 0-dim cells we get # that idx_lower == cell_index (see coords2idx above). # Get the shape of this cell (see pychomp) # Add 1 to the appropriate entries to get coords of the upper corner # Get index of vertex corresponding to these coords # Get indices of coords that have extent # Index of vertex 1 # Coords and index of vertex 2 # Index of vertex 3 # Coords and index of vertex 4 # cell_dim == dimension == 3 # First get vertices of unit cube as cartesian product of {0, 1} # Add verts of unit cube to coords_lower to get verts of this cell # Ignore cell if dim > 3
network