I am working on the file "02-NumPy.ipynb".
The first task is:
Data types

bool
uint8
int (Em Python2 é machine dependent)
int8
int32
int64
float (Sempre é machine dependent Matlab double)
float32
float64

(http://docs.scipy.org/doc/numpy/user/basics.types.html.)
Curiosidades...
Can you write Python code for it?

np.array(255, dtype=np.uint8)

float_info = '{finfo.dtype}: max={finfo.max:<18}, approx decimal precision={finfo.precision};'
print(float_info.format(finfo=np.finfo(np.float32)))
print(float_info.format(finfo=np.finfo(np.float64)))


I am working on the file "02-NumPy.ipynb".
The first task is:
Máscara condicional
Can you write Python code for it?

mask = np.where(a <= 5)  # Para quem ainda vive em MatlabTM world.
mask

mask = a <= 5  # Melhor não?
mask

a[mask]


I am working on the file "02-NumPy.ipynb".
The first task is:
Métodos das arrays
Can you write Python code for it?

a = np.array([[1, 2, 3], [1, 2, 3]])
a

print('Tipo de dados             : {}'.format(a.dtype))
print('Número total de elementos : {}'.format(a.size))
print('Número de dimensões       : {}'.format(a.ndim))
print('Forma                     : {}'.format(a.shape))
print('Memória em bytes          : {}'.format(a.nbytes))


I am working on the file "02-NumPy.ipynb".
The first task is:
Métodos que auxiliam na criação de arrays.
Can you write Python code for it?

np.zeros(a.shape) == np.zeros_like(a)

np.arange(1, 2, 0.2)

a = np.linspace(1, 10, 5)  # Olhe também `np.logspace`
a


I am working on the file "02-NumPy.ipynb".
The first task is:
Operações: +, -, , /, //, *, %
Can you write Python code for it?


I am working on the file "02-NumPy.ipynb".
The first task is:
Outros métodos matemáticos/estatísticos úteis:
Can you write Python code for it?

print('Máximo e mínimo                      : {}'.format(a.min(), a.max()))
print('Some é produto de todos os elementos : {}'.format(a.sum(), a.prod()))
print('Média e desvio padrão                : {}'.format(a.mean(), a.std()))

a.mean(axis=0)

a.mean(axis=1)


I am working on the file "02-NumPy.ipynb".
The first task is:
Python é uma linguagem excelente para "propósitos gerais", com um sintaxe
clara elegível, tipos de dados (data types) funcionais (strings, lists, sets,
dictionaries, etc) e uma biblioteca padrão vasta.
Entretanto não é um linguagem desenhada especificamente para matemática e
computação científica.  Não há forma fácil de representar conjunto de dados
multidimensionais nem ferramentas para álgebra linear e manipulação de
matrizes.
(Os blocos essenciais para quase todos os problemas de computação
científica.)
Por essas razões que o NumPy existe.  Em geral importamos o NumPy como np:
Can you write Python code for it?


I am working on the file "02-NumPy.ipynb".
The first task is:
Temos também as masked_arrays
Can you write Python code for it?

import numpy.ma as ma

ma.masked_array(a, mask)


I am working on the file "02-NumPy.ipynb".
The first task is:
Voltando às nossas lista a e b
Can you write Python code for it?

a = [0.1, 0.25, 0.03]
b = [400, 5000, 6e4]

a = np.array(a)
b = np.array(b)
c = a + b
c

np.tanh([a, b])

a * b

np.dot(a, b)

np.matrix(a) * np.matrix(b).T


I am working on the file "02-NumPy.ipynb".
The first task is:
https://en.wikipedia.org/wiki/Floating_point
Criando arrays:
Can you write Python code for it?

np.zeros(3, dtype=int)

np.zeros(5, dtype=float)

np.ones(5, dtype=complex)

a = np.empty([3, 3])
a

a.fill(np.NaN)
a


I am working on the file "02-Operationalizing/01-Operationalizing.ipynb".
The first task is:
<img src='img/fig1.png' width="66%" height="66%">
Macbeth
Can you write Python code for it?

# Read in text
with open('data/macbeth.txt', 'r') as f:
    macbeth = f.read()

# Get cast
pattern = re.compile(r'<[A-Z ]*>')
cast = list(set(re.findall(pattern, macbeth)))
cast = [x.replace('>', '').replace('<', '') for x in cast]

# Make dialogue dict
soup = BeautifulSoup(macbeth, 'lxml')
dialogue_dict_macbeth = {}
for c in cast:
    dialogue = [x.text for x in soup.find_all(c.lower().split()[0])]
    dialogue = '\n'.join([re.sub(r'<.*>', '', x).strip() for x in dialogue])
    dialogue_dict_macbeth[c] = dialogue

# Plot
plot_character_space(dialogue_dict_macbeth)


I am working on the file "02-Operationalizing/01-Operationalizing.ipynb".
The first task is:
<img src='img/fig3.png' width="66%" height="66%">
Othello
Can you write Python code for it?

# Read in text
with open('data/othello.txt', 'r') as f:
    othello = f.read()

# Get cast
pattern = re.compile(r'<[A-Z ]*>')
cast = list(set(re.findall(pattern, othello)))
cast = [x.replace('>', '').replace('<', '') for x in cast]

# Make dialogue dict
soup = BeautifulSoup(othello, 'lxml')
dialogue_dict_othello = {}
for c in cast:
    dialogue = [x.text for x in soup.find_all(c.lower().split()[0])]
    dialogue = '\n'.join([re.sub(r'<.*>', '', x).strip() for x in dialogue])
    dialogue_dict_othello[c] = dialogue

# Plot
plot_character_space(dialogue_dict_othello)


I am working on the file "02-Operationalizing/01-Operationalizing.ipynb".
The first task is:
<img src='img/fig7.png' width="66%" height="66%">
Operationalizing Tragic Collision: Most Distinctive Words
The code below looks complicated, but all it does is count how many times each character said each word in the entire text. If the character didn't say the word, it just gets tallied as a 0. We then sum all of these counts to get the number of times each word is spoken in the text. If we're intested in the most distinctive words, we'd want to know how many times a character said a specific word compared to how many times it was spoken in the entire text.
We'll make an 'EXPECTED' column that tells us if the word was distributed evenly amongst characters, how many times our target character should have said it. Then we'll add a column for the ratio between the observed occurences and the expected occurences.
TLDR: This code will tell us which words a specific character used more or less frequently than average for a character in a text.

"To do this, the Literary Lab follows an approach (which we call Most Distinctive Words) in several steps. First, we establish how often a word occurs in the corpus, and hence how often a specific character is expected to use it given the amount of words at its disposal; then we count how often the character actually utters the word, and calculate the ratio between actual and expected frequency; the higher the ratio, the greater the deviation from the average, and the more typical the word is of that character." [10]
Can you write Python code for it?

def get_mdw(dialogue_dict, character, group=False):
    # Boot up the dtm-maker
    cv = CountVectorizer()

    # Create the dtm
    dtm = cv.fit_transform(dialogue_dict.values()).toarray()

    # Put the dtm into human-readable format
    word_list = cv.get_feature_names()

    dtm_df = pd.DataFrame(dtm, columns = word_list, index = dialogue_dict.keys())

    # Create new dataframe
    mdw_df = pd.DataFrame()

    # Add a column for her observed word counts
    mdw_df[character] = dtm_df.loc[character]

    if group == False:
        # Add a column for the total counts of each word in the play
        mdw_df['WORD_TOTAL'] = dtm_df.sum()
    else:
        # Add a column for the total counts of each word for the characters in the defined group
        mdw_df['WORD_TOTAL'] = dtm_df.loc[group].sum()

    # Calculate Antigone's share of the total dialogue
    char_space = sum(mdw_df[character])/float(sum(mdw_df['WORD_TOTAL']))

    # Add a new column in which we calculate an "expected" number of times
    # Antigone would utter each word, based on its overall use in the play
    # and her share of the dialogue.

    mdw_df[character + '_EXPECTED'] = mdw_df['WORD_TOTAL']*char_space

    # How much more/less frequently does Antigone use the word than expected?
    mdw_df['OBS-EXP_RATIO'] = mdw_df[character]/(mdw_df[character + '_EXPECTED'])

    # Sort the dataframe by the Observed/Expected Ratio to show
    # Antigone's 20 "Most Distinctive Words"
    return mdw_df[(mdw_df['OBS-EXP_RATIO']>1)&(mdw_df['WORD_TOTAL']>5)].sort_values('OBS-EXP_RATIO', ascending=False).head(20)

get_mdw(dialogue_dict_antigone, 'ANTIGONE')

get_mdw(dialogue_dict_antigone, 'CREON')


I am working on the file "02-Operationalizing/01-Operationalizing.ipynb".
The first task is:
Character Space
This notebook recreates results discussed in:

Moretti, Franco. "'Operationalizing': or, the function of measurement in modern literary theory". Stanford Literary Lab Pamphlet 6. 2013

In Moretti's study, he offers several measures of the concept of <i>character space</i>. The simplest of these is to measure the relative dialogue belonging to each character in a play. Presumably the main characters will speak more and peripheral characters will speak less.
The statistical moves we will make here are not only counting the raw number of words spoken by each character, but also normalizing them. That is, converting them into a fraction of all words in the play.
In order to focus on the statistical tasks at hand, we need to parse raw text files to figure out who said what. Unfortunately, that's the hard part! We'll walk through the first one and I'll quickly do the ones after.
Jean Racine's Phèdre
Can you write Python code for it?

# Read the text of the play from its file on the hard-drive
with open('data/phedre.txt', 'r') as f:
    phedre = f.read()

print(phedre[:200])  # print first 200 characters

# Create a list, where each entry is a line from the play. We'll split on double line breaks.
# Each line starts with the name of the speaker.
phedre_list = phedre.split('\n\n')

# Create a regex pattern to match words we don't want to start the line
pattern = re.compile(r'ACT|SCENE|Scene')

# Grab list of all the dialogue lines if they don't have the words above in them
phedre_list = [x.strip() for x in phedre_list if re.match(pattern, x) == None and '\n' in x.strip()]

# Print first three dialogue turns
phedre_list[:3]


I am working on the file "02-Operationalizing/01-Operationalizing.ipynb".
The first task is:
Here's what Moretti had as most distinctive words:

But Moretti notes that these are Antigone's and Creon's most distinctive words as compared to the rest of the text (al the characters in the text). What we are interested in only the relationship between the two characters? We can look at the most distinctive words given the dialogue of only Antigone and Creon the same way, just leaving out the rest of the dialogue:
Can you write Python code for it?

get_mdw(dialogue_dict_antigone, 'ANTIGONE', group=['ANTIGONE', 'CREON'])

get_mdw(dialogue_dict_antigone, 'CREON', group=['ANTIGONE', 'CREON'])


I am working on the file "02-Operationalizing/01-Operationalizing.ipynb".
The first task is:
Now that we have the dialogue texts in a list, we can attribute dialogue words to each character.

"character-space turns smoothly into “word-space”—“the number of words allocated to a particular character”—and, by counting the words each character utters, we can determine how much textual space it occupies." [2]
Can you write Python code for it?

# Create a dictionary where each key is the name of a character
# and each entry is a single string of words spoken by them

# Initiate empty dict
dialogue_dict_phedre = {}

# Iterate through list of turns in the dialogue list
for line in phedre_list:

    # Get the name of the character
    char = line.split('\n')[0].split()[0]

    # Get the dialogue text
    dialogue = '\n'.join(line.split('\n')[1:])

    # Add dialogue text to that character
    if char not in dialogue_dict_phedre.keys():
        dialogue_dict_phedre[char] = dialogue
    else:
        dialogue_dict_phedre[char] += dialogue

# Print first 200 character's of Phaedra's dialogue
print(dialogue_dict_phedre['PHAEDRA'][:200])

def plot_character_space(dialogue):

    # Create counter to get all words in all dialogue
    total_words = 0
    for char in dialogue.keys():
        total_words += len(dialogue[char].split())

    # Create dict to record share of dialogue for each character
    dialogue_share = []
    for char in dialogue.keys():
        dialogue_share.append({'Character': char.title(), 'Dialogue Share': len(dialogue[char].split()) / total_words * 100})

    my_table = Table.from_records(dialogue_share).sort('Dialogue Share', descending=True)
    my_table.bar(column_for_categories='Character')
    plt.xticks(range(len(my_table.columns[0])), my_table.columns[0], rotation=90)

plot_character_space(dialogue_dict_phedre)


I am working on the file "02-Operationalizing/01-Operationalizing.ipynb".
The first task is:
What are each Phèdre, Macbeth, and Othello's most distinctive words? If you've read the text, does this confirm your opinion of it? Does it add anything new?

If you've already taken Data 8, or your Python text parsing skills are already advanced, try this one:
I've placed two more text files in the data folder for the two remaining dramas Moretti plots: Friedrich Schiller's Don Carlos and Henrik Ibsen's Ghosts. Write some code to plot the character space!
Can you write Python code for it?

!ls data

## YOUR CODE HERE

plot_character_space(dialogue_dict_doncarlos)
plot_character_space(dialogue_dict_ghosts)


I am working on the file "02-Project-Luther/Dataframe_construction_notebook.ipynb".
The first task is:
This notebook contains the final bits of code that was used to clean up my data before having it ready for analysis.
All commands have been commented out to avoid any data corruption/errors.
Data cleaning is an incredibly important skill - in some sense, I am thankful that this project was messy as it puched me to be creative in my cleaning techniques.
The entire scraping and cleaning process for the project too approximately 1 week.
Can you write Python code for it?

# regex_runtime = r" minutes"
# subset = ""

# who

# movie_db = unpickle_object("movie_database_final.pkl")

# movie_df = pd.DataFrame.from_items(movie_db.items(),
#                             orient='index',
#                             columns=['A','B','C','D',"E",'F',"G", 'H', "I", "J", "K", "L"])

# del movie_df['F']

# movie_df.columns = ['Rank_in_genre', "rotton_rating_(/10)", "No._of_reviews_rotton","Tomato_Freshness_(%)", "audience_rating_(/5)", "Date", "Runtime", "Box_office", "Budget", "Country","Language" ]

# movie_df.head()


I am working on the file "02-Project-Luther/Model_Creation_and_Analysis.ipynb".
The first task is:
Analysis of Results!
Ridge Analysis
Can you write Python code for it?


I am working on the file "02-Project-Luther/Model_Creation_and_Analysis.ipynb".
The first task is:
Baseline Model
As we can see - the baseline model of regular linear regression is dreadful! Let's move on to more sophisticated methods!
Can you write Python code for it?

baseline_model(X_train, X_test, y_train, y_test)


I am working on the file "02-Project-Luther/Model_Creation_and_Analysis.ipynb".
The first task is:
Elastic Net Analysis
Can you write Python code for it?

extract_model_comparisons(holdout_results, no_holdout_results, "Elastic Net")


I am working on the file "02-Project-Luther/Model_Creation_and_Analysis.ipynb".
The first task is:
Lasso Analysis
Can you write Python code for it?


I am working on the file "02-Project-Luther/Model_Creation_and_Analysis.ipynb".
The first task is:
Ridge, Lasso and Elastic Net regression - Holdouts
Can you write Python code for it?

holdout_results = holdout_grid(["Ridge", "Lasso", "Elastic Net"], X_train, X_test, y_train, y_test)

pickle_object(holdout_results, "holdout_model_results")


I am working on the file "02-Project-Luther/Model_Creation_and_Analysis.ipynb".
The first task is:
The objective of this notebook, and more broadly, this project is to see whether we can discern a linear relationship between metrics found on Rotton Tomatoes and Box Office performance.
Box office performance is measured in millions as is budget.
Because we have used scaling, interpretation of the raw coefficients will be difficult. Luckily, sklearns standard scaler has an inverse_transform method, thus, if we had to, we could reverse transform the coefficients (sc_X_train for the holdout group and sc_x for the non-holdout group) to get some interpretation. The same logic follows for interpreting target variables should we use the model for prediction.
The year, country, language and month will all be made into dummy variables. I will do this with the built in pd.get_dummies() function! This will turn all columns into type object into dummies! I will also use the optional parameter drop_first to avoid the dummy variable trap!
I will use sklearn's standard scaler on all of my variables, except for the dummies! This is important since we will be using regularized regression i.e. Lasso, Ridge, Elastic Net
I will shuffle my dataframe before the train, test split. I will utilise the X_train, y_train, X_test and y_test variables in GridsearchCV. This is an example of employing cross-validation with a holdout set. This will help guard against overfitting.
To be truly honest, I do not have enough data to justify using a hold out set, however, I want to implement it as an academic exercise! It also give me more code to write!
I will then re-implement the models with using the hold out set to compare results!
Let's get to it!
CROSS-VALIDATION WITH HOLDOUT SECTION
Can you write Python code for it?

df = unpickle_object("final_dataframe_for_analysis.pkl") #dataframe we got from webscraping and cleaning!
#see other notebooks for more info.

df.dtypes # there are all our features. Our target variable is Box_office

df.shape


I am working on the file "02-Project-Luther/Model_Creation_and_Analysis.ipynb".
The first task is:
Upon further thought, it doesnt make sense to have rank_in_genre as a predictor variable for box office budget. When the movie is release, it is not ranked immeadiately. The ranks assigned often occur many years after the movie is released and so it not related to the amount of money accrued at the box office. We will drop this variable.
Right now, our index is the name of the movie! We dont need these to be indicies, it would be cleaner to have a numeric index
The month and year columns are currently in numerical form, however, for our analysis, we require that these be of type object!
Can you write Python code for it?

df['Month'] = df['Month'].astype(object)
df['Year'] = df['Year'].astype(object)
del df['Rank_in_genre']
df.reset_index(inplace=True)
del df['index']

percentage_missing(df)

df.hist(layout=(4,2), figsize=(50,50))


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
"break" and "continue" statements
break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. A few examples:
Can you write Python code for it?

# Prints out 0,1,2,3,4

count = 0
while True:
    print(count)
    count += 1
    if count >= 5:
        break

# Prints out only odd numbers - 1,3,5,7,9
for x in range(10):
    # Check if x is even
    if x % 2 == 0:
        continue
    print(x)


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Conditions
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Notice that variable assignment is done using a single equals operator "=", whereas comparison between two variables is done using the double equals operator "==". The "not equals" operator is marked as "!=".
Can you write Python code for it?

x = 2.0001
print(x == 2) # prints out True
print(x == 3) # prints out False
print(x < 3) # prints out True


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Functions and methods
Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.
As we have seen on previous tutorials, Python makes use of blocks.
A block is a area of code of written in the format of:
    block_head:
        1st block line
        2nd block line
Where a block line is more Python code (even another block), and the block head is of the following format: block_keyword block_name(argument1,argument2, ...) Block keywords you already know are "if", "for", and "while".
Functions in python are defined using the block keyword "def", followed with the function's name as the block's name. For example:
Can you write Python code for it?

def my_function():
    print("Hello From My Function!")

my_function()


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Functions may also receive arguments (variables passed from the caller to the function). For example:
Can you write Python code for it?

def my_function_with_args(username, greeting):
    print("Hello, "+username+" , From My Function!, I wish you "+greeting)

my_function_with_args("class", "a wonderful day")


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Functions may return a value to the caller, using the keyword- 'return' . For example:
Can you write Python code for it?

def sum_two_numbers(a, b):
    return a + b

    print "I'm done"

sum_two_numbers(5,19)


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
How to call functions
Can you write Python code for it?

def my_function():
    print("Hello From My Function!")

def my_function_with_args(username, greeting):
    print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))

def sum_two_numbers(a, b):
    return a + b

# print(a simple greeting)
my_function()

#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
my_function_with_args("John Doe", "a great year!")

# after this line x will hold the value 3!
x = sum_two_numbers(1,2)


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
List Comprehension
This technique comes in handy and is often used.
Can you write Python code for it?

power_of_twos = [2**k for k in xrange(0,10)]
print power_of_twos

[k for k in xrange(0,10)]

[i*j for i in xrange(1,11) for j in xrange(1,11)]

[ [i*j for i in xrange(1,11) ] for j in xrange(1,11)]


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Lists
Lists are construct for holding multiple objects or values of different types (if this makes sense). We can dynamically add, replace, or remove elements from a list.
Usually we iterate through list in order to perform some operations, though, we can also address a specific element by its position (index) in the list.
The + and * operators work on lists in a similar way as they do on strings.
Complete documentation at https://docs.python.org/2/tutorial/datastructures.html
Can you write Python code for it?

mylist = [1, 2, "three", ("a", 7)]
print len(mylist)
print mylist

mylist[0]

mylist + [7, 8, 8]

mylist * 2


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Methods
Methods are very similar to functions with the difference that, typically, a method associated with an objects.
Can you write Python code for it?

s = "Hello WORLD"
type(s)

s.swapcase()

len()

3.75.as_integer_ratio()


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Operators
Some the arithmetic operators can be applied to strings, though they have a different interpretation
- + will concatenate two strings
- * multiplies a string with an integer, i.e. the result is that many copies of the original string.
- % has a very special purpose to fill in values into strings
Python provides a large number of operations to manipulate text strings. Examples are given at https://www.tutorialspoint.com/python/python_strings.htm
For the complete documentation refer to https://docs.python.org/2/library/string.html
Can you write Python code for it?


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Python uses indentation to define code blocks, instead of brackets. The standard Python indentation is 4 spaces, although tabs and any other space size will work, as long as it is consistent. Notice that code blocks do not need any termination.
Here is an example for using Python's "if" statement using code blocks:
if <statement is true>:
    <do something>
    ....
    ....
elif <another statement is true>: # else if
    <do something else>
    ....
    ....
else:
    <do another thing>
    ....
    ....
Can you write Python code for it?

x = 3

if x == 2:
    print("x equals two!")
    print("x equals two! ... again")
else:
    print("x does not equal to two.")
print "done?"


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
String Formatting
Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".
Some basic argument specifiers you should know:
%s - String (or any object with a string representation, like numbers)
%d - Integers
%f - Floating point numbers
%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
Can you write Python code for it?


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
Strings
Strings are defined either with a single quote or a double quotes. Many other languages interpret them differently.
Can you write Python code for it?

mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
The "in" operator could be used to check if a specified object exists within an iterable object container, such as a list:
Can you write Python code for it?

name = "John"
if name in ["John", "Rick"]:
    print("Your name is either John or Rick.")


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)
Can you write Python code for it?

my_String99 = 'Don\'t worry about apostrophes'
print(my_String99)


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
To define a floating point number, you may use one of the following notations:
Can you write Python code for it?

myfloat = 7.0
print myfloat, type(myfloat)
myfloat = float(42)
print myfloat, type(myfloat)


I am working on the file "02-Python/Introduction_class.ipynb".
The first task is:
can we use "else" clause for loops?
Can you write Python code for it?

# Prints out 0,1,2,3,4 and then it prints "count value reached 5"

count=0
while(count<5):
    print(count)
    count +=1
else:
    print("count value reached %d" %(count))

# Prints out 1,2,3,4
for i in range(1, 10):
    if(i%5==0):
        break
    print(i)
else:
    print("this is not printed because for loop is terminated because of break but not due to fail in condition")


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
"while" loops
While loops repeat as long as a certain boolean condition is met. For example:
Can you write Python code for it?

count = 0
while count < 5:
    print(count)
    count += 1  # This is the same as count = count + 1

print 'done!'

## compute the Greatest Common Denominator (GCD)
a = 15
b = 12

while a!=b:
    # put smaller number in a
    (a, b) = (a, b) if a<b else (b, a)
    b = b - a

print "The GCD is %d"%a

def fac(n):
    f = 1
    for m in range(1,n+1):
        f *= m
    print "%d! is %d" % (n,f)

fac(5)


count = 0
while count < 5:
    print(count)
    count += 1  # This is the same as count = count + 1

## compute the Greatest Common Denominator (GCD)
a = 15
b = 12

while a!=b:
    # put smaller number in a
    (a, b) = (a, b) if a<b else (b, a)
    b = b - a

print "The GCD is %d"%a


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
A statement is evaulated as true if one of the following is correct:
    1. The "True" boolean variable is given, or calculated using an expression, such as an arithmetic comparison.
    2. An object which is not considered "empty" is passed.
Here are some examples for objects which are considered as empty:
    1. An empty string: ""
    2. An empty list: []
    3. The number zero: 0
    4. The false boolean variable: False
Can you write Python code for it?

# In-class exercise:
# Modify the following code to produce a function "is_even" that takes an argument x and prints
# "x is even" if it is even, and "x is not even" otherwise.
# If you call is_even(1), you should see "1 is not even"

def is_even(x):
    if x % 2==0:
        str2 = ""
    else:
        str2 = "not"
    outstr = "%d is %s even" % (x, str2)
    print outstr

def is_even2(x)
    print "%d is " % x +("" if x % 2 == 0 else "not ")+"even"

is_even(121)
is_even(4)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
A statement is evaulated as true if one of the following is correct: 1. The "True" boolean variable is given, or calculated using an expression, such as an arithmetic comparison. 2. An object which is not considered "empty" is passed.
Here are some examples for objects which are considered as empty: 1. An empty string: "" 2. An empty list: [] 3. The number zero: 0 4. The false boolean variable: False
Loops
There are two types of loops in Python, for and while.
The "for" loop
For loops iterate over a given sequence. Here is an example:
    primes = [2, 3, 5, 7]
    for prime in primes:
        print(prime)
For loops can iterate over a sequence of numbers using the range and xrange functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange). Note that the range function is zero based.
Can you write Python code for it?

# Prints out the numbers 0,1,2,3,4
for x in range(5):
    print(x)

# Prints out 3,4,5
for x in range(3, 6):
    print(x)

# Prints out 3,5,7
for x in range(3, 8, 2):
    print(x)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
As a side note, you can use "\" to "escape" special characters. Here's another way to deal with apostrophes:
Can you write Python code for it?

mystring = 'You still don\'t have to worry about apostrophes'
print(mystring)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Conditions
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Notice that variable assignment is done using a single equals operator "=", whereas comparison between two variables is done using the double equals operator "==". The "not equals" operator is marked as "!=".
Can you write Python code for it?

x = 2
print(x == 2) # prints out True
print(x == 3) # prints out False
print(x <= 3) # prints out True
print(x >= 3) # prints out True


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Functions and methods
Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.
As we have seen on previous tutorials, Python makes use of blocks.
A block is a area of code of written in the format of:
    block_head:
        1st block line
        2nd block line
Where a block line is more Python code (even another block), and the block head is of the following format: block_keyword block_name(argument1,argument2, ...) Block keywords you already know are "if", "for", and "while".
Functions in python are defined using the block keyword "def", followed with the function's name as the block's name. For example:
Can you write Python code for it?

def my_function():
    print("Hello From My Function!")

my_function()


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Functions may also receive arguments (variables passed from the caller to the function). For example:
Can you write Python code for it?

def my_function_with_args(username, greeting):
    print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))

my_function_with_args("Peter", "a wonderful day")


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Functions may return a value to the caller, using the keyword- 'return' . For example:
Can you write Python code for it?

def sum_two_numbers(a, b=1):
    print "a = "+str(a)
    print "b = "+str(b)
    return a + b

x = sum_two_numbers(b=5,a=19)

x

def my_fun(xi, yi):
    xi[4] = 0
    return yi*2

x = range(10)
y = 2
print x
print y

print "running my_fun"

my_fun(x,y)

print(x)
print(y)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
How to call functions
Can you write Python code for it?

def my_function():
    print("Hello From My Function!")

def my_function_with_args(username, greeting):
    print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))

def sum_two_numbers(a, b):
    return a + b

# print(a simple greeting)
my_function()

#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
my_function_with_args("John Doe", "a great year!")

# after this line x will hold the value 3!
x = sum_two_numbers(1,2)

def my_fun2(x):
    return x, x**2, x**3

y1, y2, y3 = my_fun2(5)
print y1
print y2
print y3


def my_function():
    print("Hello From My Function!")

def my_function_with_args(username, greeting):
    print("Hello, %s , From My Function!, I wish you %s"%(username, greeting))

def sum_two_numbers(a, b):
    return a + b

# print(a simple greeting)
my_function()

#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
my_function_with_args("John Doe", "a great year!")

# after this line x will hold the value 3!
x = sum_two_numbers(1,2)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
In this exercise you'll use an existing function,
and while adding your own to create a fully functional program.

Add a function named list_benefits() that returns the following list of strings: "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"

Add a function named build_sentence(info) which receives a single argument containing a string and returns a sentence starting with the given string and ending with the string " is a benefit of functions!"

Run and see all the functions work together!
Can you write Python code for it?

# Modify this function to return a list of strings as defined above
def list_benefits():
    pass

# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
    pass

def name_the_benefits_of_functions():
    list_of_benefits = list_benefits()
    for benefit in list_of_benefits:
        print(build_sentence(benefit))

name_the_benefits_of_functions()


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
List Comprehension
List comprehension can be thought of as a more "pythonic" way to loop over a list. It is a technique that comes in handy and is often used.
Can you write Python code for it?

x = list(xrange(0,10))
print x

# # One way to get y = 2^x:
x = xrange(0,10)
y1 = []
for element in x:
    y1.append(2**element) # note the indent here! whitespace matters!
print "y1 = "+str(y1)

# a more "pythonic way"
y2 = [2**element for element in x]
print "y2 = "+str(y2)

[(i,j) for i in xrange(1,4) for j in xrange(1,4)]

[ [(i,j) for i in xrange(1,4) ] for j in xrange(1,4)]

# in-class exercise:
# complete the code below to plot y = 3*sin(x/3):

import math as ma
import matplotlib.pyplot as plt
% matplotlib inline

x = xrange(-20,20)
y = [3.*ma.sin(xi/3.) for xi in x] # your code goes here!
plt.plot(x,y)

# plt.plot(*zip(*[(x,3.*ma.sin(x/3.)) for x in xrange(-20,20)]))


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Lists
Lists are construct for holding multiple objects or values of different types (if this makes sense). We can dynamically add, replace, or remove elements from a list.
Usually we iterate through list in order to perform some operations, though, we can also address a specific element by its position (index) in the list.
The + and * operators work on lists in a similar way as they do on strings.
Complete documentation at https://docs.python.org/2/tutorial/datastructures.html
Can you write Python code for it?

mylist = [1, 2, "three", ("a", 7)]
print len(mylist)
print mylist

mylist[3]

mylist + [7]

mylist * 2


mylist = [1, 2, "three", ("a", 7)]
print len(mylist)
print mylist

mylist[3]

mylist[0]

mylist[-2]

mylist + [7]

mylist * 2


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Loops
There are two types of loops in Python, for and while.
The "for" loop
For loops iterate over a given sequence. Here is an example:
    primes = [2, 3, 5, 7]
    for prime in primes:
        print(prime)
For loops can iterate over a sequence of numbers using the range and xrange functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange). Note that the range function is zero based.
Can you write Python code for it?

for x in ['bob','mary','joe']:
    print x

# Prints out the numbers 0,1,2,3,4
for x in range(5):
    print(x)

print('--------------')

# Prints out 3,4,5
for x in range(3, 6):
    print(x)

print('--------------')

# Prints out 3,5,7
for x in range(3, 8, 2):
    print(x)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Python uses indentation to define code blocks, instead of brackets. The standard Python indentation is 4 spaces, although tabs and any other space size will work, as long as it is consistent. Notice that code blocks do not need any termination.
Here is an example for using Python's "if" statement using code blocks:
if <statement is true>:
    <do something>
    ....
    ....
elif <another statement is true>: # else if
    <do something else>
    ....
    ....
else:
    <do another thing>
    ....
    ....
Can you write Python code for it?

x = 2
if x == 2:
    print("x equals two!")
else:
    print("x does not equal to two.")


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
String Formatting
Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".
Some basic argument specifiers you should know:
%s - String (or any object with a string representation, like numbers)
%d - Integers
%f - Floating point numbers
%.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
Can you write Python code for it?

7./13.

print "The magic number is %.2f!" % (7.0/13.0)

# In-class exercise:
# Define output_str so that calling myadd(3, 5) displays a text "3 plus 5 is 8".

def myadd(a,b):
    c = a+b
    output_str = "%f plus %f is %f" % (a,b,c) # yay, done!
    print(output_str)

myadd(3, 5)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
Strings
Strings are defined either with a single quote or a double quotes. Many other languages interpret them differently.
Can you write Python code for it?

mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
The "and", "or" and "not" boolean operators allow building complex boolean expressions, for example:
Can you write Python code for it?

name = "John"
age = 23
if name == "John" and age == 23:
    print("Your name is John, and you are also 23 years old.")
else:
    print("You are not John.")

if name == "John" or name == "Rick":
    print("Your name is either John or Rick.")


name = "John"
age = 23
if name == "John" and age == 23:
    print("Your name is John, and you are also 23 years old.")

if name == "John" or name == "Rick":
    print("Your name is either John or Rick.")


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
The "in" operator could be used to check if a specified object exists within an iterable object container, such as a list:
Can you write Python code for it?

name = "John"
if name in ["John", "Rick"]:
    print("Your name is either John or Rick.")


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)
Can you write Python code for it?

mystring = "Don't worry about apostrophes"
print(mystring)


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
To define a floating point number, you may use one of the following notations:
Can you write Python code for it?

myfloat = 7.0
print myfloat, type(myfloat)
myfloat = float(42)
print myfloat, type(myfloat)

print 7/2
print 7./2


I am working on the file "02-Python/Introduction_orig.ipynb".
The first task is:
can we use "else" clause for loops?
Can you write Python code for it?

# Prints out 0,1,2,3,4 and then it prints "count value reached 5"

count=0
while(count<5):
    print(count)
    count +=1
else:
    print("count value reached %d" %(count))

# Prints out 1,2,3,4
for i in range(1, 10):
    if(i%5==0):
        break
    print(i)
else:
    print("this is not printed because for loop is terminated because of break but not due to fail in condition")


I am working on the file "02-Python1/02-Python-1-Lists_Instructor.ipynb".
The first task is:
There is one important difference between lists and strings: we can change the values in a list, but we cannot change the characters in a string. For example:
Can you write Python code for it?

names = ['Newton', 'Darwing', 'Turing'] # typo in Darwin's name
print('names is originally:', names)
names[1] = 'Darwin' # correct the name
print('final value of names:', names)


I am working on the file "02-Python1/02-Python-1-Lists_Instructor.ipynb".
The first task is:
and if we loop over a list, the loop variable is assigned elements one at a time:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1-Logic_Instructor.ipynb".
The first task is:
Checking out Data
Now that we’ve seen how conditionals work, we can use them to check for the suspicious features we saw in our inflammation data. In the first couple of plots, the maximum inflammation per day seemed to rise like a straight line, one unit per day.
We can check for this inside the for loop we wrote with the following conditional:
python
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
    print('Suspicious looking maxima!')
We also saw a different problem in the third dataset; the minima per day were all zero (looks like a healthy person snuck into our study). We can also check for this with an elif condition:
python
elif data.min(axis=0).sum() == 0:
    print('Minima add up to zero!')
And if neither of these conditions are true, we can use else to give the all-clear:
python
else:
    print('Seems OK!')
lets test that out then!
Can you write Python code for it?

import numpy as np

data = np.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
    print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
    print('Minima add up to zero!')
else:
    print('Seems OK!')

data = numpy.loadtxt(fname='inflammation-03.csv', delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
    print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
    print('Minima add up to zero!')
else:
    print('Seems OK!')


I am working on the file "02-Python1/02-Python-1-Logic_Instructor.ipynb".
The first task is:
One important thing to notice in the code above is that we use a double equals sign == to test for equality rather than a single equals sign because the latter is used to mean assignment.
We can also combine tests using and and or. and is only true if both parts are true:
Can you write Python code for it?

if (1 > 0) and (-1 > 0):
    print('both parts are true')
else:
    print('at least one part is false')


I am working on the file "02-Python1/02-Python-1-Logic_Instructor.ipynb".
The first task is:
The second line of this code uses the keyword if to tell Python that we want to make a choice. If the test that follows the if statement is true, the body of the if (i.e., the lines indented underneath it) are executed. If the test is false, the body of the else is executed instead. Only one or the other is ever executed.
Conditional statements don’t have to include an else. If there isn’t one, Python simply does nothing if the test is false:
Can you write Python code for it?

num = 53
print('before conditional...')
if num > 100:
    print('53 is greater than 100')
print('...after conditional')


I am working on the file "02-Python1/02-Python-1-Logic_Instructor.ipynb".
The first task is:
We can also chain several tests together using elif, which is short for “else if”. The following Python code uses elif to print the sign of a number.
Can you write Python code for it?

num = -3

if num > 0:
    print(num, "is positive")
elif num == 0:
    print(num, "is zero")
else:
    print(num, "is negative")


I am working on the file "02-Python1/02-Python-1-Logic_Instructor.ipynb".
The first task is:
while or is true if at least one part is true:
Can you write Python code for it?

if (1 < 0) or (-1 < 0):
    print('at least one test is true')


I am working on the file "02-Python1/02-Python-1-Loops_Instructor.ipynb".
The first task is:
Note also that finding the length of a string is such a common operation that Python actually has a built-in function to do it called len:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1-Loops_Instructor.ipynb".
The first task is:
The improved version uses a for loop to repeat an operation—in this case, printing—once for each thing in a collection. The general form of a loop is:
python
for variable in collection:
    do things with variable
or
```python
for page in book:
    read(page)
````
We can call the loop variable anything we like, but there must be a colon at the end of the line starting the loop, and we must indent anything we want to run inside the loop. Unlike many other languages, there is no command to end a loop (e.g. end for); what is indented after the for statement belongs to the loop.
We can use for loops to count things by increasing variables:
Can you write Python code for it?

length = 0
for vowel in 'aeiou':
    length = length + 1
print('There are', length, 'vowels')


I am working on the file "02-Python1/02-Python-1-Loops_Instructor.ipynb".
The first task is:
This is a bad approach for two reasons:

It doesn’t scale: if we want to print the characters in a string that’s hundreds of letters long, we’d be better off just typing them in.

It’s fragile: if we give it a longer string, it only prints part of the data, and if we give it a shorter one, it produces an error because we’re asking for characters that don’t exist.

word = 'tin'
print(word[0])
print(word[1])
print(word[2])
print(word[3])
Here's a better approach
Can you write Python code for it?

word = 'oxygen'
for char in word:
    print(char)


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:

 Indexing

What may also surprise you is that when Python displays an array, it shows the element with index [0, 0] in the upper left corner rather than the lower left. This is consistent with the way mathematicians draw matrices, but different from the Cartesian coordinates. The indices are (row, column) instead of (column, row) for the same reason, which can be confusing when plotting data.

We can also index a specific row or column. This can my done by using a colon in our indexing. For example, p_data[:,15] will give us column 15:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:

 Tip

You can use the `%whos` command at any time to see what variables you have created and what modules you have loaded into the computers memory. As this is an IPython command, it will only work if you are in an IPython terminal or the Jupyter Notebook.

Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
Arrays
Now back to the patients. We have the patient infomation stored in memory as the variable p_data, and we can check it's still there with type, lets find out more about the array.
Can you write Python code for it?

print("The data from the file is now {0} and has attibutes {1}".format(type(p_data), p_data.shape))


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
Arrays also know how to perform common mathematical operations on their values. The simplest operations with data are arithmetic: add, subtract, multiply, and divide. When you do such operations on arrays, the operation is done on each individual element of the array. Thus:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
Arrays and Statistics
Often, we want to do more than add, subtract, multiply, and divide values of data. Arrays also know how to do more complex operations on their values. If we want to find the average inflammation for all patients on all days, for example, we can just ask the array for its mean value
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
As a quick check, we can ask this array what its shape is:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
Do some fancy printing with them, throwing in a bit of string usage too.

 Print Formatting

We use `.format` at the end of a string to insert variables which are not strings into a string. If we have multiple items to insert then we use `{0}, {1}, {2}... etc` and order them accordinglt in the format `()`.

Can you write Python code for it?

print("The temperature of this spam is {0} please take it back".format(temp))


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
Here, we have put the average per day across all patients in the variable ave_inflammation, then asked matplotlib.pyplot to create and display a line graph of those values. The result is roughly a linear rise and fall, which is suspicious: based on other studies, we expect a sharper rise and slower fall. Let’s have a look at two other statistics:
Can you write Python code for it?

max_plot = matplotlib.pyplot.plot(p_data.max(axis=0))
matplotlib.pyplot.show(max_plot)

min_plot = matplotlib.pyplot.plot(p_data.min(axis=0))
matplotlib.pyplot.show(min_plot)


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
Let’s take a look at the average inflammation over time:
Can you write Python code for it?

ave_infl = p_data.mean(axis=0)
ave_plot = matplotlib.pyplot.plot(ave_infl)
matplotlib.pyplot.show(ave_plot)


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
So lets examine this. By typing numpy.loadtxt() we're saying, from the numpy library use the loadtxt function. It is the equivalent of selecting a file from a directory, or saying thing.component, for example, car.tyres might say, from the car I would like to inspect the tyres.
loadtxt() is a function call, which in this case has two arguments passed to it. The filename and the delimiter, both need to be input as character strings.
Strings
Character strings are sequences of characters enclosed in either "" or ''. We typically use these in keyword argument calls to function, as above, and output to the console, more of which later.
Now if we type the variable the file is attached to into the interpreter, we see the data held within it as an array, with the delimiter , seperating all the values.
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
The expression (40,) tells us we have an N×1 vector, so this is the average inflammation per day for all patients. If we average across axis 1 (columns in our 2D example), we get:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
The maximum value rises and falls perfectly smoothly, while the minimum seems to be a step function. Neither result seems particularly likely, so either there’s a mistake in our calculations or something is wrong with our data.
Subplots
You can group similar plots in a single figure using subplots. This script below uses a number of new commands.
The function matplotlib.pyplot.figure() creates a space into which we will place all of our plots.
The parameter figsize tells Python how big to make this space. Each subplot is placed into the figure using the subplot command. The subplot command takes 3 parameters. The first denotes how many total rows of subplots there are, the second parameter refers to the total number of subplot columns, and the final parameters denotes which subplot your variable is referencing.
Each subplot is stored in a different variable (axes1, axes2, axes3). Once a subplot is created, the axes are can be titled using the set_xlabel() command (or set_ylabel()). Here are our three plots side by side:
Can you write Python code for it?

import numpy
import matplotlib.pyplot

data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')

fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))

axes1 = fig.add_subplot(1, 3, 1)
axes2 = fig.add_subplot(1, 3, 2)
axes3 = fig.add_subplot(1, 3, 3)

axes1.set_ylabel('average')
axes1.plot(data.mean(axis=0))

axes2.set_ylabel('max')
axes2.plot(data.max(axis=0))

axes3.set_ylabel('min')
axes3.plot(data.min(axis=0))

fig.tight_layout()

matplotlib.pyplot.show(fig)


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
These two commands tell us that the variable is a NumPy array and then the extent of the array. In this case the rows are individual patients and the columns are their daily inflammation measurements. In this case we have 60 rows and 40 columns, known as the dimensions of the array. Using these attributes we can index the data to extract single data values:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
Thus we can make the domain smaller and essentially crop the array. By using the colon between the two limits:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
We can also use variables to define other variables
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
We're going to demonstrate how to use the NumPy library with some statistics on arthuritis patient inflammmation. We can import csv files, where the data currently is, like so:
Can you write Python code for it?

p_data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
What if we need the maximum inflammation for all patients, or the average for each day? In this case we need to average across an 'axis' of the array i.e. in x or y if we consider the data as a 2D array.
To support this, most array methods allow us to specify the axis we want to work on. If we ask for the average across axis 0 (rows in our 2D example), we get:
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
When analyzing data, though, we often want to look at partial statistics, such as the maximum value per patient or the average value per day. One way to do this is to create a new temporary array of the data we want, then ask it to do the calculation:
Can you write Python code for it?

patient_0 = p_data[0, :] # 0 on the first axis, everything on the second
print( 'maximum inflammation for patient 0:', patient_0.max())


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
and p_data[15,:] gives us the 15 row. The : says give me all the elements in this domain.
Can you write Python code for it?


I am working on the file "02-Python1/02-Python-1.ipynb".
The first task is:
and then we can change the temperature in Kelvin
Can you write Python code for it?


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
2D selection after interpolation
The code below takes a low-resolution 2D input and interpolates the data to a higher resolution.  The nature of the data, with high value at the edge of the domain, results in a poor interpolation output.   To obtain a better result, I mirrored the data across $x=0$  to obtain better values around $x=0$ (first graph).  The new data set is interpolated but now has values for $x<0$, which were required for interpolation but are not part of the valid result.  These additional values had to be truncated from the data.  This was done in the following code:
select = xnew.__ge__(0)
xnew = xnew[select].reshape(znew.shape[0],-1)
ynew = ynew[select].reshape(znew.shape[0],-1)
znew = znew[select].reshape(znew.shape[0],-1)

where the first line returns a binary array with ones where the condition is true and zeros elsewhere.  This binary array is used to slice the mirrored data; but the returned value is a (-1,)-shaped array.  The array had to be reshaped to obtain the 2D form required.  The result is shown in the second graph.
Can you write Python code for it?


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
A recurring pattern in the use of pyradi is the integration of a two-dimensional dataset along one of the dimensions. In most programming languages, this would require at least one or possibly two loops over the dimensions.  The numpy.meshgrid function has the potential of trading the complexity of looping against the complexity of loop abstraction.
In the example below the inband blackbody radiance over a spectral detector is calculated using
$
L_{\rm inband} = \int_0^\infty R_\lambda L_\lambda(T) d\lambda \approx \sum_{0.1 \mu{\rm m}}^{7 \mu{\rm m}} R_\lambda L_\lambda(T) d\lambda
$
The wavelength spectral domain (shape (S,1)) is calculated from a short wavelength up to 7 $\mu$m, covering the detector spectral band.  The detector spectral response (shape (S,1)) is calculated using using a function from pyradi.ryutils. A range of source temperatures is defined (shape (T,1)).  The same spectral range used to calculate the detector is used to calculate the blackbody radiance, using pyradi.ryplanck.planck.  The value returned by ryplanck.planck is a two-dimensional (shape (S,T)) array with spectral variation across the rows (axis=0) and temperature variation across the columns (axis=1).  The challenge is now to multiply each column in the radiance table with the spectral responsivity, without using a loop.  The numpy.meshgrid function is used to calculate an array with the responsivity variation along the rows, but with exactly the same values in all columns. Once this new array is available, it is multiplied with the radiance array. After multiplication the columns in the new array contain the product of $R_\lambda L_\lambda(T)$, with each column at a different temperature.  The summation/integral is then performed by using numpy.trapz and integrating along axis=0 (rows), which is the direction of spectral variation.  The resulting vector is the integral over all wavelengths, for the different temperatures.
Can you write Python code for it?

import pyradi.ryutils as ryutils
import pyradi.ryplanck as ryplanck
wavelength = np.linspace(0.1, 7, 305).reshape(-1, 1)
detector = ryutils.responsivity(wavelength, 6.1, 1, 15, 1.0)
temperatures = np.linspace(280, 1000, 41)
spectralBaseline = ryplanck.planck(wavelength,temperatures,'el') / np.pi
_, detVar = np.meshgrid(temperatures, detector)
spectralWeighted = spectralBaseline * detVar
inbandRadiance = np.trapz(spectralWeighted, wavelength, axis=0 )

p = ryplot.Plotter(2,1,3,figsize=(18,4))
p.plot(1,wavelength,detector,'Detector responsivity','Wavelength $\mu$m', 'Responsivity A/W',pltaxis=[0.5,7,0,0.8])
p.plot(2,temperatures,inbandRadiance,'Inband radiance vs temperature','Temperature K', 'Inband radiance W/(m$^2$.sr)')


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Accessing files on the internet
The Python urllib2 package makes is relatively easy to access a file on the internet.  See also the functions
ryfiles.unzipGZipfile,
ryfiles.untarTarfile, and
ryfiles.downloadFileUrl.
Python functions
Can you write Python code for it?

def square(value):
    return value ** 2

print(square(5))
print(square(5.))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Arrays can be reshaped to different shapes, even ranks, provided the number of elements don't change. This code creates a rank-three array filled with zeros. The first parameter of this function must be a tuple of the required size, in this case 3x3x4.  The array is initially flattened, made into a rank-one array.  Using the reshape function it is then reshaped to different sizes (this is only of illustrative use, no real mathematical or physical meaning). If one of the reshape parameters is -1, it is interpreted that this axis must be made whatever value fits the reshaping.
Can you write Python code for it?

a = np.zeros((3,3,4))
print(a.shape)
b = a.flatten()
print(b.shape)
b = b.reshape(18,-1)
print(b.shape)
b = b.reshape(3,-1,3)
print(b.shape)
b = b.reshape(-1,1)
print(b.shape)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Calculate the inverse of the Fick matrix. Rotation matrices are symmetrical, hence the inverse is the same as the transpose.
The product of three inverse matrices does not calculate correctly, so it was necessary to simplify the inverse. A second simplification was necessary to simplify the equations to the results shown below.
Can you write Python code for it?

R3I =   sympy.trigsimp(sympy.trigsimp(sympy.Inverse(Rz_s())))
R2I =   sympy.trigsimp(sympy.trigsimp(sympy.Inverse(Ry_s())))
R1I =   sympy.trigsimp(sympy.trigsimp(sympy.Inverse(Rx_s())))

R1I * R2I * R3I


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Changing the shape of an array
https://docs.scipy.org/doc/numpy-dev/user/quickstart.html#changing-the-shape-of-an-array
The order of the elements in the array resulting from ravel() is normally 'C-style', that is, the rightmost index 'changes the fastest', so the element after a[0,0] is a[0,1]. If the array is reshaped to some other shape, again the array is treated as 'C-style'. Numpy normally creates arrays stored in this order, so ravel() will usually not need to copy its argument, but if the array was made by taking slices of another array or created with unusual options, it may need to be copied. The functions ravel() and reshape() can also be instructed, using an optional argument, to use FORTRAN-style arrays, in which the leftmost index changes the fastest.
Can you write Python code for it?

import numpy as np
a = np.floor(10*np.random.random((3,4)))
print(a)
print(a.shape)
b = a.ravel() # flatten the array
print(b)
a.shape = (6, 2)
print(a.T)
a = a.reshape(4,3)
print(a)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Context Managers
From http://jeffknupp.com/blog/2016/03/07/python-with-context-managers/:
"We need a convenient method for indicating a particular variable has some cleanup associated with it, and to guarantee that cleanup happens, no matter what. Given that requirement, the syntax for using context managers makes a lot of sense:
with something_that_returns_a_context_manager() as my_resource:
    do_something(my_resource)
    ...
    print('done using my_resource')

That's it! Using with, we can call anything that returns a context manager (like the built-in open() function). We assign it to a variable using ... as <variable_name>. Crucially, the variable only exists within the indented block below the with statement. Think of with as creating a mini-function: we can use the variable freely in the indented portion, but once that block ends, the variable goes out of scope. When the variable goes out of scope, it automatically calls a special method that contains the code to clean up the resource."
HTTP server
The following starts a simple HTTP server in the current working directory.
# Python 3.x
$ python3 -m http.server

# Python 2.x
$ python -m SimpleHTTPServer 8000

See also here:
https://docs.python.org/3/library/http.server.html
https://docs.python.org/2.7/library/basehttpserver.html
and here:
https://gist.github.com/bradmontgomery/2219997
https://www.acmesystems.it/python_httpd
For more sophisticated applications consider one of the many full stack Python web frameworks such as Django, or a microframework such as Flask.
https://wiki.python.org/moin/WebFrameworks
https://www.airpair.com/python/posts/django-flask-pyramid
Hardware interfacing
Serial ports
Python can be used to interface to hardware, one such example investigated here is using the serial port to communicate.  The pyserial module provides this capability, see the documentation. It is on the Anaconda distribution but not included in the standard download.  To install type conda install pyserial  at the prompt in a command...
Can you write Python code for it?

try:
    import pyradi.ryutils as ryutils
    print(ryutils.VersionInformation('matplotlib,numpy,pyradi,scipy,pandas'))
except:
    print("pyradi.ryutils not found")


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Control flow in Python uses for or while.
The enumerate function is a very useful means to iterate over a list or tuple and get the index as well as the list value at that index.
The while loop executes as long as the conditional in the while statement is true. If the conditional is false, the else clause is executed.
Can you write Python code for it?

a = [ 'a', 'b', 'c', ]
for item in a:
    print(item)
print('-------------')

strTmp = 'This string'
for i,item in enumerate(strTmp):
    print('character {} is {}'.format(i,item))
print('-------------')

for i,item in enumerate(a):
    print('index {} has value {}'.format(i,item))
print('-------------')

while False:
    print(a)
else:
    print('it is False!!')
print('-------------')

count = 0
while count < 5:
    print(f'{count} is  less than 5')
    count = count + 1


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Creating Numpy arrays
There are many more ways to create arrays we are only using one method.  See http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
Numpy essentially provides arrays and matrices. A matrix is the numpy equivalent of a linear algebra matrix, while the array is just a structured set of data. Matrices and arrays both have N-dimensional structure, but the operations on these are different. Numpy arrays perform operations on element-wise level (unless otherwise told), which means that the multiplication of two numpy arrays yield a new array of the same size where each element is the multiplication of the elements in the same location of the two input arrays.
Numpy uses the linear algebra term, rank to denote the array number of dimensions. Rank-two arrays can only appear in one guise, that of an MxN array.  A vector (one-dimensional array) can appear in three different guises, each of which is slightly different: (N,) (N,1) or (1,N) - and these different forms don't interoperate very well (as they seem to do in Matlab).
For example the transpose of (N,) is still (N,) because (N,) is a rank-one array, for which the transpose is not defined. So when using vectors (and we do use them extensively) be careful of this this difference.
Numpy arrays have a shape attribute that returns the size of the array. For rank-two arrays, the first value can be interpreted as the number of rows, then columns.
Can you write Python code for it?

a = np.asarray([1, 2, 3]) # construct from a list
print(a.shape)
print(np.ones(a.shape).shape)

b = np.array(a) #construct from another array
print(b.shape)
print(np.zeros(a.shape).shape)

c = np.linspace(0,10,11) # 11 elements, ranging from 0 to 10
print(c.shape)

d = np.eye(3)
print(d.shape)
print(d)

vis = np.asarray(4)
print(vis)
print(type(vis))
print(vis.shape)
visr = vis.reshape(-1,)
print(visr)
print(type(visr))
print(visr.shape)

vis = np.asarray([4])
print(vis)
print(type(vis))
print(vis.shape)

vis = np.asarray([[4],[6]])
print(vis)
print(type(vis))
print(vis.shape)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Dictionaries dic={key1:val1, key2:val2} are like arrays, but the emements can be identified by any data type. In the example below we can extract values from the dictionary based on strings, but these key values can be any type type, even an object or a function.  The key values of the dictionary are available by calling the keys() function of the dictionary.
The Collections module adds the capability to ease the use of dictionaries, see
http://docs.python.org/2/library/collections.html
Can you write Python code for it?

dicBand = {'NIR':'1-2', 'SWIR':'1.5-3', 'MWIR':'3-5',}
dicBand['LWIR']='8-12'
dicBand['VIS']='0.4-0.7'

print(dicBand)
print(dicBand['LWIR'])
print('-------------')

for key in dicBand:
    print('key={} value={}'.format(key,dicBand[key]))
print(' ')
print('-------------')

for key in sorted(dicBand.keys()):
    print('key={} value={}'.format(key,dicBand[key]))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Docstrings and introspection
From PEP-0257: "A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object." Docstrings are considered the documentation for that module, function or class. It is normally written in a multiline comment. An example:
def add(x, y):
    """Return the sum of x and y.
    """
    return x + y

Introspection is when you access an objects docstring from within a development environment. For example, while you are using the function you want to read its documenation.  Introspection is implemented in IPython by typing a question mark immediately after e.g., a function name and running the cell.  The doc string will appear in a new panel at the bottom of the IPython browser page. For example when you type and run
import pyradi.ryutils as ryutils
ryutils.abshumidity?

The docstring is displayed as shown below.
http://legacy.python.org/dev/peps/pep-0257/
http://www.pythonforbeginners.com/basics/python-docstrings
Can you write Python code for it?


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Don't use mutable objects as default function parameters
Python will create the mutable object (default parameter) the first time the function is defined (i.e. the function is created or imported) - not when it is called.
http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb?create=1
Can you write Python code for it?

def append_to_list(value, def_list=[]):
    def_list.append(value)
    return def_list

my_list = append_to_list(1)
print(my_list)

my_other_list = append_to_list(2)
print(my_other_list)

import time
def report_arg(my_default=time.time()):
    print(my_default)

report_arg()
time.sleep(1)
report_arg()


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Element-wise conditionals with Numpy
Avoid divide-by-zero errors in np.log
Use the where parameter of numpy's ufuncs:
numpy.log(x,
    out=None, where=True, casting='same_kind', order='K', dtype=None,
        subok=True[, signature, extobj])
            = <ufunc 'log'>

out ndarray, None, or tuple of ndarray and None, optional
A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
where array_like, optional
This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
Use it like this:
res = np.log2(m, out=np.zeros_like(m), where=(m!=0))

No RuntimeWarning is raised, and
- log(m) [the ufunc] is used for elements where the condition is True
- out is assigned to out=np.zeros_like(m) for elements where the condition is False (the log is not computed)
out=np.zeros_like(m) is quite important. The output might look alright when you forget this, but you will be using uninitialized memory.
https://stackoverflow.com/questions/21752989/numpy-efficiently-avoid-0s-when-taking-logmatrix/52209380
https://numpy.org/doc/stable/reference/ufuncs.html
Can you write Python code for it?

import numpy as np
m = np.array([[1., 0], [2, 3]])
res = np.log2(m)

m = np.array([[1., 0], [2, 3]])
res = np.log2(m, out=np.zeros_like(m), where=(m!=0))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
From http://www.programiz.com/python-programming/operator-overloading: Class functions that begins with double underscore (__) are called special functions in Python. To overload the + sign, we will need to implement __add__() function in the class.  What actually happens is that, when you do p1 + p2, Python will call p1.__add__(p2) which in turn is Point.__add__(p1,p2).   The reference gives a complete list of operators that can be overloaded, including: Addition + __add__,
Subtraction - __sub__,
Multiplication * __mul__,
Power ** __pow__,
Division / __truediv__,
Floor Division // __floordiv__,
Remainder  (modulo) % __mod__,
Bitwise Left Shift << __lshift__,
Bitwise Right Shift >> __rshift__,
Bitwise AND & __and__,
Bitwise OR | __or__,
Bitwise XOR ^ __xor__, and
Bitwise NOT ~ __invert__.
Python does not limit operator overloading to arithmetic operators only. We can overload comparison operators as well.  Comparison operators that can be overloaded include:
Less than <  __lt__,
Less than or equal to   <= __le__,
Equal to ==  __eq__,
Not equal to !=  __ne__,
Greater than > __gt__, and
Greater than or equal to >= __ge__
Can you write Python code for it?

class Point:
    def __init__(self,x = 0,y = 0):
        self.x = x
        self.y = y

    def __str__(self):
        return "({0},{1})".format(self.x,self.y)

    def __lt__(self,other):
        self_mag = (self.x ** 2) + (self.y ** 2)
        other_mag = (other.x ** 2) + (other.y ** 2)
        return self_mag < other_mag

    def __add__(self,other):
        x = self.x + other.x
        y = self.y + other.y
        return Point(x,y)

p1 = Point(2,3)
p2 = Point(-1,2)
print(p1 + p2)
print(Point(1,1) < Point(-2,-3))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Functions can have named parameters where values are assigned by name. Furthermore, function parameters can define default values to be used if the user does not supply a value.
Can you write Python code for it?

def power(value, power=2):
    return value ** power

print(power(value=5))
print(power(value=5, power=4))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Functions can return anything, here it returns (1) a tuple of values, (2) a function that can be called. If you only want some of the values in the returned tuple, indicate those to ignore with underscore _.
Can you write Python code for it?

def square(value):
    dicStr = {0:'zero',2:'two',5:'five',}
    rtnval = (value, value ** 2, dicStr[value])
    return rtnval

print(square(5))
print(square(5)[1])
print(square(5)[-1])
print('-------------')

def squarefn():
    return square
print(squarefn()(5))
print('-------------')

_, _, rtn = squarefn()(5)
print(rtn)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Further experimentation with array indexes with more complex logical patterns. We first create some data with no real physical significance, but all arrays have the same length, as if these are all time samples on the same timeline a.  Two selections are made, the first based only on the value of b and the second based on the value of b and c. These selections are index arrays with zeros where the conditions are not met, and ones where the conditions are met. The selecx index arrays are then used to extract only the required values from the a, b, c arrays when plotting.  To make the selected regions easier to spot, they are slightly shifted up or down. The plots are for the following selection filtering definitions:

All values
b in the range (18,75)
b in the range (18,30) and c in the range [0,infinity]
b in the range (15,75) and c in the range [0,infinity], but at intervals of 1 on a axis.

You can stick any number of logical tests inside the np.all().
Can you write Python code for it?

%matplotlib inline
import numpy as np
import pyradi.ryutils as ryutils
import pyradi.ryplot as ryplot

a = np.linspace(0,10,2001)
b = a ** 2.
c = 10 * np.sin(b/(5 * np.pi))
select0 = np.all([ b > 18., b<75], axis=0)
select1 = np.all([ b > 18., b<30, c>=0], axis=0)
select2 = np.all([ a%1==0, b > 15., b<75, c>0], axis=0)
#the next line builds a complex selection of three sections from the larger set.
select3 = np.logical_or(np.logical_or(np.all([ a>1, a<2], axis=0), np.all([ a>4, a<5], axis=0) ),
                        np.all([ a>6, a<7], axis=0))

print(select3)
print('a size {}'.format(a.shape))
print('a for (b > 18., b<75) size {}'.format(a[select0].shape))
print('a for (b > 18., b<30, c>=0) size {}'.format(a[select1].shape))
print('a for (a%1==0, b > 15., b<75, c>0) size {}'.format(a[select2].shape))
print('a for (a>1, a<2, d>4, d<5 ) size {}'.format(a[select3].shape))

p = ryplot.Plotter(1,figsize=(9,4))
p.plot(1,a,b,'', 'a', 'y', label=['b : all values'])
p.plot(1,a,c,'', 'a', 'y', label=['c : all values'])
p.plot(1,a[select0],b[select0]+2, label=['b+2 :  b > 15., b<75'])
p.plot(1,a[select0],c[select0]+2,label=['c+2 :  b > 15., b<75'])
p.plot(1,a[select1],b[select1]-2,label=['b-2 : b > 15., b<75, c>0'])
p.plot(1,a[select1],c[select1]-2,label=['c-2 : b > 15., b<75, c>0'])
p.plot(1,a[select2],b[select2]-4,label=['b-4 : a%1==0, b > 15., b<75, c>0'],markers='x')
p.plot(1,a[select2],c[select2]-4,label=['c-4 : a%1==0, b > 15., b<75, c>0'],markers='x')

p.plot(1,a,5 * select3 * c-4,label=['c selectively multiplied'])


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Generators, iterators and yield --- more detail to follow.
http://anandology.com/python-practice-book/iterators.html
http://www.jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/
https://wiki.python.org/moin/Generators
http://www.bogotobogo.com/python/python_generators.php
http://zetcode.com/lang/python/itergener/
http://nbviewer.ipython.org/github/empet/pytwist/blob/master/Generators-and-Dynamical-Systs.ipynb
Functions as objects
A function name is just a name bound to a function object and can be handled just as any other name.  Study the following code to see how the predicate function is passed as a function parameter.  This example counts the number of times that a certain condition ocurred.
https://www.jeffknupp.com/blog/2013/11/29/improve-your-python-decorators-explained/
Can you write Python code for it?

def is_even(value):
    """Return True if *value* is even.
    """
    #this is a comment
    return (value % 2) == 0

print(is_even(4))
print(is_even(5))

def count_occurrences(target_list, predicate):
    """Return the number of times applying the callable *predicate* to a
    list element returns True.
    """
    return sum([1 for e in target_list if predicate(e)])

my_predicate = is_even
my_list = [2, 4, 6, 7, 9, 11]
result = count_occurrences(my_list, my_predicate)
print(result)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Get the one'th element of the list at six'th location in the list
Can you write Python code for it?


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Get the third element of the list just preceding it
Can you write Python code for it?


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Here is another example, the function name is an entry into a dictionary.
http://code.activestate.com/recipes/65126-dictionary-of-methodsfunctions/
Can you write Python code for it?

import string

def function1(i):
    print('called function 1 with {}'.format(i))

def function2(i):
    print('called function 2 with {}'.format(i))

tokenDict = {"cat":function1, "dog":function2}

# simulate, say, lines read from a file
lines = ["cat","cat","dog"]

for i,line in enumerate(lines):
    tokenDict[line](i)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
If we don't know what method to call on a class, we can use a dispatch method to determine it at runtime.
Can you write Python code for it?

print(str)

class Switcher(object):
    def numbers_to_methods_to_strings(self, argument):
        """Dispatch method"""
        # prefix the method_name with 'number_' because method names
        # cannot begin with an integer.
        method_name = 'number_' + str(argument)
        # Get the method from 'self'. Default to a lambda.
        method = getattr(self, method_name, lambda: "nothing")
        # Call the method as we return it
        return method()

    def number_0(self):
        return "zero"

    def number_1(self):
        return "one"

    def number_2(self):
        return "two"

sw = Switcher()

print(sw.numbers_to_methods_to_strings(0),sw.numbers_to_methods_to_strings(2))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
In NumPy, there are several flavors of array containing one element.
1. A rank-0 array with shape ().
1. A rank-1 array with shape (1,).
1. A rank-2 array with shape (1,1).
Note that using an empty tuple () as index/slice gives you all the elements in the dataset, as an array object for rank-1 and higher arrays, and as a scalar element for rank-0 arrays.
The form [...] or  [()] is in fact the only way you can get the value of a rank-0 array, because [0] is not allowed for a rank-0 array.
Expanded from the original writeup in  Python and HDF5 by Andrew Collette.
Can you write Python code for it?

a = 3 * np.array(1) #rank 0
print('a.shape of a rank-0 array with single element: {}'.format(a.shape))
print('a[0] is not allowed for rank-0 with shape ()')
print('a returns the full array with all dimensions included (a scalar): {}'.format(a))
print('a[...] returns the full array with all dimensions included (a scalar): {}'.format( a[...]))
print('a[()] returns the full array with all dimensions included (a scalar): {}\n'.format( a[()]))

a = 5 * np.array([1])#rank 1
print('a.shape of a rank-1 array with single element: {}'.format(a.shape))
print('a[0] returns the value of the 0th element: {}'.format( a[0]))
print('a[...] returns the full array with all dimensions included: {}'.format( a[...]))
print('a[()] returns the full array with all dimensions included: {}\n'.format( a[()]))

a = 7 * np.ones((1,1))#rank 2
print('a.shape of a rank-2 array with single element: {}'.format(a.shape))
print('a[0] returns the value of the 0th row: {}'.format( a[0]))
print('a[0,0] returns the value of the 0th col of the 0th row: {}'.format( a[0,0]))
print('a[...] returns the full array with all dimensions included: {}'.format( a[...]))
print('a[()] returns the full array with all dimensions included: {}'.format( a[()]))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
In Python 3, you can use the Ellipsis literal ... as a 'nop' placeholder for code:
def will_do_something():
    ...

Finally a tongue-in-the-cheek application of ellipsis is found here
Can you write Python code for it?


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
In Python, (almost) everything is an object. What we commonly refer to as "variables"
in Python are more properly called names. Likewise, "assignment" is really the binding
of a name to an object. Each binding has a scope that defines its visibility, usually
the block in which the name originates.
https://www.jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/

If an existing object is bound to a new name, changing the object by using the new name will also change the value object bound to the original name, because it is the same object (just bound to different names). To create a truly new object use the copy() function (for some data types).  Slicing (:) lists and tuples creates a copy of the original structure (see later).
https://docs.python.org/2/library/copy.html
http://effbot.org/pyfaq/how-do-i-copy-an-object-in-python.htm
http://pymotw.com/2/copy/
Can you write Python code for it?

import copy

a = [1, 2, 3, ]
b = a
c = copy.copy(a) # shallow copy
# c = copy.deepcopy(a) # deep (recursive) copy
print('a={}'.format(a))
print('b={}'.format(b))
print('c={}'.format(c))
print('----------------------')
b[2] = 'z'
print('a={}'.format(a))
print('b={}'.format(b))
print('c={}'.format(c))
print('----------------------')
b = a[:]  # slicing all makes a copy of 'a' then bind 'b' to this copy
b[2] = 'b'
c[2] = 'c'
print('a={}'.format(a))
print('b={}'.format(b))
print('c={}'.format(c))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
In the following example a is a rank-one array and b is a rank-two array. The b array can be transposed, but transpose operator is not defined for the rank-one array.
Can you write Python code for it?

a = np.asarray([1, 2, 3]) # construct from a list
print('array a has {} elements, shape {}'.format(a.shape[0],a.shape))
print('array a.T has {} elements, shape {}'.format(a.T.shape[0],a.T.shape))
print('a is equal to a.T: {}'.format(np.array_equal(a,a.T)))
print(' ')
b = a.reshape(-1,1)
print('array b has {} rows and {} columns, shape {}'.format(b.shape[0], b.shape[1], b.shape))
print('array b.T has {} rows and {} columns, shape {}'.format(b.T.shape[0], b.T.shape[1], b.T.shape))
print('b is equal to b.T: {}'.format(np.array_equal(b,b.T)))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Inspect
The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.
You need to import the module file or create an instance of a class to be able to use inspect.
The getmembers() function retrieves the members of an object such as a class or module. The sixteen functions whose names begin with is are mainly provided as convenient choices for the second argument to getmembers().
https://docs.python.org/2/library/inspect.html
https://stackoverflow.com/questions/12994366/using-inspect-getmembers
https://stackoverflow.com/questions/33204160/listing-all-class-members-with-python-inspect-module
Can you write Python code for it?

##
# get a list of all functions in a module file

import inspect

#import the module you want to inspect
import pyradi.rytarggen as rytarggen

# get all function members of the module
all_functions = inspect.getmembers(rytarggen, inspect.isfunction)

# build a string with all the functions in quotes with commas separating
pstr = ""
for item in all_functions:
    pstr += "'{}', ".format(item[0])

print(pstr)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
List/dictionary comprehension is a shorthand way to define a list/dictionary. In the first example create a new list of square root values of items from an old list, but only for even values.  In the second examples a dictionary is built using dictionary comprehension. Note that zip takes two lists and create a new list where elements of the new list are tuples with matching elements from the two input lists - pair them up element by element, much like a zip closing up.
http://carlgroner.me/Python/2011/11/09/An-Introduction-to-List-Comprehensions-in-Python.html
http://www.secnetix.de/olli/Python/list_comprehensions.hawk
http://docs.python.org/2.7/library/functions.html
http://howchoo.com/g/how-to-use-list-comprehension-in-python
http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
Can you write Python code for it?

import math
nlist = [0, 1, 2, 3, 4]
newl = [math.sqrt(i) for i in nlist if not i%2]
print(newl)

keys = ['a', 'n', 'z']
values = ['testing', 'this', 'dict']
d = {key: value for (key, value) in zip(keys, values)}
print(d)

print(zip(keys, values))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Modulus operator on floats
The modulus operator (returning the fraction of a division) also works for floats, returning the fractional remainder.
Can you write Python code for it?

a = np.linspace(5.5, 9.5, 9)
b = a % 2
print(a)
print(b)
print(b==0)
print(a[b==0])


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Moving and deleting files
Through pathlib, you also have access to basic file system level operations like moving, updating, and even deleting files. For the most part, these methods do not give a warning or wait for confirmation before information or files are lost. Be careful when using these methods.
To move a file, use .replace(). Note that if the destination already exists, .replace() will overwrite it. To avoid possibly overwriting the destination path, the simplest is to test whether the destination exists before replacing:
if not destination.exists():
    source.replace(destination)

Directories and files can be deleted using .rmdir() and .unlink() respectively.
When you are renaming files, useful methods might be .with_name() and .with_suffix(). They both return the original path but with the name or the suffix replaced, respectively.  Then call the .replace() method
Can you write Python code for it?

path = Path.cwd() / 'test001.txt'
print(path)
if path.exists():
    print('executing .replace()')
    path.replace(path.with_suffix('.py'))
else:
    print(f'{path} does not exist')


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Named Tuples:  collections.namedtuple
https://docs.python.org/2/library/collections.html#collections.namedtuple
collections.namedtuple(typename, field_names[, verbose=False][, rename=False])
Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable.
The field_names are a sequence of strings or single string with each fieldname separated by whitespace and/or commas.
Any valid Python identifier may be used for a fieldname except for names starting with an underscore. Valid identifiers consist of letters, digits, and underscores but do not start with a digit or underscore and cannot be a keyword such as class, for, return, global, pass, print, or raise.
If renameis true, invalid fieldnames are automatically replaced with positional names. For example, ['abc', 'def', 'ghi', 'abc'] is converted to ['abc', '_1', 'ghi', '_3'], eliminating the keyword def and the duplicate fieldname abc.
If verbose is true, the class definition is printed just before being built.
Standard tuples use integer indexes to access the tuple members.  This is very easy to use, but it becomes cumbersome to remember which integer index associates with which member:
Can you write Python code for it?

# http://pymotw.com/2/collections/namedtuple.html
bob = ('Bob', 30, 'male')
print('Representation:{}'.format(bob))

jane = ('Jane', 29, 'female')
print('\nField by index: {}'.format(jane[0]))

print('\nFields by index:')
for p in [ bob, jane ]:
    # use tuple unpacking to unpack the tuple in to its elements
    print('{} is a {} year old {}'.format(*p))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
None is a singleton, meaning that there is only one instance of the variable, visible everywhere.
You test for None by using is or is not because you are testing against the singleton handle.
Can you write Python code for it?

a = None
b = 5
print(a is None, b is not None, b is None)


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Note that .parent returns a new Path object, whereas the other properties return strings. This means for instance that .parent can be chained as in the last example or even combined with / to create completely new paths. .parent.parent work back one more parent level.
Can you write Python code for it?

p = Path(cwd) / 'README.md'
print(p)
print(p.name)
print(p.parent)
print(p.stem)
print(p.suffix)
print(p.anchor)
print(p.parent.parent / ('mynewname' + p.suffix))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Numpy Basics
Before numpy can be used it must be imported into the workspace. Note that it is already pre-loaded in IPython, but not in other scipts.  By general convention, it is normally imported as np.
Working with NumPy arrays.
Numpy performance tricks
Can you write Python code for it?


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Numpy array application for spectral calculations
Numpy is much more than just array-like containers, there is also a comprehensive number of mathematical and logical operators to process the array contents.  Numpy arrays are used as a basis for other packages such as the image processing library scikit-image and the data analysis package pandas.  Numpy also forms the basis of the pyradi package, but serves mainly as a container for spectral values.
In pyradi spectral variables are respresented as arrays (of all ranks).  For example, a set of wavelength values can be represented as an array. Spectral variables such as optics transmittance have values at specific wavelengths. So the transmittance vector and wavelength vector correspond on an element-by-element basis: the value of transmittance at the specific transmittance. The example below defines a wavelength band from 300 nm to 800 nm in 101 samples, and then calculates a filter transmittance using pyradi. The filter response is then plotted. IPython presents the graph in the document itself because the %matplotlib inline magic is used.
Can you write Python code for it?

%matplotlib inline
import numpy as np
import pyradi.ryutils as ryutils
import pyradi.ryplot as ryplot

wl = np.linspace(0.3, 0.8, 101)
sfilter = ryutils.sfilter(wl, 0.5, 0.2, 6, 0.5, 0.1)
p = ryplot.Plotter(1,figsize=(9,4))
p.plot(1,wl,sfilter,'Filter Transmittance', 'Wavelength $\mu$m', 'Transmittance')
p.saveFig('filter.png')


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Numpy arrays can be written to raw binary format using the array.tofile('filename') function.  This is not the binary numpy format, but raw bytes with no header. These file are not portable across endian boundaries.
Array pre-allocation instead of stacking
Stacking is a slow process, use with care for large arrays. Another way would be to calculate the size of the array beforehand and pre-allocate an array of that size. Once the array exists, the values can be assigned by slicing.  In this case the number of rows must remain the same, but a new column will be added to allow space for the spectral vector and the radiance array.
Can you write Python code for it?

newsize = (radiance.shape[0], radiance.shape[1] + 1)
ndata = np.zeros(newsize)

print('Original data shape is {}'.format(data.shape))
print('Newly assigned data shape is {}'.format(ndata.shape))

ndata[:,:1] = wl.reshape(-1,1) #first column
ndata[:,1:] = radiance # second column onwards

print('The stacked and pre-allocated arrays are exactly the same {}'.format(np.array_equal(data, ndata)))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Numpy integration and meshgrid
Integration in Numpy is quite easy, the function numpy.trapz does all the work:
Can you write Python code for it?

for n in [4, 11, 101, 1001, 100001]:
    t = np.linspace(0,  np.pi/2, n)
    s = np.sin(t)
    int = np.trapz(s, t, axis=0)
    print('Integral of sin over [0,pi/2] in {} samples is {} '.format(n,int))


I am working on the file "02-PythonWhirlwindCheatSheet.ipynb".
The first task is:
Numpy meshgrid
The numpy.meshgrid function takes two vectors and returns two two-dimensional arrays that loop the vector values along the two orthogonal dimensions. The returned arrays are the same for any form of input vectors, (N,), (1,N), or (N,1).
This is best illustrated by an example
Can you write Python code for it?

valCols = np.asarray([1, 2, 3, 4, 5])
valRows = np.asarray([10, 20, 30])
varCol, varRow = np.meshgrid(valCols, valRows)
print('valCols vector is  (shape={}) \n{}'.format(valCols.shape,valCols))
print('valRows vector is  (shape={}) \n{}'.format(valRows.shape,valRows))
print('varCol array is (shape={})\n{}'.format(varCol.shape,varCol))
print('varRow array is  (shape={})\n{}'.format(varRow.shape,varRow))