Quadtree
Classes for all astroQTpy quadtree types.
- class astroqtpy.quadtree.Chi2QuadTree(x_min: float, x_max: float, y_min: float, y_max: float, data: ndarray, model_func: callable, weights: ndarray = None, max_chi2: float = 10, split_threshold: float = 0.2, node_statistic: str = 'mean', N_points: int = 20, min_depth: int = 3, max_depth: int = 6, N_proc: int = 1, verbose: bool = False, filename_points: str = 'points.txt', filename_nodes: str = 'nodes.txt', overwrite: bool = False)
Chi2 quadtree.
A quadtree class for mapping 2-parameter reduced \(\chi^2\) values.
\[\chi^2_r = \frac{\chi^2}{\nu}\]where \(\nu\) is the number of degrees of freedom and
\[\chi^2 = \sum_{i=0}^{N-1} \frac{\Big(y_i - f(x_i | a, b)\Big)^2}{\sigma_i^2}\]Note
The model_func argument must be callable and take two arguments: x_data and parameters (a, b). It must return a single float value y. For example:
def my_model(x, parameters): a, b = parameters # unpack parameters # ... do something ... y = a * x + b return y
See tutorials for more examples.
- Parameters:
x_min (float) – Minimum x value (param a) for this quadtree.
x_max (float) – Maximum x value (param a)for this quadtree.
y_min (float) – Minimum y value (param b)for this quadtree.
y_max (float) – Maximum y value (param b)for this quadtree.
data (
np.ndarray) – Data array. Must have shape (2, N).model_func (callable) – Function \(f(x | a, b)\) to calculate a model to compare to data.
weights (
np.ndarray, optional) – Data weights, typically expressed as \(1/\sigma^2\). Defaults to None.max_chi2 (float, optional) – Largest permitted reduced \(\chi^2\). Defaults to 10.
split_threshold (float, optional) – Threshold discrepancy in order to split nodes. Defaults to 0.2.
node_statistic (str, optional) – Statistic to compute node values [‘count’, ‘mean’, ‘std’, or ‘median’]. Defaults to ‘mean’.
N_points (int, optional) – Maximum number of points per node. Defaults to 20.
min_depth (int, optional) – Minimum quadtree depth. Defaults to 3.
max_depth (int, optional) – Maximum quadtree depth. Defaults to 6.
N_proc (int, optional) – Number of cores for multiprocessing. Defaults to 1.
verbose (bool, optional) – Option to print node values in real time. Defaults to False.
filename_points (str, optional) – Name of output file to save points. Defaults to ‘points.txt’.
filename_nodes (str, optional) – Name of output file to save nodes. Defaults to ‘nodes.txt’.
overwrite (bool, optional) – Option to automatically overwrite previously saved results. Defaults to False.
- draw_tree(ax: Axes, cmap: str = 'RdYlGn_r', vmin: float = None, vmax: float = None, show_colors: bool = True, show_lines: bool = True, show_points: bool = False, show_values: bool = False) ScalarMappable
Draw quadtree.
Plot the entire quadtree on a given axis.
- Parameters:
ax (
matplotlib.axes.Axes) – Matplotlib axis for plotting.cmap (str, optional) – Matplotlib colormap. Defaults to ‘RdYlGn_r’.
vmin (float, optional) – Minimum value for colorbar. Defaults to None.
vmax (float, optional) – Maximum value for colorbar. Defaults to None.
show_colors (bool, optional) – Option to show node colors. Defaults to True.
show_lines (bool, optional) – Option to plot node boundary lines. Defaults to True.
show_points (bool, optional) – Option to plot node points. Defaults to False.
show_values (bool, optional) – Option to print node values on plot. Defaults to False.
- Returns:
Matplotlib ScalarMappable.
- Return type:
matplotlib.cm.ScalarMappable
- evaluate_multiple_points(node: QuadNode, N_points: int) None
Evaulate multiple points.
Evaluate multiple points contained within a given node in parallel.
- Parameters:
node (QuadNode) – Node in which to evaulate points.
N_points (int) – Maximum number of points to evaulate within this node.
- evaluate_point(node: QuadNode, rng_seed: int = 123456) QuadPoint
Evaluate point.
Calculate the value of one point within a given node.
- fill(node: QuadNode, N_points: int) None
Fill.
Fill a given node with points.
- Parameters:
node (QuadNode) – The quadtree node to be filled.
N_points (int) – Number of points to put inside this node.
- get_chi2_min() QuadPoint
Get chi^2 min.
Get the point with the smallest reduced chi^2 value.
- Returns:
Quadtree point with least chi^2.
- Return type:
- load_points() None
Load points.
Load all points from a previously saved quadtree.
- print_all_nodes() None
Print all nodes.
Print all current quadtree node values to a file (‘filename_nodes’).
- print_all_points() None
Print all points.
Print all current quadtree points to a file (‘filename_points’).
- run_quadtree() None
Run quadtree.
Run the quadtree from a previously saved run, or start a new run.
- class astroqtpy.quadtree.Hist2dQuadTree(x_min: float, x_max: float, y_min: float, y_max: float, node_statistic: str = 'count', N_points: int = 20, min_depth: int = 3, max_depth: int = 6)
2D histogram quadtree.
A class for creating a 2D histogram quadtree given some data.
- Parameters:
x_min (float) – Minimum x value for this quadtree.
x_max (float) – Maximum x value for this quadtree.
y_min (float) – Minimum y value for this quadtree.
y_max (float) – Maximum y value for this quadtree.
node_statistic (str, optional) – Statistic to compute node values [‘count’, ‘mean’, ‘std’, or ‘median’]. Defaults to ‘count’.
N_points (int, optional) – Maximum number of points per node. Defaults to 20.
min_depth (int, optional) – Minimum quadtree depth. Defaults to 3.
max_depth (int, optional) – Maximum quadtree depth. Defaults to 6.
- add_data(x: ndarray, y: ndarray, z: ndarray = None)
Add data.
Add x, y, and z (optional) data to this quadtree object to create a histogram.
- Parameters:
x (
np.ndarray) – x data array.y (
np.ndarray) – y data array. Must have same length as x.z (
np.ndarray, optional) – z data array. Must have same length as x. Defaults to None.
- draw_tree(ax: Axes, cmap: str = 'RdYlGn_r', vmin: float = None, vmax: float = None, show_colors: bool = True, show_lines: bool = True, show_points: bool = False, show_values: bool = False) ScalarMappable
Draw quadtree.
Plot the entire quadtree on a given axis.
- Parameters:
ax (
matplotlib.axes.Axes) – Matplotlib axis for plotting.cmap (str, optional) – Matplotlib colormap. Defaults to ‘RdYlGn_r’.
vmin (float, optional) – Minimum value for colorbar. Defaults to None.
vmax (float, optional) – Maximum value for colorbar. Defaults to None.
show_colors (bool, optional) – Option to show node colors. Defaults to True.
show_lines (bool, optional) – Option to plot node boundary lines. Defaults to True.
show_points (bool, optional) – Option to plot node points. Defaults to False.
show_values (bool, optional) – Option to print node values on plot. Defaults to False.
- Returns:
Matplotlib ScalarMappable.
- Return type:
matplotlib.cm.ScalarMappable
- evaluate_multiple_points(node: QuadNode, N_points: int) None
Evaulate multiple points.
Evaluate multiple points contained within a given node in parallel.
- Parameters:
node (QuadNode) – Node in which to evaulate points.
N_points (int) – Maximum number of points to evaulate within this node.
- evaluate_point(node: QuadNode, rng_seed: int = 123456) QuadPoint
Evaluate point.
Method not needed here.
- fill(node: QuadNode, N_points: int) None
Fill.
Fill a given node with points.
- Parameters:
node (QuadNode) – The quadtree node to be filled.
N_points (int) – Number of points to put inside this node.
- load_points() None
Load points.
Load all points from a previously saved quadtree.
- print_all_nodes() None
Print all nodes.
Print all current quadtree node values to a file (‘filename_nodes’).
- print_all_points() None
Print all points.
Print all current quadtree points to a file (‘filename_points’).
- run_quadtree() None
Run quadtree.
Run the quadtree from a previously saved run, or start a new run.
- class astroqtpy.quadtree.NbodyQuadTree(x_min: float, x_max: float, y_min: float, y_max: float, simulation_func: callable, split_threshold: float = 0.2, node_statistic: str = 'mean', N_points: int = 20, min_depth: int = 3, max_depth: int = 6, N_proc: int = 1, verbose: bool = False, filename_points: str = 'points.txt', filename_nodes: str = 'nodes.txt', overwrite: bool = False)
Nbody quadtree.
A class for creating a quadtree for running N-body simulations.
Note
The simulation_func argument must be callable and take a single argument (x, y). It must return a single float value. For example:
def my_function(parameters): x, y = parameters # unpack parameters # ... do something ... number = float(1) return number
See tutorials for more examples.
- Parameters:
x_min (float) – Minimum x value for this quadtree.
x_max (float) – Maximum x value for this quadtree.
y_min (float) – Minimum y value for this quadtree.
y_max (float) – Maximum y value for this quadtree.
simulation_func (callable) – Function to calculate the outcome of an Nbody simulation.
split_threshold (float, optional) – Threshold discrepancy in order to split nodes. Defaults to 0.2.
node_statistic (str, optional) – Statistic to compute node values [‘count’, ‘mean’, ‘std’, or ‘median’]. Defaults to ‘mean’.
N_points (int, optional) – Maximum number of points per node. Defaults to 20.
min_depth (int, optional) – Minimum quadtree depth. Defaults to 3.
max_depth (int, optional) – Maximum quadtree depth. Defaults to 6.
N_proc (int, optional) – Number of cores for multiprocessing. Defaults to 1.
verbose (bool, optional) – Option to print node values in real time. Defaults to False.
filename_points (str, optional) – Name of output file to save points. Defaults to ‘points.txt’.
filename_nodes (str, optional) – Name of output file to save nodes. Defaults to ‘nodes.txt’.
overwrite (bool, optional) – Option to automatically overwrite previously saved results. Defaults to False.
- draw_tree(ax: Axes, cmap: str = 'RdYlGn_r', vmin: float = None, vmax: float = None, show_colors: bool = True, show_lines: bool = True, show_points: bool = False, show_values: bool = False) ScalarMappable
Draw quadtree.
Plot the entire quadtree on a given axis.
- Parameters:
ax (
matplotlib.axes.Axes) – Matplotlib axis for plotting.cmap (str, optional) – Matplotlib colormap. Defaults to ‘RdYlGn_r’.
vmin (float, optional) – Minimum value for colorbar. Defaults to None.
vmax (float, optional) – Maximum value for colorbar. Defaults to None.
show_colors (bool, optional) – Option to show node colors. Defaults to True.
show_lines (bool, optional) – Option to plot node boundary lines. Defaults to True.
show_points (bool, optional) – Option to plot node points. Defaults to False.
show_values (bool, optional) – Option to print node values on plot. Defaults to False.
- Returns:
Matplotlib ScalarMappable.
- Return type:
matplotlib.cm.ScalarMappable
- evaluate_multiple_points(node: QuadNode, N_points: int) None
Evaulate multiple points.
Evaluate multiple points contained within a given node in parallel.
- Parameters:
node (QuadNode) – Node in which to evaulate points.
N_points (int) – Maximum number of points to evaulate within this node.
- evaluate_point(node: QuadNode, rng_seed: int = 123456) QuadPoint
Evaluate point.
Calculate the value of one point within a given node from an N-body simulation.
- fill(node: QuadNode, N_points: int) None
Fill.
Fill a given node with points.
- Parameters:
node (QuadNode) – The quadtree node to be filled.
N_points (int) – Number of points to put inside this node.
- load_points() None
Load points.
Load all points from a previously saved quadtree.
- print_all_nodes() None
Print all nodes.
Print all current quadtree node values to a file (‘filename_nodes’).
- print_all_points() None
Print all points.
Print all current quadtree points to a file (‘filename_points’).
- run_quadtree() None
Run quadtree.
Run the quadtree from a previously saved run, or start a new run.
- class astroqtpy.quadtree.RandomQuadTree(x_min: float, x_max: float, y_min: float, y_max: float, split_threshold: float = 0.2, node_statistic: str = 'mean', N_points: int = 20, min_depth: int = 3, max_depth: int = 6, N_proc: int = 1, verbose: bool = False, filename_points: str = 'points.txt', filename_nodes: str = 'nodes.txt', overwrite: bool = False)
Random quadtree.
A class for creating a quadtree with randomly sampled points.
- Parameters:
x_min (float) – Minimum x value for this quadtree.
x_max (float) – Maximum x value for this quadtree.
y_min (float) – Minimum y value for this quadtree.
y_max (float) – Maximum y value for this quadtree.
split_threshold (float, optional) – Threshold discrepancy in order to split nodes. Defaults to 0.2.
node_statistic (str, optional) – Statistic to compute node values [‘count’, ‘mean’, ‘std’, or ‘median’]. Defaults to ‘mean’.
N_points (int, optional) – Maximum number of points per node. Defaults to 20.
min_depth (int, optional) – Minimum quadtree depth. Defaults to 3.
max_depth (int, optional) – Maximum quadtree depth. Defaults to 6.
N_proc (int, optional) – Number of cores for multiprocessing. Defaults to 1.
verbose (bool, optional) – Option to print node values in real time. Defaults to False.
filename_points (str, optional) – Name of output file to save points. Defaults to ‘points.txt’.
filename_nodes (str, optional) – Name of output file to save nodes. Defaults to ‘nodes.txt’.
overwrite (bool, optional) – Option to automatically overwrite previously saved results. Defaults to False.
- draw_tree(ax: Axes, cmap: str = 'RdYlGn_r', vmin: float = None, vmax: float = None, show_colors: bool = True, show_lines: bool = True, show_points: bool = False, show_values: bool = False) ScalarMappable
Draw quadtree.
Plot the entire quadtree on a given axis.
- Parameters:
ax (
matplotlib.axes.Axes) – Matplotlib axis for plotting.cmap (str, optional) – Matplotlib colormap. Defaults to ‘RdYlGn_r’.
vmin (float, optional) – Minimum value for colorbar. Defaults to None.
vmax (float, optional) – Maximum value for colorbar. Defaults to None.
show_colors (bool, optional) – Option to show node colors. Defaults to True.
show_lines (bool, optional) – Option to plot node boundary lines. Defaults to True.
show_points (bool, optional) – Option to plot node points. Defaults to False.
show_values (bool, optional) – Option to print node values on plot. Defaults to False.
- Returns:
Matplotlib ScalarMappable.
- Return type:
matplotlib.cm.ScalarMappable
- evaluate_multiple_points(node: QuadNode, N_points: int) None
Evaulate multiple points.
Evaluate multiple points contained within a given node in parallel.
- Parameters:
node (QuadNode) – Node in which to evaulate points.
N_points (int) – Maximum number of points to evaulate within this node.
- evaluate_point(node: QuadNode, rng_seed: int = 123456) QuadPoint
Evaluate point.
Calculate the value of one point within a given node as either 1 or 0.
- fill(node: QuadNode, N_points: int) None
Fill.
Fill a given node with points.
- Parameters:
node (QuadNode) – The quadtree node to be filled.
N_points (int) – Number of points to put inside this node.
- load_points() None
Load points.
Load all points from a previously saved quadtree.
- print_all_nodes() None
Print all nodes.
Print all current quadtree node values to a file (‘filename_nodes’).
- print_all_points() None
Print all points.
Print all current quadtree points to a file (‘filename_points’).
- run_quadtree() None
Run quadtree.
Run the quadtree from a previously saved run, or start a new run.