[change] extracted plotting and started new module for plotting; improved docstring quality
This commit is contained in:
parent
7921d12c80
commit
71f59b3c64
@ -2,6 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import operator
|
||||||
import os
|
import os
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import glob
|
import glob
|
||||||
@ -151,7 +152,6 @@ class Comparison(object):
|
|||||||
return rlist
|
return rlist
|
||||||
|
|
||||||
def get_array(self, phase, method_name):
|
def get_array(self, phase, method_name):
|
||||||
import operator
|
|
||||||
method = operator.methodcaller(method_name)
|
method = operator.methodcaller(method_name)
|
||||||
pdf_list = self.get_all(phase)
|
pdf_list = self.get_all(phase)
|
||||||
rarray = map(method, pdf_list)
|
rarray = map(method, pdf_list)
|
||||||
@ -258,6 +258,15 @@ class PDFDictionary(object):
|
|||||||
'time is not implemented yet! Sorry!')
|
'time is not implemented yet! Sorry!')
|
||||||
return PDFDictionary(picksdict_from_picks(cat[0]))
|
return PDFDictionary(picksdict_from_picks(cat[0]))
|
||||||
|
|
||||||
|
def get_all(self, phase):
|
||||||
|
rlist = list()
|
||||||
|
for phases in self.pdf_data.values():
|
||||||
|
try:
|
||||||
|
rlist.append(phases[phase])
|
||||||
|
except KeyError:
|
||||||
|
continue
|
||||||
|
return rlist
|
||||||
|
|
||||||
def generate_pdf_data(self, type='exp'):
|
def generate_pdf_data(self, type='exp'):
|
||||||
"""
|
"""
|
||||||
Returns probabiliy density function dictionary containing the
|
Returns probabiliy density function dictionary containing the
|
||||||
@ -350,31 +359,9 @@ class PDFstatistics(object):
|
|||||||
self.directory = directory
|
self.directory = directory
|
||||||
self.evtlist = list()
|
self.evtlist = list()
|
||||||
self.return_phase = None
|
self.return_phase = None
|
||||||
|
self.make_fnlist()
|
||||||
|
|
||||||
|
def make_fnlist(self, fn_pattern='*.xml'):
|
||||||
def readTheta(self, arname, dir, fnpattern):
|
|
||||||
"""
|
|
||||||
Loads an array from file into object instance.
|
|
||||||
:param arname: Name of Array beeing created.
|
|
||||||
:type arname: string
|
|
||||||
:param dir: Directory where file is to be found.
|
|
||||||
:type dir: string
|
|
||||||
:param fnpattern: file name pattern for reading multiple files into one array.
|
|
||||||
:type fnpattern: string
|
|
||||||
:return: a list with all args* from the files.
|
|
||||||
"""
|
|
||||||
exec('self.' + arname +' = []')
|
|
||||||
filelist = glob.glob1(dir, fnpattern)
|
|
||||||
for file in filelist:
|
|
||||||
fid = open(os.path.join(dir,file), 'r')
|
|
||||||
list = []
|
|
||||||
for line in fid.readlines():
|
|
||||||
list.append(eval(line))
|
|
||||||
exec('self.' + arname + ' += list')
|
|
||||||
fid.close()
|
|
||||||
|
|
||||||
|
|
||||||
def makeFileList(self, fn_pattern='*.xml'):
|
|
||||||
"""
|
"""
|
||||||
Takes a file pattern and searches for that recursively in the set path for the object.
|
Takes a file pattern and searches for that recursively in the set path for the object.
|
||||||
:param fn_pattern: A pattern that can identify all datafiles. Default Value = '*.xml'
|
:param fn_pattern: A pattern that can identify all datafiles. Default Value = '*.xml'
|
||||||
@ -389,7 +376,6 @@ class PDFstatistics(object):
|
|||||||
evtlist.append(os.path.join(root, file))
|
evtlist.append(os.path.join(root, file))
|
||||||
self.evtlist = evtlist
|
self.evtlist = evtlist
|
||||||
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
"""Iterating over the PDFstatistics object yields every single pdf from the list of events"""
|
"""Iterating over the PDFstatistics object yields every single pdf from the list of events"""
|
||||||
assert isinstance(self.return_phase, str), 'phase has to be set before being able to iterate over items...'
|
assert isinstance(self.return_phase, str), 'phase has to be set before being able to iterate over items...'
|
||||||
@ -401,7 +387,6 @@ class PDFstatistics(object):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
def set_return_phase(self, type):
|
def set_return_phase(self, type):
|
||||||
"""
|
"""
|
||||||
Sets the phase typ of event data that is returned on iteration over the object.
|
Sets the phase typ of event data that is returned on iteration over the object.
|
||||||
@ -414,40 +399,45 @@ class PDFstatistics(object):
|
|||||||
else:
|
else:
|
||||||
self.return_phase = type.upper()
|
self.return_phase = type.upper()
|
||||||
|
|
||||||
|
def quantile_distances(self, value):
|
||||||
def getQD(self,value):
|
|
||||||
"""
|
"""
|
||||||
Takes a probability value and and returns the distance
|
takes a probability value and and returns the distance
|
||||||
between two complementary quantiles.
|
between two complementary quantiles
|
||||||
For example: getQD(0.3) yields Quantile(1-0.3) - Quantile(0.3)
|
|
||||||
:param value: 0 < value < 0.5
|
.. math::
|
||||||
|
|
||||||
|
QA_\alpha = Q(1 - \alpha) - Q(\alpha)
|
||||||
|
|
||||||
|
:param value: probability value :math:\alpha
|
||||||
:type value: float
|
:type value: float
|
||||||
:return: returns a list of all quantile distances for all pdfs in
|
:return: list of all quantile distances for all pdfs in
|
||||||
the list of events.
|
the list of events.
|
||||||
"""
|
"""
|
||||||
QDlist = []
|
rlist = []
|
||||||
for pdf in self:
|
for pdf in self:
|
||||||
QD = pdf.quantile_distance(value)
|
rval = pdf.quantile_distance(value)
|
||||||
QDlist.append(QD)
|
rlist.append(rval)
|
||||||
return QDlist
|
return rlist
|
||||||
|
|
||||||
|
|
||||||
def getQDQ(self,value):
|
def quantile_distance_fractions(self, value):
|
||||||
"""
|
"""
|
||||||
Takes a probability value and and returns the fraction of
|
takes a probability value and returns the fraction of two
|
||||||
two quantile distances.
|
corresponding quantile distances
|
||||||
For example:
|
|
||||||
getQDQ(x) = getQD(0.5-x)/getQD(x)
|
.. math::
|
||||||
(Quantile(1-0.5-x) - Quantile(x)) / (Quantile(1-x) - Quantile(x))
|
|
||||||
:param value: 0 < value < 0.25
|
Q\Theta_\alpha = \frac{QA(0.5 - \alpha)}{QA(\alpha)}
|
||||||
|
|
||||||
|
:param value: probability value :math:\alpha
|
||||||
:return: returns a list of all quantile fractions for all pdfs in
|
:return: returns a list of all quantile fractions for all pdfs in
|
||||||
the list of events.
|
the list of events.
|
||||||
"""
|
"""
|
||||||
QDQlist = []
|
rlist = list()
|
||||||
for pdf in self:
|
for pdf in self:
|
||||||
QDQ = pdf.qtile_dist_quot(value)
|
rval = pdf.quantile_dist_frac(value)
|
||||||
QDQlist.append(QDQ)
|
rlist.append(rval)
|
||||||
return QDQlist
|
return rlist
|
||||||
|
|
||||||
|
|
||||||
def getSTD(self):
|
def getSTD(self):
|
||||||
@ -481,61 +471,6 @@ class PDFstatistics(object):
|
|||||||
'Actual phase type: {0}'.format(self.return_phase))
|
'Actual phase type: {0}'.format(self.return_phase))
|
||||||
|
|
||||||
|
|
||||||
def getBinList(self,l_boundary,u_boundary,nbins = 100):
|
|
||||||
"""
|
|
||||||
Helper function for self.histplot(). Takes in two boundaries and
|
|
||||||
a number of bins and creates a list of bins which can be passed
|
|
||||||
to self.histplot().
|
|
||||||
:param l_boundary: Any number.
|
|
||||||
:type l_boundary: float
|
|
||||||
:param u_boundary: Any number that is greater than l_boundary.
|
|
||||||
:type u_boundary: float
|
|
||||||
:param nbins: Any positive integer.
|
|
||||||
:type nbins: int
|
|
||||||
:return: A list of equidistant bins.
|
|
||||||
"""
|
|
||||||
if u_boundary <= l_boundary:
|
|
||||||
raise ValueError('Upper boundary must be greather than lower!')
|
|
||||||
elif nbins <= 0:
|
|
||||||
raise ValueError('Number of bins is not valid.')
|
|
||||||
binlist = []
|
|
||||||
for i in range(nbins):
|
|
||||||
binlist.append(l_boundary + i*(u_boundary-l_boundary)/nbins)
|
|
||||||
return binlist
|
|
||||||
|
|
||||||
|
|
||||||
def histplot(self, array, binlist, xlab = 'Values',
|
|
||||||
ylab = 'Frequency', title = None, fnout = None):
|
|
||||||
"""
|
|
||||||
Method to quickly show some distribution of data. Takes array like data,
|
|
||||||
and a list of bins. Editing detail and inserting a legend is not possible.
|
|
||||||
:param array: List of values.
|
|
||||||
:type array: Array like
|
|
||||||
:param binlist: List of bins.
|
|
||||||
:type binlist: list
|
|
||||||
:param xlab: A label for the x-axes.
|
|
||||||
:type xlab: str
|
|
||||||
:param ylab: A label for the y-axes.
|
|
||||||
:type ylab: str
|
|
||||||
:param title: A title for the Plot.
|
|
||||||
:type title: str
|
|
||||||
:param fnout: A path to save the plot instead of showing.
|
|
||||||
Has to contain filename and type. Like: 'path/to/file.png'
|
|
||||||
:type fnout. str
|
|
||||||
:return: -
|
|
||||||
"""
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
plt.hist(array,bins = binlist)
|
|
||||||
plt.xlabel(xlab)
|
|
||||||
plt.ylabel(ylab)
|
|
||||||
if title:
|
|
||||||
plt.title(title)
|
|
||||||
if fnout:
|
|
||||||
plt.savefig(fnout)
|
|
||||||
else:
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
|
|
||||||
def getPDFDict(self, month, evt):
|
def getPDFDict(self, month, evt):
|
||||||
"""
|
"""
|
||||||
Helper function for __iter__(). Should not be called directly.
|
Helper function for __iter__(). Should not be called directly.
|
||||||
@ -583,13 +518,19 @@ class PDFstatistics(object):
|
|||||||
def main():
|
def main():
|
||||||
root_dir ='/home/sebastianp/Codetesting/xmls/'
|
root_dir ='/home/sebastianp/Codetesting/xmls/'
|
||||||
Insheim = PDFstatistics(root_dir)
|
Insheim = PDFstatistics(root_dir)
|
||||||
Insheim.makeFileList()
|
Insheim.make_fnlist()
|
||||||
Insheim.set_return_phase('p')
|
Insheim.set_return_phase('p')
|
||||||
Insheim.getSTD()
|
Insheim.getSTD()
|
||||||
qdlist = Insheim.getQDQ(0.3)
|
qdlist = Insheim.quantile_distance_fractions(0.2)
|
||||||
binlist = Insheim.getBinList(0.,3.)
|
|
||||||
print qdlist
|
print qdlist
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
import cProfile
|
||||||
|
|
||||||
|
pr = cProfile.Profile()
|
||||||
|
pr.enable()
|
||||||
|
main()
|
||||||
|
pr.disable()
|
||||||
|
# after your program ends
|
||||||
|
pr.print_stats(sort="calls")
|
@ -363,6 +363,18 @@ class ProbabilityDensityFunction(object):
|
|||||||
return m
|
return m
|
||||||
|
|
||||||
def quantile_distance(self, prob_value):
|
def quantile_distance(self, prob_value):
|
||||||
|
"""
|
||||||
|
takes a probability value and and returns the distance
|
||||||
|
between two complementary quantiles
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
|
||||||
|
QA_\alpha = Q(1 - \alpha) - Q(\alpha)
|
||||||
|
|
||||||
|
:param value: probability value :math:\alpha
|
||||||
|
:type value: float
|
||||||
|
:return: quantile distance
|
||||||
|
"""
|
||||||
if 0 >= prob_value or prob_value >= 0.5:
|
if 0 >= prob_value or prob_value >= 0.5:
|
||||||
raise ValueError('Value out of range.')
|
raise ValueError('Value out of range.')
|
||||||
ql = self.quantile(prob_value)
|
ql = self.quantile(prob_value)
|
||||||
@ -370,7 +382,20 @@ class ProbabilityDensityFunction(object):
|
|||||||
return qu - ql
|
return qu - ql
|
||||||
|
|
||||||
|
|
||||||
def qtile_dist_quot(self,x):
|
def quantile_dist_frac(self, x):
|
||||||
|
"""
|
||||||
|
takes a probability value and returns the fraction of two
|
||||||
|
corresponding quantile distances (
|
||||||
|
:func:`pylot.core.util.pdf.ProbabilityDensityFunction
|
||||||
|
#quantile_distance`)
|
||||||
|
|
||||||
|
.. math::
|
||||||
|
|
||||||
|
Q\Theta_\alpha = \frac{QA(0.5 - \alpha)}{QA(\alpha)}
|
||||||
|
|
||||||
|
:param value: probability value :math:\alpha
|
||||||
|
:return: quantile distance fraction
|
||||||
|
"""
|
||||||
if x <= 0 or x >= 0.25:
|
if x <= 0 or x >= 0.25:
|
||||||
raise ValueError('Value out of range.')
|
raise ValueError('Value out of range.')
|
||||||
return self.quantile_distance(0.5-x)/self.quantile_distance(x)
|
return self.quantile_distance(0.5-x)/self.quantile_distance(x)
|
||||||
|
57
pylot/core/util/plotting.py
Normal file
57
pylot/core/util/plotting.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
def create_bin_list(l_boundary, u_boundary, nbins=100):
|
||||||
|
"""
|
||||||
|
takes two boundaries and a number of bins and creates a list of bins for
|
||||||
|
histogram plotting
|
||||||
|
:param l_boundary: Any number.
|
||||||
|
:type l_boundary: float
|
||||||
|
:param u_boundary: Any number that is greater than l_boundary.
|
||||||
|
:type u_boundary: float
|
||||||
|
:param nbins: Any positive integer.
|
||||||
|
:type nbins: int
|
||||||
|
:return: A list of equidistant bins.
|
||||||
|
"""
|
||||||
|
if u_boundary <= l_boundary:
|
||||||
|
raise ValueError('Upper boundary must be greather than lower!')
|
||||||
|
elif nbins <= 0:
|
||||||
|
raise ValueError('Number of bins is not valid.')
|
||||||
|
binlist = []
|
||||||
|
for i in range(nbins):
|
||||||
|
binlist.append(l_boundary + i * (u_boundary - l_boundary) / nbins)
|
||||||
|
return binlist
|
||||||
|
|
||||||
|
|
||||||
|
def histplot(array, binlist, xlab='Values',
|
||||||
|
ylab='Frequency', title=None, fnout=None):
|
||||||
|
"""
|
||||||
|
function to quickly show some distribution of data. Takes array like data,
|
||||||
|
and a list of bins. Editing detail and inserting a legend is not possible.
|
||||||
|
:param array: List of values.
|
||||||
|
:type array: Array like
|
||||||
|
:param binlist: List of bins.
|
||||||
|
:type binlist: list
|
||||||
|
:param xlab: A label for the x-axes.
|
||||||
|
:type xlab: str
|
||||||
|
:param ylab: A label for the y-axes.
|
||||||
|
:type ylab: str
|
||||||
|
:param title: A title for the Plot.
|
||||||
|
:type title: str
|
||||||
|
:param fnout: A path to save the plot instead of showing.
|
||||||
|
Has to contain filename and type. Like: 'path/to/file.png'
|
||||||
|
:type fnout. str
|
||||||
|
:return: -
|
||||||
|
"""
|
||||||
|
|
||||||
|
plt.hist(array, bins=binlist)
|
||||||
|
plt.xlabel(xlab)
|
||||||
|
plt.ylabel(ylab)
|
||||||
|
if title:
|
||||||
|
plt.title(title)
|
||||||
|
if fnout:
|
||||||
|
plt.savefig(fnout)
|
||||||
|
else:
|
||||||
|
plt.show()
|
Loading…
Reference in New Issue
Block a user