neural_body package¶
Submodules¶
neural_body.BenrulesRealTimeSim module¶
Real-Time basic simulator for planetary motions with neural network inference for prediction of pluto’s position.
This module contains the BenrulesRealTimeSim class, which creates a real time simulator of the sun, planets, and pluto. The non-real-time version was forked from GitHub user benrules2 at the below repo: https://gist.github.com/benrules2/220d56ea6fe9a85a4d762128b11adfba The simulator originally would simulate a fixed number of time steps and then output a record of the past simulation. The code was repackaged into a class and methods added to allow querying and advancing of the simulator in real-time at a fixed time step. Further additions were made to then integrate a class for loading a neural network (NeuralNet) that would load a Tensorflow model, take a vector containing all other planetary positions (X, Y, Z) and output the predicted position of Pluto in the next time step.
-
class
neural_body.BenrulesRealTimeSim.
BenrulesRealTimeSim
(in_config_df, time_step=100)[source]¶ Bases:
object
Class containing a basic, real-time simulator for planet motions that also interacts with the NNModelLoader class to load a neural network that predicts the motion of one of the bodies in the next time step.
Instance Variables: :ivar _bodies: Current physical state of each body at the current time step :ivar _body_locations_hist: Pandas dataframe containing the positional
history of all bodies in the simulation.
- Variables
_time_step – The amount of time the simulation uses between time steps. The amount of “simulation time” that passes.
_nn – NNModelLoader object instance that contains the neural network loaded in Tensorflow.
-
property
bodies
¶ Getter that retrieves the current state of the entire system in the simulation.
- Return bodies
Returns the list of bodies. Each item in the list is a Body object containing the physical state of the body.
-
property
body_locations_hist
¶ Getter that returns a Pandas dataframe with the entire simulation history.
- Return body_locations_hist
Pandas dataframe containing the entire history of the simulation. The positional data of all bodies over all time steps.
-
property
current_time_step
¶ Getter that retrieves the current time step the simulator is at.
- Return current_time_step
Current time step the simulator is at.
-
get_next_sim_state
()[source]¶ Function to calculate the position of all system bodies in the next time step.
When this method is called, the current system state is passed to the neural network to calculate the position of a certain body in the next time step. After the neural network completes, the simulation then advances all bodies ahead using “physics”. The positions of all bodies resulting from the “physics” are then packaged into a dictionary with the body name as key and a list containing the x,y,z coordinates of the body as the value attached to that key. The predicted position from the neural network is also packaged as a dictionary with the name as key and predicted coordinates as the value.
- Returns
simulation_positions - Dictionary containing all body positions in the next time step calculated with “physics”.
pred_pos - Dictionary containing the predicted position of a body using the neural network.
-
property
max_time_step_reached
¶ Getter that retrieves the maximum time step the simulation has reached.
- Return max_time_step_reached
Max time step the simulation has reached.
-
property
satellite_predicting_name
¶ Getter that retrieves the name of the planet the neural network is trying to predict the position of.
- Return planet_predicting_name
Name of the planet the neural network is trying to predict.
neural_body.grav2 module¶
-
neural_body.grav2.
boxes
(screen, scr_width, scr_height)[source]¶ Function that places UI divider boxes on the screen
Function to draw the boundries of the three areas of the application. These being the menu, solar system view, and inner planet view. These views are static and rendered first, putting them at the bottom of the visual instnaces
List of input parameters:
- Parameters
screen – the usable area for drawing within the application window
scr_width – the width of the screen in pixels. This will always be an integer used for object placement math.
scr_height – the height of the screen in pixels. This will always be an integer used for object placement math.
scr_height – the height of the screen in pixels. This will always be an integer used for object placement math.
-
neural_body.grav2.
click_handler
(click_now)[source]¶ Function to capture characteristics of user clicks
Function to easily capture mouse location and actions. click_now is used to prevent clicks from being repeated each time the simulation screen is re-rendered.
List of input parameters:
- Parameters
click_now – contains an integer of either 0 or 1 that denotes whether or not the mouse was pressed down during the previous program cycle in order to handle sustained presses
List of returned values:
- Returns
action_flag - Flag is set to 0 if the click is a continued press, 1 if the click is new
click_now - Set to 0 if the left mouse button is released, 1 if pressed
click_x - The inetger x-coordinate of the mouse
click_y - The integer y-coordinate of the mouse
-
neural_body.grav2.
main
()[source]¶ Main function of the program, where all of the fun begins. Basic environment attributes are set such as the screen dimensions, number of planets (total on screen, main view plus inner planet view), the planet tail length, and the program clock
Method that will display main menu, as well as handles actions to the menu. If mouse is hovering over menu item the text for that menu item will change from grey to white. If clicked on or hovered over, menu item will activate.
Methods used:
text_handler - displays text on the screen
pygame.draw - draws rectangles on the screen to separate parts of the application.
List of input parameters:
- Parameters
screen – window created by pygame used to display application
states – A list of the current states of the system, see list of states below
scr_width – An in that represents the width of the screen
scr_height – An int that represents the height of the screen
numDays – An int that represents how many Earth days have passed in the simulation
List of Menu Selection Areas:
- play_pause - List that contains the length and width parameters
for the play/pause menu option
- toggle - List that contains the length and width parameters for
the view toggle menu option
- adjust - List that contains the length and width parameters for
the speed adjustment menu option
- upload - List that contains the length and width parameters for
the file upload menu option
- nasa_right - List that contains the length and width parameters
for the display pluto menu option
- key_menu_option - List that contains the length and width
parameters for the display key menu option
- day_select - List that contains the length and width parameters
for the point in time selection menu option
List of states of the application:
- pause - An int that specifies whether or not the simulation runs,
0 means it’s running, 1 means it is paused
- view - An int that determines what view is displayed, 0 for
overhead view, 1 for side view
- speed - An int that determines the speed of the simulation,
1x, 2x, 4x, etc
- rev - An int that would determine if the simulation is running
in reverse or not, not currently implemented
- click_now - An int that determines whether the mouse is being
clicked, 0 means no click, 1 means click
- input_active - An int that determines whether the file input
text box shows, 0, means no show, 1 means it will show
- nasa - String that activates pluto, Yes, means NASA is correct,
and pluto is not a planet, No means NASA is incorrect and pluto is a planet
- input2_active - An int that displays the travel to a day
textbox, 0 means no show, 1 means it shows
- textbox2_active - An int that determines whether the travel to
textbox is showing, 0 means no, 1 means yes
- input2_text - String that determines what day will be traveled
to after the travel to day is determined
- Returns
returns the above list of states to feed information to the program for control of specific features
Function to place menu option text on the screen
Function to display the base text of the menu on the screen. All values start out as light grey but are overlaid with white text by another method when moused over
List of input parameters:
- Parameters
screen – the usable area for drawing within the application window
scr_width – the width of the screen in pixels. This will always be an integer used for object placement math.
scr_height – the height of the screen in pixels. This will always be an integer used for object placement math.
-
neural_body.grav2.
orbits
(screen, num_planets, tail_length, clock, scr_width, scr_height)[source]¶ Function that runs the simulation. Handles function calls to display all visual elements and determine orbital locations.
This class operates as the master of the program, containing the infinite loop that the program relies on in order to function. All important interactions between the various components of the program are routed through this class, and many vital mathematic operations reside within it. Because global variables are not used, this class acts as an intermediate to pass variables as parameters and receive them back as returned values.
Methods used: - pygame.draw.circle - replicates the planets drawn on the screen - text_handler - names each of the replicated planets - BenrulesRealTimeSim - creates a real time simulator of the sun,
planets, and pluto.
pygame.image.load - loads an image from a file
pygame.transform.scale - scales an image to a specified width and height
get_next_sim_state - updates the dictionary of current simulation data to the next time_step
screen.fill - changes the color of the entire pygame canvas
screen.blit - method to display a supplied image on the scren as output
pygame.draw.line - pygame method used to draw a line, here is used to draw the trails
pygame.draw.circle - pygame method used to draw circles, here used to represent the planets
current_time_step - Setter function used to change the point in time of the simulation
pygame.event.get() - method to handle a user interaction event
pygame.quit() - Pygame method used to quit running pygame
sys.exit() - Method used to exit the program
pygame.display.flip() - method to refresh the pygame display
clock.tick(60) - method used to set a frame rate of 60 frames per second
List of input parameters:
- Parameters
num_planets – Int representing the number of planet bodies on the entire screen, inner planets view + solar system view
tail_length – Int representing the length of the tail to follow behind the orbiting planets
clock – a local variable storing a copy of the pygame clock time
screen – window created by pygame used to display application
scr_width – An in that represents the width of the screen
scr_height – An int that represents the height of the screen
List of internal variables:
input_text - string containing user-entered filepath for a new init file
past_input - string used to store input_text between when it is reset and when it needs to be used to call a file
textbox_active - Int that determines whether the input textbox is to be displayed, 0 if no, 1 if yes
pause - An int that specifies whether or not the simulation runs, 0 means it’s running, 1 means it is paused
view - An int that determines what view is displayed, 0 for overhead view, 1 for side view
speed - An int that determines the speed of the simulation, 1x, 2x, 4x, etc
click_now - An int that determines whether the mouse is being clicked , 0 means no click, 1 means click
input_active - An int that determines whether the file input text box shows, 0, means no show, 1 means it will show
nasa - String that activates pluto, Yes, means NASA is correct, and pluto is not a planet, No means NASA is incorrect and pluto is a planet
input2_active - An int that displays the travel to a day textbox, 0 means no show, 1 means it shows
textbox2_active - An int that determines whether the travel to textbox is showing, 0 means no, 1 means yes
input2_text - String that determines what day will be traveled to after the travel to day is determined
zoom - Int used to scaled the planets to fit on the screen
zoom_factor - Int used to scale the zoom to give varying views of the solar system if changed
zoom_i - Int used to scale the inner planets coordinates to fit on the screen
num_planets - Number of total bodies being displayed on the screen, whole solar system view + inner planets view
time_step - Int used to determine length of time between
curr_time_step - Int that represents how many time steps have passed in the simulation
start_string - String containing the default path for an init file
simulation - An object encompassing an n-body simulation that predicts a single body dependent on the state of all other simulated bodies in the system.
sunx - Int representing the x coordinate of the sun image
suny - Int representing the y coordinate of the sun image
sun_i_x - Int representing the x coordinate of the sun image for the inner planets view
sun_i_y - Int representing the x coordinate of the sun image for the inner planets view
sun_img - Object used to hold the image of the sun that is printed to the screen
current_positions - Dictionary that contains the name and a list of x, y coordinates for each non-predicted planet
predicted_positions - Dictionary that contains the name and list of x, y coordinates for the predicted planet(s)
x1 - X coordinate relative to the printed sun image for Mercury
xi1 - X coordinate relative to the printed sun image for Mercury for the inner planets view
y1 - Y coordinate relative to the printed sun image for Mercury
yi1 - Y coordinate relative to the printed sun image for Mercury for the inner planets view
x2 - X coordinate relative to the printed sun image for Venus
xi2 - X coordinate relative to the printed sun image for Venus for the inner planets view
y2 - Y coordinate relative to the printed sun image for Venus
yi2 - Y coordinate relative to the printed sun image for Venus for the inner planets view
x3 - X coordinate relative to the printed sun image for Earth
xi3 - X coordinate relative to the printed sun image for Earth for the inner planets view
y3 - Y coordinate relative to the printed sun image for Earth
yi3 - Y coordinate relative to the printed sun image for Earth for the inner planets view
x4 - X coordinate relative to the printed sun image for Mars
xi4 - X coordinate relative to the printed sun image for Mars for the inner planets view
y4 - Y coordinate relative to the printed sun image for Mars
yi4 - X coordinate relative to the printed sun image for Mars for the inner planets view
x5 - X coordinate relative to the printed sun image for Jupiter
y5 - Y coordinate relative to the printed sun image for Jupiter
x6 - X coordinate relative to the printed sun image for Saturn
y6 - Y coordinate relative to the printed sun image for Saturn
x7 - X coordinate relative to the printed sun image for Uranus
y7 - Y coordinate relative to the printed sun image for Uranus
x8 - X coordinate relative to the printed sun image for Pluto
y8 - Y coordinate relative to the printed sun image for Pluto
x9 - X coordinate relative to the printed sun image for Neptune
y9 - Y coordinate relative to the printed sun image for Neptune
x_track[] - List that contains the x coordinates for the planets to print the trails
y_track[] - List that contains the y coordinates fot the planets to print the trails
-
neural_body.grav2.
print_key
(screen)[source]¶ Method to display a planet key
Method that displays a key of planet colors and names when selected in order to aid in user planet idenntification
List of input parameters:
- Parameters
screen – window created by pygame used to display application
-
neural_body.grav2.
sun
(screen, x, y)[source]¶ Function to place the sun image on the screen
A simple function for loading the sun image and placing it on the screen at a given coordinate. The size is static
List of input parameters:
- Parameters
screen – window created by pygame used to display application
x – the integer x-coordinate pixel value where the sun should be placed
y – the integer y-coordinate pixel value where the sun should be placed
-
neural_body.grav2.
text_handler
(screen, text, scr_x, scr_y, size, color)[source]¶ Function to place text on the screen at a desired location
Function to place custom text on the screen at any desired location. The color of the text is currently restricted to shades of grey and white to match the visual theme we are pursuing.
List of input parameters:
- Parameters
screen – the usable area for drawing within the application window
text – string of text to be displayed on the screen. One line only.
scr_x – the x coordinate for the text to be located at. Must be an integer
scr_y – the y coordinate for the text to be located at. Must be an integer
size – the size of the text to be displayed, in pixels (height). Must be an integer
color – grey shade of the output. Must be an integer between 0 and 255.