src package¶
Subpackages¶
Submodules¶
src.constants module¶
This file is used to save all possible project wide constants.
Includes source folder, the project path, etc.
Example
Import statement at top of script:
from src.constants import PROJECT_PATH, FIGURE_PATH, GWS_DIR
src.plot_settings module¶
Sets a consistent plotting settings across the project.
Example
Usage with simple plots:
from src.plot_settings import (
ps_defaults,
label_subplots,
get_dim,
set_dim,
PALETTE,
STD_CLR_LIST,
CAM_BLUE,
BRICK_RED,
OX_BLUE,
)
ps_defaults(use_tex=True)
# ---- example set of graphs ---
import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
x = np.linspace(0, np.pi, num=100)
axs[0, 0].plot(x, np.sin(x), color=STD_CLR_LIST[0])
axs[0, 1].plot(x, np.cos(x), color=STD_CLR_LIST[1])
axs[1, 0].plot(x, np.sinc(x), color=STD_CLR_LIST[2])
axs[1, 1].plot(x, np.abs(x), color=STD_CLR_LIST[3])
# set size
set_dim(fig, fraction_of_line_width=1, ratio=(5 ** 0.5 - 1) / 2)
# label subplots
label_subplots(axs, start_from=0, fontsize=10)
- src.plot_settings.get_dim(width=398.3386, fraction_of_line_width=1, ratio=0.6180339887498949)¶
- Return figure height, width in inches to avoid scaling in latex.
Default width is src.constants.REPORT_WIDTH. Default ratio is golden ratio, with figure occupying full page width.
- Parameters
width (float, optional) – Textwidth of the report to make fontsizes match. Defaults to src.constants.REPORT_WIDTH.
fraction_of_line_width (float, optional) – Fraction of the document width which you wish the figure to occupy. Defaults to 1.
ratio (float, optional) – Fraction of figure width that the figure height should be. Defaults to (5 ** 0.5 - 1)/2.
- Returns
Dimensions of figure in inches
- Return type
fig_dim (tuple)
Examples
Here is an example of using this function:
>>> dim_tuple = get_dim(fraction_of_line_width=1, ratio=(5 ** 0.5 - 1) / 2)
- src.plot_settings.label_subplots(axs, labels=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], start_from=0, fontsize=10, x_pos=0.02, y_pos=0.95)¶
Adds (a), (b), (c) at the top left of each subplot panel. Labelling order achieved through ravelling the input list / np.array.
- Parameters
axs (Sequence[matplotlib.axes]) – list or np.array of matplotlib.pyplot.axes.
labels (Sequence[str]) – A sequence of labels for the subplots.
start_from (int, optional) – skips first start_from labels. Defaults to 0.
fontsize (int, optional) – Font size for labels. Defaults to 10.
x_pos (float, optional) – Relative x position of labels. Defaults to 0.02.
y_pos (float, optional) – Relative y position of labels. Defaults to 0.95.
- Return type
None- Returns
void; alters the matplotlib.pyplot.axes objects
Examples
Here is an example of using this function:
>>> label_subplots(axs, start_from=0, fontsize=10)
- src.plot_settings.ps_defaults(use_tex=True, dpi=600)¶
Apply plotting style to produce nice looking figures. Call this at the start of a script which uses matplotlib. Can enable matplotlib LaTeX backend if it is available.
- Parameters
use_tex (bool, optional) – Whether or not to use latex matplotlib backend. Defaults to True.
dpi (int, optional) – Which dpi to set for the figures. Defaults to 600 dpi (high quality). 150 dpi probably fine for notebooks. Largest dpi needed for presentations.
- Return type
None
- src.plot_settings.set_dim(fig, width=398.3386, fraction_of_line_width=1, ratio=0.6180339887498949)¶
- Set aesthetic figure dimensions to avoid scaling in latex.
Default width is src.constants.REPORT_WIDTH. Default ratio is golden ratio, with figure occupying full page width.
- Parameters
fig (matplotlib.pyplot.figure) – Figure object to resize.
width (float) – Textwidth of the report to make fontsizes match. Defaults to src.constants.REPORT_WIDTH.
fraction_of_line_width (float, optional) – Fraction of the document width which you wish the figure to occupy. Defaults to 1.
ratio (float, optional) – Fraction of figure width that the figure height should be. Defaults to (5 ** 0.5 - 1)/2.
- Return type
None- Returns
void; alters current figure to have the desired dimensions
Examples
Here is an example of using this function:
>>> set_dim(fig, fraction_of_line_width=1, ratio=(5 ** 0.5 - 1) / 2)
src.utils module¶
General project util functions
- src.utils.calculate_byte_size_recursively(obj, seen=None)¶
Recursively calculate size of objects in memory in bytes. From: https://github.com/bosswissam/pysize. Meant as a helper function for get_byte_size. :type obj:
object:param obj: The python object to get the size of :type obj: object :type seen:Optional[set] :param seen: This variable is needed to for the recusrivefunction evaluations, to ensure each object only gets counted once. Leave it at “None” to get the full byte size of an object. Defaults to None.
- Returns
The size of the object in bytes
- Return type
int
- src.utils.get_byte_size(obj)¶
Return human readable size of a python object in bytes. :type obj:
object:param obj: The python object to analyse :type obj: object- Returns
Human readable string with the size of the object
- Return type
str
- src.utils.human_readable_size(num, suffix='B')¶
Convert a number of bytes into human readable format. This function is meant as a helper function for get_byte_size. :type num:
int:param num: The number of bytes to convert :type num: int :type suffix:str:param suffix: The suffix to use for bytes. Defaults to ‘B’. :type suffix: str, optional- Returns
A human readable version of the number of bytes.
- Return type
str
- src.utils.timeit(method)¶
timeit is a wrapper for performance analysis which should return the time taken for a function to run. Alters log_time dict if fed in. Add @timeit to the function you want to time. Function needs **kwargs if you want it to be able to feed in log_time dict.
- Parameters
method (Callable) – the function that it takes as an input
Examples
Here is an example with the tracking functionality and without:
>>> @timeit ... def loop(**kwargs): ... total = 0 ... for i in range(int(10e2)): ... for j in range(int(10e2)): ... total += 1 >>> tmp_log_d = {} >>> loop(log_time=tmp_log_d) >>> print(tmp_log_d["loop"]) >>> loop()
- Return type
Callable