API reference¶
This section describes the API of the backend - i.e. the code_generator and code_parser module.
code_generator¶
The module consists of a class DataHandler which includes the code generator methods and some data container classes (Formula and PlotData).
-
simple_plotter.core.code_generator.
check_valid_input
(input_value)¶ Checks if an input value corresponds to a valid value - i.e. not None, “”, “None”,…
Parameters: input_value – Any type of input value Returns: True if valid Return type: bool
Formula¶
-
class
simple_plotter.core.code_generator.
Formula
(function_name='y', var_name='x', equation='x**2', constants=None, set_var_name=None, set_min_val=None, set_max_val=None, no_sets=None, var_unit=None, function_unit=None, set_var_unit=None, explicit_set_values=None)¶ Data container for definition of the equation
Parameters: - function_name (str) – Name of the “return value” - e.g. y = …
- var_name (str) – Name of the function variable - e.g. x
- equation (str) – String representation of an equation (python, numpy code)
- constants (list) – List with a dictionary for each constant - see notes
- set_var_name (str) – Name of the set parameter
- set_min_val (float) – Min. value of the set parameter
- set_max_val (float) – Max. value of the set parameter
- no_sets (int) – Number of set parameter values to create between min. and max. value
- var_unit (str) – Unit for the variable (only used for display)
- function_unit (str) – Unit of the function return value (only used for display)
- set_var_unit (str) – Unit of set parameter (only used for display)
- explicit_set_values (str) – String of explicit values for set parameters (like a list separated with comma)
Notes
- the constants dictionary consists of following keys (all values are strings): “Const. name”, “Value”, “Unit” and “Comment”
PlotData¶
-
class
simple_plotter.core.code_generator.
PlotData
(start_val=-10.0, end_val=10.0, no_pts=50, x_log=False, y_log=False, swap_xy=False, grid=False, user_data=None, y_min=None, y_max=None, plot_title=None)¶ Data container for plot definition
Parameters: - start_val (float) – start of x value range
- end_val (float) – end of x value range
- no_pts (int) – Number of data points for x value
- x_log (bool) – Sets x scale to logarithmic if True
- y_log (bool) – Sets y scale to logarithmic if True
- swap_xy (bool) – Swaps x and y axis if True
- grid (bool) – Enables grid if True
- user_data (list) – Not implemented yet
- y_min (float) – Min y value for plot display
- y_max (float) – Max y value for plot display
- plot_title (str) – Manually defined plot title (if None, plot title will be created from formula automatically)
Notes
- All float values will be converted to str and may also be passed as str directly.
DataHandler¶
-
class
simple_plotter.core.code_generator.
DataHandler
(formula, plot_data, export_csv=False, filename=None, code_checker=None, plot_lib='matplotlib')¶ Main class for parser - includes all code generator methods
Parameters: - formula (Formula) – Formula container object
- plot_data (PlotData) – plot data container object
- export_csv (bool) – If true code to export curve values to csv will be embedded
- filename (str) – Name of the project file (JSON)
- code_checker (CodeChecker) – Code checker object
- plot_lib (str) – Library used for plotting - either ‘matplotlib’ or ‘kivy-garden/graph’
-
check_code
()¶ Checks the code for each segment
Returns: Consisting of: - list: List of error codes
- list: List of error logs
Return type: tuple Notes
- following error codes exist:
- 0: Error in imports
- 1: Error in function definition
- 2: Error in constants definition
- 3: Error variable definition
- 4: Error in set constants definition
- 5: Error in plot call
- 6: Error in plot setup
-
check_const_validity
()¶ Checks the validity of defined constants
Returns: True if all constants are valid, false otherwise Return type: bool Note
Constants need to have at least a name and a value
-
static
check_valid_input
(input_value)¶ Checks if an input value corresponds to a valid value - i.e. not None, “”, “None”,…
Returns: True if valid input value Return type: bool
-
combine_code
()¶ Assembles complete code
Returns: combined python code Return type: str
-
load_project
(filename)¶ loads a project from JSON file
-
plot
()¶ plots the function, if code is valid
Returns: Consisting of: - list: Error codes
- list: Error logs
Return type: tuple
-
save_project
(filename=None)¶ saves project object to JSON file
-
write_constdefs
()¶ Write constants definition
-
write_csv_export
()¶ writes the code to export to filename.csv
Returns: python code for export to csv file Return type: str
-
write_funcdef
()¶ Writes the function definition
-
write_imports
()¶ Writes the imports
-
write_plot_call
()¶ Writes the plot call code
-
write_plot_setup
()¶ writes the plot setup code
Returns: python code for plotting section Return type: str
-
write_py_file
(filename)¶ writes a python file with code generated by combine_code
-
write_setvardef
()¶ writes definition for set variable
Returns: python code for definitions Return type: str
-
write_vardef
()¶ Write variable definition
code_parser¶
This module consist of a data container class Code and the CodeChecker class, which uses ast to analyze code elements and identify unwanted code.
Code¶
CodeChecker¶
-
class
simple_plotter.core.code_parser.
CodeChecker
(allowed_imports=None, allowed_calls=None, allowed_names=None, allowed_aliases=None, allowed_FunctionDefs=None)¶ Checker for illegal statements in code
Parameters: - allowed_imports (list) – List of allowed imports
- allowed_calls (list) – List of allowed calls
- allowed_names (list) – List of allowed names (e.g. function names not covered in methods of allowed aliases)
- allowed_aliases (list) – List of allowed module/class/function aliases
- allowed_FunctionDefs (list) – List of allowed names for Function definition
-
allowed_imports
¶ List of allowed imports
Type: list
-
allowed_calls
¶ List of allowed calls
Type: list
-
allowed_names
¶ List of allowed names (e.g. function names not covered in methods of allowed aliases)
Type: list
-
allowed_aliases
¶ List of allowed module/class/function aliases
Type: list
-
allowed_node_types
¶ List of node ast node types (Classes) - defaults to Import, FunctionDef, Assign,
Type: list
-
Call and Expr
-
allowed_FunctionDefs
¶ List of allowed names for Function definition
Type: list
-
check_code
(code)¶ Checks if the code is valid
Parameters: code (Code) – Code object to analyze Returns: consisting of: - bool: True if code is valid
- list: Error log
Return type: tuple
-
generic_visit
(node)¶ Called if no explicit visitor function exists for a node.
advanced_graph¶
This module includes a class AnnotatedLinePlot which is a specialized extension of the kivy-garden.graph package. The AnnotatedLinePlot adds some matplotlib-like behavior (e.g. automatic axis-scaling, a legend, plot title) to the garden.graph LinePlot class.
AnnotatedLinePlot¶
-
class
simple_plotter.core.advanced_graph.
AnnotatedLinePlot
(title=None, show_legend=True, **kwargs)¶ Extends the kivy-graden/graph by some additional features, like a title, legend, automatic colors
Parameters: - title (str) – Title to place above the plot
- show_legend (bool) – If true legend will be shown below the graph
- **kwargs – Keyword arguments for kivy_garden.graph.Graph class
-
get_data_limits
()¶ Calculate x- and y-data limits
Returns: tuple consisting of: - float: min. x-value
- float: max. x-value
- float: min. y-value
- float: max. y-value
Return type: tuple
-
plot
(x_values, y_values, color=None, label=None)¶ Adds a plot to the graph
Parameters: - x_values (list) – List of x-values
- y_values (list) – List of y-values
- color (tuple) – RGBA color value (0…1.) for the curves color- e.g. (0.5, 1., 1., 1.)
- label (str) – Legend label for the curve
Note
If color is None the color will automatically be assigned based on the predefined standard colors.
-
recalculate_axis_limits
()¶ Recalculates the axis limits and updates the graph
-
recalculate_x_axis
()¶ Recalculates the x-axis limits and ticks
Returns: tuple consisting of: - float: y-axis min. value
- float: y-axis max. value
- float: tick major spacing
- int: number minor ticks
Return type: tuple
-
recalculate_y_axis
()¶ Recalculates y-axis limits
Returns: tuple consisting of: - float: y-axis min. value
- float: y-axis max. value
- float: tick major spacing
- int: number minor ticks
Return type: tuple
-
standard_colors
¶ Returns a list of 16 standard color RGBA values
Type: list
-
x_data_limits
¶ Returns data limits (xmin and xmax) in terms of x-coordinates
Type: tuple
-
y_data_limits
¶ Returns data limits (ymin and ymax) in terms of y-coordinates
Type: tuple