1 of 39

Biocomplexity Institute

Indiana University

Bloomington, IN 47405

USA

�Generous funding by:�NIH U24 EB028887, NIH R01 GM122424, NIH R01 GM123032, NIH P41 GM109824, NSF 1720625

The workshop will begin at 10:00 EDT

Screensharing and microphones have been disabled

Please submit questions / concerns vis zoom chat

User support will be done in zoom breakout rooms.

​

CompuCell3D User Training Workshop�Introduction to Python Programming

2 of 39

CompuCell3D User Training Workshop�Introduction to Python Programming

Biocomplexity Institute

Indiana University

Bloomington, IN 47405

USA

�Generous funding by:�NIH U24 EB028887, NIH R01 GM122424, NIH R01 GM123032, NIH P41 GM109824, NSF 1720625

Instructor: Dr. Bobby Madamanchi,

Moderator: Mr. Hayden Fennell

​

3 of 39

Why Learn Python?

  • Reaction-Network design and analysis
  • Adding behaviors to model biological cells
  • Analyzing data from experiments

​

​

  • Python is a convenient, widely used language that works well at these tasks
  • Will use ‘programming’ a bit differently in reaction/network analysis vs. biological cell model construction

​

​

4 of 39

Topics

  • Learn the necessary programming concepts to build, study, and analyze advanced models in CompuCell3D
    • Model and simulate biochemical networks such as chemical, gene regulatory, signaling, etc..
    • Analyze results
    • Create decision-making algorithms, functions and data types
    • Implement behaviors in biological cell models

​

5 of 39

Keys to Success

  • No one (including me) knows everything about everything related to a programming language

In software, there is always something new to learn

At least once today, we’ll come across something that I don’t know… and that’s ok!

  • Don’t know something? Look it up!

A coder’s best friends: manuals, other coders (e.g., Stack Overflow, open source software), and a trusty debugger

  • Solutions in programming are almost never unique

Focus on what you want to do

Experiment with different ways of how you do it

  • #1 key to success: perseverance

Feeling overwhelmed? Stop, take a breath, and then break up a task into a set of simpler, more manageable sub-tasks

​

6 of 39

nanoHUB Environment

  • Creating a nanoHUB account
  • Logging in
  • Finding Jupyter Notebook App
  • Running cells

7 of 39

Logging in to nanoHUB

Go to nanohub.org

Hit Login

8 of 39

Create a nanoHUB Account

Go to https://nanohub.org

From ‘Menu’, select ‘sign up’

Choose to sign up with your university of choice (if it is a US university) or sign in with Google.

Choose an appropriate username / password / user info

If its your first time signing up you will need to provide some biographical information

​

​

​

9 of 39

Logging in to nanoHUB

Sign in using your preferred method

10 of 39

Launch The Jupyter Notebook App on nanoHUB

https://nanohub.org/tools/jupyter70

Click the Collect button to add Jupyter to your favorites & then click Launch Tool

​

Jupyter Notebook (202105)

11 of 39

Opening a New Notebooks

Click New and select Python3 to launch a new notebook

​

12 of 39

Using Jupyter in nanoHUB

  • Native language is Python
  • Boxes are interactive ‘cells’
  • Cells are interactive edit boxes where you will type Python code
  • Type “shift+enter” to evaluate (execute code in) selected cells

​

​

13 of 39

Saving Notebooks

  • To save use “Save as” in the File Pulldown Menu
  • nanoHUB saves Notebooks in the session subdirectory by default
  • For safety always download your work to your local computer before you end a session using “Download as”. Use .pynb as the file type

14 of 39

Exercise: calculating distance

  • Type expressions in the cell, see the output
  • You can type expressions in each cell.
  • Type shift-enter to evaluate
  • Type some mathematical expressions…
  • Operators: +, -, /, *, **

��Problem

Given: Two positive integers a and b,

Return: The integer corresponding to the sum of a and b.

​

​

​

15 of 39

You’ve just used Scalar Types

  • programs manipulate objects
  • objects have a type that defines the kinds of things programs can do to them
    • Ana is a human so she can walk, speak English, etc.
  • objects are
    • scalar (cannot be subdivided)
    • non-scalar (have internal structure that can be accessed)

16 of 39

Scalar Types

  • int – represents integers e.g., 3
  • float – represents real numbers e.g., 5.1
  • bool – represents Boolean values e.g. True
  • NoneType – special; has one value: None
  • To get the type of an object: “type()”
    • x = 5.1
    • type(x)
    • >>> float

17 of 39

Type Conversion

  • A.k.a., “cast”
  • Convert an object from one type to another

E.g., truncate a float to an integer

int(5.1)

>>> 5

​

E.g., convert an integer to float

float(5)

>>> 5.0

18 of 39

EXPRESSIONS

  • combine objects and operators to form expressions
  • an expression has a value, which has a type
  • syntax for a simple expression

<object> <operator> <object>

19 of 39

OPERATORS ON ints and floats

  • i+j
  • i-j
  • i*j
  • i/j

→ the sum

→ the difference

→ the product

→ division

  • i%j

→ the remainder when i is divided by j

  • i**j → i to the power of j

if both are ints, result is int

if either or both are floats, result is float

result is float

Exercises

20 of 39

How to do math calculations

  • Ordinary order of operations applies
  • operator precedence without parentheses ()
    • **
    • *
    • /
    • + and –
    • executed left to right, as appear in expression

Exercises

21 of 39

Assigning values to variables

  • equal sign is an assignment of a value to a variable name

pi =

3.14159

pi_approx = 22/7

  • value stored in computer memory
  • an assignment binds name to value
  • retrieve value associated with name or variable by invoking the name, by typing pi

22 of 39

Can change the values stored in variables

  • value for area does not change until you tell the computer to do the calculation again

pi

radius

area

3.14

2.2

15.1976

3.2

pi = 3.14

radius = 2.2

area = pi*(radius**2) radius = radius+1

Exercises

23 of 39

STRINGS

  • letters, special characters, spaces, digits
  • enclose in quotation marks or single quotes

hi = "hello there"

  • concatenate strings

name = "ana"

greet = hi + name

greeting = hi + " " + name

  • do some operations on a string as defined in Python docs

silly = hi + " " + name * 3

24 of 39

OUTPUT: print

  • used to output stuff to console
  • function is print

x = 1

​

print(x)

x_str = str(x)

print("my fav num is",

x, ".",

"x =", x)

print("my fav num is "

+ x_str

+ ". " + "x = " + x_str)

25 of 39

COMPARISON OPERATORS ON �int, float, string

  • i and j are variable names
  • comparisons below evaluate to a Boolean

i > j

i >= j

j > i

i <= j

i == j ⇾ equality test, True if i is the same as j

i != j ⇾ inequality test, True if i not the same as j

26 of 39

LOGIC OPERATORS ON bools

  • a and b are variable names (of type Boolean)

True if a is False False if a is True

not a ⇾

​

a and b ⇾

a or b ⇾

True if both are True

True if either or both are True

A

B

A and B

A or B

True

True

True

True

True

False

False

True

False

True

False

True

False

False

False

False

27 of 39

CONTROL FLOW - BRANCHING

if <condition>:

<expression>

<expression>

...

if <condition>:

<expression>

<expression>

...

else:

<expression>

<expression>

...

if <condition>:

<expression>

<expression>

...

elif <condition>:

<expression>

<expression>

...

else:

<expression>

<expression>

...

  • <condition> has a value True or False
  • evaluate expressions in that block if <condition> is True

28 of 39

BLOCKS / INDENTATION

print("x

and y are

equal"

if y != 0

print("therefore

, x / y is",

x/y

print("x

is

smaller"

else:

print("thanks!")

  • blocks are a unit of computation
  • Indentation matters in Python – partitions blocks
  • how you denote blocks of code

​

​

if x == y:

print("x and y are equal")

if y != 0:

print("therefore, x / y is", x/y) elif x < y:

print("x is smaller")

print("y is smaller")

29 of 39

Assignment and Comparison: �= vs ==

​

​

if x == y:

print("x and y are equal") if y != 0:

print("therefore, x / y is", x/y) elif x < y:

print("x is smaller") else:

print("y is smaller") print("thanks!")

30 of 39

Exercise: determine if a number is even or odd

Choose a number. Depending on whether the number is even or odd, print out an appropriate message to the user.

Hint: how does an even / odd number react differently when divided by 2?

Extras:

  1. If the number is a multiple of 4, print out a different message.
  2. Make two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num, tell that to the user. If not, print a different appropriate message.

​

31 of 39

CONTROL FLOW:

while LOOPS

while <condition>:

<expression>

<expression>

...

  • <condition>

evaluates to a Boolean

  • if <condition> is True, do all the steps inside the while code block
  • check <condition> again
  • repeat until <condition>

is False

32 of 39

CONTROL FLOW:

while and for LOOPS

  • iterate through numbers in a sequence

​

# more complicated with while loop n = 0

while n < 5:

print(n) n = n+1

​

# shortcut with for loop for n in range(5):

print(n)

33 of 39

for LOOPS

for <variable> in range(<some_num>):

<expression>

<expression>

...

  • each time through the loop, <variable>

takes a value

  • first time, <variable> starts at the smallest value

gets the next value in the sequence

  • next time, <variable>
  • etc.
  • For is over a sequence of things

34 of 39

range(start,stop,step)

  • default values are start = 0 and step = 1 and optional
  • loop until value is stop - 1

​

mysum = 0

for i in range(7, 10): mysum += i

print(mysum)

​

​

mysum = 0

for i in range(5, 11, 2): mysum += i

print(mysum)

35 of 39

Exercise: Calculate the sum of number

Problem

Given: Two positive integers a and b (a<b<10000).

Return: The sum of all odd integers from a through b, inclusively.

Sample Dataset

100 200

Sample Output

7500

36 of 39

break STATEMENT

  • immediately exits whatever loop it is in
  • skips remaining expressions in code block
  • exits only innermost loop!

​

while <condition_1>: while <condition_2>:

<expression_a>

break

<expression_b>

<expression_c>

37 of 39

break STATEMENT

mysum

+=

i

if

mysum

== 5

break

mysum += 1 print(mysum)

  • what happens in this program?

mysum = 0

for i in range(5, 11, 2): mysum

if mysum == 5:

break

38 of 39

for VS while LOOPS

while loops

  • unbounded number of iterations
  • can end early via break
  • can use a counter but must initialize before loop and increment it inside loop
  • may not be able to rewrite a while loop using a for loop

for loops

  • know number of iterations
  • can end early via

break

  • uses a counter
  • can rewrite a for

​

​

using a while loop

39 of 39

Module Questionnaire

Please take a minute or two to let us know about your experience with this module by filling out the brief zoom survey

​

Feel free to provide additional comments and suggestions in the slack or by email to us as well (hfennel@iu.edu)

Funding Sources: NIH U24 EB028887, NSF 2120200, 2000281, 1720625

Previous Funding : NIH R01 GM122424

​