def getFileNames(self, maskName: str, sampleFreq: float) -> Tuple[str, str]: """Get the name of a mask file This method is here to give consistent file names for mask files. Parameters ---------- maskName : str The name of the mask sampleFreq : float The sampling frequency of the data Returns ------- infoFile : str The name of the mask infoFile winFile : str The name of the mask winFile """ checkAndMakeDir(self.datapath) sampleFreqStr = fileFormatSampleFreq(sampleFreq) name: str = maskName + "_{}".format(sampleFreqStr) infoFile: str = os.path.join(self.datapath, name + ".info") winFile: str = os.path.join( self.datapath, name) # no need for extension here, numpy adds one return infoFile, winFile
def writeResult(self, specdir: str, postpend: str, freq, data: np.ndarray, variances: np.ndarray, **kwargs): """Write the transfer function file Parameters ---------- specdir : str The spectra data being used for the transfer function estimate postpend : str The optional postpend to the transfer function file data : np.ndarray The transfer function estimates variances : np.ndarray The transfer function variances remotesite : str, optional Optionally, if there is a remote site remotechans : List[str], optional Optionally add the remote channels if there is a remote site """ # path for writing out to sampleFreqStr = fileFormatSampleFreq(self.decParams.sampleFreq) if postpend == "": filename = "{}_fs{:s}_{}".format(self.outSite, sampleFreqStr, specdir) else: filename = "{}_fs{:s}_{}_{}".format(self.outSite, sampleFreqStr, specdir, postpend) datapath = os.path.join(self.outpath, sampleFreqStr) checkAndMakeDir(datapath) outfile = os.path.join(datapath, filename) # now construct the transferFunctionData object numFreq = len(freq) dataDict = {} varDict = {} for i in range(0, self.outSize): for j in range(0, self.inSize): key = "{}{}".format(self.outChannels[i], self.inChannels[j]) dataArray = np.empty(shape=(numFreq), dtype="complex") varArray = np.empty(shape=(len(freq)), dtype="float") for ifreq in range(0, numFreq): dataArray[ifreq] = data[ifreq][i, j] varArray[ifreq] = variances[ifreq][i, j] dataDict[key] = dataArray varDict[key] = varArray tfData = TransferFunctionData(freq, dataDict, varDict) # now make the writer and write out tfWriter = TransferFunctionWriter(outfile, tfData) tfWriter.setHeaders( sampleFreq=self.decParams.sampleFreq, insite=self.inSite, inchans=self.inChannels, outsite=self.outSite, outchans=self.outChannels, ) if "remotesite" in kwargs: tfWriter.addHeader("remotesite", kwargs["remotesite"]) if "remotechans" in kwargs: tfWriter.addHeader("remotechans", kwargs["remotechans"]) tfWriter.write()
def getTransferFunctionData(projData: ProjectData, site: str, sampleFreq: float, **kwargs) -> TransferFunctionData: """Get transfer function data Parameters ---------- projData : projecData The project data site : str Site to get the transfer functiond data for sampleFreq : int, float The sampling frequency for which to get the transfer function data specdir : str, optional The spectra directories used postpend : str, optional The postpend on the transfer function files """ from resistics.transfunc.io import TransferFunctionReader options: Dict = dict() options["specdir"]: str = projData.config.configParams["Spectra"][ "specdir"] options["postpend"]: str = "" options = parseKeywords(options, kwargs) # deal with the postpend if options["postpend"] != "": postpend = "_{}".format(options["postpend"]) else: postpend = options["postpend"] siteData = projData.getSiteData(site) sampleFreqStr = fileFormatSampleFreq(sampleFreq) path = os.path.join( siteData.transFuncPath, "{:s}".format(sampleFreqStr), "{}_fs{:s}_{}{}".format(site, sampleFreqStr, options["specdir"], postpend), ) # check path if not checkFilepath(path): projectWarning("No transfer function file with name {}".format(path)) return False projectText( "Reading transfer function for site {}, sample frequency {}, file {}". format(site, sampleFreq, path)) tfReader = TransferFunctionReader(path) tfReader.printInfo() return tfReader.tfData
def test_fileFormatSampleFreq() -> None: from resistics.common.io import fileFormatSampleFreq assert fileFormatSampleFreq(128) == "128_000" assert fileFormatSampleFreq(4096.1567) == "4096_157"
def viewStatisticDensityplot( projData: ProjectData, site: str, sampleFreq: Union[int, float], stat: str, crossplots: List[List[str]], **kwargs ) -> Union[Figure, None]: """View statistic data as a density plot for a single sampling frequency of a site Parameters ---------- projData : ProjectData A project instance site : str The site for which to plot statistics stat : str The statistic to plot sampleFreq : float The sampling frequency for which to plot statistics crossplots : List[List[str]] The statistic element pairs to crossplot declevel : int The decimation level to plot eFreqI : int The evaluation frequency index specdir : str The spectra directory maskname : str Mask name xlim : List, optional Limits for the x axis ylim : List, optional Limits for the y axis maxcols : int The maximum number of columns in the plots show : bool, optional Show the spectra plot save : bool, optional Save the plot to the images directory plotoptions : Dict, optional Dictionary of plot options Returns ------- matplotlib.pyplot.figure or None A matplotlib figure unless the plot is not shown and is saved, in which case None and the figure is closed. If no data was found, None. """ from resistics.common.plot import ( savePlot, plotOptionsSpec, getPlotRowsAndCols, colorbar2dSpectra, ) options = {} options["declevel"] = 0 options["eFreqI"] = 0 options["specdir"] = projData.config.configParams["Spectra"]["specdir"] options["maskname"] = "" options["xlim"] = [] options["ylim"] = [] options["maxcols"] = 2 options["show"] = True options["save"] = False options["plotoptions"] = plotOptionsSpec() options = parseKeywords(options, kwargs) projectText( "Plotting density plot for statistic {}, site {} and sampling frequency {}".format( stat, site, sampleFreq ) ) statData = getStatisticDataForSampleFreq( projData, site, sampleFreq, stat, declevel=options["declevel"], specdir=options["specdir"], ) statMeas = list(statData.keys()) if len(statMeas) == 0: projectWarning( "No statistic files for site {}, sampling frequency {}, statistic {} and decimation level {}".format( site, sampleFreq, stat, options["declevel"] ) ) return None # get the evaluation frequency eFreq = statData[statMeas[0]].evalFreq[options["eFreqI"]] # get the mask data maskWindows = [] if options["maskname"] != "": maskData = getMaskData(projData, site, options["maskname"], sampleFreq) maskWindows = maskData.getMaskWindowsFreq( options["declevel"], options["eFreqI"] ) # plot information nrows, ncols = getPlotRowsAndCols(options["maxcols"], len(crossplots)) plotfonts = options["plotoptions"]["plotfonts"] fig = plt.figure(figsize=options["plotoptions"]["figsize"]) # suptitle st = fig.suptitle( "{} density plots for {}, sampling frequency {} Hz,\ndecimation level {} and evaluation frequency {} Hz".format( stat, site, sampleFreq, options["declevel"], eFreq ), fontsize=plotfonts["suptitle"], ) st.set_y(0.98) # now plot the data for idx, cplot in enumerate(crossplots): ax = plt.subplot(nrows, ncols, idx + 1) plt.title("Crossplot {}".format(cplot), fontsize=plotfonts["title"]) plotAll1 = [] plotAll2 = [] for meas in statMeas: stats = statData[meas].getStats(maskwindows=maskWindows) plotI1 = statData[meas].winStats.index(cplot[0]) plotData1 = np.squeeze(stats[:, options["eFreqI"], plotI1]) plotI2 = statData[meas].winStats.index(cplot[1]) plotData2 = np.squeeze(stats[:, options["eFreqI"], plotI2]) # add to all data if plotData1.size == 0: continue if plotData1.size == 1: plotAll1 = plotAll1 + [float(plotData1)] plotAll2 = plotAll2 + [float(plotData2)] else: plotAll1 = plotAll1 + plotData1.tolist() plotAll2 = plotAll2 + plotData2.tolist() plotAll1 = np.array(plotAll1) plotAll2 = np.array(plotAll2) nbins = 200 if len(options["xlim"]) > 0: plt.xlim(options["xlim"]) rangex = options["xlim"] else: minx = np.percentile(plotAll1, 2) maxx = np.percentile(plotAll1, 98) ax.set_xlim(minx, maxx) rangex = [minx, maxx] if len(options["ylim"]) > 0: plt.ylim(options["ylim"]) rangey = options["ylim"] else: miny = np.percentile(plotAll2, 2) maxy = np.percentile(plotAll2, 98) ax.set_ylim(miny, maxy) rangey = [miny, maxy] plt.hist2d( plotAll1, plotAll2, bins=(nbins, nbins), range=[rangex, rangey], cmap=plt.cm.inferno, ) # axis format plt.xlabel(cplot[0], fontsize=plotfonts["axisLabel"]) plt.ylabel(cplot[1], fontsize=plotfonts["axisLabel"]) plt.grid(True) # set tick sizes for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(plotfonts["axisTicks"]) # plot format, show and save # fig.tight_layout(rect=[0.02, 0.02, 0.98, 0.92]) if options["save"]: impath = projData.imagePath sampleFreqStr = fileFormatSampleFreq(sampleFreq) filename = "statDensityplot_{:s}_{:s}_{:s}_dec{:d}_efreq{:d}_{:s}".format( stat, site, sampleFreqStr, options["declevel"], options["eFreqI"], options["specdir"], ) if options["maskname"] != "": filename = "{}_{}".format(filename, options["maskname"]) savename = savePlot(impath, filename, fig) projectText("Image saved to file {}".format(savename)) if options["show"]: plt.show(block=options["plotoptions"]["block"]) if not options["show"] and options["save"]: plt.close(fig) return None return fig
def viewStatisticHistogram( projData: ProjectData, site: str, sampleFreq: float, stat: str, **kwargs ) -> Union[Figure, None]: """View statistic histograms for a single sampling frequency of a site Parameters ---------- projData : ProjectData A project instance site : str The site for which to plot statistics stat : str The statistic to plot sampleFreq : float The sampling frequency for which to plot statistics declevel : int The decimation level to plot eFreqI : int The evaluation frequency index specdir : str The spectra directory maskname : str Mask name numbins : int The number of bins for the histogram data binning xlim : List, optional Limits for the x axis maxcols : int The maximum number of columns in the plots show : bool, optional Show the spectra plot save : bool, optional Save the plot to the images directory plotoptions : Dict, optional Dictionary of plot options Returns ------- matplotlib.pyplot.figure or None A matplotlib figure unless the plot is not shown and is saved, in which case None. If no data was found, None. """ from resistics.common.plot import savePlot, plotOptionsSpec, getPlotRowsAndCols options = {} options["declevel"] = 0 options["eFreqI"] = 0 options["specdir"] = projData.config.configParams["Spectra"]["specdir"] options["maskname"] = "" options["numbins"] = 40 options["xlim"] = [] options["maxcols"] = 4 options["show"] = True options["save"] = False options["plotoptions"] = plotOptionsSpec() options = parseKeywords(options, kwargs) projectText( "Plotting histogram for statistic {}, site {} and sampling frequency {}".format( stat, site, sampleFreq ) ) statData = getStatisticDataForSampleFreq( projData, site, sampleFreq, stat, declevel=options["declevel"], specdir=options["specdir"], ) statMeas = list(statData.keys()) if len(statMeas) == 0: projectWarning( "No statistic files for site {}, sampling frequency {}, statistic {} and decimation level {}".format( site, sampleFreq, stat, options["declevel"] ) ) return None # get the statistic components statComponents = statData[statMeas[0]].winStats # get the evaluation frequency eFreq = statData[statMeas[0]].evalFreq[options["eFreqI"]] # get the mask data maskWindows = [] if options["maskname"] != "": maskData = getMaskData(projData, site, options["maskname"], sampleFreq) maskWindows = maskData.getMaskWindowsFreq( options["declevel"], options["eFreqI"] ) # plot information nrows, ncols = getPlotRowsAndCols(options["maxcols"], len(statComponents)) numbins = options["numbins"] plotfonts = options["plotoptions"]["plotfonts"] fig = plt.figure(figsize=options["plotoptions"]["figsize"]) # suptitle st = fig.suptitle( "{} histogram for {}, sampling frequency {} Hz, decimation level {} and evaluation frequency {} Hz".format( stat, site, sampleFreq, options["declevel"], eFreq ), fontsize=plotfonts["suptitle"], ) st.set_y(0.98) # now plot the data for idx, val in enumerate(statComponents): ax = plt.subplot(nrows, ncols, idx + 1) plt.title("Histogram {}".format(val), fontsize=plotfonts["title"]) plotData = np.empty(shape=(0)) for meas in statMeas: stats = statData[meas].getStats(maskwindows=maskWindows) plotData = np.concatenate( (plotData, np.squeeze(stats[:, options["eFreqI"], idx])) ) # remove infinities and nans plotData = plotData[np.isfinite(plotData)] # x axis options xlim = ( options["xlim"] if len(options["xlim"]) > 0 else [np.min(plotData), np.max(plotData)] ) plt.xlim(xlim) plt.xlabel("Value", fontsize=plotfonts["axisLabel"]) # now plot with xlim in mind plt.hist(plotData, numbins, range=xlim, facecolor="red", alpha=0.75) plt.grid() # y axis options plt.ylabel("Count", fontsize=plotfonts["axisLabel"]) # set tick sizes for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(plotfonts["axisTicks"]) # plot format, show and save fig.tight_layout(rect=[0.02, 0.02, 0.98, 0.92]) if options["save"]: impath = projData.imagePath sampleFreqStr = fileFormatSampleFreq(sampleFreq) filename = "statHist_{:s}_{:s}_{:s}_dec{:d}_efreq{:d}_{:s}".format( stat, site, sampleFreqStr, options["declevel"], options["eFreqI"], options["specdir"], ) if options["maskname"] != "": filename = "{}_{}".format(filename, options["maskname"]) savename = savePlot(impath, filename, fig) projectText("Image saved to file {}".format(savename)) if options["show"]: plt.show(block=options["plotoptions"]["block"]) if not options["show"] and options["save"]: plt.close(fig) return None return fig
def viewStatistic( projData: ProjectData, site: str, sampleFreq: Union[int, float], stat: str, **kwargs ) -> Union[Figure, None]: """View statistic data for a single sampling frequency of a site Parameters ---------- projData : ProjectData A project instance site : str The site for which to plot statistics stat : str The statistic to plot sampleFreq : float The sampling frequency for which to plot statistics declevel : int The decimation level to plot eFreqI : int The evaluation frequency index specdir : str The spectra directory maskname : str Mask name clim : List, optional Limits for colourbar axis xlim : List, optional Limits for the x axis ylim : List, optional Limits for the y axis colortitle : str, optional Title for the colourbar show : bool, optional Show the spectra plot save : bool, optional Save the plot to the images directory plotoptions : Dict, optional Dictionary of plot options Returns ------- matplotlib.pyplot.figure or None A matplotlib figure unless the plot is not shown and is saved, in which case None and the figure is closed. If no data was found, None. """ from resistics.common.plot import savePlot, plotOptionsSpec, getPlotRowsAndCols options = {} options["declevel"] = 0 options["eFreqI"] = 0 options["specdir"] = projData.config.configParams["Spectra"]["specdir"] options["maskname"] = "" options["clim"] = [] options["xlim"] = [] options["ylim"] = [] options["colortitle"] = "" options["show"] = True options["save"] = False options["plotoptions"] = plotOptionsSpec() options = parseKeywords(options, kwargs) projectText( "Plotting statistic {} for site {} and sampling frequency {}".format( stat, site, sampleFreq ) ) statData = getStatisticDataForSampleFreq( projData, site, sampleFreq, stat, declevel=options["declevel"], specdir=options["specdir"], ) statMeas = list(statData.keys()) if len(statMeas) == 0: projectWarning( "No statistic files for site {}, sampling frequency {}, statistic {} and decimation level {}".format( site, sampleFreq, stat, options["declevel"] ) ) return None # get the evaluation frequency eFreq = statData[statMeas[0]].evalFreq[options["eFreqI"]] # get the mask data maskWindows = [] if options["maskname"] != "": maskData = getMaskData(projData, site, options["maskname"], sampleFreq) maskWindows = maskData.getMaskWindowsFreq( options["declevel"], options["eFreqI"] ) # setup the figure plotfonts = options["plotoptions"]["plotfonts"] fig = plt.figure(figsize=options["plotoptions"]["figsize"]) # get the date limits siteData = projData.getSiteData(site) if len(options["xlim"]) == 0: start = siteData.getMeasurementStart(statMeas[0]) end = siteData.getMeasurementEnd(statMeas[0]) for meas in statMeas: start = min(start, siteData.getMeasurementStart(meas)) end = max(end, siteData.getMeasurementEnd(meas)) options["xlim"] = [start, end] # do the plots for meas in statMeas: statData[meas].view( options["eFreqI"], fig=fig, xlim=options["xlim"], ylim=options["ylim"], clim=options["clim"], label=meas, plotfonts=options["plotoptions"]["plotfonts"], maskwindows=maskWindows, ) # add a legened plt.legend(markerscale=4, fontsize=plotfonts["legend"]) # do the title after all the plots fig.suptitle( "{} values for {}, sampling frequency = {:.2f} Hz, decimation level = {} and evaluation frequency {} Hz".format( stat, site, sampleFreq, options["declevel"], eFreq ), fontsize=plotfonts["suptitle"], ) # plot format, show and save fig.tight_layout(rect=[0.02, 0.02, 0.98, 0.92]) if options["save"]: impath = projData.imagePath sampleFreqStr = fileFormatSampleFreq(sampleFreq) filename = "stat_{:s}_{:s}_{:s}_dec{:d}_efreq{:d}_{:s}".format( stat, site, sampleFreqStr, options["declevel"], options["eFreqI"], options["specdir"], ) if options["maskname"] != "": filename = "{}_{}".format(filename, options["maskname"]) savename = savePlot(impath, filename, fig) projectText("Image saved to file {}".format(savename)) if options["show"]: plt.show(block=options["plotoptions"]["block"]) if not options["show"] and options["save"]: plt.close(fig) return None return fig