Skip to content

Commit 0497119

Browse files
Avoid render error due to incomplete code (#235)
1 parent a69518f commit 0497119

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

03_array.ipynb

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@
9898
"source": [
9999
"# Load data with h5py\n",
100100
"# this creates a pointer to the data, but does not actually load\n",
101-
"import h5py\n",
102101
"import os\n",
103-
"f = h5py.File(os.path.join('data', 'random.hdf5'), mode='r')\n",
104-
"dset = f['/x']"
102+
"\n",
103+
"import h5py\n",
104+
"\n",
105+
"f = h5py.File(os.path.join(\"data\", \"random.hdf5\"), mode=\"r\")\n",
106+
"dset = f[\"/x\"]"
105107
]
106108
},
107109
{
@@ -134,7 +136,7 @@
134136
"# Compute sum of large array, one million numbers at a time\n",
135137
"sums = []\n",
136138
"for i in range(0, 1_000_000_000, 1_000_000):\n",
137-
" chunk = dset[i: i + 1_000_000] # pull out numpy array\n",
139+
" chunk = dset[i : i + 1_000_000] # pull out numpy array\n",
138140
" sums.append(chunk.sum())\n",
139141
"\n",
140142
"total = sum(sums)\n",
@@ -174,16 +176,14 @@
174176
"cell_type": "code",
175177
"execution_count": null,
176178
"metadata": {
177-
"jupyter": {
178-
"source_hidden": true
179-
}
179+
"tags": []
180180
},
181181
"outputs": [],
182182
"source": [
183183
"sums = []\n",
184184
"lengths = []\n",
185185
"for i in range(0, 1_000_000_000, 1_000_000):\n",
186-
" chunk = dset[i: i + 1_000_000] # pull out numpy array\n",
186+
" chunk = dset[i : i + 1_000_000] # pull out numpy array\n",
187187
" sums.append(chunk.sum())\n",
188188
" lengths.append(len(chunk))\n",
189189
"\n",
@@ -226,6 +226,7 @@
226226
"outputs": [],
227227
"source": [
228228
"import dask.array as da\n",
229+
"\n",
229230
"x = da.from_array(dset, chunks=(1_000_000,))\n",
230231
"x"
231232
]
@@ -379,12 +380,13 @@
379380
"metadata": {},
380381
"outputs": [],
381382
"source": [
382-
"import numpy as np\n",
383383
"import dask.array as da\n",
384+
"import numpy as np\n",
384385
"\n",
385-
"x = da.random.normal(10, 0.1, size=(20000, 20000), # 400 million element array \n",
386-
" chunks=(1000, 1000)) # Cut into 1000x1000 sized chunks\n",
387-
"y = x.mean(axis=0)[::100] # Perform NumPy-style operations"
386+
"x = da.random.normal(\n",
387+
" 10, 0.1, size=(20000, 20000), chunks=(1000, 1000) # 400 million element array\n",
388+
") # Cut into 1000x1000 sized chunks\n",
389+
"y = x.mean(axis=0)[::100] # Perform NumPy-style operations"
388390
]
389391
},
390392
{
@@ -403,7 +405,7 @@
403405
"outputs": [],
404406
"source": [
405407
"%%time\n",
406-
"y.compute() # Time to compute the result"
408+
"y.compute() # Time to compute the result"
407409
]
408410
},
409411
{
@@ -535,12 +537,13 @@
535537
"metadata": {},
536538
"outputs": [],
537539
"source": [
538-
"import h5py\n",
539-
"from glob import glob\n",
540540
"import os\n",
541+
"from glob import glob\n",
541542
"\n",
542-
"filenames = sorted(glob(os.path.join('data', 'weather-big', '*.hdf5')))\n",
543-
"dsets = [h5py.File(filename, mode='r')['/t2m'] for filename in filenames]\n",
543+
"import h5py\n",
544+
"\n",
545+
"filenames = sorted(glob(os.path.join(\"data\", \"weather-big\", \"*.hdf5\")))\n",
546+
"dsets = [h5py.File(filename, mode=\"r\")[\"/t2m\"] for filename in filenames]\n",
544547
"dsets[0]"
545548
]
546549
},
@@ -563,7 +566,7 @@
563566
"import matplotlib.pyplot as plt\n",
564567
"\n",
565568
"fig = plt.figure(figsize=(16, 8))\n",
566-
"plt.imshow(dsets[0][::4, ::4], cmap='RdBu_r');"
569+
"plt.imshow(dsets[0][::4, ::4], cmap=\"RdBu_r\");"
567570
]
568571
},
569572
{
@@ -628,7 +631,8 @@
628631
"metadata": {
629632
"jupyter": {
630633
"source_hidden": true
631-
}
634+
},
635+
"tags": []
632636
},
633637
"outputs": [],
634638
"source": [
@@ -640,7 +644,15 @@
640644
"cell_type": "markdown",
641645
"metadata": {},
642646
"source": [
643-
"**Plot the mean of this array along the time (`0th`) axis**"
647+
"**Plot the mean of this array along the time (`0th`) axis**\n",
648+
"\n",
649+
"Complete the following:\n",
650+
"\n",
651+
"```python\n",
652+
"result = ...\n",
653+
"fig = plt.figure(figsize=(16, 8))\n",
654+
"plt.imshow(result, cmap='RdBu_r')\n",
655+
"```"
644656
]
645657
},
646658
{
@@ -652,25 +664,22 @@
652664
]
653665
},
654666
"outputs": [],
655-
"source": [
656-
"# complete the following:\n",
657-
"fig = plt.figure(figsize=(16, 8))\n",
658-
"plt.imshow(..., cmap='RdBu_r')"
659-
]
667+
"source": []
660668
},
661669
{
662670
"cell_type": "code",
663671
"execution_count": null,
664672
"metadata": {
665673
"jupyter": {
666674
"source_hidden": true
667-
}
675+
},
676+
"tags": []
668677
},
669678
"outputs": [],
670679
"source": [
671680
"result = x.mean(axis=0)\n",
672681
"fig = plt.figure(figsize=(16, 8))\n",
673-
"plt.imshow(result, cmap='RdBu_r');"
682+
"plt.imshow(result, cmap=\"RdBu_r\");"
674683
]
675684
},
676685
{
@@ -699,7 +708,7 @@
699708
"source": [
700709
"result = x[0] - x.mean(axis=0)\n",
701710
"fig = plt.figure(figsize=(16, 8))\n",
702-
"plt.imshow(result, cmap='RdBu_r');"
711+
"plt.imshow(result, cmap=\"RdBu_r\");"
703712
]
704713
},
705714
{
@@ -756,21 +765,22 @@
756765
},
757766
"outputs": [],
758767
"source": [
759-
"import h5py\n",
760-
"from glob import glob\n",
761768
"import os\n",
769+
"from glob import glob\n",
770+
"\n",
762771
"import dask.array as da\n",
772+
"import h5py\n",
763773
"\n",
764-
"filenames = sorted(glob(os.path.join('data', 'weather-big', '*.hdf5')))\n",
765-
"dsets = [h5py.File(filename, mode='r')['/t2m'] for filename in filenames]\n",
774+
"filenames = sorted(glob(os.path.join(\"data\", \"weather-big\", \"*.hdf5\")))\n",
775+
"dsets = [h5py.File(filename, mode=\"r\")[\"/t2m\"] for filename in filenames]\n",
766776
"\n",
767777
"arrays = [da.from_array(dset, chunks=(500, 500)) for dset in dsets]\n",
768778
"\n",
769779
"x = da.stack(arrays, axis=0)\n",
770780
"\n",
771781
"result = x[:, ::2, ::2]\n",
772782
"\n",
773-
"da.to_zarr(result, os.path.join('data', 'myfile.zarr'), overwrite=True)"
783+
"da.to_zarr(result, os.path.join(\"data\", \"myfile.zarr\"), overwrite=True)"
774784
]
775785
},
776786
{
@@ -797,23 +807,27 @@
797807
"source": [
798808
"import numpy as np\n",
799809
"\n",
810+
"\n",
800811
"# make a random collection of particles\n",
801812
"def make_cluster(natoms, radius=40, seed=1981):\n",
802813
" np.random.seed(seed)\n",
803-
" cluster = np.random.normal(0, radius, (natoms,3))-0.5\n",
814+
" cluster = np.random.normal(0, radius, (natoms, 3)) - 0.5\n",
804815
" return cluster\n",
805816
"\n",
817+
"\n",
806818
"def lj(r2):\n",
807-
" sr6 = (1./r2)**3\n",
808-
" pot = 4.*(sr6*sr6 - sr6)\n",
819+
" sr6 = (1.0 / r2) ** 3\n",
820+
" pot = 4.0 * (sr6 * sr6 - sr6)\n",
809821
" return pot\n",
810822
"\n",
823+
"\n",
811824
"# build the matrix of distances\n",
812825
"def distances(cluster):\n",
813826
" diff = cluster[:, np.newaxis, :] - cluster[np.newaxis, :, :]\n",
814-
" mat = (diff*diff).sum(-1)\n",
827+
" mat = (diff * diff).sum(-1)\n",
815828
" return mat\n",
816829
"\n",
830+
"\n",
817831
"# the lj function is evaluated over the upper triangle\n",
818832
"# after removing distances near zero\n",
819833
"def potential(cluster):\n",
@@ -886,11 +900,12 @@
886900
"source": [
887901
"import dask.array as da\n",
888902
"\n",
903+
"\n",
889904
"# compute the potential on the entire\n",
890905
"# matrix of distances and ignore division by zero\n",
891906
"def potential_dask(cluster):\n",
892907
" d2 = distances(cluster)\n",
893-
" energy = da.nansum(lj(d2))/2.\n",
908+
" energy = da.nansum(lj(d2)) / 2.0\n",
894909
" return energy"
895910
]
896911
},
@@ -909,7 +924,7 @@
909924
"source": [
910925
"from os import cpu_count\n",
911926
"\n",
912-
"dcluster = da.from_array(cluster, chunks=cluster.shape[0]//cpu_count())"
927+
"dcluster = da.from_array(cluster, chunks=cluster.shape[0] // cpu_count())"
913928
]
914929
},
915930
{
@@ -974,7 +989,7 @@
974989
"metadata": {
975990
"anaconda-cloud": {},
976991
"kernelspec": {
977-
"display_name": "Python 3",
992+
"display_name": "Python 3 (ipykernel)",
978993
"language": "python",
979994
"name": "python3"
980995
},
@@ -988,7 +1003,7 @@
9881003
"name": "python",
9891004
"nbconvert_exporter": "python",
9901005
"pygments_lexer": "ipython3",
991-
"version": "3.7.6"
1006+
"version": "3.10.4"
9921007
}
9931008
},
9941009
"nbformat": 4,

0 commit comments

Comments
 (0)