From 7c8a437dd88c8317354d63d3ff5f0511e3885b23 Mon Sep 17 00:00:00 2001 From: sebastianp Date: Tue, 9 Aug 2016 11:07:18 +0200 Subject: [PATCH 1/4] [task] additional changes for phase separation --- pylot/core/pick/compare.py | 57 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/pylot/core/pick/compare.py b/pylot/core/pick/compare.py index 5c4bed4d..39f43a3a 100644 --- a/pylot/core/pick/compare.py +++ b/pylot/core/pick/compare.py @@ -345,7 +345,7 @@ class PDFstatistics(object): ''' def __init__(self, directory): self.directory = directory - + self.return_phase = None def readTheta(self, arname, dir, fnpattern): exec('self.' + arname +' = []') @@ -363,24 +363,25 @@ class PDFstatistics(object): self.evtlist = glob.glob1((os.path.join(self.directory)), '*.xml') - def nextPDF(self): + def __iter__(self): + assert isinstance(self.return_phase, str), 'phase has to be set before being able to iterate over items...' for evt in self.evtlist: self.getPDFDict(self.directory, evt) for station, pdfs in self.pdfdict.pdf_data.items(): try: - yield pdfs['P'] + yield pdfs[self.return_phase] except KeyError: - yield np.nan - try: - yield pdfs['S'] - except KeyError: - yield np.nan + continue + def set_return_phase(self, type): + if type.upper() not in 'PS': + raise ValueError("phase type must be either 'P' or 'S'!") + else: + self.return_phase = type.upper() def getQD(self,value): - pdfgen = self.nextPDF() QDlist = [] - for pdf in pdfgen: + for pdf in self: try: QD = pdf.quantile_distance(value) QDlist.append(QD) @@ -391,9 +392,8 @@ class PDFstatistics(object): def getQDQ(self,value): - pdfgen = self.nextPDF() QDQlist = [] - for pdf in pdfgen: + for pdf in self: try: QDQ = pdf.qtile_dist_quot(value) QDQlist.append(QDQ) @@ -404,24 +404,23 @@ class PDFstatistics(object): def getSTD(self): - pdfgen = self.nextPDF() - self.p_std = [] - self.s_std = [] - for pdf in pdfgen: + std = [] + for pdf in self: try: - self.p_std.append(pdf['P'].standard_deviation()) + std.append(pdf.standard_deviation()) except KeyError: - pass - try: - self.s_std.append(pdf['S'].standard_deviation()) - except KeyError: - pass - self.makeArray() + continue + std = np.array(std) + self.set_stdarray(std) - - def makeArray(self): - self.p_stdarray = np.array(self.p_std) - self.s_stdarray = np.array(self.s_std) + def set_stdarray(self, array): + if self.return_phase == 'P': + self.p_stdarray = array + elif self.return_phase == 'S': + self.s_stdarray = array + else: + raise ValueError('phase type not set properly...\n' + 'Actual phase type: {0}'.format(self.return_phase)) def getBinList(self,l_boundary,u_boundary,nbins = 100): @@ -468,9 +467,11 @@ def main(): root_dir ='/home/sebastianp/Codetesting/xmls/' Insheim = PDFstatistics(root_dir) Insheim.makeFileList() + Insheim.set_return_phase('p') Insheim.getSTD() - Insheim.getStatistics() qdlist = Insheim.getQDQ(0.3) + binlist = Insheim.getBinList(0.,3.) + Insheim.histplot(qdlist,binlist) print qdlist From 37ed561f73c0deb86f7300a9db9ba53dd0f15438 Mon Sep 17 00:00:00 2001 From: sebastianp Date: Tue, 9 Aug 2016 12:21:00 +0200 Subject: [PATCH 2/4] removing unnecessary code --- pylot/core/pick/compare.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/pylot/core/pick/compare.py b/pylot/core/pick/compare.py index 39f43a3a..3116f137 100644 --- a/pylot/core/pick/compare.py +++ b/pylot/core/pick/compare.py @@ -382,24 +382,16 @@ class PDFstatistics(object): def getQD(self,value): QDlist = [] for pdf in self: - try: - QD = pdf.quantile_distance(value) - QDlist.append(QD) - except AttributeError as e: - if e.message == "'float' object has no attribute 'quantile_distance'": - continue + QD = pdf.quantile_distance(value) + QDlist.append(QD) return QDlist def getQDQ(self,value): QDQlist = [] for pdf in self: - try: - QDQ = pdf.qtile_dist_quot(value) - QDQlist.append(QDQ) - except AttributeError as e: - if e.message == "'float' object has no attribute 'quantile_distance'": - continue + QDQ = pdf.qtile_dist_quot(value) + QDQlist.append(QDQ) return QDQlist @@ -413,6 +405,7 @@ class PDFstatistics(object): std = np.array(std) self.set_stdarray(std) + def set_stdarray(self, array): if self.return_phase == 'P': self.p_stdarray = array @@ -471,7 +464,6 @@ def main(): Insheim.getSTD() qdlist = Insheim.getQDQ(0.3) binlist = Insheim.getBinList(0.,3.) - Insheim.histplot(qdlist,binlist) print qdlist From f8623fc4545026b96423ecebd921cb535a8eb38f Mon Sep 17 00:00:00 2001 From: Sebastian Wehling-Benatelli Date: Tue, 9 Aug 2016 13:10:25 +0200 Subject: [PATCH 3/4] [enhancement] added capability of searching for files in subdirectories --- pylot/core/pick/compare.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pylot/core/pick/compare.py b/pylot/core/pick/compare.py index 39f43a3a..b3302fe0 100644 --- a/pylot/core/pick/compare.py +++ b/pylot/core/pick/compare.py @@ -345,6 +345,7 @@ class PDFstatistics(object): ''' def __init__(self, directory): self.directory = directory + self.evtlist = list() self.return_phase = None def readTheta(self, arname, dir, fnpattern): @@ -358,10 +359,15 @@ class PDFstatistics(object): exec('self.' + arname + ' += list') fid.close() - - def makeFileList(self, fn_pattern='*'): - self.evtlist = glob.glob1((os.path.join(self.directory)), '*.xml') - + def makeFileList(self, fn_pattern='*.xml'): + evtlist = list() + evtlist = glob.glob1((os.path.join(self.directory)), fn_pattern) + if not evtlist: + for root, _, files in os.walk(self.directory): + for file in files: + if file.endswith(fn_pattern[1:]): + evtlist.append(os.path.join(root, file)) + self.evtlist = evtlist def __iter__(self): assert isinstance(self.return_phase, str), 'phase has to be set before being able to iterate over items...' From 14c22d73b7b4db931de9dc4f9e107b0515dbe938 Mon Sep 17 00:00:00 2001 From: sebastianp Date: Wed, 10 Aug 2016 10:06:34 +0200 Subject: [PATCH 4/4] [enhancement] adding a save option for plotting --- pylot/core/pick/compare.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pylot/core/pick/compare.py b/pylot/core/pick/compare.py index 3116f137..4fc8ee19 100644 --- a/pylot/core/pick/compare.py +++ b/pylot/core/pick/compare.py @@ -424,7 +424,8 @@ class PDFstatistics(object): def histplot(self, array, binlist, xlab = 'Values', - ylab = 'Frequency', title = None, label=None): + ylab = 'Frequency', title = None, label=None, + fnout = None): import matplotlib.pyplot as plt plt.hist(array,bins = binlist) plt.xlabel('Values') @@ -434,7 +435,10 @@ class PDFstatistics(object): if label: title_str += ' (' + label + ')' plt.title(title_str) - plt.show() + if fnout: + plt.savefig(fnout+'histplot.png') + else: + plt.show() def getPDFDict(self, month, evt):