Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Python - Software Carpentry at Koper 2018

Views: 158
Kernel: Python 3
import numpy
import matplotlib.pyplot
matplotlib.pyplot.plot(data_ave) matplotlib.pyplot.show()
Image in a Jupyter notebook
data = numpy.loadtxt(fname='./data/inflammation-03.csv', delimiter=',') data_ave = numpy.mean(data, axis=0) data_max = numpy.max(data, axis=0) data_min = numpy.min(data, axis=0) matplotlib.pyplot.plot(data_ave, label='average') matplotlib.pyplot.plot(data_max, label='max') matplotlib.pyplot.plot(data_min, label='min') matplotlib.pyplot.ylabel('inflammation') matplotlib.pyplot.xlabel('day') matplotlib.pyplot.title('inflammation-03.csv') matplotlib.pyplot.legend() matplotlib.pyplot.show()
Image in a Jupyter notebook
word = 'oxygen' for char in word: print(char)
o x y g e n
vowels = 0 for vowel in 'aiueo': vowels = vowels + 1 print("Now at",vowel) print("Vowels: ",vowels)
Now at o Vowels: 5
print("Vowels: ", len('aiueo'))
Vowels: 5
for n in range(1,4): print(n)
1 2 3
result = 1 for p in range(3): result = result * 5 print(result) print(5 ** 3)
125 125
xs = [1,2,3] for i, x in enumerate(xs): print("i=", i, ", x=", x)
i= 0 , x= 1 i= 1 , x= 2 i= 2 , x= 3
cc = [2, 4, 3] x = 5 result = 0 for i, c in enumerate(cc): result = result + c * x**i print(result)
97
odds = [1, 3, 5, 7] print("odds are", odds)
odds are [1, 3, 5, 7]
print("first odd", odds[0])
first odd 1
print("second to last odd", odds[-2])
second to last odd 5
for number in odds: print(number)
1 3 5 7
names = ["Curie", "Darwing", "Turing"] print("names are", names)
names are ['Curie', 'Darwing', 'Turing']
names[1] = "Darwin"
print("names are now", names)
names are now ['Curie', 'Darwin', 'Turing']
name = "Darxin"
name[3] = "w"
----------------------------------------- TypeErrorTraceback (most recent call last) <ipython-input-53-a52eec4d30d4> in <module>() ----> 1 name[3] = "w" TypeError: 'str' object does not support item assignment
salsa = ["peppers", "onions", "cilantro", "tomatoes"] my_salsa = list(salsa) my_salsa[0] = "hot peppers" print("my salsa",my_salsa)
my salsa ['hot peppers', 'onions', 'cilantro', 'tomatoes']
print("original salsa", salsa)
original salsa ['peppers', 'onions', 'cilantro', 'tomatoes']
x = [["pepper", "zucchini", "onion"], ["cabbage", "lettuce", "garlic"], ["apple", "pear", "banana"]] print(x[0])
['pepper', 'zucchini', 'onion']
print([x[0]])
[['pepper', 'zucchini', 'onion']]
print(x[0][0])
pepper
x = [1, 2.0, 'ABC', [1, "D"]] print(x)
[1, 2.0, 'ABC', [1, 'D']]
x[0] = 'more pepper' print(x)
['more pepper', 2.0, 'ABC', [1, 'D']]
x.append("append this") print(x)
['more pepper', 2.0, 'ABC', [1, 'D'], 'append this']
del x[2]
print(x)
['more pepper', 2.0, [1, 'D'], 'append this']
x.reverse() print(x)
['append this', [1, 'D'], 2.0, 'more pepper']
x.pop() print(x)
['append this', [1, 'D'], 2.0]
odds = [1,3,5,7] primes = list(odds) primes.append(2) print("primes are",primes)
primes are [1, 3, 5, 7, 2]
print("odds are",odds)
odds are [1, 3, 5, 7]
print("h" + "e")
he
word = "hello" result = [] for char in word: result.append(char) print(result)
['h', 'e', 'l', 'l', 'o']
result2 = "" for char in result: result2 = result2 + char print(result2)
hello
string_for_slicing = "Observation date: 02-Feb-2013" list_for_slicing = [["fluorine", "F"],["chlorine", "Cl"], ["bromine", "Br"], ["iodine", "I"], ["astatine", "At"]] print(string_for_slicing[-4:]) print(list_for_slicing[-4:])
2013 [['chlorine', 'Cl'], ['bromine', 'Br'], ['iodine', 'I'], ['astatine', 'At']]
%cd /home/jule/proj/stim
/home/jule/proj/stim
import glob
files = glob.glob('./data/inflammation-*.csv') files.sort() for f in files: data = numpy.loadtxt(fname=f, delimiter=',') data_ave = numpy.mean(data, axis=0) data_max = numpy.max(data, axis=0) data_min = numpy.min(data, axis=0) matplotlib.pyplot.plot(data_ave, label='average') matplotlib.pyplot.plot(data_max, label='max') matplotlib.pyplot.plot(data_min, label='min') matplotlib.pyplot.ylabel('inflammation') matplotlib.pyplot.xlabel('day') matplotlib.pyplot.title(f) matplotlib.pyplot.legend() matplotlib.pyplot.show()
Image in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebook
files = glob.glob('./data/inflammation-*.csv') files.sort() data0 = numpy.loadtxt(fname=files[0], delimiter=',') data1 = numpy.loadtxt(fname=files[1], delimiter=',') data0_ave = numpy.mean(data0, axis=0) data1_ave = numpy.mean(data1, axis=0) diff = data0_ave - data1_ave matplotlib.pyplot.plot(diff) matplotlib.pyplot.title("Difference of averages") matplotlib.pyplot.show()
Image in a Jupyter notebook
filenames = glob.glob('./data/inflammation*.csv') composite_data = numpy.zeros((60,40)) for f in filenames: data = numpy.loadtxt(fname=f, delimiter=',') composite_data += data composite_data /= len(filenames) data_ave = numpy.mean(composite_data, axis=0) data_max = numpy.max(composite_data, axis=0) data_min = numpy.min(composite_data, axis=0) matplotlib.pyplot.plot(data_ave, label='average') matplotlib.pyplot.plot(data_max, label='max') matplotlib.pyplot.plot(data_min, label='min') matplotlib.pyplot.ylabel('inflammation') matplotlib.pyplot.xlabel('day') matplotlib.pyplot.title('inflammation-averaged.csv') matplotlib.pyplot.legend() matplotlib.pyplot.show()
Image in a Jupyter notebook
num = 37 if num > 100: print("greater") else: print("not greater")
not greater
num = 53 if num > 100: print("greater") elif num == 0: print("it's zero") elif num == 42: print("it's the answer") else: print("?")
?
if (1 > 0) or (-1 < 0): print("one of them is true (or both)") else: print("none is true")
one of them is true (or both)
data = numpy.loadtxt(fname="./data/inflammation-01.csv", delimiter=',') data_max = numpy.max(data, axis=0) data_min = numpy.min(data, axis=0) if data_max[0] == 0 and data_max[20] == 20: print("This data has suspicious maxima") elif numpy.sum(data_min) == 0: print("Minima add up to 0") else: print("Seems Ok")
This data has suspicious maxima
if not '': print('not (empty string) is true') if 'word': print('word is true') if []: print('empty list is true') if [1, 2, 3]: print('non-empty list is true') if 0: print('zero is true') if -1: print('-one is true')
not (empty string) is true word is true non-empty list is true -one is true
3 > 2 > 1
True
3 > 2 < 4 "==" 3 > 2 and 2 < 4
True
a = data_max[0] b = data_max[1] #if .9*b < a < 1.1*b: if abs(b-a) <= .1*b: print("True") else: print("False")
False
def fahr_to_celsius(temp): return ((temp - 32) * (5/9))
print("freezing point of water", fahr_to_celsius(32),"C") print("boiling point of water", fahr_to_celsius(212),"C")
freezing point of water 0.0 C boiling point of water 100.0 C
def celsius_to_kelvin(temp_c): return temp_c + 273.15
print("freezing point of water", celsius_to_kelvin(0),"K")
freezing point of water 273.15 K
def fahr_to_kelvin(temp_f): temp_c = fahr_to_celsius(temp_f) temp_k = celsius_to_kelvin(temp_c) return temp_k
print("freezing point of water", fahr_to_kelvin(32),"K")
freezing point of water 273.15 K
def plot_inflammation(f): """ Plot the averages, maxima, and minima of the columns in the file called f. """ data = numpy.loadtxt(fname=f, delimiter=',') data_ave = numpy.mean(data, axis=0) data_max = numpy.max(data, axis=0) data_min = numpy.min(data, axis=0) matplotlib.pyplot.plot(data_ave, label='average') matplotlib.pyplot.plot(data_max, label='max') matplotlib.pyplot.plot(data_min, label='min') matplotlib.pyplot.ylabel('inflammation') matplotlib.pyplot.xlabel('day') matplotlib.pyplot.title(f) matplotlib.pyplot.legend() matplotlib.pyplot.show() def check_data(f): data = numpy.loadtxt(fname=f, delimiter=',') data_max = numpy.max(data, axis=0) data_min = numpy.min(data, axis=0) if data_max[0] == 0 and data_max[20] == 20: print("This data has suspicious maxima") elif numpy.sum(data_min) == 0: print("Minima add up to 0") else: print("Seems Ok") files = glob.glob('./data/inflammation-*.csv') files.sort() for f in files: plot_inflammation(f) check_data(f)
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
Minima add up to 0
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
Minima add up to 0
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
This data has suspicious maxima
Image in a Jupyter notebook
Minima add up to 0
Image in a Jupyter notebook
This data has suspicious maxima
help(plot_inflammation)
Help on function plot_inflammation in module __main__: plot_inflammation(f) Plot the averages, maxima, and minima of the columns in the file called f.