You are here

Revision of Writing Demo Scripts from Sun, 04/30/2023 - 09:05

Revisions allow you to track differences between multiple versions of your content, and revert back to older versions.

Wiki home pageDemo scripts are invaluable for helping others learn OpenMx, and, during development, to test that the integrity of the program is maintained as well as to test new features.

Style and Comments

Please include a style header that matches these guidelines when creating a demo:

  1. Include the program name labeled with 'Program:'
  2. Include the author name labeled with 'Author:'
  3. Include the date in YYYY.MM.DD format and labeled with 'Date:'
  4. Include the type of model being used in the demo and label it with 'ModelType:'
  5. Include the type of data being used in the demo and label it with 'DataType:'
  6. Include the substantive field being used in the demo and label it with 'Field:'
  7. Include the purpose of the demo and label it with 'Purpose:'
  8. Keep an ongoing revision history of the demo and label it with 'RevisionHistory:'

Here is an example of a style header:

# -----------------------------------------------------------------------------
# Program: ExampleDemo.R  
# Author: Ross Gore
# Date: 2011.06.16 
#
# ModelType: Model Type Suggestions
# DataType: Data Type Suggestions
# Field: Field Suggestions
#
# Purpose:
#      One Factor model to demonstrate style header for demos.
#
# RevisionHistory:
#      Ross Gore -- 2011.06.16 initial creation
# -----------------------------------------------------------------------------

Comments in the demo should adhere to the following guidelines:

  1. Each major action in the source code should be followed by a comment describing it and followed by a line of "-"s up to column 80.
  2. If additional detail is needed to describe minor actions within the block of source code then a comment should be inserted immediately following minor action and followed by a line of "-"s up to column 40.
  3. If even further detail is needed then comments should be inserted on the same line as the source code following the item needing further explanation.

Here is an example of the standard setup of a demo along with the commenting style.

require(OpenMx)
# Load Library
# -----------------------------------------------------------------------------

data(myRawData)
# Prepare Data
# -----------------------------------------------------------------------------

myMxModel <- mxModel("ExampleDemoModel", type ="..."
        mxPath(from="...") # this particular path is very important
        # Added a path to my model
        # -------------------------------------
) # close model
# Create an MxModel object
# -----------------------------------------------------------------------------

myMxModelFit <- mxRun(myMxModel)
# Fit model with mxRun
# -----------------------------------------------------------------------------

summary(myMxModelFit)
# Print a summary of the results
# -----------------------------------------------------------------------------

How to write

Avoid using functions which will not work for all users/platforms. Things to avoid include:

Avoid Instead
source() Include the source in the demo so it is self-contained
setwd() use data() to avoid needing to set the current directory
read.table
(unless from a URL)
data(, package="OpenMx")
Undefined variables check that your script is not dependent on variables which it does not set. "rm(list = ls())" will remove all variables from your environment - a bit extreme but if your script still runs it is a good check that you are not using variables you don't set'

Where to put demos

Demo scripts with the intention of teaching or sharing a technique can be added to the "/trunk/demos/" directory.
nb: When adding a file to this directory, please manually update the "00INDEX" file in this directory with the name of the file and a 1-line description of its purpose.

Test scripts with the intention of verifying OpenMx functionality can be added to the "/trunk/models/passing" or "/trunk/models/failing".

nb: Test scripts differ from other scripts in that they MUST verify that the results obtained are those returned in Mx 1.x using omxCheckCloseEnough() and related functions.

Using omxCheckCloseEnough()

omxCloseEnough() simply compares the elements of two compatible arrays, returning TRUE if no pair of elements differ by more than epsilon (the maximum allowed difference)
omxCloseEnough() takes three arguments:

   omxCheckCloseEnough(a, b, epsilon = 10^(-15))

'a' and 'b' are numeric vectors or matrices, and 'epsilon' is the maximum allowed (absolute?) difference between the elements of 'a' and the elements of 'b'.

In usage, you would typically fill vector a with results from the results of an OpenMx mxRun() statement, while the elements of b will be hard-coded from a comparable Mx run. nb:

For convenience, you can run mx 1.x from OpenMx using the function omxOriginalMx(), which takes two arguments: 'mx.filename' is a string that is the path and name of the mx script to run (relative be found in the directory returned by getwd() ), and 'output.directory' similarly the relative path to the directory where output files will be written.

notes: 'mx' must appear somewhere in your $PATH.
While you can use omxOriginalMx() to get the correct values for omxCloseEnough(). comment this code out in the demo, and hard code the results: We cannot assume that developers and users have installed Mx 1.X.

Python Script :

# OpenMX Demo Script
 
# Load the OpenMX module
import openmx as omx
 
# Set up the simulation
omx.initialize()  # Initialize the OpenMX environment
 
# Set the simulation parameters
basis_set = "LDA"  # Select the basis set (e.g., LDA, GGA)
cutoff_radius = 8.0  # Set the cutoff radius for the numerical integration
 
# Create the system
system = omx.System(basis=basis_set, cutoff_radius=cutoff_radius)
 
# Define the atoms and their positions
atom1 = system.add_atom(symbol="H", position=[0.0, 0.0, 0.0])
atom2 = system.add_atom(symbol="O", position=[0.0, 0.0, 1.0])
 
# Set up the calculation
calculation = omx.Calculation(system=system)
 
# Perform a self-consistent field (SCF) calculation
calculation.scf()
 
# Get the final electronic structure and energy
electronic_structure = calculation.get_electronic_structure()
total_energy = calculation.get_total_energy()
 
# Print the results
print("Final Electronic Structure:")
print(electronic_structure)
print("Total Energy:", total_energy)
 
# Clean up
calculation.finalize()  # Finalize the calculation
omx.finalize()  # Finalize the OpenMX environment
Script provided by Pile ou face

This script demonstrates the basic steps of setting up a calculation in OpenMX. It initializes the OpenMX environment, sets up the simulation parameters, creates the system by adding atoms and their positions, performs a self-consistent field (SCF) calculation, retrieves the electronic structure and total energy, and finally prints the results. The script concludes by finalizing the calculation and the OpenMX environment.

Please note that this is just a simplified example, and there are many other advanced features and options available in OpenMX for more complex simulations. You can refer to the OpenMX documentation for more detailed information on the various functionalities and capabilities of the software.

Comments

Textmate 2 with dropdown select support attached to make this trivial (just type demo and tab)

remove the .txt extension (added to trick the upload filter)

-----------------------------------------------------------------------------

Program: ${1:ExampleDemo}.R

Author: ${2:$TM_FULLNAME}

Date: date +%Y-%m-%d

#

ModelType: ${3:CFA,EFA,ACE,Multilevel,Mixture,Growth Curve,Growth Mixture,Saturated,Locus Likelihood,Log-odds,Joint,Ordinal,Continuous}

DataType: ${4|Time Series,Likert,Twin,Count,Frequencies,Log-odds,Binary,Survey,Personality,Family,Joint,Ordinal,Continuous,Simulated,Clustered|}

Field: ${5|Path model,Behavior Genetics,Genetic Epidemiology,Functional Magnetic Resonance Imaging (FMRI)|}

#

Purpose:

${6:One Factor model to demonstrate style header for demos.}

#

RevisionHistory:

$2 -- date +%Y-%m-%d initial creation

-----------------------------------------------------------------------------