cityseer.tools.mock

A collection of functions for the generation of mock data. This module is predominately used for writing code tests, but can otherwise be useful for demonstration and utility purposes.

mock_graph

mock_graph(wgs84_coords=False)
           -> nx.MultiGraph

Generates a NetworkXMultiGraph for testing or experimentation purposes.

Parameters
wgs84_coords
bool

If set to True, the x and y attributes will be in WGS84 geographic coordinates instead of a projected cartesion coordinate system. By default False

Returns
nx.MultiGraph

A NetworkXMultiGraph with x and y node attributes.

Notes
from cityseer.tools import mock, plot
networkX_multigraph = mock.mock_graph()
plot.plot_nX(networkX_multigraph)

Example graphMock graph.

primal_graph

primal_graph()
             -> nx.MultiGraph
Returns
nx.MultiGraph

A primal NetworkXMultiGraph for pytest tests.

dual_graph

dual_graph()
           -> nx.MultiGraph
Returns
nx.MultiGraph

A dual NetworkXMultiGraph for pytest tests.

diamond_graph

diamond_graph()
              -> nx.MultiGraph

Generates a diamond shaped NetworkXMultiGraph for testing or experimentation purposes. For manual checks of all node and segmentised methods.

Returns
nx.MultiGraph

A NetworkXMultiGraph with x and y node attributes.

Notes
#     3
#    / \
#   /   \
#  /  a  \
# 1-------2
#  \  |  /
#   \ |b/ c
#    \|/
#     0
# a = 100m = 2 * 50m
# b = 86.60254m
# c = 100m
# all inner angles = 60º

get_graph_extents

get_graph_extents(networkX_multigraph)
                  -> Tuple[float, float, float, float]
Parameters
networkX_multigraph
nx.MultiGraph

A NetworkXMultiGraph with x and y node parameters.

Returns
Tuple

A tuple of min_x, min_y, max_x, max_y values.

mock_data_dict

mock_data_dict(networkX_multigraph,
               length=50,
               random_seed=None)
               -> dict

Generates a dictionary containing mock data for testing or experimentation purposes.

Parameters
networkX_multigraph
nx.MultiGraph

A NetworkX graph with x and y attributes. This is used in order to determine the spatial extents of the network. The returned data will be within these extents.

length
int

The number of data elements to return in the dictionary, by default 50.

random_seed
int

An optional random seed, by default None.

Returns
dict

A dictionary where each entry consists of a key representing a distinct data point uid, and corresponding x, y and live values.

mock_categorical_data

mock_categorical_data(length,
                      num_classes=10,
                      random_seed=None)
                      -> np.ndarray

Generates a numpy array containing mock categorical data for testing or experimentation purposes.

Parameters
length
int

The number of categorical elements to return in the array.

num_classes
int

The maximum number of unique classes to return in the randomly assigned categorical data. The classes are randomly generated from a pool of unique class labels of length num_classes. The number of returned unique classes will be less than or equal to num_classes. By default 10

random_seed
int

An optional random seed, by default None

Returns
np.ndarray

A numpy array containing categorical data elements. The number of elements will match the length parameter. The categorical data will consist of randomly selected letters.

mock_numerical_data

mock_numerical_data(length,
                    min=0,
                    max=100000,
                    num_arrs=1,
                    random_seed=None)
                    -> np.ndarray

Generates a 2d numpy array containing mock numerical data for testing or experimentation purposes.

Parameters
length
int

The number of numerical elements to return in the array.

min
int

The (inclusive) minimum value in the min, max range of randomly generated integers.

max
int

The (exclusive) maximum value in the min, max range of randomly generated integers.

num_arrs
int

The number of arrays to nest in the returned 2d array.

random_seed
int

An optional random seed, by default None

Returns
np.ndarray

A 2d numpy array containing numerical data elements. The first dimension corresponds to the number of data arrays, and is set with the num_arrs parameter. The length of the second dimension will represents the number of data elements and will match the length parameter. The numerical data will consist of randomly selected integers.

mock_species_data

mock_species_data(random_seed=None)
                  -> Tuple[np.ndarray, np.ndarray]

A generator function returning a series of randomly generated counts and corresponding probabilities for testing diversity measures. The data is generated in varying lengths from randomly assigned integers between 1 and 10. Matching integers are then collapsed into species “classes” with probabilities computed accordingly.

Parameters
random_seed
int

An optional random seed, by default None

Yields
counts

The number of members for each species class.

probs

The probability of encountering the respective species classes.

Notes
from cityseer.tools import mock

for counts, probs in mock.mock_species_data():
    cs = [c for c in counts]
    print(f'c = {cs}')
    ps = [round(p, 3) for p in probs]
    print(f'p = {ps}')

# c = [1]
# p = [1.0]

# c = [1, 1, 2, 2]
# p = [0.167, 0.167, 0.333, 0.333]

# c = [3, 2, 1, 1, 1, 3]
# p = [0.273, 0.182, 0.091, 0.091, 0.091, 0.273]

# c = [3, 3, 2, 2, 1, 1, 1, 2, 1]
# p = [0.188, 0.188, 0.125, 0.125, 0.062, 0.062, 0.062, 0.125, 0.062]

# etc.

fetch_osm_response

fetch_osm_response(geom_osm,
                   timeout=30,
                   max_tries=3)
                   -> requests.Response

Fetches an OSM response.

Parameters
geom_osm
str

A string representing a polygon for the request extents, formatting according to OSM conventions.

timeout
int

An optional timeout, by default 30s

max_tries
int

The number of attempts to fetch a response before raising, by default 3

Returns
requests.Response

An OSM API response.

make_buffered_osm_graph

make_buffered_osm_graph(lng,
                        lat,
                        buffer)
                        -> nx.MultiGraph

Prepares a networkXMultiGraph from an OSM request for a buffered region around a given lng and lat parameter.

Parameters
lng
float

The longitude argument for the request.

lat
float

The latitude argument for the request.

buffer
float

The buffer distance.

Returns
nx.MultiGraph

A networkXMultiGraph with x and y node attributes that have been converted to UTM.