This notebook forms part of a series on computational optical radiometry. The notebooks can be downloaded from Github. These notebooks are constantly revised and updated, please revisit from time to time.
The date of this document and module versions used in this document are given at the end of the file.
Feedback is appreciated: neliswillers at gmail dot com.
The pyradi library has a reasonably complete collection of Planck radiator models, both spectral and wide band. A comprehensive collection of physical constants, pertinent to optical radiation is also included. This notebook introduces these functions in the pyradi.ryplanck
library.
from IPython.display import display
from IPython.display import Image
from IPython.display import HTML
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
The ryfiles.listFiles
function
returns a list of file paths to files in a file system, searching a directory structure along the specified path, looking for files that matches the glob pattern. If specified, the search will continue into sub-directories. A list of matching names is returned.
The function supports a local or network reachable filesystem, but not URLs.
The function signature is:
listFiles(root, patterns='*', recurse=1, return_folders=0, useRegex=False)
root (string)
directory root from where the search must take place.patterns (string)
glob/regex pattern for filename matching.recurse (unt)
flag to indicate if subdirectories must also be searched (optional).return_folders (int)
flag to indicate if folder names must also be returned (optional).useRegex (bool)
flag to indicate if patterns areregular expression strings (optional).import pyradi.ryfiles as ryfiles
print(ryfiles.listFiles('.', '05*.ipynb'))
['05a-PlottingWithPyradi-GeneralAndCartesian.ipynb', '05b-PlottingWithPyradi-Polar-and-3D.ipynb', '.ipynb_checkpoints\\05a-PlottingWithPyradi-GeneralAndCartesian-checkpoint.ipynb', '.ipynb_checkpoints\\05b-PlottingWithPyradi-Polar-and-3D-checkpoint.ipynb']
import pyradi.ryfiles as ryfiles
print(ryfiles.listFiles('.', '05*', return_folders=1))
['05a-PlottingWithPyradi-GeneralAndCartesian.bbl', '05a-PlottingWithPyradi-GeneralAndCartesian.bib', '05a-PlottingWithPyradi-GeneralAndCartesian.blg', '05a-PlottingWithPyradi-GeneralAndCartesian.ipynb', '05a-PlottingWithPyradi-GeneralAndCartesian.log', '05a-PlottingWithPyradi-GeneralAndCartesian.pdf', '05a-PlottingWithPyradi-GeneralAndCartesian.tex', '05b-PlottingWithPyradi-Polar-and-3D.bbl', '05b-PlottingWithPyradi-Polar-and-3D.bib', '05b-PlottingWithPyradi-Polar-and-3D.blg', '05b-PlottingWithPyradi-Polar-and-3D.ipynb', '05b-PlottingWithPyradi-Polar-and-3D.log', '05b-PlottingWithPyradi-Polar-and-3D.pdf', '05b-PlottingWithPyradi-Polar-and-3D.tex', '05', '.git\\objects\\05', '.git\\objects\\06\\05114a0c65030f3ab63c80822e07a03700ef35', '.git\\objects\\51\\059c45e9c9d5cfd46021370481d725b6eecccd', '.git\\objects\\7d\\05a03633f8985bca427403a7ff84ba35e199e6', '.git\\objects\\99\\05b062b719b832d586eb36cbd3ec21fe74d328', '.git\\objects\\d8\\052cd1780d21a06306ec3090e7657f3ea3e17e', '.git\\objects\\d9\\05835262f64f8ca948425e72c5f4ee07305246', '.ipynb_checkpoints\\05a-PlottingWithPyradi-GeneralAndCartesian-checkpoint.ipynb', '.ipynb_checkpoints\\05a-PlottingWithPyradi-GeneralAndCartesian-checkpoint.pdf', '.ipynb_checkpoints\\05b-PlottingWithPyradi-Polar-and-3D-checkpoint.ipynb', '05\\05-testfile.txt', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_13_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_14_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_15_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_17_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_19_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_22_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_22_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_22_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_25_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_25_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_25_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_29_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_31_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_32_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_3.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_4.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_5.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_35_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_36_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_3.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_4.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_5.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_40_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_40_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_42_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_43_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_44_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_46_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_47_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_48_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_50_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_51_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_52_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_53_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_55_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_56_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_57_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_60_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_62_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_64_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_66_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_68_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_70_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_72_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_73_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_74_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_75_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_76_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_77_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_78_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_79_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_81_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_83_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_84_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_85_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_87_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_90_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_91_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_91_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_93_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_96_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_97_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_10_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_11_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_12_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_13_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_14_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_15_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_16_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_17_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_17_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_18_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_22_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_23_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_24_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_25_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_26_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_27_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_28_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_29_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_32_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_33_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_35_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_36_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_38_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_39_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_39_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_40_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_41_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_46_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_47_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_49_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_50_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_54_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_55_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_58_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_59_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_59_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_61_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_62_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_64_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_65_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_72_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_73_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_74_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_75_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_75_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_83_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_84_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_85_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_86_0.png']
print(ryfiles.listFiles('.', '05*.ipynb', useRegex=1))
[]
print(ryfiles.listFiles('.', '05[ab].*', useRegex=1))
['05a-PlottingWithPyradi-GeneralAndCartesian.bbl', '05a-PlottingWithPyradi-GeneralAndCartesian.bib', '05a-PlottingWithPyradi-GeneralAndCartesian.blg', '05a-PlottingWithPyradi-GeneralAndCartesian.ipynb', '05a-PlottingWithPyradi-GeneralAndCartesian.log', '05a-PlottingWithPyradi-GeneralAndCartesian.pdf', '05a-PlottingWithPyradi-GeneralAndCartesian.tex', '05b-PlottingWithPyradi-Polar-and-3D.bbl', '05b-PlottingWithPyradi-Polar-and-3D.bib', '05b-PlottingWithPyradi-Polar-and-3D.blg', '05b-PlottingWithPyradi-Polar-and-3D.ipynb', '05b-PlottingWithPyradi-Polar-and-3D.log', '05b-PlottingWithPyradi-Polar-and-3D.pdf', '05b-PlottingWithPyradi-Polar-and-3D.tex', 'tape7-05b', '.git\\objects\\08\\2acc4bfa2505b8d6fe0f85446c972a05a42888', '.git\\objects\\0d\\7f4c205a5f765006ae18faf50ffcbe4a430767', '.git\\objects\\2f\\f2c19f11277ca6a4895b1fbb5fcaf805abafbc', '.git\\objects\\38\\4b27fe04aa105acd84ebdbb16611729d4342f4', '.git\\objects\\40\\dcbc05a06258a6fc781a76ef07a6ee877b65b4', '.git\\objects\\6b\\42dbd56bd6e9ba05a54d592cfba05733d3aa20', '.git\\objects\\78\\656e36efc86279d39fe23f5b7b73dac4f005b9', '.git\\objects\\7b\\dc9c6407a05b826590e66950ca58484a07c69b', '.git\\objects\\7d\\05a03633f8985bca427403a7ff84ba35e199e6', '.git\\objects\\99\\05b062b719b832d586eb36cbd3ec21fe74d328', '.git\\objects\\9a\\e05ae9a9ff4ed6b6d749d024585c86433296da', '.git\\objects\\b2\\bd7a8d1a1ba253d4b5cdc12a05a1b76c8a573c', '.git\\objects\\c0\\91f1f7728fb9da43f08705ad8ebe3968cefea6', '.git\\objects\\e8\\8705a4a9b8a5682b325f4fe715b42065c08d43', '.ipynb_checkpoints\\05a-PlottingWithPyradi-GeneralAndCartesian-checkpoint.ipynb', '.ipynb_checkpoints\\05a-PlottingWithPyradi-GeneralAndCartesian-checkpoint.pdf', '.ipynb_checkpoints\\05b-PlottingWithPyradi-Polar-and-3D-checkpoint.ipynb', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_13_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_14_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_15_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_17_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_19_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_22_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_22_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_22_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_25_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_25_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_25_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_29_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_31_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_32_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_3.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_4.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_34_5.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_35_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_36_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_3.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_4.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_38_5.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_40_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_40_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_42_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_43_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_44_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_46_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_47_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_48_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_50_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_51_2.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_52_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_53_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_55_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_56_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_57_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_60_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_62_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_64_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_66_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_68_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_70_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_72_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_73_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_74_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_75_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_76_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_77_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_78_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_79_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_81_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_83_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_84_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_85_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_87_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_90_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_91_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_91_1.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_93_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_96_0.png', 'pic\\05a-PlottingWithPyradi-GeneralAndCartesian_97_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_10_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_11_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_12_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_13_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_14_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_15_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_16_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_17_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_17_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_18_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_22_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_23_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_24_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_25_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_26_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_27_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_28_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_29_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_32_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_33_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_35_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_36_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_38_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_39_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_39_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_40_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_41_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_46_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_47_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_49_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_50_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_54_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_55_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_58_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_59_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_59_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_61_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_62_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_64_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_65_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_72_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_73_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_74_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_75_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_75_1.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_83_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_84_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_85_0.png', 'pic\\05b-PlottingWithPyradi-Polar-and-3D_86_0.png']
print(ryfiles.listFiles('.', '^05[ab].*\.[^h].*', useRegex=1))
['05a-PlottingWithPyradi-GeneralAndCartesian.bbl', '05a-PlottingWithPyradi-GeneralAndCartesian.bib', '05a-PlottingWithPyradi-GeneralAndCartesian.blg', '05a-PlottingWithPyradi-GeneralAndCartesian.ipynb', '05a-PlottingWithPyradi-GeneralAndCartesian.log', '05a-PlottingWithPyradi-GeneralAndCartesian.pdf', '05a-PlottingWithPyradi-GeneralAndCartesian.tex', '05b-PlottingWithPyradi-Polar-and-3D.bbl', '05b-PlottingWithPyradi-Polar-and-3D.bib', '05b-PlottingWithPyradi-Polar-and-3D.blg', '05b-PlottingWithPyradi-Polar-and-3D.ipynb', '05b-PlottingWithPyradi-Polar-and-3D.log', '05b-PlottingWithPyradi-Polar-and-3D.pdf', '05b-PlottingWithPyradi-Polar-and-3D.tex']
The information in this section does not describe any pyradi functionality, but may be useful in this context.
To split a qualified path into the folder and filename use os.path.split
, yielding a tuple with the path as the first element and the filename as the second element.
import os.path
print(type(os.path.split('05\\05-testfile.txt')))
print(os.path.split('05\\05-testfile.txt'))
print(os.path.basename('05\\05-testfile.txt'))
print(os.path.dirname('05\\05-testfile.txt'))
<class 'tuple'> ('05', '05-testfile.txt') 05-testfile.txt 05
To join a folder and a filename use os.path.join
, but there is a catch here; os.path.join
does not expect the tuple, but simply a number of arguments. Study the documentation carefully, because os.path.join
may discard some arguments.
Alternatively, use os.sep
or os.path.sep
to get the pathname separator ('/' for POSIX and '\' for Windows) and then use this character to join the list.
print(os.path.join(* os.path.split('05\\05-testfile.txt')))
print(os.sep.join(os.path.split('05\\05-testfile.txt')))
print(os.path.sep.join(os.path.split('05\\05-testfile.txt')))
05\05-testfile.txt 05\05-testfile.txt 05\05-testfile.txt
To test for the existance of a file use os.path.lexists
or os.path.exists
, see the documentation to confirm the difference between the two.
print(os.path.lexists('05\\05-testfile.txt'))
print(os.path.lexists('06\\05-testfile.txt'))
True False
The ryfiles.downloadFileUrl
function downloads a file from a URL. The URL is used to download a file, to the saveFilename specified. If no saveFilename is given, the basename of the URL is used. The name of the downloaded file is returned, or None if the download file.
The function signature is:
`downloadFileUrl(url, saveFilename=None)`
url (string)
the url to be accessed.saveFilename (string)
path to where the file must be saved (optional).The ryfiles.downloadUntar
downloads and untars a compressed tar archive, and saves all files to the specified directory. The tarfilename is used to open the tar file, extracting to the destinationDir specified. If no destinationDir is given, the local directory '.' is used. Returns a list of the intarred filenames, or None of not succesful. Before downloading, a check is done to determine if the file was already downloaded and exists in the local file system.
The file signature is:
`downloadUntar(tgzFilename, url, destinationDir=None, tarFilename=None)`
tgzFilename (string)
the name of the tar archive file.url (string)
url where to look for the file (not including the filename).destinationDir (string)
to where the files must be extracted (optional).tarFilename (string)
downloaded tar filename (optional).The ryfiles.untarTarfile
untars a tar archive, and save all files to the specified directory. The tarfilename is used to open a file, extraxting to the saveDirname specified. If no saveDirname is given, the local directory '.' is used. Returns a list of filenames saved, or None if failed.
The file signature is:
`untarTarfile(tarfilename, saveDirname=None)`
tarfilename (string)
the name of the tar archive.saveDirname (string)
to where the files must be extractedThe ryfiles.unzipGZipfile
unzips a file that was compressed using the gzip format. The zipfilename is used to open a file, to the saveFilename specified. If no saveFilename is given, the basename of the zipfilename is used, but with the file extension removed. Returns the filename of the saved file, or None if failed.
The file signature is:
`unzipGZipfile(zipfilename, saveFilename=None)`
zipfilename (string)
the zipfilename to be decompressed.saveFilename (string)
to where the file must be saved (optional).The following example downloads a file, uncompress and untar the file, saving all the files in the tar archive to the destination directory (the current working directory in this case).
tgzFilename = 'arrayplotdemo.tgz'
destinationDir = '.'
tarFilename = 'arrayplotdemo.tar'
url = 'https://raw.githubusercontent.com/NelisW/pyradi/master/pyradi/data/'
dlNames = ryfiles.downloadUntar(tgzFilename, url, destinationDir, tarFilename)
if dlNames:
print('filesAvailable are {}'.format(dlNames))
filesAvailable are ['arrayplotdemo.txt']
The ryutils.detectSignalToNoise
function solves for the signal to noise ratio, given the threshold to noise ratio and probability of detection. Using the theory of matched filter design, calculate the signal to noise ratio, to achieve a required probability of detection. The function returns the signal to noise ratio required to achieve the probability of detection.
Reference: "Electro-optics handbook," Tech. Rep. EOH-11, RCA, 1974. RCA Technical Series Publication.
When there is a signal present, the probability of detection (signal plus noise exceeds the threshold) is given by
$$ P_d = \frac{1}{2}\left[ 1+{\rm erf}\left( \frac{i_s-i_t}{\sqrt{2}i_n} \right) \right] $$where ${\rm erf}$ is the error function:
$$ {\rm erf}(z)=\frac{2}{\sqrt{\pi}}\int_0^z e^{-t^2}dt. $$The function signature is:
`detectSignalToNoise(ThresholdToNoise, pD)`
ThresholdToNoise (float)
the threshold to noise ratio (unitless).pD (float)
the probability of detection (unitless).The ryutils.detectThresholdToNoise
function solve for threshold to noise ratio, given pulse width and FAR, for matched filter. Using the theory of matched filter design, calculate the threshold to noise ratio, to achieve a required false alarm rate. The function returns the threshold to noise ratio. Reference R. D. Hippenstiel, Detection Theory: Applications and Digital Signal Pro-cessing, CRC Press, 2002.
The average false alarm rate is given by $$ {FAR}= \frac{1}{2 t_p \sqrt{3}}\exp^{-i_t^2/(2i_n^2)}, $$ where $t_p$ is the pulse width, $i_t$ is the threshold value, and $i_n$ is the rms noise value at the input to the threshold detector.
The function signature is:
detectThresholdToNoise(pulseWidth, FAR)
pulseWidth (float)
the signal pulse width in [s].FAR (float)
the false alarm rate in [alarms/s].import pyradi.ryutils as ryutils
pulsewidth = 100e-9
FAR = 15
probDetection = 0.999
ThresholdToNoise = ryutils.detectThresholdToNoiseTpFAR(pulsewidth,FAR)
SignalToNoise = ryutils.detectSignalToNoiseThresholdToNoisePd(ThresholdToNoise, probDetection)
print('For a laser pulse with width={0}, a FAR={1} and Pd={2},'.format(pulsewidth,FAR,probDetection))
print('the Threshold to Noise ratio must be {0}'.format(ThresholdToNoise))
print('and the Signal to Noise ratio must be {0}'.format(SignalToNoise))
print(' ')
For a laser pulse with width=1e-07, a FAR=15 and Pd=0.999, the Threshold to Noise ratio must be 4.933070468777455 and the Signal to Noise ratio must be 8.023302774945268
It is frequently necessary to determine the operational detection distance of a source and sensor combination. The problem is usually stated as follows: "What operating detection range can be achieved with a given source intensity, atmospheric attenuation, and sensor sensitivity?"
The inband irradiance is given by (Sec. 7.5)
The objective is to solve for $R$ where the irradiance $E_{{\cal S}}=E_{{\cal S}\theta}$ is at the threshold at which the range must be determined. Normally, $E_{{\cal S}\theta}=SNR\times NEE$ where the SNR is selected to yield a given probability of detection.
Assuming a target smaller than the sensor field of view, and using the effective transmittance the above equation can be simplified to the form
$$ E_{{\cal S}\theta}=\frac{I_{{\cal S}} \tau_{\rm eff}(R)}{R^2} $$where $I_{{\cal S}}$ is the source intensity. This equation can be solved is $\tau_{\rm eff}(R)$ is available in lookup table form.
The ryutils.rangeEquation
function solves the range equation for arbitrary transmittance vs range, with the equation given by
where $E$ is the threshold irradiance in [W/m2], and $I$ is the intensity in [W/sr]. This range equation holds for the case where the target is smaller than the field of view.
The range $R$ must be in [m], and $\tau_a(R)$ is calculated from a lookup table of atmospheric transmittance vs. range. The transmittance lookup table can be calculated from the simple Bouguer law, or it can have any abritrary shape, provided it decreases with increasing range. The user supplies the lookup table in the form of an array of range values and an associated array of transmittance values. The range values need not be on constant linear range increment.
The parameter $n$
$n$=2 (default value) the general case of a radiating source smaller than the field of view.
$n$=4 the special case of a laser rangefinder illuminating a splot smaller than the sensor field of view, viewed against the sky. In this case there is an $R^2$ attenuation from the laser to the source and another $R^2$ attenuation from the source to the receiver, hence $R^4$ overall.
The function signature is:
rangeEquation(Intensity, Irradiance, rangeTab, tauTab, rangeGuess = 1, n = 2)
Intensity (float or np.array[N,] or [N,1])
in [W/sr].Irradiance (float or np.array[N,] or [N,1])
in [W/m2].rangeTab (np.array[N,] or [N,1])
range vector for tauTab lookup in [m].tauTab (np.array[N,] or [N,1])
transmittance vector for lookup range in [m].rangeGuess (float)
starting value range estimate in [m] (optional).n (float)
range power (2 or 4) (optional).If the range solution is doubtful (e.g. not a trustworthy solution) the returned value is made negative. The following example attempts to solve the equation for three cases, only one of which provides a stable solution.
import numpy as np
from scipy.interpolate import interp1d
rangeTab = np.linspace(0, 10000, 1000)
tauTab = np.exp(- 0.00015 * rangeTab)
Intensity=200
Irradiancetab=[10e-100, 10e-6, 10e-1]
for Irradiance in Irradiancetab:
r = ryutils.rangeEquation(Intensity = Intensity, Irradiance = Irradiance, rangeTab = rangeTab,
tauTab = tauTab, rangeGuess = 1, n = 2)
#test the solution by calculating the irradiance at this range.
tauTable = interp1d(rangeTab, tauTab, kind = 'linear')
if np.abs(r[0]) < rangeTab[2]:
rr = rangeTab[2]
strError = "Check range resolution in lookup table"
elif np.abs(r[0]) > rangeTab[-1]:
rr = rangeTab[-1]
strError = "Check maximum range in lookup table"
else:
rr = r[0]
strError = ""
irrad = Intensity * tauTable(rr) / rr ** 2
print('Range equation solver with irradiance threshold of {} W/m2:'.format(Irradiance))
print(' intensity of {4} W/sr at range {0} m the irradiance is {1} W/m2, error is {2:.2e}. \n{3}\n'.format(
r[0],irrad, (irrad - Irradiance) / Irradiance, strError, Intensity))
Range equation solver with irradiance threshold of 1e-99 W/m2: intensity of 200 W/sr at range -11381.128909552699 m the irradiance is 4.462603202968597e-07 W/m2, error is 4.46e+92. Check maximum range in lookup table Range equation solver with irradiance threshold of 1e-05 W/m2: intensity of 200 W/sr at range 3452.03409008682 m the irradiance is 1.000000000000012e-05 W/m2, error is 1.19e-14. Range equation solver with irradiance threshold of 1.0 W/m2: intensity of 200 W/sr at range -14.12716136943599 m the irradiance is 0.49750424774943763 W/m2, error is -5.02e-01. Check range resolution in lookup table
The next example plots the detection range for a missile given the threshold irradiance, viewing an aircraft with signature of 200W/sr, through an atmosphere with an attenuation coefficient of 0.15 km-1. The graphs shows that when viewing the target through the atmosphere, an irradiance threshold (sensitivity) of 1 $\mu$W/m$^2$ will provide a detection distance of 8 km. A missile with sensitivity of 0.1 $\mu$W/m$^2$ provides a detection distance of 15 km. The example shows that you need ten times better sensitivity to double the detection distance. Increased detection distance comes at a tremendous cost!
import pyradi.ryutils as ryutils
import pyradi.ryplot as ryplot
import numpy as np
from scipy.interpolate import interp1d
%matplotlib inline
rangeTab = np.linspace(0, 100000, 1000)
atmogamma = 0.00015
tauTab = np.exp(- atmogamma * rangeTab)
Intensity=200
Irradiancetab=np.linspace(0.05e-6, 100e-6, 400)
r = np.zeros((Irradiancetab.shape))
for i,Irradiance in enumerate(Irradiancetab):
r[i] = ryutils.rangeEquation(Intensity = Intensity, Irradiance = Irradiance, rangeTab = rangeTab,
tauTab = tauTab, rangeGuess = 1, n = 2)
p=ryplot.Plotter(1, 2, 1, figsize=(12,12))
p.semilogX(1,Irradiancetab*1e6,r,'Range equation for 200 W/sr target through {} km-1 atmosphere'.format(atmogamma*1000.),
'Irradiance threshold [$\mu$W/m2]','Detection range [m]');
The ryutils.abshumidity
function calculates atmopsheric absolute humidity [g/m3] for temperature in [K] between 248 K and 342 K. This function provides two similar equations, but with different constants.
Atmospheric humidity is commonly expressed in relative humidity (RH). Relative humidity is the quantity of water vapor in the atmosphere, expressed as a percentage of the maximum absolute humidity. The maximum absolute humidity is determined by the water-vapor partial pressure at the atmospheric temperature and is given by this equation (Equation 1 in the function): $$ q=\frac{1325.252}{T} 10^{7.5892(T - 273.15)/(T -32.44)}, $$ in units of [g/m$^3$] and temperature in [K]. This equation has an error of less than 1% over the range $-$20 $^\circ$C to 0 $^\circ$C, and less than 0.1% for the temperature range 0 $^\circ$C to 50 $^\circ$C.
The alternative equation (Equation 1 in the function) is: $$ q=\frac{1324.37872}{T} \exp[ 17.67*(T - 273.16)/(T - 29.66)) $$
The function signature is:
abshumidity(T, equationSelect = 1)
temperature (np.array[N,] or [N,1])
in [K].equationSelect (int)
select the equation to be used.In this example the values from the equations are compared to data from:
import pyradi.ryplot as ryplot
%matplotlib inline
data=np.asarray([
[ 50 , 82.78 ] ,
[ 45 , 65.25 ] ,
[ 40 , 50.98 ] ,
[ 35 , 39.47 ] ,
[ 30 , 30.26 ] ,
[ 25 , 22.97 ] ,
[ 20 , 17.24 ] ,
[ 15 , 12.8 ] ,
[ 10 , 9.38 ] ,
[ 5 , 6.79 ] ,
[ 0 , 4.85 ] ,
[ -5 , 3.41 ] ,
[ -10 , 2.36 ] ,
[ -15 , 1.61 ] ,
[ -20 , 1.08 ] ,
[ -25 , 0.71 ] ])
temperature = data[:,0]+273.15
absh = ryutils.abshumidity(temperature).reshape(-1,1)
data = np.hstack((data,absh))
# absh = ryutils.abshumidity(temperature, 2).reshape(-1,1)
# data = np.hstack((data,absh))
data = np.hstack((data, 100 * np.reshape((data[:,1]-data[:,2])/data[:,2],(-1,1))))
print(' deg C Testvalue Fn value % Error')
print(data)
p=ryplot.Plotter(1, 2, 1, figsize=(12,6))
p.plot(1,data[:,0].T, data[:,2].T,'Absolute humidity vs temperature','Temperature [$^\circ$C]','Absolute humidity g/m$^3$]')
p.plot(2,data[:,0].T, data[:,3].T,'\% humidity error vs temperature','Temperature [$^\circ$C]','\% error')
# p.saveFig('abshum.eps')
#highest ever recorded absolute humidity was at dew point of 34C
print('Highest recorded absolute humidity was {0}, dew point {1} deg C'.\
format(ryutils.abshumidity(np.asarray([34 + 273.15]))[0],34))
deg C Testvalue Fn value % Error [[ 5.00000000e+01 8.27800000e+01 8.28288052e+01 -5.89229459e-02] [ 4.50000000e+01 6.52500000e+01 6.53105744e+01 -9.27481918e-02] [ 4.00000000e+01 5.09800000e+01 5.10475988e+01 -1.32422991e-01] [ 3.50000000e+01 3.94700000e+01 3.95318519e+01 -1.56460806e-01] [ 3.00000000e+01 3.02600000e+01 3.03161305e+01 -1.85150640e-01] [ 2.50000000e+01 2.29700000e+01 2.30097997e+01 -1.72968594e-01] [ 2.00000000e+01 1.72400000e+01 1.72744162e+01 -1.99232035e-01] [ 1.50000000e+01 1.28000000e+01 1.28192871e+01 -1.50453884e-01] [ 1.00000000e+01 9.38000000e+00 9.39702200e+00 -1.81142459e-01] [ 5.00000000e+00 6.79000000e+00 6.79913283e+00 -1.34323439e-01] [ 0.00000000e+00 4.85000000e+00 4.85173751e+00 -3.58120391e-02] [-5.00000000e+00 3.41000000e+00 3.41141647e+00 -4.15215865e-02] [-1.00000000e+01 2.36000000e+00 2.36126844e+00 -5.37186742e-02] [-1.50000000e+01 1.61000000e+00 1.60720482e+00 1.73915730e-01] [-2.00000000e+01 1.08000000e+00 1.07451558e+00 5.10408511e-01] [-2.50000000e+01 7.10000000e-01 7.04731527e-01 7.47585870e-01]] Highest recorded absolute humidity was 37.51799299496529, dew point 34 deg C
The
ryutils.cart2polar
and
ryutils.polar2cart
provides conversion between polar and cartesian coordinates.
The ryutils.cart2polar
function signature:
`cart2polar(x, y)`
x (float np.array)
x values in array format.y (float np.array)
y values in array format.returns
r (float np.array)
radial component for given (x,y).theta (float np.array)
angular component for given (x,y).The ryutils.polar2cart
function signature:
`polar2cart(r, theta)`
r (float np.array)
radial values in array format.theta (float np.array)
angular values in array format.returns
x (float np.array)
x component for given (r, theta).y (float np.array)
y component for given (r, theta).This transformation code was written by Joe Kington.
import pyradi.ryutils as ryutils
import numpy as np
r, t = ryutils.cart2polar(5, 5)
x, y = ryutils.polar2cart(r, t)
print('{} {} -> {} {}'.format(x, y, r, t))
r, t = ryutils.cart2polar(np.asarray([3, 5]), np.asarray([5, 3]))
x, y = ryutils.polar2cart(r, t)
print('{} {} -> {} {}'.format(x, y, r, t))
r, t = ryutils.cart2polar(np.asarray([[3, 5], [5, 3]]), np.asarray([[5, 3], [3, 5]]))
x, y = ryutils.polar2cart(r, t)
print('{} {} -> {} {}'.format(x, y, r, t))
5.000000000000001 5.000000000000001 -> 7.0710678118654755 0.7853981633974483 [3. 5.] [5. 3.] -> [5.83095189 5.83095189] [1.03037683 0.5404195 ] [[3. 5.] [5. 3.]] [[5. 3.] [3. 5.]] -> [[5.83095189 5.83095189] [5.83095189 5.83095189]] [[1.03037683 0.5404195 ] [0.5404195 1.03037683]]
The
ryutils.index_coords
function calculates zero-based (x,y) coordinate arrrays for a numpy array indices, relative to some origin.
This function calculates two meshgrid arrays containing the coordinates of the input array. The origin of the new coordinate system defaults to the center of the image, unless the user supplies a new origin.
The data format can be data.shape = (rows, cols, frames) or data.shape = (frames, rows, cols), the format of which is indicated by the framesFirst parameter.
The function signature is:
`index_coords(data, origin=None, framesFirst=True)`
data (np.array)
array for which coordinates must be calculated.origin ( (x-orig, y-orig) )
data-coordinates of where origin should beframesFirst (bool)
True if data.shape is (frames, rows, cols), False if data.shape is (rows, cols, frames)returns
x (float np.array)
x coordinates in array format.y (float np.array)
y coordinates in array format.This transformation code was originally written by Joe Kington.
The following example demonstrates the variations obtained for frames first or frames last, and the location of the origin.
import pyradi.ryutils as ryutils
import numpy as np
data = np.ones([2, 3, 4])
x, y = ryutils.index_coords(data, origin=None, framesFirst=True)
print('framesFirst=True, origin=None')
print('x={}\ny={}'.format(x,y))
x, y = ryutils.index_coords(data, origin=None, framesFirst=False)
print('\nframesFirst=False, origin=None')
print('x={}\ny={}'.format(x,y))
x, y = ryutils.index_coords(data, origin=(0,0), framesFirst=True)
print('\nframesFirst=True, origin=(0,0)')
print('x={}\ny={}'.format(x,y))
x, y = ryutils.index_coords(data, origin=(3,2), framesFirst=True)
print('\nframesFirst=True, origin=(3,2)')
print('x={}\ny={}'.format(x,y))
framesFirst=True, origin=None x=[[-2 -1 0 1] [-2 -1 0 1] [-2 -1 0 1]] y=[[-1 -1 -1 -1] [ 0 0 0 0] [ 1 1 1 1]] framesFirst=False, origin=None x=[[-1 0 1] [-1 0 1]] y=[[-1 -1 -1] [ 0 0 0]] framesFirst=True, origin=(0,0) x=[[0 1 2 3] [0 1 2 3] [0 1 2 3]] y=[[0 0 0 0] [1 1 1 1] [2 2 2 2]] framesFirst=True, origin=(3,2) x=[[-3 -2 -1 0] [-3 -2 -1 0] [-3 -2 -1 0]] y=[[-2 -2 -2 -2] [-1 -1 -1 -1] [ 0 0 0 0]]
The ryfiles.epsLaTexFigure
function writes the LaTeX code to include an eps graphic as a latex figure. The text is added to an existing file.
The function signature is:
epsLaTexFigure(filename, epsname, caption, scale, filemode='a')
fname (string)
text writing output path and filename.epsname (string)
filename/path to eps file (relative to where the LaTeX document is built).caption (string)
figure caption.scale (double)
figure scale to textwidth [0..1].filemode (string)
file open mode (a=append, w=new file) (optional).ryfiles.epsLaTexFigure('eps.tex', 'picture.eps', 'This is the caption', 0.75,'w')
import os.path
if os.path.sep == '/':
!cat eps.tex
else:
!type eps.tex
\begin{figure}[tb] \centering \includegraphics[width=0.75\textwidth,height=w\textheight,keepaspectratio]{picture.eps} \caption{This is the caption. \label{fig:picture.eps}} \end{figure} \begin{figure}[tb] \centering \includegraphics[width=0.75\textwidth,height=w\textheight,keepaspectratio]{picture.eps} \caption{This is the caption. \label{fig:picture.eps}} \end{figure} \begin{figure}[tb] \centering \includegraphics[width=0.75\textwidth,height=w\textheight,keepaspectratio]{picture.eps} \caption{This is the caption. \label{fig:picture.eps}} \end{figure} \begin{figure}[tb] \centering \includegraphics[width=0.75\textwidth,height=w\textheight,keepaspectratio]{picture.eps} \caption{This is the caption. \label{fig:picture.eps}} \end{figure}
The ryfiles.arrayToLaTex
function writes a numpy array to latex table format in output file.
The table can contain only the array data (no top header or
left column side-header), or you can add either or both of the
top row or side column headers. Leave 'header' or 'leftcol' as
None is you don't want these.
The output format of the array data can be specified, i.e. scientific notation or fixed decimal point.
The function signature is:
arrayToLaTex(filename, arr, header=None, leftCol=None,formatstring='%1.4e', filemode='wt')
fname (string)
text writing output path and filename.arr (np.array[N,M])
array with table data.header (string)
column header in final latex format (optional).leftCol ([string])
left column each row, in final latex format (optional).formatstring (string)
output format precision for array data (see numpy.savetxt) (optional).filemode (string)
file open mode (a=append, w=new file) (optional).With \usepackage{siunitx}
you can use the siunitx \num
formatter to format the output.
arr = np.asarray([[1.0,2,3],[4,5,6],[7,8,9]])
ryfiles.arrayToLaTex('array.tex', arr)
ryfiles.arrayToLaTex('array.tex', arr, formatstring='%.1f',filemode='a')
headeronly = 'Col1 & Col2 & Col3'
ryfiles.arrayToLaTex('array.tex', arr, headeronly, formatstring='%.3f',filemode='a')
header = 'Col 1 & Col 2 & Col 3'
leftcol = ['XX','Row 1','Row 2','Row 3']
ryfiles.arrayToLaTex('array.tex', arr, header, leftcol, formatstring=r'\num{%.6e}',filemode='a')
import os.path
if os.path.sep == '/':
!cat array.tex
else:
!type array.tex
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_19740/2572545408.py in <module> 1 arr = np.asarray([[1.0,2,3],[4,5,6],[7,8,9]]) 2 ryfiles.arrayToLaTex('array.tex', arr) ----> 3 ryfiles.arrayToLaTex('array.tex', arr, formatstring='%.1f',filemode='a') 4 headeronly = 'Col1 & Col2 & Col3' 5 ryfiles.arrayToLaTex('array.tex', arr, headeronly, formatstring='%.3f',filemode='a') K:\WorkN\pyradi\pyradi\pyradi\ryfiles.py in arrayToLaTex(filename, arr, header, leftCol, formatstring, filemode) 892 893 file=open(filename, filemode) --> 894 file.write('\\begin{{tabular}}{{ {0} }}\n\hline\n'.format('|'+ numcols*'c|').encode('utf-8')) 895 896 #write the header TypeError: write() argument must be str, not bytes
The ryutils.upMu
function returns an upright $\mu$ symbol, as required in the SI system for the $10^{-6}$ prefix.
The upright symbol requires that the siunitx LaTeX package be installed on the computer running the code. This function also changes the Matplotlib rcParams file. This symbol can be used in graphs or text.
The function signature is:
upMu(uprightMu=True, textcomp=False)
uprightMu (bool)
signals upright (True, default) or regular (False) symbol (optional).textcomp (bool)
if True use the textcomp package, else use siunitx package (optional).The ryutils.upMu
function returns a string with LaTeX code for the micro symbol.
import pyradi.ryutils as ryutils
print('Using siunitx package:')
print('{} renders in LaTeX as an upright symbol'.format(ryutils.upMu()))
print('{} renders in LaTeX as an upright symbol'.format(ryutils.upMu(True)))
print('{} renders in LaTeX as an italic/slanted symbol'.format(ryutils.upMu(False)))
print('\nUsing textcomp package:')
print('{} renders in LaTeX as an upright symbol'.format(ryutils.upMu(textcomp=True)))
print('{} renders in LaTeX as an upright symbol'.format(ryutils.upMu(True,textcomp=True)))
print('{} renders in LaTeX as an italic/slanted symbol'.format(ryutils.upMu(False,textcomp=True)))
The ryfiles.cleanFilename
function cleans a string by removing selected characters. It creates a legal and 'clean' source string from a string by removing some
clutter and characters not allowed in filenames. A default set is given but the user can override the default string.
The function signature is:
cleanFilename(sourcestring, removestring =" %:/,.\\[]")
sourcestring (string)
the string to be cleaned.removestring (string)
remove all these characters from the string (optional).It is quite useful to use a figure caption as filename, first cleaning it up.
print ('\nTest CleanFilename function:')
inString="aa bb%cc:dd/ee,ff.gg\\hh[ii]jj"
print('{0}\n{1}\n'.format(inString, ryfiles.cleanFilename(inString) ))
inString="aa bb%cc:dd/ee,ff.gg\\hh[ii]jj"
print('{0}\n{1}'.format(inString, ryfiles.cleanFilename(inString, "abcd") ))
The ryfiles.read2DLookupTable
Reads a 2D lookup table and extract the data. The table has the following format:
line 1: xlabel ylabel title
line 2: 0 (vector of y (col) abscissa)
lines 3 and following: (element of x (row) abscissa), followed
by table data.
From line/row 3 onwards the first element is the x abscissa value followed by the row of data, one point for each y abscissa value.
The file format can depicted as follows:
x-name y-name ordinates-name
0 y1 y2 y3 y4
x1 v11 v12 v13 v14
x2 v21 v22 v23 v24
x3 v31 v32 v33 v34
x4 v41 v42 v43 v44
x5 v51 v52 v53 v54
x6 v61 v62 v63 v64
The function has the signature:
`read2DLookupTable(filename)`
fname (string)
input path and filenameThe function returns the following:
xVec ((np.array[N]))
x abscissaeyVec ((np.array[M]))
y abscissaedata ((np.array[N,M]))
data corresponding the x,yxlabel (string)
x abscissa labelylabel (string)
y abscissa labeltitle (string)
dataset titlefilecont = ['x-name y-name ordinates-name\n',
'0 1 2 3 4\n',
'10 11 12 13 14\n',
'20 21 22 23 24\n',
'30 31 32 33 34\n',
'40 41 42 43 44\n',
'50 51 52 53 54\n',
'60 61 62 63 64\n']
with open('2dfile.dat', 'wt') as f:
f.writelines(filecont)
import os.path
if os.path.sep == '/':
!cat 2dfile.dat
else:
!type 2dfile.dat
xVec,yVec,data,xlabel, ylabel, title = ryfiles.read2DLookupTable('2dfile.dat')
print('\n')
print('xVec={}'.format(xVec))
print('yVec={}'.format(yVec))
print('data={}'.format(data))
print('xlabel={}'.format(xlabel))
print('ylabel={}'.format(ylabel))
print('title={}'.format(title))
Jake vdPlas makes a good point why and how histograms are not really good probability density estimators here. Depending on how the data points fall into the bins strange effects may arise. He writes 'The problem with our two binnings stems from the fact that the height of the block stack often reflects not on the actual density of points nearby, but on coincidences of how the bins align with the data points. This mis-alignment between points and their blocks is a potential cause of the poor histogram results.
The histogram approach is to have (normally same-size) fixed bin edges, in to which the data points are falling and being counted. The Kernel Density method works the other way around make fixed-width block around each data point and add up the blocks' heights. This way the weights add up around the data points' locations. This is somewhat making a bin centred around each data point and then adding up the bin counts at a fine resolution.
Working with rectangular blocks the new density function has rough edges at the blocks' corners. The next logical step is to use smooth functions (called kernels) instead of rectangular blocks. This yields smooth functions with more appropriate density distributions than does the traditional histogram. In this approach you must define/select the kernel shape and width. If the kernel is too wide a kernel's influence can be too wide, masking the data points. If the kernel is too narrow it will leave 'holes' between the data points. Just image a dirac delta kernel function which simply repeats the data set.
scikit-learn provides an efficient library for kernel density estimation. The library provides six different kernel shapes and a method whereby an appropriate kernel width can be found. The pyradi.ryprob.KDEbounded
function packages the scikit learn functionality in an easily usable single function.
The gaussian kernels typically used in KDE reaches to infinity and the density function domain therefore spans infinity in x. Suppose the data set is known to be bounded by zero on the lower end: non-zero values are not possible. The KDE algorithm will happily return a nonzero density for $x<0$, so some means must be found to eliminate density outside the domain bounds.
To correct this non-zero density outside of the valid domain:
This procedure ensures that the PDF derivative at the boundary is zero and that the density 'outside' the domain is accounted for by mirroring the density back into the domain.
The next challenge is finding the correct bandwidth. From JvdP book:
''The choice of bandwidth within KDE is extremely important to finding a suitable density estimate, and is the knob that controls the bias–variance trade-off in the estimate of density: too narrow a bandwidth leads to a high-variance estimate (i.e., over-fitting), where the presence or absence of a single point makes a large difference. Too wide a bandwidth leads to a high-bias estimate (i.e., under-fitting) where the structure in the data is washed out by the wide kernel.
There is a long history in statistics of methods to quickly estimate the best bandwidth based on rather stringent assumptions about the data: if you look up the KDE implementations in the SciPy and StatsModels packages, for example, you will see implementations based on some of these rules.
In machine learning contexts, we've seen that such hyperparameter tuning often is done empirically via a cross-validation approach. With this in mind, the KernelDensity estimator in Scikit-Learn is designed such that it can be used directly within the Scikit-Learn's standard grid search tools. Here we will use GridSearchCV to optimize the bandwidth for the preceding dataset. Because we are looking at such a small dataset, we will use leave-one-out cross-validation, which minimizes the reduction in training set size for each cross-validation trial''
We will not discuss the matter much deeper here, oher than to show how the kernel with optimisation can be done. In the KDEbounded
function below, if bandwidth=np.nan
the bandwidth optimisation will be done. If the bandwidth
is assigned a numerical value, that value will be used.
The Leave-One-Out algorithm used here is quite slow for large data sets. So use a smaller data set to determine the best bandwidth and then use the value numerically for the large runs.
The pyradi.ryprob.KDEbounded
function requires a scikit learn version higher than 0.19. This work was done with version 0.20.1
A small writeup of this work with more examples is given in
https://github.com/NelisW/PythonNotesToSelf/blob/master/KernelDensityEstimation/KernelDensityEstimation.ipynb
https://kdepy.readthedocs.io/en/latest/examples.html#boundary-correction-using-mirroring
https://jakevdp.github.io/PythonDataScienceHandbook/05.13-kernel-density-estimation.html
https://jakevdp.github.io/blog/2013/12/01/kernel-density-estimation/
https://mglerner.github.io/posts/histograms-and-kernel-density-estimation-kde-2.html?p=28
https://github.com/tommyod/KDEpy
https://kdepy.readthedocs.io/en/latest/examples.html
https://towardsdatascience.com/histograms-and-density-plots-in-python-f6bda88f5ac0
https://towardsdatascience.com/how-to-find-probability-from-probability-density-plots-7c392b218bab
https://github.com/admond1994/calculate-probability-from-probability-density-plots/blob/master/cal_probability.ipynb
https://stats.stackexchange.com/questions/405357/how-to-choose-the-bandwidth-of-a-kde-in-python
https://www.homeworkhelponline.net/blog/math/tutorial-kde
https://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html
https://scikit-learn.org/stable/modules/cross_validation.html#cross-validation
https://scikit-learn.org/stable/modules/density.html
https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html
https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KernelDensity.html
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.html
https://glowingpython.blogspot.co.za/2012/08/kernel-density-estimation-with-scipy.html
https://pythonhosted.org/PyQt-Fit/KDE_tut.html
The pyradi.ryprob.KDEbounded
function parameters are as follows:
import numpy as np
import pyradi.ryprob as ryprob
import pyradi.ryplot as ryplot
def make_data(N, f=0.3, rseed=1):
rand = np.random.RandomState(rseed)
x = rand.randn(N) #mean-0 var=1
# create bimodal data by adding const to entries with index > f*N
# this means that some of the date keep old values and some shift
x[int(f * N):] += 5
return x
# helper function to set up experiments
def showexperiment(bounds=(np.nan,np.nan),bandwidth=np.nan,scale=1,offset=0,kernel='gaussian'):
x = scale * make_data(20) + offset
if not np.isnan(bounds[0]) and not np.isnan(bounds[1]):
lowbnd = bounds[0]
uppbnd = bounds[1]
x_d = np.linspace(lowbnd,uppbnd, 1000)
else:
lowbnd = np.nan
uppbnd = np.nan
tenp = (np.max(x) - np.min(x)) / 10
x_d = np.linspace(np.min(x)-tenp,np.max(x)+tenp, 1000)
x_d,kde,bandwidth,kernel = ryprob.KDEbounded(x_d,x,bandwidth=bandwidth,lowbnd=lowbnd,uppbnd=uppbnd, kernel=kernel)
print(f'x.min={np.min(x):.4e} x.max={np.max(x):.4e} x.shape={x.shape}')
print(f'x.min={np.min(x_d):.4e} x.max={np.max(x_d):.4e} x.shape={x_d.shape}')
print(f'Bandwidth={bandwidth}')
print(f'kernel={kernel}')
print(f'Integral of PDF={np.trapz(kde,x_d)}')
p = ryplot.Plotter(1,1,1)
p.plot(1,x,np.full_like(x, 0.011*np.max(kde)),linestyle='',markers='^')
p.plot(1,x_d,kde,'Kernel Density Estimate','$x_d$','$p$')
showexperiment(bounds=[-3.301,7.74481], bandwidth=np.nan, scale=1,offset=0)
saveHeaderArrayTextFile', 'loadColumnTextFile', 'loadHeaderTextFile
try:
import pyradi.ryutils as ryutils
print(ryutils.VersionInformation('matplotlib,numpy,pyradi,scipy,pandas'))
except:
print("pyradi.ryutils not found")