This commit is contained in:
madrocketsci committed 2026-11-07 20:19:25 -05:00
1 parent 88624bc20a
commit 81d46a7d32
24 files changed
+1335

No files matched your search

@@ -0,0 +1,4 @@
this,file,contains,comma,separated,data
1,2,3,4,5,6
2.0,0,0,0,0,0
2.5,1,0,0,0,0
1 this file contains comma separated data
2 1 2 3 4 5 6
3 2.0 0 0 0 0 0
4 2.5 1 0 0 0 0
+170
View File
@@ -0,0 +1,170 @@
#!/usr/bin/env python3
import os,sys,math
import numpy as np
import matplotlib.pyplot as plt
###########################
## Basic filesystem stuff##
###########################
print(os.getcwd())
flist = os.listdir()
#a list of files within your current working directory
for I in range(0,len(flist)): #iterate with an index going from 0 to len(flist)
print(flist[I]) #print each element of the file list
if(os.path.isfile(flist[I])):
print("{} is a file".format(flist[I]))
if(os.path.isdir(flist[I])):
print("{} is a folder".format(flist[I]))
ap = os.path.abspath(flist[I]) #returns the absolute path as a string
print("abs path is {}:".format(ap))
## joining and splitting paths and filenames
d1 = os.getcwd() #get your current working directory which should be "a_working_folder"
f2 = os.path.join("subfolder","file.txt") #this is joining two path fragments
#should result in "subfolder/file.txt"
print(f2)
f3 = os.path.join(d1,f2)
print(f3)
#should result in "~/Desktop/a_working_folder/subfolder/file.txt" or something to that effect
#both of these should work if you are currently working from a_working_folder
try:
fp = open(f2,"r")
#this is the "raw" way of opening text files for reading in python
#you're probably going to be using pandas for most things, but open is useful for
#using things like pickle, or parsing text files yourself
print("{} was found and opened successfully.".format(f2))
except:
print("For some reason, {} could not be found".format(f2))
#do stuff with the data
fp.close() #close your file pointer
try:
fp = open(f3,"r")
#this is the "raw" way of opening text files for reading in python
#you're probably going to be using pandas for most things, but open is useful for
#using things like pickle, or parsing text files yourself
print("{} was found and opened successfully.".format(f3))
except:
print("For some reason, {} could not be found".format(f3))
#do stuff with the data
fp.close() #close your file pointer
#This should not work if you are currently working from a_working_folder
f4 = "file.txt"
try:
fp = open(f4,"r")
#this is the "raw" way of opening text files for reading in python
#you're probably going to be using pandas for most things, but open is useful for
#using things like pickle, or parsing text files yourself
print("{} was found and opened successfully.".format(f4))
except:
print("For some reason, {} could not be found".format(f4))
#do stuff with the data
fp.close() #close your file pointer
#This should work if you are currently working from a_working_folder
f5 = "../a_different_project_of_yours/a_third_file.csv"
try:
fp = open(f5,"r")
#this is the "raw" way of opening text files for reading in python
#you're probably going to be using pandas for most things, but open is useful for
#using things like pickle, or parsing text files yourself
print("{} was found and opened successfully.".format(f5))
except:
print("For some reason, {} could not be found".format(f5))
#do stuff with the data
ln = " "
I = 0
while(ln!=""):
ln = fp.readline()
lns = ln.split(",") #split the string ln anywhere there is a comma
for J in range(0,len(lns)):
print("{:d},{:d}: {}".format(I,J,lns[J]))
I+=1
fp.close() #close your file pointer
#suppose you wanted to do something with a piece of f5, such as get just the filename or the directory root
d5list = os.path.split(f5) #this is going to return a two element list with ["../a_different_project_of_yours/", "a_third_file.csv"]
d5 = d5list[0]
f5b = d5list[1]
#Now let's suppose you want to check your extension
ext5 = os.path.splitext(f5b)[1] #this separates out the ".csv" from the "a_third_file.csv"
#This is equivalent to two steps
thing1 = os.path.splitext(f5b) #a two element list
ext5 = thing1[1] #select the second element from the list
#Here is a file listing function that searches a folder for all files with a particular extension
def myflist(pth, ext):
flst = os.listdir(pth) #find all the files and folders in pth
flst2 = [] #what we will eventually output
for I in range(0,len(flst)):
#(you could also do something like for f in flst, if you don't want the index)
f = flst[I]
f2 = os.path.join(pth,f)
#here I am prepending the path onto the filename so that the files can be found from wherever we are working
b1 = False #a native python boolean variable (for logic)
if(os.path.isfile(f2)):
b1 = True
b2 = False
f2ext = os.path.splitext(f2)[1] #pull the extension off of f2
if(f2ext.lower() == ext.lower()):
#f2ext and ext should both be strings
#python string objects have various helper methods like .split, or .lower, or .find
#.lower() takes the string and returns a new string that is lowercase
#this makes the extension search case-insensitive, in case you want to include both ".png" and ".PNG", for example
b2 = True
if(b1 and b2):
#if it's both a file and has the extension we want
flst2.append(f2) #append the file with the path to a new filelist
return flst2
fl = myflist("./subfolder",".txt")
for f in fl:
print("files with extension .txt")
print(f)
fl = myflist("./subfolder",".png")
for f in fl:
print("files with extension .png")
print(f)
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "./"
}
]
}
+7
View File
@@ -0,0 +1,7 @@
A file containing some text.
1
2
3
4
5
Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

@@ -0,0 +1,3 @@
The other file in this folder is the IGRF-13 dataset for the earth's magnetic field shape.
@@ -0,0 +1,199 @@
# 13th Generation International Geomagnetic Reference Field Schmidt semi-normalised spherical harmonic coefficients, degree n=1,13
# in units nanoTesla for IGRF and definitive DGRF main-field models (degree n=1,8 nanoTesla/year for secular variation (SV))
c/s deg ord IGRF IGRF IGRF IGRF IGRF IGRF IGRF IGRF IGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF DGRF IGRF SV
g/h n m 1900.0 1905.0 1910.0 1915.0 1920.0 1925.0 1930.0 1935.0 1940.0 1945.0 1950.0 1955.0 1960.0 1965.0 1970.0 1975.0 1980.0 1985.0 1990.0 1995.0 2000.0 2005.0 2010.0 2015.0 2020.0 2020-25
g 1 0 -31543 -31464 -31354 -31212 -31060 -30926 -30805 -30715 -30654 -30594 -30554 -30500 -30421 -30334 -30220 -30100 -29992 -29873 -29775 -29692 -29619.4 -29554.63 -29496.57 -29441.46 -29404.8 5.7
g 1 1 -2298 -2298 -2297 -2306 -2317 -2318 -2316 -2306 -2292 -2285 -2250 -2215 -2169 -2119 -2068 -2013 -1956 -1905 -1848 -1784 -1728.2 -1669.05 -1586.42 -1501.77 -1450.9 7.4
h 1 1 5922 5909 5898 5875 5845 5817 5808 5812 5821 5810 5815 5820 5791 5776 5737 5675 5604 5500 5406 5306 5186.1 5077.99 4944.26 4795.99 4652.5 -25.9
g 2 0 -677 -728 -769 -802 -839 -893 -951 -1018 -1106 -1244 -1341 -1440 -1555 -1662 -1781 -1902 -1997 -2072 -2131 -2200 -2267.7 -2337.24 -2396.06 -2445.88 -2499.6 -11.0
g 2 1 2905 2928 2948 2956 2959 2969 2980 2984 2981 2990 2998 3003 3002 2997 3000 3010 3027 3044 3059 3070 3068.4 3047.69 3026.34 3012.20 2982.0 -7.0
h 2 1 -1061 -1086 -1128 -1191 -1259 -1334 -1424 -1520 -1614 -1702 -1810 -1898 -1967 -2016 -2047 -2067 -2129 -2197 -2279 -2366 -2481.6 -2594.50 -2708.54 -2845.41 -2991.6 -30.2
g 2 2 924 1041 1176 1309 1407 1471 1517 1550 1566 1578 1576 1581 1590 1594 1611 1632 1663 1687 1686 1681 1670.9 1657.76 1668.17 1676.35 1677.0 -2.1
h 2 2 1121 1065 1000 917 823 728 644 586 528 477 381 291 206 114 25 -68 -200 -306 -373 -413 -458.0 -515.43 -575.73 -642.17 -734.6 -22.4
g 3 0 1022 1037 1058 1084 1111 1140 1172 1206 1240 1282 1297 1302 1302 1297 1287 1276 1281 1296 1314 1335 1339.6 1336.30 1339.85 1350.33 1363.2 2.2
g 3 1 -1469 -1494 -1524 -1559 -1600 -1645 -1692 -1740 -1790 -1834 -1889 -1944 -1992 -2038 -2091 -2144 -2180 -2208 -2239 -2267 -2288.0 -2305.83 -2326.54 -2352.26 -2381.2 -5.9
h 3 1 -330 -357 -389 -421 -445 -462 -480 -494 -499 -499 -476 -462 -414 -404 -366 -333 -336 -310 -284 -262 -227.6 -198.86 -160.40 -115.29 -82.1 6.0
g 3 2 1256 1239 1223 1212 1205 1202 1205 1215 1232 1255 1274 1288 1289 1292 1278 1260 1251 1247 1248 1249 1252.1 1246.39 1232.10 1225.85 1236.2 3.1
h 3 2 3 34 62 84 103 119 133 146 163 186 206 216 224 240 251 262 271 284 293 302 293.4 269.72 251.75 245.04 241.9 -1.1
g 3 3 572 635 705 778 839 881 907 918 916 913 896 882 878 856 838 830 833 829 802 759 714.5 672.51 633.73 581.69 525.7 -12.0
h 3 3 523 480 425 360 293 229 166 101 43 -11 -46 -83 -130 -165 -196 -223 -252 -297 -352 -427 -491.1 -524.72 -537.03 -538.70 -543.4 0.5
g 4 0 876 880 884 887 889 891 896 903 914 944 954 958 957 957 952 946 938 936 939 940 932.3 920.55 912.66 907.42 903.0 -1.2
g 4 1 628 643 660 678 695 711 727 744 762 776 792 796 800 804 800 791 782 780 780 780 786.8 797.96 808.97 813.68 809.5 -1.6
h 4 1 195 203 211 218 220 216 205 188 169 144 136 133 135 148 167 191 212 232 247 262 272.6 282.07 286.48 283.54 281.9 -0.1
g 4 2 660 653 644 631 616 601 584 565 550 544 528 510 504 479 461 438 398 361 325 290 250.0 210.65 166.58 120.49 86.3 -5.9
h 4 2 -69 -77 -90 -109 -134 -163 -195 -226 -252 -276 -278 -274 -278 -269 -266 -265 -257 -249 -240 -236 -231.9 -225.23 -211.03 -188.43 -158.4 6.5
g 4 3 -361 -380 -400 -416 -424 -426 -422 -415 -405 -421 -408 -397 -394 -390 -395 -405 -419 -424 -423 -418 -403.0 -379.86 -356.83 -334.85 -309.4 5.2
h 4 3 -210 -201 -189 -173 -153 -130 -109 -90 -72 -55 -37 -23 3 13 26 39 53 69 84 97 119.8 145.15 164.46 180.95 199.7 3.6
g 4 4 134 146 160 178 199 217 234 249 265 304 303 290 269 252 234 216 199 170 141 122 111.3 100.00 89.40 70.38 48.0 -5.1
h 4 4 -75 -65 -55 -51 -57 -70 -90 -114 -141 -178 -210 -230 -255 -269 -279 -288 -297 -297 -299 -306 -303.8 -305.36 -309.72 -329.23 -349.7 -5.0
g 5 0 -184 -192 -201 -211 -221 -230 -237 -241 -241 -253 -240 -229 -222 -219 -216 -218 -218 -214 -214 -214 -218.8 -227.00 -230.87 -232.91 -234.3 -0.3
g 5 1 328 328 327 327 326 326 327 329 334 346 349 360 362 358 359 356 357 355 353 352 351.4 354.41 357.29 360.14 363.2 0.5
h 5 1 -210 -193 -172 -148 -122 -96 -72 -51 -33 -12 3 15 16 19 26 31 46 47 46 46 43.8 42.72 44.58 46.98 47.7 0.0
g 5 2 264 259 253 245 236 226 218 211 208 194 211 230 242 254 262 264 261 253 245 235 222.3 208.95 200.26 192.35 187.8 -0.6
h 5 2 53 56 57 58 58 58 60 64 71 95 103 110 125 128 139 148 150 150 154 165 171.9 180.25 189.01 196.98 208.3 2.5
g 5 3 5 -1 -9 -16 -23 -28 -32 -33 -33 -20 -20 -23 -26 -31 -42 -59 -74 -93 -109 -118 -130.4 -136.54 -141.05 -140.94 -140.7 0.2
h 5 3 -33 -32 -33 -34 -38 -44 -53 -64 -75 -67 -87 -98 -117 -126 -139 -152 -151 -154 -153 -143 -133.1 -123.45 -118.06 -119.14 -121.2 -0.6
g 5 4 -86 -93 -102 -111 -119 -125 -131 -136 -141 -142 -147 -152 -156 -157 -160 -159 -162 -164 -165 -166 -168.6 -168.05 -163.17 -157.40 -151.2 1.3
h 5 4 -124 -125 -126 -126 -125 -122 -118 -115 -113 -119 -122 -121 -114 -97 -91 -83 -78 -75 -69 -55 -39.3 -19.57 -0.01 15.98 32.3 3.0
g 5 5 -16 -26 -38 -51 -62 -69 -74 -76 -76 -82 -76 -69 -63 -62 -56 -49 -48 -46 -36 -17 -12.9 -13.55 -8.03 4.30 13.5 0.9
h 5 5 3 11 21 32 43 51 58 64 69 82 80 78 81 81 83 88 92 95 97 107 106.3 103.85 101.04 100.12 98.9 0.3
g 6 0 63 62 62 61 61 61 60 59 57 59 54 47 46 45 43 45 48 53 61 68 72.3 73.60 72.78 69.55 66.0 -0.5
g 6 1 61 60 58 57 55 54 53 53 54 57 57 57 58 61 64 66 66 65 65 67 68.2 69.56 68.69 67.57 65.5 -0.3
h 6 1 -9 -7 -5 -2 0 3 4 4 4 6 -1 -9 -10 -11 -12 -13 -15 -16 -16 -17 -17.4 -20.33 -20.90 -20.61 -19.1 0.0
g 6 2 -11 -11 -11 -10 -10 -9 -9 -8 -7 6 4 3 1 8 15 28 42 51 59 68 74.2 76.74 75.92 72.79 72.9 0.4
h 6 2 83 86 89 93 96 99 102 104 105 100 99 96 99 100 100 99 93 88 82 72 63.7 54.75 44.18 33.30 25.1 -1.6
g 6 3 -217 -221 -224 -228 -233 -238 -242 -246 -249 -246 -247 -247 -237 -228 -212 -198 -192 -185 -178 -170 -160.9 -151.34 -141.40 -129.85 -121.5 1.3
h 6 3 2 4 5 8 11 14 19 25 33 16 33 48 60 68 72 75 71 69 69 67 65.1 63.63 61.54 58.74 52.8 -1.3
g 6 4 -58 -57 -54 -51 -46 -40 -32 -25 -18 -25 -16 -8 -1 4 2 1 4 4 3 -1 -5.9 -14.58 -22.83 -28.93 -36.2 -1.4
h 6 4 -35 -32 -29 -26 -22 -18 -16 -15 -15 -9 -12 -16 -20 -32 -37 -41 -43 -48 -52 -58 -61.2 -63.53 -66.26 -66.64 -64.5 0.8
g 6 5 59 57 54 49 44 39 32 25 18 21 12 7 -2 1 3 6 14 16 18 19 16.9 14.58 13.10 13.14 13.5 0.0
h 6 5 36 32 28 23 18 13 8 4 0 -16 -12 -12 -11 -8 -6 -4 -2 -1 1 1 0.7 0.24 3.02 7.35 8.9 0.0
g 6 6 -90 -92 -95 -98 -101 -103 -104 -106 -107 -104 -105 -107 -113 -111 -112 -111 -108 -102 -96 -93 -90.4 -86.36 -78.09 -70.85 -64.7 0.9
h 6 6 -69 -67 -65 -62 -57 -52 -46 -40 -33 -39 -30 -24 -17 -7 1 11 17 21 24 36 43.8 50.94 55.40 62.41 68.1 1.0
g 7 0 70 70 71 72 73 73 74 74 74 70 65 65 67 75 72 71 72 74 77 77 79.0 79.88 80.44 81.29 80.6 -0.1
g 7 1 -55 -54 -54 -54 -54 -54 -54 -53 -53 -40 -55 -56 -56 -57 -57 -56 -59 -62 -64 -72 -74.0 -74.46 -75.00 -75.99 -76.7 -0.2
h 7 1 -45 -46 -47 -48 -49 -50 -51 -52 -52 -45 -35 -50 -55 -61 -70 -77 -82 -83 -80 -69 -64.6 -61.14 -57.80 -54.27 -51.5 0.6
g 7 2 0 0 1 2 2 3 4 4 4 0 2 2 5 4 1 1 2 3 2 1 0.0 -1.65 -4.55 -6.79 -8.2 0.0
h 7 2 -13 -14 -14 -14 -14 -14 -15 -17 -18 -18 -17 -24 -28 -27 -27 -26 -27 -27 -26 -25 -24.2 -22.57 -21.20 -19.53 -16.9 0.6
g 7 3 34 33 32 31 29 27 25 23 20 0 1 10 15 13 14 16 21 24 26 28 33.3 38.73 45.24 51.82 56.5 0.7
h 7 3 -10 -11 -12 -12 -13 -14 -14 -14 -14 2 0 -4 -6 -2 -4 -5 -5 -2 0 4 6.2 6.82 6.54 5.59 2.2 -0.8
g 7 4 -41 -41 -40 -38 -37 -35 -34 -33 -31 -29 -40 -32 -32 -26 -22 -14 -12 -6 -1 5 9.1 12.30 14.00 15.07 15.8 0.1
h 7 4 -1 0 1 2 4 5 6 7 7 6 10 8 7 6 8 10 16 20 21 24 24.0 25.35 24.96 24.45 23.5 -0.2
g 7 5 -21 -20 -19 -18 -16 -14 -12 -11 -9 -10 -7 -11 -7 -6 -2 0 1 4 5 4 6.9 9.37 10.46 9.32 6.4 -0.5
h 7 5 28 28 28 28 28 29 29 29 29 28 36 28 23 26 23 22 18 17 17 17 14.8 10.93 7.03 3.27 -2.2 -1.1
g 7 6 18 18 18 19 19 19 18 18 17 15 5 9 17 13 13 12 11 10 9 8 7.3 5.42 1.64 -2.88 -7.2 -0.8
h 7 6 -12 -12 -13 -15 -16 -17 -18 -19 -20 -17 -18 -20 -18 -23 -23 -23 -23 -23 -23 -24 -25.4 -26.32 -27.61 -27.50 -27.2 0.1
g 7 7 6 6 6 6 6 6 6 6 5 29 19 18 8 1 -2 -5 -2 0 0 -2 -1.2 1.94 4.92 6.61 9.8 0.8
h 7 7 -22 -22 -22 -22 -22 -21 -20 -19 -19 -22 -16 -18 -17 -12 -11 -12 -10 -7 -4 -6 -5.8 -4.64 -3.28 -2.32 -1.8 0.3
g 8 0 11 11 11 11 11 11 11 11 11 13 22 11 15 13 14 14 18 21 23 25 24.4 24.80 24.41 23.98 23.7 0.0
g 8 1 8 8 8 8 7 7 7 7 7 7 15 9 6 5 6 6 6 6 5 6 6.6 7.62 8.21 8.89 9.7 0.1
h 8 1 8 8 8 8 8 8 8 8 8 12 5 10 11 7 7 6 7 8 10 11 11.9 11.20 10.84 10.04 8.4 -0.2
g 8 2 -4 -4 -4 -4 -3 -3 -3 -3 -3 -8 -4 -6 -4 -4 -2 -1 0 0 -1 -6 -9.2 -11.73 -14.50 -16.78 -17.6 -0.1
h 8 2 -14 -15 -15 -15 -15 -15 -15 -15 -14 -21 -22 -15 -14 -12 -15 -16 -18 -19 -19 -21 -21.5 -20.88 -20.03 -18.26 -15.3 0.6
g 8 3 -9 -9 -9 -9 -9 -9 -9 -9 -10 -5 -1 -14 -11 -14 -13 -12 -11 -11 -10 -9 -7.9 -6.88 -5.59 -3.16 -0.5 0.4
h 8 3 7 7 6 6 6 6 5 5 5 -12 0 5 7 9 6 4 4 5 6 8 8.5 9.83 11.83 13.18 12.8 -0.2
g 8 4 1 1 1 2 2 2 2 1 1 9 11 6 2 0 -3 -8 -7 -9 -12 -14 -16.6 -18.11 -19.34 -20.56 -21.1 -0.1
h 8 4 -13 -13 -13 -13 -14 -14 -14 -15 -15 -7 -21 -23 -18 -16 -17 -19 -22 -23 -22 -23 -21.5 -19.71 -17.41 -14.60 -11.7 0.5
g 8 5 2 2 2 3 4 4 5 6 6 7 15 10 10 8 5 4 4 4 3 9 9.1 10.17 11.61 13.33 15.3 0.4
h 8 5 5 5 5 5 5 5 5 5 5 2 -8 3 4 4 6 6 9 11 12 15 15.5 16.22 16.71 16.16 14.9 -0.3
g 8 6 -9 -8 -8 -8 -7 -7 -6 -6 -5 -10 -13 -7 -5 -1 0 0 3 4 4 6 7.0 9.36 10.85 11.76 13.7 0.3
h 8 6 16 16 16 16 17 17 18 18 19 18 17 23 23 24 21 18 16 14 12 11 8.9 7.61 6.96 5.69 3.6 -0.4
g 8 7 5 5 5 6 6 7 8 8 9 7 5 6 10 11 11 10 6 4 2 -5 -7.9 -11.25 -14.05 -15.98 -16.5 -0.1
h 8 7 -5 -5 -5 -5 -5 -5 -5 -5 -5 3 -4 -4 1 -3 -6 -10 -13 -15 -16 -16 -14.9 -12.76 -10.74 -9.10 -6.9 0.5
g 8 8 8 8 8 8 8 8 8 7 7 2 -1 9 8 4 3 1 -1 -4 -6 -7 -7.0 -4.87 -3.54 -2.02 -0.3 0.4
h 8 8 -18 -18 -18 -18 -19 -19 -19 -19 -19 -11 -17 -13 -20 -17 -16 -17 -15 -11 -10 -4 -2.1 -0.06 1.64 2.26 2.8 0.0
g 9 0 8 8 8 8 8 8 8 8 8 5 3 4 4 8 8 7 5 5 4 4 5.0 5.58 5.50 5.33 5.0 0.0
g 9 1 10 10 10 10 10 10 10 10 10 -21 -7 9 6 10 10 10 10 10 9 9 9.4 9.76 9.45 8.83 8.4 0.0
h 9 1 -20 -20 -20 -20 -20 -20 -20 -20 -21 -27 -24 -11 -18 -22 -21 -21 -21 -21 -20 -20 -19.7 -20.11 -20.54 -21.77 -23.4 0.0
g 9 2 1 1 1 1 1 1 1 1 1 1 -1 -4 0 2 2 2 1 1 1 3 3.0 3.58 3.45 3.02 2.9 0.0
h 9 2 14 14 14 14 14 14 14 15 15 17 19 12 12 15 16 16 16 15 15 15 13.4 12.69 11.51 10.76 11.0 0.0
g 9 3 -11 -11 -11 -11 -11 -11 -12 -12 -12 -11 -25 -5 -9 -13 -12 -12 -12 -12 -12 -10 -8.4 -6.94 -5.27 -3.22 -1.5 0.0
h 9 3 5 5 5 5 5 5 5 5 5 29 12 7 2 7 6 7 9 9 11 12 12.5 12.67 12.75 11.74 9.8 0.0
g 9 4 12 12 12 12 12 12 12 11 11 3 10 2 1 10 10 10 9 9 9 8 6.3 5.01 3.13 0.67 -1.1 0.0
h 9 4 -3 -3 -3 -3 -3 -3 -3 -3 -3 -9 2 6 0 -4 -4 -4 -5 -6 -7 -6 -6.2 -6.72 -7.14 -6.74 -5.1 0.0
g 9 5 1 1 1 1 1 1 1 1 1 16 5 4 4 -1 -1 -1 -3 -3 -4 -8 -8.9 -10.76 -12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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3 0.35 0.38 0.38 0.4 0.0
h 13 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.6 0.53 0.49 0.48 0.5 0.0
g 13 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.1 -0.05 0.02 0.08 0.1 0.0
h 13 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3 0.38 0.44 0.48 0.5 0.0
g 13 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4 0.41 0.42 0.46 0.5 0.0
h 13 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.2 -0.22 -0.25 -0.30 -0.4 0.0
g 13 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0 -0.10 -0.26 -0.35 -0.5 0.0
h 13 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.5 -0.57 -0.53 -0.43 -0.4 0.0
g 13 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1 -0.18 -0.26 -0.36 -0.4 0.0
h 13 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.9 -0.82 -0.79 -0.71 -0.6 0.0
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from numpy import sin,cos,tan,pi,sqrt,linspace,zeros,exp
def plot_simplescatter():
fnout = "plot_simplescatter.png"
x = np.linspace(0,5,10)
y = np.sin(x*2*pi/5)
y2 = np.sin((x-1)*2*pi/5)
y3 = np.sin((x-2)*2*pi/5)
hf = plt.figure(1,figsize=(6,4))
#some magic for getting plot outputs in higher resolutions and with different fonts
plt.rcParams["font.family"] = "serif"
#plt.rcParams["font.serif"] = ["Times New Roman"]./
plt.rcParams["font.size"] = 12
plt.rcParams["figure.dpi"] = 300 #(default: 100.0) = 100 .
plt.plot(x,y,'--',color=[1,0,0])
plt.plot(x,y2,'d',markersize=2,color=[0.5,0.5,0])
plt.plot(x,y3,'.',color=[0,0,1])
plt.xlabel("x")
plt.ylabel("y")
plt.title("High dpi and times-new-romanish font")
plt.savefig(fnout)
plt.close()
return
def plot_histogram():
x = np.random.randn((1000))
x2 = x*2.0 + 1.0
xmean = np.mean(x2)
xstd = np.std(x2)
plt.figure()
plt.hist(x2,bins=25)
plt.plot(xmean,0,'kd')
plt.plot(xmean+xstd,0,'rd')
plt.plot(xmean-xstd,0,'rd')
ax = plt.gca()
ax.set_xlim([-6,6])
plt.savefig("plot_histogram.png")
plt.close()
return
def plot_adifferenthist():
x = np.random.randn((1000))
x2 = x*2.0 + 1.0
xmean = np.mean(x2)
xstd = np.std(x2)
#same statistical distribution, but we're going to use
#numpy's histogram function to make a contour plot
xbins = np.linspace(-6,6,25) #bin edges
xbins2 = 0.5*(xbins[0:(len(xbins)-1)]+xbins[1:len(xbins)]) #bin cetners
[xbc,xbinedges] = np.histogram(x2,bins=xbins)
print(xbins.shape)
print(xbc.shape)
plt.figure()
plt.plot(xbins2,xbc,'d-',color=[0,1,0],markeredgecolor=[1,0,0],markerfacecolor=[0,0,1],markersize=5,linewidth=2)
plt.plot(xmean,0,'kd')
plt.plot(xmean+xstd,0,'rd')
plt.plot(xmean-xstd,0,'rd')
ax = plt.gca()
ax.set_xlim([-6,6])
plt.savefig("plot_adifferenthist.png")
plt.close()
return
def plot_filled_contour():
#matlab style histogram plot
x = np.linspace(0,5,20)
y = np.linspace(-5,5,40)
[x2,y2] = np.meshgrid(x,y,indexing='ij')
z = sqrt(x2**2 - y2**2/2.0 + 3.0)
z[np.abs(np.imag(z))>1E-5] = np.nan #cut off complex part of figure
zmin = np.nanmin(z) #min ignoring nans
zmax = np.nanmax(z) #max ignoring nans
lvls = np.linspace(zmin,zmax,100) #how many contour levels to divide the plot up into (more is smoother)
plt.figure(1)
plt.contourf(x2,y2,z,levels=lvls,cmap='jet')
plt.colorbar()
plt.title("some contour plot")
plt.xlabel("x [someunits]")
plt.ylabel("y [someunits]")
plt.savefig("plot_filled_contour.png")
plt.close()
return
if(__name__=="__main__"):
#checks that you are calling this script directly, as opposed to importing it as a library
plot_simplescatter()
plot_histogram()
plot_adifferenthist()
plot_filled_contour()
exit(0)
+128
View File
@@ -0,0 +1,128 @@
# More Python 101
## Computer file systems and file paths
Computers store files on hard drives (among other things) - every device storing files typically has a filesystem, which are how the files are stored on the medium. There are a few different types, the differences between which should hopefully be transparent. The top level aspect of the filesystem is a heirarchy of folders in which files are stored. (There are also symlinks, hardlinks, and a few other constructs, but files and folders are what you should focus on at first.)
Typically there is a root folder. On Windows it would be something like C:\ or D:\ for a particular hard drive. On macs, your filesystem root is designated by a single backslash '/'
Your filesystem:
/ - your root folder
/Users - where your user folder goes
/jessicareynolds (or similar)
(various configuration files of yours)
/Desktop
/Downloads
/...
/Applications - where your mac GUI applications are (mostly installed)
... (other folders)
/Volumes - if you plug in external hard drives they probably show up here
/some_external_media
/opt - I think I saw that anaconda installed itself to /opt somewhere, but you probably won't need to know where.
/usr - directory containing various command line utilities that you can call in your terminal.
Each file has a location that can be specified by a string known as a filepath. There are both absolute and relative filepaths. Absolute file paths begin with a /, and are how you get from your file-system root to the file involved.
"/Users/jessicareynolds/Desktop/a_working_folder/subfolder/file.txt"
Relative file paths are how to get from your programs "current working directory" to the file. When running scripts from spyder, spyder has a particular directory it is currently working out of. It should be the same directory as the one listed in your file-tree tab. You can also run
os.getcwd()
in the command prompt to get a string that is the current filepath.
Suppose you are currently working from a_working_folder on your desktop.
Your current working directory should be something like
"/Users/jessicareynolds/Desktop/a_working_folder" - or it may be shortened to
"~/Desktop/a_working_folder" - here ~ is shorthand for the user's folder. (I'm guessing from Linux, it may be different on macs.)
The relative path to file.txt would be "subfolder/file.txt", or maybe "./subfolder/file.txt"
Let's say your desktop has the following stuff on it
~/Desktop
/a_working_folder
your_python_script.py
/subfolder
file.txt
/subfolder_b
anotherfile.txt
/a_different_project_of_yours
a_third_file.csv
Current working directory: "~/Desktop/a_working_folder"
The path from there to file.txt would be "./subfolder/file.txt"
The path to anotherfile.txt would be "./subfolder_b/anotherfile.txt"
The path to a_third_file.csv would be "../a_different_project_of_yours/a_third_file.csv"
. - where you currently are
.. - one folder up from path
If you were loading these datafiles from your python script, you would have to use either a relative, or an absolute path pointing to the file from the current working directory used to run your script. While your current working directory is probably where you place your jupyter notebook, or your python file, it's not necessarily the case. (Check the file browser tab, or run os.getcwd() if you are not sure.)
## Other useful commands for browsing filesystems
The filesystem tree and file-paths are a text representation of the structure of folders that your operating system gives you in your OS file browser.
You can navigate around, create folders and files, and move, copy and delete folders and files from the terminal.
Some terminal commands
ls - list contents of a directory
cd - change directory - you would call this
cd (somefilepath)
cd .. (move up one folder)
cd ./subfolder (move from ~/Desktop/a_working_folder to ~/Desktop/a_working_folder/subfolder
cd / - move to the root folder
mkdir - create a folder in your current working directory
rmdir - remove a folder in your current working directory
mv (source) (destination)
mv ./subfolder/* ./subfolder_b - take all files and folders in subfolder and move them over to subfolder_b
cp (source) (destination) - same as move, but you are copying files. Does not copy folders without a flag like -R (recursive)
Also, you can get help on how to use a terminal command using the man-pages command
man (command) brings up a help manual
pwd - how to get your present working directory from the terminal
## Python Versions of this stuff (see filepathpractice.py)
Within a python script or notebook, there are various methods for maneuvering around filesystems. Most of these are within the os and os.path module
os.listdir() - lists the files and folders within a directory. It will return a list that can be iterated over with subsequent commands
os.getcwd() - get current working directory
os.path.abspath(rel_path_to_something) - returns the absolute path, given a relative path to a file or folder. The absolute path can sometimes be useful in unambiguously locating things.
os.path.isfile(pathstring) - is the thing at pathstring a file (or something else?)
os.path.isdir(pathstring) - is the thing at pathstring a folder (or something else?)
os.mkdir(path) - make a folder
you may have to wrap it in a try/catch block to avoid errors if the folder already exists
os.chdir(path) - change your current working directory from within a python script
### Joining and splitting paths
(see the example file for the rest of this tutorial.)
+287
View File
@@ -0,0 +1,287 @@
#!/usr/bin/python3
import os,sys,math
import numpy as np
import matplotlib.pyplot as plt
"""
Numpy arrays are pythons version of most of the numeric stuff that matlab does.
Numpy provides a multidimensional array type (numpy.ndarray), and various math operations for manipulating them.
"""
arr = np.array([1,2,3])
print(arr)
print(type(arr))
print(arr.shape) # arr.shape is a tuple (a built-in python array type usually used for coordinates) containing the dimensions of the underlying array
print(type(arr.shape))
print(arr.size) # arr.size is an integer that is
arr = np.array([[1,2,3],[4,5,6]]) #np.array is turning a nested list into a multidimensional numpy array
print(arr.shape)
print(arr.size)
#selecting rows and columns of an array
a = arr[0,0:3] #selects the first row (row 0, columns 0,1,2)
b = arr[1,:] #selects the second row
c = arr[:,1] #selects the second column
print(a)
print(b)
print(c)
print(a.shape)
print(b.shape)
print(c.shape)
M = np.random.rand(3,3) #a way to get uniform pseudo-random numbers (0 to 1)
M2 = np.eye(3) # 3x3 identity matrix
print(M)
print(M2)
diagelems = np.diag(M) #returns the diagonal elements of M
print(diagelems)
M3 = np.diag(diagelems) #creates a diagonal matrix from a set of diagonal elements
print(M3)
#Sometimes, when selecting from a multidimensional matrix, you are left with a (N,M,1,L,O) slice and need to reduce it to (N,M,L,O)
#numpy.squeeze does this
arr = np.random.rand(2,3,4,5)
print(arr.shape)
arrb = arr[:,1:2,:,:]
print(arrb.shape)
arrc = np.squeeze(arrb)
print(arrc.shape)
#If you need to rearrange the rows and columns of a multidimensional array, transpose does this
arrd = np.transpose(arrc,axes=(1,0,2)) #rearranges a_i_j_k to a_j_i_k
print(arrd.shape)
print(M)
print(np.transpose(M))
#A few notes on the differences between python lists and numpy arrays:
a = list(M) #returns a list from the numpy matrix array
print(a) #note, that this list is a list of 1d numpy arrays
#list[ array, array, array ]
#a list can contain any built-in python data type
a = [1, 3.3, "a string", ["another","list"]]
print(a)
print(len(a))
print(a[3])
#lists can be "ragged" - each element can be a list with its own length
a = [[],[1],[2,3],[4,[5,6]]]
#numpy arrays must be rectangular - they have a particular size along each dimension
try:
arr = np.array(a) #this is going to fail
except Exception as e:
print("Error: {}".format(e))
a = [1,2,3,4,5,6]
arr = np.array(a) #this should work
print(a)
print(arr)
arrb = np.array([6,5,4,3,2,1])
#numpy arrays can be used transparently with arithmetic operators
#the arithmetic operators do *elementwise operations*
arrc = arrb+arr
print("{} + {} = {}".format(arr,arrb,arrc))
#This does elementwise multiplication, not matrix multiplication
M = np.eye(3)
M2 = np.random.rand(3,3)
M3 = np.linalg.inv(M2)
M4 = M*M2 #elementwise multiplication
print(M)
print(M2)
print(M4)
#In order to do matrix multiplication, you need to use the functions in numpy.linalg
#numpy.linalg.matmul
M4 = np.linalg.matmul(M,M2)
print(M2)
print(M4)
#M is the identity matrix
#M3 is the inverse of M4
M4 = np.linalg.matmul(M3,M4)
print(M4) #this may be printed awkwardly with some tiny elements
from numpy import pi,cos,sin,tan,sqrt,exp,linspace
#sometimes I get tired of typing np.pi, np.cos, etc, and just want to import these functions into the script's namespace
#this does that, so I don't have to keep remembering to type the numpy module prefix prior to every numpy command
x = linspace(0,360,1000)
y = cos(pi*x/180)
hf = plt.figure(1,figsize=(4,4))
plt.plot(x,y,linestyle='-',color=[0.5,0.5,0.5])
plt.savefig("someplot.png") #suppose you want to save the figure as an image instead of displaying it. This does that
plt.close() #closes this figure so you can make subsequent figure calls
### Numpy datatypes
#every numpy array has a dtype, a data type specifier. Everything in the array is going to have this type
a = np.zeros((3,3),dtype=np.float64) #np.float64 is the default - a double precision float
b = np.asarray(np.random.rand(3,3),dtype=np.float32) #equivalent to matlabs 'single' or C's 'float'
c = a*b #c's datatype will be adjusted to be the most accurate
print(a.dtype)
print(b.dtype)
print(c.dtype)
#in addition to floats, there are complex datatypes and integer types
d = np.ones((3,3),dtype=np.complex128) #dtype is specified on array creation
d[0,0] = d[0,0] + 1j #number j is how you express complex literals in python
print(d)
print(d.dtype)
f = np.zeros((3,3),dtype=np.uint8) #creating an integer array
f[0,0] = f[0,0] + 0.5 #this shouldn't work, because f is an integer type
print(f)
### Statistical operations
#numpy has some builtin simple statistics operations
a = np.random.rand(15,3) #a 15 row, 3 column 2d array of random numbers
sum_of_everything = np.sum(a) #should sum all elements of a and end up with soemthing like 0.5*15*3, 22.5
print(sum_of_everything)
#how to sum only the rows or only certain dimensions
b = np.sum(a,axis=1) #sums each row, and leaves the results in columns
print(b)
print(b.shape)
b = np.sum(a,axis=0)
print(b)
print(b.shape)
c = np.mean(a,axis=1) #takes the mean of each row
print(c)
print(c.shape)
a = np.random.rand(15,100) #since each row is 100 samples of a uniform(0,1) distribution, the mean of each row should be 0.5
c = np.mean(a,axis=1)
print(c)
print(c.shape)
c = np.std(a,axis=1) #standard deviation of each row
print(c)
d = np.var(a,axis=1) #the variance of each row
print(d)
print(d - c**2) #the variance should be the standard deviation squared in this simple 1d case
#so this difference should be very small numbers
def norm_of_a_vector(v):
v = np.asarray(v)
norm = np.sqrt(np.sum(v*v))
#v*v - elementwise squaring of v array
#(could also do v**2)
#np.sum(.) - sum over all elements of the squared array, returns a numpy scalar
#np.sqrt(.) - take the square root of the numpy scalar
return norm
a = [3,4,0]
b = norm_of_a_vector(a)
print(b) #should be 5
### Reshaping arrays
a = np.random.rand(2,2)
b = np.reshape(a,(4,1)) #reshapes a into a 4x1 column vector
print(b)
print(b.shape)
b = np.reshape(b,(1,4)) #turns b into a row vector
print(b)
print(b.shape)
b = np.squeeze(b) #turns b into a size 4 1d array
print(b)
print(b.shape)
## concatenating arrays
a = np.random.rand(3,1) #generate 3 column vectors
b = np.ones((3,1))
c = np.zeros((3,1))
print(a.shape)
print(b.shape)
print(c.shape)
m = np.stack((a,b,c),axis=2) #concatenates a,b,c along a new axis
print(m)
print(m.shape)
m = np.concat((a,b,c),axis=1) #concatenates a,b,c along a pre-existing axis (the axis 1 in this case)
print(m)
print(m.shape)
#suppose we had started with 1d arrays instead of column vector arrays
a = np.random.rand(3) #generate 3 column vectors
b = np.ones((3))
c = np.zeros((3))
print(a.shape)
print(b.shape)
print(c.shape)
#concat won't work for this (for making a matrix), because we don't have a row axis to concatenate along
try:
m = np.concat((a,b,c),axis=1) #concatenates a,b,c along a pre-existing axis (which doesn't exist in this case)
print(m)
print(m.shape)
except Exception as e:
print("error {}".format(e))
#stacking it and creating the 1 axis does work though
m = np.stack((a,b,c),axis=1)
print(m)
print(m.shape)
#you can also concatenate the 3 1-d arrays into a 1-d array of length 9
m = np.concat((a,b,c))
print(m)
print(m.shape)
#https://note.nkmk.me/en/python-numpy-concatenate-stack-block/
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env python3
# I am also learning pandas, so some of this may be trivial
import numpy as np
import pandas as pd
#converting back and forth between pandas series and numpy arrays
s = pd.Series([1,3,5,np.nan,6,8])
s2 = np.array(s)
print(s)
print(s2)
s3 = pd.Series(s2)
print(s3)
# https://pandas.pydata.org/docs/user_guide/10min.html
dates = pd.date_range("20130101",periods=6)
data = np.random.randn(6,4)
df = pd.DataFrame(data,index=dates,columns=["a","b","c","ddd"])
print(df)
Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

+7
View File
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "./"
}
]
}