- import zipfile
- import urllib.request
- urllib.request.urlretrieve("http://pla.esac.esa.int/pla/aio/product-action?COSMOLOGY.FILE_ID=COM_CosmoParams_base-plikHM-TTTEEE-lowl-lowE_R3.00.zip", "COM_CosmoParams_base-plikHM-TTTEEE-lowl-lowE_R3.00.zip")
- with zipfile.ZipFile('./COM_CosmoParams_base-plikHM-TTTEEE-lowl-lowE_R3.00.zip', 'r') as zip_ref:
- zip_ref.extractall() #You can specify the directory inside () to extract to
Live Programming and Computing with Python, R, Sage, Octave, Maxima, Singular, Gap, GP, HTML & Macaulay2
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Extract/unzip and analyze the Planck chains/MCMC samples from the Planck Legacy Archive:
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Test GetDist with this code, see more examples here:
The outputs are:

- # Show plots inline, and load main getdist plot module and samples class
- from __future__ import print_function
- import sys, os
- sys.path.insert(0,os.path.realpath(os.path.join(os.getcwd(),'..')))
- from getdist import plots, MCSamples
- import getdist
- # use this *after* importing getdist if you want to use interactive plots
- # %matplotlib notebook
- import matplotlib.pyplot as plt
- import IPython
- print('GetDist Version: %s, Matplotlib version: %s'%(getdist.__version__, plt.matplotlib.__version__))
- # matplotlib 2 may not work very well without usetex on, can uncomment
- # plt.rcParams['text.usetex']=True
- #Let test GetDist by getting some random samples for demonstration and then
- #make some random covariance, then independent samples from Gaussian
- import numpy as np
- ndim = 4
- nsamp = 10000
- np.random.seed(10)
- A = np.random.rand(ndim,ndim)
- cov = np.dot(A, A.T)
- samps = np.random.multivariate_normal([0]*ndim, cov, size=nsamp)
- A = np.random.rand(ndim,ndim)
- cov = np.dot(A, A.T)
- samps2 = np.random.multivariate_normal([0]*ndim, cov, size=nsamp)
- # Get the getdist MCSamples objects for the samples, specifying same parameter
- # names and labels; if not specified weights are assumed to all be unity
- names = ["x%s"%i for i in range(ndim)]
- labels = ["x_%s"%i for i in range(ndim)]
- samples1 = MCSamples(samples=samps,names = names, labels = labels)
- samples2 = MCSamples(samples=samps2,names = names, labels = labels, label='Second set')
- # Triangle plot
- g = plots.get_subplot_plotter()
- g.triangle_plot([samples1, samples2], filled=True)
- plt.show()
- # Get 1D marginalized comparison plot
- g = plots.get_single_plotter(width_inch=3)
- g.plot_1d([samples1, samples2], 'x1')
- plt.show()
- # Get filled 2D comparison plot with legend
- g = plots.get_single_plotter(width_inch=4, ratio=1)
- g.plot_2d([samples1, samples2], 'x1', 'x2', filled=True)
- g.add_legend(['sim 1', 'sim 2'], colored_text=True);
- plt.show()
The outputs are:
- Attachments
-
- gdplot1.png
- (79.97 KiB) Not downloaded yet
- gdplot1.png
- (79.97 KiB) Not downloaded yet
-
- gdplot2.png (9.62 KiB) Viewed 14441 times
- gdplot2.png (9.62 KiB) Viewed 14441 times
-
- gdplot3.png (17.53 KiB) Viewed 14441 times
- gdplot3.png (17.53 KiB) Viewed 14441 times
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Test TensorFlow and Keras with Fashion MNIST dataset, take over from the code below:
- from __future__ import absolute_import, division, print_function, unicode_literals
- # TensorFlow and tf.keras
- import tensorflow as tf
- from tensorflow import keras
- # Helper libraries
- import numpy as np
- import matplotlib.pyplot as plt
- print(tf.__version__)
- fashion_mnist = tf.keras.datasets.fashion_mnist
- (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
- print(test_labels)
- class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
- 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
- print(train_images.shape)
- print(len(train_labels))
- print(train_labels)
- print(test_images.shape)
- print(len(test_labels))
- #Preprocess the data
- plt.figure()
- plt.imshow(train_images[0])
- plt.colorbar()
- plt.grid(False)
- plt.show()
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Use tikzplotlib to convert matplotlib figures into TikZ/PGF Plots for native inclusion into LaTeX or ConTeXt documents.
The output of tikzplotlib is in PGFPlots, a TeX library that sits on top of PGF/TikZ and describes graphs in terms of axes, data and so on:

The output of tikzplotlib is in PGFPlots, a TeX library that sits on top of PGF/TikZ and describes graphs in terms of axes, data and so on:
- import matplotlib.pyplot as plt
- import numpy as np
- plt.style.use("ggplot")
- t = np.arange(0.0, 2.0, 0.1)
- s = np.sin(2 * np.pi * t)
- s2 = np.cos(2 * np.pi * t)
- plt.plot(t, s, "o-", lw=4.1)
- plt.plot(t, s2, "o-", lw=4.1)
- plt.xlabel("time (s)")
- plt.ylabel("Voltage (mV)")
- #plt.title("Simple plot $\\frac{\\alpha}{2}$")
- plt.grid(True)
- plt.show()
- import tikzplotlib
- tikzplotlib.save("test.tex")
- Attachments
-
- tikz_convert.png
- (46.74 KiB) Not downloaded yet
- tikz_convert.png
- (46.74 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Testing NBODYKIT, a massively parallel, large-scale structure toolkit (see this paper):

- from nbodykit.lab import *
- import numpy as np
- import matplotlib.pyplot as plt
- #Below, we create a very simple catalog of uniformly distributed
- #particles in a box of side length L=1 h^−1 Mpc
- catalog = UniformCatalog(nbar=100, BoxSize=1.0)
- BoxSize = 2500.
- """Catalogs have a fixed size and a set of columns describing the particle data.
- In this case, our catalog has “Position” and “Velocity” columns.
- Users can easily manipulate the existing column data or add new columns"""
- catalog['Position'] *= BoxSize # re-normalize units of Position
- #catalog['Mass'] = 10**(np.random(12, 15, size=len(catalog))) # add some random mass values
- mesh = catalog.to_mesh(Nmesh=64, BoxSize=BoxSize)# Interpolate the particles onto a mesh of size 64^3
- #We can save our mesh to disk to later re-load using nbodykit
- mesh.save('mesh.bigfile')
- #or preview a low-resolution, 2D projection of the mesh to make sure everythings looks as expected
- plt.imshow(mesh.preview(axes=[0,1], Nmesh=32))
- plt.show()
- #Use the FFTPower algorithm to compute the power spectrum P(k,μ)
- #of the density mesh using a fast Fourier transform via
- result = FFTPower(mesh,mode="2d", Nmu=5)
- #with the measured power stored as the power attribute of the result variable.
- #The algorithm result and meta-data, input parameters, etc. can then be saved to disk as a JSON file
- result.save("power-result.json")
- Attachments
-
- nbodykit_catalog.png
- (10.59 KiB) Not downloaded yet
- nbodykit_catalog.png
- (10.59 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
NLTK - the Natural Language Toolkit is installed, test it:
- import nltk
- #Simple nltk demo, Tokenize and tag some text
- sentence = """At eight o'clock on Thursday morning Arthur didn't feel very good."""
- print(sentence)
- nltk.download('punkt')
- tokens = nltk.word_tokenize(sentence)
- print(tokens)
- nltk.download('averaged_perceptron_tagger')
- tagged = nltk.pos_tag(tokens)
- print(tagged[0:6])
- #Identify named entities
- nltk.download('maxent_ne_chunker')
- nltk.download('words')
- entities = nltk.chunk.ne_chunk(tagged)
- print(entities)
- #Display a parse tree:
- from nltk.corpus import treebank
- nltk.download('treebank')
- t = treebank.parsed_sents('wsj_0001.mrg')[0]
- print(t)
0
TSSFL Stack is dedicated to empowering and accelerating teaching and learning, fostering scientific research, and promoting rapid software development and digital technologies
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Test SymPy:


- from sympy.plotting import plot
- from sympy import *
- plot( sin(x),cos(x), (x, -pi, pi))
- from sympy.plotting import plot
- from sympy import *
- x=Symbol('x')
- plot(x**2, line_color='red')
- Attachments
-
- sympy3.png
- (39.53 KiB) Not downloaded yet
- sympy3.png
- (39.53 KiB) Not downloaded yet
-
- sympy_plot.png
- (21.57 KiB) Not downloaded yet
- sympy_plot.png
- (21.57 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
3-D plotting with SymPy:




- from sympy import symbols
- from sympy.plotting import plot3d
- x, y = symbols('x y')
- #Fig 1
- plot3d(x*y, (x, -5, 5), (y, -5, 5))
- #Fig 2
- plot3d(x*y, -x*y, (x, -5, 5), (y, -5, 5))
- #Fig 3
- plot3d((x**2 + y**2, (x, -5, 5), (y, -5, 5)), (x*y, (x, -3, 3), (y, -3, 3)))
- #Fig 4
- from sympy import symbols, cos, sin
- from sympy.plotting import plot3d_parametric_surface
- u, v = symbols('u v')
- plot3d_parametric_surface(cos(u + v), sin(u - v), u - v, (u, -5, 5), (v, -5, 5))
- Attachments
-
- example1.png
- (128.56 KiB) Not downloaded yet
- example1.png
- (128.56 KiB) Not downloaded yet
-
- example2.png
- (160.44 KiB) Not downloaded yet
- example2.png
- (160.44 KiB) Not downloaded yet
-
- example3.png
- (149.97 KiB) Not downloaded yet
- example3.png
- (149.97 KiB) Not downloaded yet
-
- example4.png
- (85.53 KiB) Not downloaded yet
- example4.png
- (85.53 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Further examples with SymPy:





- from sympy import plot_implicit, symbols, Eq, And
- x, y = symbols('x y')
- #Fig 1
- plot_implicit(y > x**2)
- #PlotGrid Class
- from sympy import symbols
- from sympy.plotting import plot, plot3d, PlotGrid
- p1 = plot(x, x**2, x**3, (x, -5, 5))
- p2 = plot((x**2, (x, -6, 6)), (x, (x, -5, 5)))
- p3 = plot(x**3, (x, -5, 5))
- p4 = plot3d(x*y, (x, -5, 5), (y, -5, 5))
- Attachments
-
- example1.png
- (9.06 KiB) Not downloaded yet
- example1.png
- (9.06 KiB) Not downloaded yet
-
- example2.png
- (22.67 KiB) Not downloaded yet
- example2.png
- (22.67 KiB) Not downloaded yet
-
- example3.png
- (24.71 KiB) Not downloaded yet
- example3.png
- (24.71 KiB) Not downloaded yet
-
- example4.png
- (17.47 KiB) Not downloaded yet
- example4.png
- (17.47 KiB) Not downloaded yet
-
- example5.png
- (128.56 KiB) Not downloaded yet
- example5.png
- (128.56 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
- Eli
- Senior Expert Member
- Reactions: 189
- Posts: 5925
- Joined: 10 years ago
- Location: Tanzania
- Contact:
Plotting options with SymPy:



- from sympy import plot_implicit, symbols, Eq, And
- x, y = symbols('x y')
- #PlotGrid Class
- from sympy import symbols
- from sympy.plotting import plot, plot3d, PlotGrid
- p1 = plot(x, x**2, x**3, (x, -5, 5))
- p2 = plot((x**2, (x, -6, 6)), (x, (x, -5, 5)))
- p3 = plot(x**3, (x, -5, 5))
- p4 = plot3d(x*y, (x, -5, 5), (y, -5, 5))
- #Plotting vertically in a single line:
- PlotGrid(2, 1 , p1, p2)
- #Plotting horizontally in a single line:
- PlotGrid(1, 3 , p2, p3, p4)
- #Plotting in a grid form:
- PlotGrid(2, 2, p1, p2 ,p3, p4)
- Attachments
-
- example1.png
- (33.2 KiB) Not downloaded yet
- example1.png
- (33.2 KiB) Not downloaded yet
-
- example2.png
- (66.81 KiB) Not downloaded yet
- example2.png
- (66.81 KiB) Not downloaded yet
-
- example3.png
- (82.69 KiB) Not downloaded yet
- example3.png
- (82.69 KiB) Not downloaded yet
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
-
- Similar Topics
- Replies
- Views
- Last post
-
- Information
-
Who is online
Users browsing this forum: No registered users and 0 guests