Optics App

App used to help design the lens system used in the microscope. See Microscope optics V2 for the full details of how the app is used to design and select all the lenses in the system.

There are three model sections in the app:

Focal length / mag from position

This section models how the focal length and mag changes given the positions of the lens, the object seen by the lens, and the image of the object on the other side of the lens.

You can also add graphs to this section to visualize in 2D and 3D the effect of changing these parameters.

Image/lens pos and mag from lens/object pos and focal length

This section models how the image and les pos need to be given a desired lens and object position and focal length.

You can also add graphs to this section to visualize in 2D and 3D the effect of changing these parameters.

4-lens system

This section lets you chain upto 4 lenses in a system and for each lens it lets you change its focal length, its position, and the object position and it estimates the image position and the mag for that and all subsequent lenses based on the previous lenses in the chain.

The object position can only be given for the first lens, the others are computed from the previous lenses.

For each lens in the chain you can also add 2D and 3D graphs that lets you explore how the output parameters may change, given input parameters.

class ceed.tools.optics.OpticsApp(**kwargs)

Bases: kivy.app.App

The app that shows all the formula.

formula_container_widget

Widget that contains all the formula.

theme

The flat material design style theme to use.

focal_len_from_io_f: ceed.tools.optics.LensFocalLengthFormula

Formula that computes focal length and mag from the other parameters.

image_from_f: ceed.tools.optics.LensFixedObjectFormula

Formula that computes the parameters for a fixed object.

objective_lens: ceed.tools.optics.LensFixedObjectFormula

Formula that computes the parameters for a fixed object.

This is the first lens in the 4-lens chain.

cam_lens_further: ceed.tools.optics.LensFixedObjectFormula

Formula that computes the parameters for a fixed object.

This is the second lens in the 4-lens chain.

cam_lens_closer: ceed.tools.optics.LensFixedObjectFormula

Formula that computes the parameters for a fixed object.

This is the third lens in the 4-lens chain.

cam_lens_closer2: ceed.tools.optics.LensFixedObjectFormula

Formula that computes the parameters for a fixed object.

This is the forth and final lens in the 4-lens chain.

build()

Initializes the application; it will be called only once. If this method returns a widget (tree), it will be used as the root widget and added to the window.

Returns

None or a root Widget instance if no self.root exists.

class ceed.tools.optics.PlotWidget(**kwargs)

Bases: kivy.uix.boxlayout.BoxLayout

Widget that visualizes a plot for a formula.

plot: ceed.tools.optics.FormulaPlot

The plot that visualizes the formula.

formula_widget: ceed.tools.optics.FormulaWidget

The formula to visualize.

mouse_x_val

x-pos of the mouse in formula input domain.

mouse_x2_val

x2-pos of the mouse in formula input domain.

mouse_y_val

y value at the current mouse pos in formula output domain.

graph_min_height

Smallest height for the graph.

select_x_variable(x_prop, variable_name)

Sets the input variable to display on the horizontal/vertical.

populate_x_variables()

Updates the list of input variables the user can select from in the GUI, when selecting the variable to show on an axis.

class ceed.tools.optics.FormulaVariableBehavior

Bases: kivy._event.EventDispatcher

Visualization for a formula variable with an attached name.

name

The name of the property that is displayed to user.

class ceed.tools.optics.PropertyDisplayBinding(**kwargs)

Bases: kivy._event.EventDispatcher

Tracks a property (input/output variable) and updates the widget representing the property with the new value.

prop_from_display_setter

Lambda that can be used to convert the property from the displayed value in the GUI when that changes (that could e.g. be a string) to the correct type when setting the property value of the formula.

display_from_prop_setter

Like prop_from_display_setter, but converts the property so it can be displayed in the GUI (e.g. to string from float).

obj

The object to track.

obj_prop

The property of obj to track.

read_only

Whether it’s read only and cannot be updated from the GUI.

prop_value

Current value of the property as it’s shown in the GUI.

set_obj_property(value)

Callback to set the from the GUI.

class ceed.tools.optics.FormulaWidget(**kwargs)

Bases: kivy.uix.boxlayout.BoxLayout

Widget that displays a formula, its inputs, outputs and graphs.

formula: ceed.tools.optics.CeedFormula

The formula visulized by the widget.

props_container_x

Widget container for all the input values.

props_container_y

Widget container for all the output values.

plots_container

Widget container for all the graphs displayed for the formula.

description

Description shown for the formula.

name

Name shown for the formula.

hidden_variables

List of all the input/output variables that are not shown in the GUI.

populate_widget()

Adds widgets for all the variables.

add_plot()

Adds a new plot for the formula.

remove_plot(plot_widget)

Removes an existing plot from the formula.

class ceed.tools.optics.LensFocalLengthFormula(**kwargs)

Bases: ceed.tools.optics.CeedFormula

Only valid when not a virtual image.

lens_pos_src: Optional[ceed.tools.optics.CeedFormula]

The previous formula in the chain, if any, that computes this input variable as its output.

image_pos_src: Optional[ceed.tools.optics.CeedFormula]

The previous formula in the chain, if any, that computes this input variable as its output.

object_pos_src: Optional[ceed.tools.optics.CeedFormula]

The previous formula in the chain, if any, that computes this input variable as its output.

lens_pos

The value of the input variable (automatically set from _src if set).

image_pos

The value of the input variable (automatically set from _src if set).

object_pos

The value of the input variable (automatically set from _src if set).

focal_length

The computed output variable value.

magnification

The computed output variable value.

compute_focal_length(variables={})

Similar to LensFixedObjectFormula.compute_image_pos().

compute_magnification(variables={})

Similar to LensFixedObjectFormula.compute_image_pos().

class ceed.tools.optics.LensFixedObjectFormula(**kwargs)

Bases: ceed.tools.optics.CeedFormula

Computes the properties of a lens, where the object (input distance) is fixed.

lens_pos_src: Optional[ceed.tools.optics.CeedFormula]

The previous formula in the chain, if any, that computes this input variable as its output.

focal_length_src: Optional[ceed.tools.optics.CeedFormula]

The previous formula in the chain, if any, that computes this input variable as its output.

object_pos_src: Optional[ceed.tools.optics.CeedFormula]

The previous formula in the chain, if any, that computes this input variable as its output.

base_magnification_src: Optional[ceed.tools.optics.CeedFormula]

The previous formula in the chain, if any, that computes this input variable as its output.

lens_pos

The value of the input variable (automatically set from _src if set).

focal_length

The value of the input variable (automatically set from _src if set).

object_pos

The value of the input variable (automatically set from _src if set).

base_magnification

The value of the input variable (automatically set from _src if set).

image_pos

The computed output variable value.

img_lens_pos

The computed output variable value.

magnification

The computed output variable value.

compute_image_pos(variables={})

Computes and returns this output variable.

If the variables dict contains a formula for a input variable, that variable value is gotten from from variables. Otherwise it gets it from the variable property. THis allows us to support computing whole ranges for the input variables as their values can be an array.

compute_magnification(variables={})

Similar to compute_image_pos().

compute_img_lens_pos(variables={})

Similar to compute_image_pos().

class ceed.tools.optics.CeedFormula(**kwargs)

Bases: kivy._event.EventDispatcher

A formula that computes a output value(s), given some input value or input value range.

The input value(s) can be formula themselves so that we can have a formula DAG, with some formula leaves. This allows the output of a leaf to be re-computed when any of the inputs or its parents change in the graph.

The formula is computed from properties that are input parameters and it generates one or more output values that are also stored as properties.

The input properties (e.g. lens_pos) can have a second, corresponding property with the _src suffix (e.g. lens_pos_src), which if set to a tuple of (CeedFormula, property_name), the given property of the formula will be used for the input value (lens_pos_src) instead of the property of this instance (lens_pos). This allows chaining.

Additionally, each output property must have a corresponding method with a compute_ prefix that when called returns the output given the inputs stored in the class properties. It can be given a set of CeedFormula that is used to look up values for any of the input properties, instead of using the value currently stored in the instance property for that input.

x_variables: List[str]

List of properties of this class (by name) whose values are used as inputs to the formula’s function.

There must be a corresponding method with a compute_ prefix for each variable.

There may also be a corresponding property with a _src` suffix that contains a formula to use to look up the property value as the output of that formula, instead of using this property value directly.

y_variables: List[str]

List of properties of this class (by name) whose values are outputs of the formula’s function.

Items in y_variables may depend on other items in y_variables as input to their calculation. See y_dependency_ordered.

plots

All the plots that use this formula and need to be updated when the inputs change.

variable_descriptions: Dict[str, str]

A mapping that gets a nicer description for each variable.

dependency_graph: Dict[str, List[str]] = {}

Mapping that maps each y-variable to a list of all the y and x variables it depends on.

y_dependency_ordered: List[str] = []

As mentioned, y (output) variables may depend on other output variables. This orders y variables from leaf to dependant variables, such that any variable is only dependant on other y variables that are listed previously in y_dependency_ordered.

It is automatically computed.

bind_variable_to_src(variable, variable_src)

For each x-variable, if there’s a corresponding _src suffixed variable that is set, this method will track that formula and update the our property when the source changes.

update_result(variable, *largs)

Called automatically when a x-variable changes and it recomputes all the y-variables.

variables_in_subtree(variable, in_subtree, input_variables)

Checks whether the variable or its dependencies is a input variable. If so it’ll need to be computed over the range that the input is sampled on. If not, it’s a constant value that we can just get from the formula properties.

We also check if the variable itself is a input variable.

infer_variable_value(variable, variables, in_subtree, input_variables)

Computes the value of variable for the range of values of the input variables.

Variables accumulates the values of variables as they are computed in the graph, starting from the root.

in_subtree stores whether a variable contains any of the input_variables in it’s dependency tree (it depends on it).

get_variable_dep_leaves(variable)

Gets set of all the (formula, variable) tuples that this variable depends on upto the root, but only those that are roots in the sense that the variables don’t depend on other variable themselves.

This will let us start from them and compute the full graph until reaching the the variable.

class ceed.tools.optics.FormulaPlot(**kwargs)

Bases: kivy._event.EventDispatcher

Given a CeedFormula, it uses that to compute the the 2D/3D formula output, given the range of input values.

It supports updating the plot from a formula whose input is the output of another formula in a chain. In which case it starts with the input to root formula and evaluates all the formula in the chain until the last one. Then uses that to evaluate the final formula on a range of values that is then displayed in the graph.

graph: ceed.tools.optics.FormulaGraph

The graph in which to display the formula outputs.

plot

The specific 2D/3D plot in the graph.

start

The horizontal axis start value from which to evaluate.

end

The horizontal axis end value until which to evaluate.

x2_start

The vertical axis start value from which to evaluate/display.

x2_end

The vertical axis end value until which to evaluate/display.

y_start

The depth (z) axis start value from which to display, if 3D.

y_end

The depth (z) axis end value until which to display, if 3D.

num_points

The number of horizontal/vertical points at which to evaluate the formula.

formula: ceed.tools.optics.CeedFormula

The CeedFormula that takes the x (and x2 if 3D) input values and computes the output values.

x_variable

Name of the horizontal input variable.

x2_variable

Name of the vertical input variable, if any (3D).

x_variable_formula: ceed.tools.optics.CeedFormula

The formula used to generate the horizontal input variable, if it is part of a lens chain and the variable is the result of a previous formula in the chain.

x2_variable_formula: ceed.tools.optics.CeedFormula

The formula used to generate the vertical input variable, if it is part of a lens chain and the variable is the result of a previous formula in the chain.

y_variable

The name of the output (y) variable computed.

num_contours

The number of equal value contours to display for 3D plots.

track_ylim

For 3D plots, whether to update the plot outout range as the output (y) range changes or if the leave it unchanged.

colors = <itertools.cycle object>

Plot colors to use.

compute_contours()

Computes the equal value contours for 3D plots.

create_plot()

Creates and displays the plot for this formula/graph.

refresh_plot(from_variables=True)

Updates plot when any of the variables or parameters change.

reset_y_axis()

Resets the y (output)-axis to the previous value.

class ceed.tools.optics.FormulaGraph(**kwargs)

Bases: kivy_garden.graph.Graph

The graph on which the 2D/3D plots are drawn.

plot_widget: ceed.tools.optics.PlotWidget

The PlotWidget that contains this graph.

on_touch_down(touch)

Receive a touch down event.

Parameters
touch: MotionEvent class

Touch received. The touch is in parent coordinates. See relativelayout for a discussion on coordinate systems.

Returns

bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.