learning scipy for numerical and scientific computing · 2020. 12. 17. · scientific computing...

33
Scientific Computing Examples Learning SciPy for Numerical and Scientific Computing Francisco Blanco-Silva University of South Carolina

Upload: others

Post on 06-Aug-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

Learning SciPy for Numerical and Scientific Computing

Francisco Blanco-Silva

University of South Carolina

Page 2: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTINGA PERSONAL PERSPECTIVE

Scientific Computing is concerned with constructing mathematical modelsand quantitative analysis techniques, and using computers to analyze andsolve scientific problems.

I Trust the Mathematicians: High-level Mathematics do solve challengingresearch problems in simple ways—even if you don’t understand why orhow yet!

I Trust the Engineer, Biologist, Chemist, Physicist, . . . : There are differentways to solve any problem. Rather than dismiss a different point ofview, embrace it, work it out, explore the source of the problem, andlook for connections with other techniques.

I Trust the Computer Scientist: Writing low-level code from scratchseldom guarantees best results.

I Find a reliable way to communicate through software.I Big guns: Solving these problems usually require massive amounts of

calculations and are often executed on supercomputers or distributedcomputing platforms.

Page 3: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTINGA PERSONAL PERSPECTIVE

Scientific Computing is concerned with constructing mathematical modelsand quantitative analysis techniques, and using computers to analyze andsolve scientific problems.

I Trust the Mathematicians: High-level Mathematics do solve challengingresearch problems in simple ways—even if you don’t understand why orhow yet!

I Trust the Engineer, Biologist, Chemist, Physicist, . . . : There are differentways to solve any problem. Rather than dismiss a different point ofview, embrace it, work it out, explore the source of the problem, andlook for connections with other techniques.

I Trust the Computer Scientist: Writing low-level code from scratchseldom guarantees best results.

I Find a reliable way to communicate through software.I Big guns: Solving these problems usually require massive amounts of

calculations and are often executed on supercomputers or distributedcomputing platforms.

Page 4: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTINGA PERSONAL PERSPECTIVE

Scientific Computing is concerned with constructing mathematical modelsand quantitative analysis techniques, and using computers to analyze andsolve scientific problems.

I Trust the Mathematicians: High-level Mathematics do solve challengingresearch problems in simple ways—even if you don’t understand why orhow yet!

I Trust the Engineer, Biologist, Chemist, Physicist, . . . : There are differentways to solve any problem. Rather than dismiss a different point ofview, embrace it, work it out, explore the source of the problem, andlook for connections with other techniques.

I Trust the Computer Scientist: Writing low-level code from scratchseldom guarantees best results.

I Find a reliable way to communicate through software.I Big guns: Solving these problems usually require massive amounts of

calculations and are often executed on supercomputers or distributedcomputing platforms.

Page 5: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTINGA PERSONAL PERSPECTIVE

Scientific Computing is concerned with constructing mathematical modelsand quantitative analysis techniques, and using computers to analyze andsolve scientific problems.

I Trust the Mathematicians: High-level Mathematics do solve challengingresearch problems in simple ways—even if you don’t understand why orhow yet!

I Trust the Engineer, Biologist, Chemist, Physicist, . . . : There are differentways to solve any problem. Rather than dismiss a different point ofview, embrace it, work it out, explore the source of the problem, andlook for connections with other techniques.

I Trust the Computer Scientist: Writing low-level code from scratchseldom guarantees best results.

I Find a reliable way to communicate through software.I Big guns: Solving these problems usually require massive amounts of

calculations and are often executed on supercomputers or distributedcomputing platforms.

Page 6: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTINGA PERSONAL PERSPECTIVE

Scientific Computing is concerned with constructing mathematical modelsand quantitative analysis techniques, and using computers to analyze andsolve scientific problems.

I Trust the Mathematicians: High-level Mathematics do solve challengingresearch problems in simple ways—even if you don’t understand why orhow yet!

I Trust the Engineer, Biologist, Chemist, Physicist, . . . : There are differentways to solve any problem. Rather than dismiss a different point ofview, embrace it, work it out, explore the source of the problem, andlook for connections with other techniques.

I Trust the Computer Scientist: Writing low-level code from scratchseldom guarantees best results.

I Find a reliable way to communicate through software.

I Big guns: Solving these problems usually require massive amounts ofcalculations and are often executed on supercomputers or distributedcomputing platforms.

Page 7: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTINGA PERSONAL PERSPECTIVE

Scientific Computing is concerned with constructing mathematical modelsand quantitative analysis techniques, and using computers to analyze andsolve scientific problems.

I Trust the Mathematicians: High-level Mathematics do solve challengingresearch problems in simple ways—even if you don’t understand why orhow yet!

I Trust the Engineer, Biologist, Chemist, Physicist, . . . : There are differentways to solve any problem. Rather than dismiss a different point ofview, embrace it, work it out, explore the source of the problem, andlook for connections with other techniques.

I Trust the Computer Scientist: Writing low-level code from scratchseldom guarantees best results.

I Find a reliable way to communicate through software.I Big guns: Solving these problems usually require massive amounts of

calculations and are often executed on supercomputers or distributedcomputing platforms.

Page 8: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

COMPUTATIONAL RESOURCESSOFTWARE

I Optimized Algebra LibrariesI BLASI LAPACK

I Computer Algebra systemsI MATLABI MathematicaI SciLabI GNU Octave

I Computer LanguagesI CI FortranI Perl

(with PDL)

I RI juliaI Python

I with PyLab: ipython + NumPy + SciPy + matplotlibI with scikits and Pandas on top of that

Page 9: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

COMPUTATIONAL RESOURCESSOFTWARE

I Optimized Algebra LibrariesI BLASI LAPACK

I Computer Algebra systemsI MATLABI MathematicaI SciLabI GNU Octave

I Computer LanguagesI CI FortranI Perl

(with PDL)

I RI juliaI Python

I with PyLab: ipython + NumPy + SciPy + matplotlibI with scikits and Pandas on top of that

Page 10: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

COMPUTATIONAL RESOURCESSOFTWARE

I Optimized Algebra LibrariesI BLASI LAPACK

I Computer Algebra systemsI MATLABI MathematicaI SciLabI GNU Octave

I Computer LanguagesI CI FortranI Perl

(with PDL)

I RI juliaI Python

I with PyLab: ipython + NumPy + SciPy + matplotlibI with scikits and Pandas on top of that

Page 11: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

COMPUTATIONAL RESOURCESSOFTWARE

I Optimized Algebra LibrariesI BLASI LAPACK

I Computer Algebra systemsI MATLABI MathematicaI SciLabI GNU Octave

I Computer LanguagesI CI FortranI Perl (with PDL)I RI juliaI Python

I with PyLab: ipython + NumPy + SciPy + matplotlibI with scikits and Pandas on top of that

Page 12: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

COMPUTATIONAL RESOURCESSOFTWARE

I Optimized Algebra LibrariesI BLASI LAPACK

I Computer Algebra systemsI MATLABI MathematicaI SciLabI GNU Octave

I Computer LanguagesI CI FortranI Perl (with PDL)I RI juliaI Python

I with PyLab: ipython + NumPy + SciPy + matplotlib

I with scikits and Pandas on top of that

Page 13: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

COMPUTATIONAL RESOURCESSOFTWARE

I Optimized Algebra LibrariesI BLASI LAPACK

I Computer Algebra systemsI MATLABI MathematicaI SciLabI GNU Octave

I Computer LanguagesI CI FortranI Perl (with PDL)I RI juliaI Python

I with PyLab: ipython + NumPy + SciPy + matplotlibI with scikits and Pandas on top of that

Page 14: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

THE STRUCTURE OF SCIPYSIMILARITY TO THE DIFFERENT AREAS OF MATHEMATICS

Pure Mathematics

AnalysisMultivariate

& VectorCalculus

ODEs FunctionalAnalysis

MeasureTheory Calculus of

Variations

HarmonicAnalysis

ComplexAnalysis

StochasticAnalysisGeometric

Analysis

PDEs

Combinatorics

Enumerative

Extremal

GraphTheory

Geometry

ConvexGeometry

DifferentialGeometryManifolds

DiscreteGeometry

Topology

AlgebraicTopology

Algebra

ElementaryNumberTheory

AbstractAlgebraicGeometry

Linear

AppliedMathematics

ProbabilityStochasticProcesses

Statistics

NumericalAnalysis

SymbolicComputation

Page 15: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

THE STRUCTURE OF SCIPYSIMILARITY TO THE DIFFERENT AREAS OF MATHEMATICS

Pure Mathematics

AnalysisMultivariate

& VectorCalculus

ODEs FunctionalAnalysis

MeasureTheory Calculus of

Variations

HarmonicAnalysis

ComplexAnalysis

StochasticAnalysisGeometric

Analysis

PDEs

Combinatorics

Enumerative

Extremal

GraphTheory

Geometry

ConvexGeometry

DifferentialGeometryManifolds

DiscreteGeometry

Topology

AlgebraicTopology

Algebra

ElementaryNumberTheory

AbstractAlgebraicGeometry

Linear

AppliedMathematics

ProbabilityStochasticProcesses

Statistics

NumericalAnalysis

SymbolicComputation

scipy.miscscipy.constants

Page 16: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

THE STRUCTURE OF SCIPYSIMILARITY TO THE DIFFERENT AREAS OF MATHEMATICS

Pure Mathematics

AnalysisMultivariate

& VectorCalculus

ODEs FunctionalAnalysis

MeasureTheory Calculus of

Variations

HarmonicAnalysis

ComplexAnalysis

StochasticAnalysisGeometric

Analysis

PDEs

Combinatorics

Enumerative

Extremal

GraphTheory

Geometry

ConvexGeometry

DifferentialGeometryManifolds

DiscreteGeometry

Topology

AlgebraicTopology

Algebra

ElementaryNumberTheory

AbstractAlgebraicGeometry

Linear

AppliedMathematics

ProbabilityStochasticProcesses

Statistics

NumericalAnalysis

SymbolicComputation

scipy.miscscipy.constants

numpy.polynomialscipy.linalgscipy.sparse.linalg

Page 17: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

THE STRUCTURE OF SCIPYSIMILARITY TO THE DIFFERENT AREAS OF MATHEMATICS

Pure Mathematics

AnalysisMultivariate

& VectorCalculus

ODEs FunctionalAnalysis

MeasureTheory Calculus of

Variations

HarmonicAnalysis

ComplexAnalysis

StochasticAnalysisGeometric

Analysis

PDEs

Combinatorics

Enumerative

Extremal

GraphTheory

Geometry

ConvexGeometry

DifferentialGeometryManifolds

DiscreteGeometry

Topology

AlgebraicTopology

Algebra

ElementaryNumberTheory

AbstractAlgebraicGeometry

Linear

AppliedMathematics

ProbabilityStochasticProcesses

Statistics

NumericalAnalysis

SymbolicComputation

scipy.miscscipy.constants

scipy.specialscipy.integratescipy.optimizescipy.interpolatescipy.fftpack

numpy.polynomialscipy.linalgscipy.sparse.linalg

Page 18: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

THE STRUCTURE OF SCIPYSIMILARITY TO THE DIFFERENT AREAS OF MATHEMATICS

Pure Mathematics

AnalysisMultivariate

& VectorCalculus

ODEs FunctionalAnalysis

MeasureTheory Calculus of

Variations

HarmonicAnalysis

ComplexAnalysis

StochasticAnalysisGeometric

Analysis

PDEs

Combinatorics

Enumerative

Extremal

GraphTheory

Geometry

ConvexGeometry

DifferentialGeometryManifolds

DiscreteGeometry

Topology

AlgebraicTopology

Algebra

ElementaryNumberTheory

AbstractAlgebraicGeometry

Linear

AppliedMathematics

ProbabilityStochasticProcesses

Statistics

NumericalAnalysis

SymbolicComputation

scipy.miscscipy.constants

scipy.specialscipy.integratescipy.optimizescipy.interpolatescipy.fftpack

numpy.polynomialscipy.linalgscipy.sparse.linalg

scipy.clusterscipy.sparse.csgraph

Page 19: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

THE STRUCTURE OF SCIPYSIMILARITY TO THE DIFFERENT AREAS OF MATHEMATICS

Pure Mathematics

AnalysisMultivariate

& VectorCalculus

ODEs FunctionalAnalysis

MeasureTheory Calculus of

Variations

HarmonicAnalysis

ComplexAnalysis

StochasticAnalysisGeometric

Analysis

PDEs

Combinatorics

Enumerative

Extremal

GraphTheory

Geometry

ConvexGeometry

DifferentialGeometryManifolds

DiscreteGeometry

Topology

AlgebraicTopology

Algebra

ElementaryNumberTheory

AbstractAlgebraicGeometry

Linear

AppliedMathematics

ProbabilityStochasticProcesses

Statistics

NumericalAnalysis

SymbolicComputation

scipy.miscscipy.constants

scipy.specialscipy.integratescipy.optimizescipy.interpolatescipy.fftpack

numpy.polynomialscipy.linalgscipy.sparse.linalg

scipy.clusterscipy.sparse.csgraph

scipy.spatialscipy.odr

Page 20: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

THE STRUCTURE OF SCIPYSIMILARITY TO THE DIFFERENT AREAS OF MATHEMATICS

Pure Mathematics

AnalysisMultivariate

& VectorCalculus

ODEs FunctionalAnalysis

MeasureTheory Calculus of

Variations

HarmonicAnalysis

ComplexAnalysis

StochasticAnalysisGeometric

Analysis

PDEs

Combinatorics

Enumerative

Extremal

GraphTheory

Geometry

ConvexGeometry

DifferentialGeometryManifolds

DiscreteGeometry

Topology

AlgebraicTopology

Algebra

ElementaryNumberTheory

AbstractAlgebraicGeometry

Linear

AppliedMathematics

ProbabilityStochasticProcesses

Statistics

NumericalAnalysis

SymbolicComputation

scipy.miscscipy.constants

scipy.specialscipy.integratescipy.optimizescipy.interpolatescipy.fftpack

numpy.polynomialscipy.linalgscipy.sparse.linalg

scipy.clusterscipy.sparse.csgraph

scipy.spatialscipy.odr

scipy.signalscipy.ndimagescipy.statsscipy.stats.mstats

Page 21: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONEXTRACT THE STRUCTURAL MODEL OF A MOLECULE OF Nb4W13O47

Page 22: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

We take the following (naı̈ve) approach:I Segmentation of the atoms by thresholding and morphological

operations.

I Connected component labeling to extract each atom for posteriorexamination.

I Computation of the centers of mass of each label identified as an atom.I Computation of the Voronoi diagram of the lattice formed by the

previous points.

Page 23: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

We take the following (naı̈ve) approach:I Segmentation of the atoms by thresholding and morphological

operations.I Connected component labeling to extract each atom for posterior

examination.

I Computation of the centers of mass of each label identified as an atom.I Computation of the Voronoi diagram of the lattice formed by the

previous points.

Page 24: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

We take the following (naı̈ve) approach:I Segmentation of the atoms by thresholding and morphological

operations.I Connected component labeling to extract each atom for posterior

examination.I Computation of the centers of mass of each label identified as an atom.

I Computation of the Voronoi diagram of the lattice formed by theprevious points.

Page 25: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

We take the following (naı̈ve) approach:I Segmentation of the atoms by thresholding and morphological

operations.I Connected component labeling to extract each atom for posterior

examination.I Computation of the centers of mass of each label identified as an atom.I Computation of the Voronoi diagram of the lattice formed by the

previous points.

Page 26: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

1 # Preamble2 import numpy3 import scipy4 from scipy.ndimage import binary_opening, label, center_of_mass,

distance_transform_edt5 # Load the image6 img = scipy.misc.imread(’NbW-STEM.png’)7 # Apply a threshold to segment atoms8 BWatoms = (img > 0.62)9 # Perform a binary operation to eliminate outliers

10 BWatoms = binary_opening(BWatoms, structure=numpy.ones((2,2)))11 # Segmentation of each atom12 structuring_element = [[0,1,0],[1,1,1],[0,1,0]]13 segmentation,segments = label(BWatoms, structuring_element)14 # Computation of centers of mass of each atom15 coords = center_of_mass(img, segmentation, range(1,segments+1))16 xcoords = array([x[0] for x in coords])17 ycoords = array([x[1] for x in coords])18 # Compute the Voronoi diagram of the lattice19 L1, L2 = distance_transform_edt(segmentation==0, return_distances=False,

return_indices=True)20 Voronoi = segmentation[L1,L2]

Page 27: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

1 # Preamble2 import numpy3 import scipy4 from scipy.ndimage import binary_opening, label, center_of_mass,

distance_transform_edt5 # Load the image6 img = scipy.misc.imread(’NbW-STEM.png’)7 # Apply a threshold to segment atoms8 BWatoms = (img > 0.62)9 # Perform a binary operation to eliminate outliers

10 BWatoms = binary_opening(BWatoms, structure=numpy.ones((2,2)))11 # Segmentation of each atom12 structuring_element = [[0,1,0],[1,1,1],[0,1,0]]13 segmentation,segments = label(BWatoms, structuring_element)14 # Computation of centers of mass of each atom15 coords = center_of_mass(img, segmentation, range(1,segments+1))16 xcoords = array([x[0] for x in coords])17 ycoords = array([x[1] for x in coords])18 # Compute the Voronoi diagram of the lattice19 L1, L2 = distance_transform_edt(segmentation==0, return_distances=False,

return_indices=True)20 Voronoi = segmentation[L1,L2]

Page 28: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

1 # Preamble2 import numpy3 import scipy4 from scipy.ndimage import binary_opening, label, center_of_mass,

distance_transform_edt5 # Load the image6 img = scipy.misc.imread(’NbW-STEM.png’)7 # Apply a threshold to segment atoms8 BWatoms = (img > 0.62)9 # Perform a binary operation to eliminate outliers

10 BWatoms = binary_opening(BWatoms, structure=numpy.ones((2,2)))11 # Segmentation of each atom12 structuring_element = [[0,1,0],[1,1,1],[0,1,0]]13 segmentation,segments = label(BWatoms, structuring_element)14 # Computation of centers of mass of each atom15 coords = center_of_mass(img, segmentation, range(1,segments+1))16 xcoords = array([x[0] for x in coords])17 ycoords = array([x[1] for x in coords])18 # Compute the Voronoi diagram of the lattice19 L1, L2 = distance_transform_edt(segmentation==0, return_distances=False,

return_indices=True)20 Voronoi = segmentation[L1,L2]

Page 29: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

1 # Preamble2 import numpy3 import scipy4 from scipy.ndimage import binary_opening, label, center_of_mass,

distance_transform_edt5 # Load the image6 img = scipy.misc.imread(’NbW-STEM.png’)7 # Apply a threshold to segment atoms8 BWatoms = (img > 0.62)9 # Perform a binary operation to eliminate outliers

10 BWatoms = binary_opening(BWatoms, structure=numpy.ones((2,2)))11 # Segmentation of each atom12 structuring_element = [[0,1,0],[1,1,1],[0,1,0]]13 segmentation,segments = label(BWatoms, structuring_element)14 # Computation of centers of mass of each atom15 coords = center_of_mass(img, segmentation, range(1,segments+1))16 xcoords = array([x[0] for x in coords])17 ycoords = array([x[1] for x in coords])18 # Compute the Voronoi diagram of the lattice19 L1, L2 = distance_transform_edt(segmentation==0, return_distances=False,

return_indices=True)20 Voronoi = segmentation[L1,L2]

Page 30: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

1 # Preamble2 import numpy3 import scipy4 from scipy.ndimage import binary_opening, label, center_of_mass,

distance_transform_edt5 # Load the image6 img = scipy.misc.imread(’NbW-STEM.png’)7 # Apply a threshold to segment atoms8 BWatoms = (img > 0.62)9 # Perform a binary operation to eliminate outliers

10 BWatoms = binary_opening(BWatoms, structure=numpy.ones((2,2)))11 # Segmentation of each atom12 structuring_element = [[0,1,0],[1,1,1],[0,1,0]]13 segmentation,segments = label(BWatoms, structuring_element)14 # Computation of centers of mass of each atom15 coords = center_of_mass(img, segmentation, range(1,segments+1))16 xcoords = array([x[0] for x in coords])17 ycoords = array([x[1] for x in coords])18 # Compute the Voronoi diagram of the lattice19 L1, L2 = distance_transform_edt(segmentation==0, return_distances=False,

return_indices=True)20 Voronoi = segmentation[L1,L2]

Page 31: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

1 # Preamble2 import numpy3 import scipy4 from scipy.ndimage import binary_opening, label, center_of_mass,

distance_transform_edt5 # Load the image6 img = scipy.misc.imread(’NbW-STEM.png’)7 # Apply a threshold to segment atoms8 BWatoms = (img > 0.62)9 # Perform a binary operation to eliminate outliers

10 BWatoms = binary_opening(BWatoms, structure=numpy.ones((2,2)))11 # Segmentation of each atom12 structuring_element = [[0,1,0],[1,1,1],[0,1,0]]13 segmentation,segments = label(BWatoms, structuring_element)14 # Computation of centers of mass of each atom15 coords = center_of_mass(img, segmentation, range(1,segments+1))16 xcoords = array([x[0] for x in coords])17 ycoords = array([x[1] for x in coords])18 # Compute the Voronoi diagram of the lattice19 L1, L2 = distance_transform_edt(segmentation==0, return_distances=False,

return_indices=True)20 Voronoi = segmentation[L1,L2]

Page 32: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

SCIPY IN ACTIONCOMPUTATION OF STRUCTURAL MODELS

1 # Preamble2 import numpy3 import scipy4 from scipy.ndimage import binary_opening, label, center_of_mass,

distance_transform_edt5 # Load the image6 img = scipy.misc.imread(’NbW-STEM.png’)7 # Apply a threshold to segment atoms8 BWatoms = (img > 0.62)9 # Perform a binary operation to eliminate outliers

10 BWatoms = binary_opening(BWatoms, structure=numpy.ones((2,2)))11 # Segmentation of each atom12 structuring_element = [[0,1,0],[1,1,1],[0,1,0]]13 segmentation,segments = label(BWatoms, structuring_element)14 # Computation of centers of mass of each atom15 coords = center_of_mass(img, segmentation, range(1,segments+1))16 xcoords = array([x[0] for x in coords])17 ycoords = array([x[1] for x in coords])18 # Compute the Voronoi diagram of the lattice19 L1, L2 = distance_transform_edt(segmentation==0, return_distances=False,

return_indices=True)20 Voronoi = segmentation[L1,L2]

Page 33: Learning SciPy for Numerical and Scientific Computing · 2020. 12. 17. · Scientific Computing Examples SOME BASIC PRINCIPLES IN SCIENTIFIC COMPUTING A PERSONAL PERSPECTIVE Scientific

Scientific Computing Examples

FOR MORE INFORMATION, EXAMPLES, IDEAS, . . .

blancosilva.wordpress.com