From 505add6ae19ce896012eb1ebec2a9cb4b78cf832 Mon Sep 17 00:00:00 2001 From: "J.A. de Jong" Date: Sat, 29 Dec 2018 15:34:24 +0100 Subject: [PATCH] Added loads of comments and readability improvements. --- .gitignore | 1 + CMakeLists.txt | 13 +++++++------ lasp/lasp_atomic.py | 15 +++++++-------- lasp/lasp_avstream.py | 3 ++- lasp/lasp_common.py | 16 +++------------- lasp/lasp_config.py | 12 +++++------- lasp/lasp_measurement.py | 12 ++++++------ lasp/lasp_octavefilter.py | 4 ++-- lasp/lasp_playback.py | 9 ++++++++- lasp/lasp_reverb.py | 2 +- lasp/lasp_slm.py | 6 +++++- lasp/lasp_weighcal.py | 2 +- lasp/tools/plot.py | 1 - lasp/wrappers.pyx | 11 +++++++---- tools/__init__.py | 6 ------ 15 files changed, 55 insertions(+), 58 deletions(-) delete mode 100644 tools/__init__.py diff --git a/.gitignore b/.gitignore index 361b80b..0553215 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ lasp_octave_fir.* lasp/ui_* resources_rc.py lasp/device/lasp_daqdevice.c +.ropeproject diff --git a/CMakeLists.txt b/CMakeLists.txt index b9aaf31..c1a12d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,8 @@ endif(LASP_FLOAT STREQUAL "double") if(NOT DEFINED LASP_DEBUG) - message(FATAL_ERROR "LASP_DEBUG flag not defined. Please set -DLASP_DEBUG=TRUE or -DLASP_DEBUG=FALSE") + message(FATAL_ERROR "LASP_DEBUG flag not defined. Please set -DLASP_DEBUG=TRUE + or -DLASP_DEBUG=FALSE") endif(NOT DEFINED LASP_DEBUG) # ##################### END Cmake variables converted to a macro @@ -46,14 +47,14 @@ if(LASP_DEBUG) add_definitions(-DTRACERNAME=${TRACERNAME}) add_definitions(-DDEBUG) add_definitions(-DTRACER=1) - + set(CYTHON_VARIABLES "#cython: boundscheck=True") # This will produce html files set(CYTHON_ANNOTATE ON) # Add the __FILENAME__ macro # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") else() - message("Building release code") + message("Building LASP for release") set(CMAKE_BUILD_TYPE Release) set(CYTHON_ANNOTATE OFF) set(CYTHON_NO_DOCSTRINGS ON) @@ -61,7 +62,7 @@ else() # Strip unnecessary symbols # set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--gc-sections") # set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--gc-sections") - + add_definitions(-DTRACER=0 -DNDEBUG) endif(LASP_DEBUG) @@ -85,9 +86,9 @@ set(CMAKE_C_FLAGS_RELEASE "-O2 -mfpmath=sse -march=x86-64 -mtune=native \ # set(CMAKE_C_FLAGS_RELEASE "-O2 -march=native -mtune=native -fomit-frame-pointer") if(LASP_USE_BLAS) - add_definitions(-DLASP_USE_BLAS=1) + add_definitions(-DLASP_USE_BLAS=1) else() - add_definitions(-DLASP_USE_BLAS=0) + add_definitions(-DLASP_USE_BLAS=0) endif(LASP_USE_BLAS) # ############################# End compilation flags diff --git a/lasp/lasp_atomic.py b/lasp/lasp_atomic.py index 9f10a13..b95ad30 100644 --- a/lasp/lasp_atomic.py +++ b/lasp/lasp_atomic.py @@ -2,13 +2,13 @@ # -*- coding: utf-8 -*- """ Provides a simple atomic variable: - + >>> a = Atomic(0) Retrieve the value >>> b = a() Set a new value: ->>> a <<= b +>>> a <<= b Get conversion to boolean: >>> if a: do something @@ -20,11 +20,12 @@ Atomic increment: """ from threading import Lock + class Atomic: def __init__(self, val): self._val = val self._lock = Lock() - + def __iadd__(self, toadd): with self._lock: self._val += toadd @@ -34,18 +35,16 @@ class Atomic: with self._lock: self._val -= toadd return self - - + def __bool__(self): with self._lock: return self._val - + def __ilshift__(self, other): with self._lock: self._val = other return self - + def __call__(self): with self._lock: return self._val - \ No newline at end of file diff --git a/lasp/lasp_avstream.py b/lasp/lasp_avstream.py index 53134cd..67d5feb 100644 --- a/lasp/lasp_avstream.py +++ b/lasp/lasp_avstream.py @@ -29,7 +29,8 @@ class AvStream: self.nchannels = len(daq.channels_en) self.samplerate = daq.input_rate self.blocksize = daq.blocksize - self.sensitivity = np.asarray(daqconfig.input_sensitivity)[daq.channels_en] + self.sensitivity = np.asarray(daqconfig.input_sensitivity)[ + daq.channels_en] except Exception as e: raise RuntimeError(f'Could not initialize DAQ device: {str(e)}') diff --git a/lasp/lasp_common.py b/lasp/lasp_common.py index 176cf6e..990ca91 100644 --- a/lasp/lasp_common.py +++ b/lasp/lasp_common.py @@ -7,28 +7,18 @@ Common definitions used throughout the code. """ __all__ = ['P_REF', 'FreqWeighting', 'TimeWeighting', 'getTime', 'calfile', - 'W_REF', 'U_REF', 'I_REF', 'DEFAULT_FIGSIZE_H', 'DEFAULT_FIGSIZE_W', - 'GOLDEN', 'PLOT_COLORS_LIST', 'PLOT_NOCOLORS_LIST'] - -PLOT_COLORS_LIST = ['b', 'g', 'r', 'c', 'm', 'y', 'k', '#BE6500'] -PLOT_NOCOLORS_LIST = ['k', '0.5', 'k', '0.5', '0.5', '0.5', '0.5', 'k'] - -DEFAULT_FIGSIZE_W = 8 -GOLDEN = (np.sqrt(5.)+1)/2 -DEFAULT_FIGSIZE_H = DEFAULT_FIGSIZE_W/GOLDEN - + 'W_REF', 'U_REF', 'I_REF'] # Reference sound pressure level P_REF = 2e-5 -W_REF = 1e-12 # 1 picoWatt -I_REF = 1e-12 # 1 picoWatt/ m^2 +W_REF = 1e-12 # 1 picoWatt +I_REF = 1e-12 # 1 picoWatt/ m^2 # Reference velocity for sound velocity level U_REF = 5e-8 # Todo: fix This -# calfile = '/home/anne/wip/UMIK-1/cal/7027430_90deg.txt' calfile = None diff --git a/lasp/lasp_config.py b/lasp/lasp_config.py index 49000b5..cd5dbff 100644 --- a/lasp/lasp_config.py +++ b/lasp/lasp_config.py @@ -9,16 +9,14 @@ import numpy as np LASP_NUMPY_FLOAT_TYPE = np.float64 + def zeros(shape): return np.zeros(shape, dtype=LASP_NUMPY_FLOAT_TYPE) + + def ones(shape): return np.ones(shape, dtype=LASP_NUMPY_FLOAT_TYPE) + + def empty(shape): return np.empty(shape, dtype=LASP_NUMPY_FLOAT_TYPE) - -class config: - default_figsize = (12,7) - default_comment_lenght_measoverview = 50 - default_xlim_freq_plot = (50, 10000) - default_ylim_freq_plot = (20, 100) - versiontxt = '0.1' diff --git a/lasp/lasp_measurement.py b/lasp/lasp_measurement.py index d491b40..d5b620d 100644 --- a/lasp/lasp_measurement.py +++ b/lasp/lasp_measurement.py @@ -12,7 +12,7 @@ The ASCEE hdf5 measurement file format contains the following fields: 'samplerate': The audio data sample rate in Hz. 'nchannels': The number of audio channels in the file 'sensitivity': (Optionally) the stored sensitivity of the record channels. - This can be a single value, or an array of sensitivities for + This can be a single value, or a list of sensitivities for each channel. Both representations are allowed. - Datasets: @@ -32,9 +32,8 @@ that can be stored for the integer bit depth to get a number between -1.0 and The video dataset can possibly be not present in the data. - - """ + __all__ = ['Measurement', 'scaleBlockSens'] from contextlib import contextmanager import h5py as h5 @@ -42,7 +41,6 @@ import numpy as np from .lasp_config import LASP_NUMPY_FLOAT_TYPE import wave import os -import numbers class BlockIter: @@ -177,7 +175,9 @@ class Measurement: # Sensitivity try: sens = f.attrs['sensitivity'] - self._sens = sens*np.ones(self.nchannels) if isinstance(sens, float) else sens + self._sens = sens * \ + np.ones(self.nchannels) if isinstance( + sens, float) else sens except KeyError: self._sens = np.ones(self.nchannels) @@ -198,7 +198,7 @@ class Measurement: Args: mode: Opening mode for the file. Should either be 'r', or 'r+' """ - if mode not in ('r','r+'): + if mode not in ('r', 'r+'): raise ValueError('Invalid file opening mode.') with h5.File(self.fn, mode) as f: yield f diff --git a/lasp/lasp_octavefilter.py b/lasp/lasp_octavefilter.py index f8cf038..ff0a237 100644 --- a/lasp/lasp_octavefilter.py +++ b/lasp/lasp_octavefilter.py @@ -6,7 +6,8 @@ Author: J.A. de Jong - ASCEE Provides the FIR implementation of the octave filter bank """ -__all__ = ['OctaveFilterBank'] +__all__ = ['OctaveFilterBank', 'ThirdOctaveFilterBank'] + from .filter.bandpass_fir import OctaveBankDesigner, ThirdOctaveBankDesigner from .wrappers import Decimator, FilterBank as pyxFilterBank import numpy as np @@ -28,7 +29,6 @@ class FilterBank: assert np.isclose(fs, 48000), "Only sampling frequency" \ " available is 48 kHz" - maxdecimation = self.decimation(self.xs[0]) self.decimators = [] for dec in maxdecimation: diff --git a/lasp/lasp_playback.py b/lasp/lasp_playback.py index a235533..a4b952f 100644 --- a/lasp/lasp_playback.py +++ b/lasp/lasp_playback.py @@ -23,10 +23,17 @@ class Playback: def __init__(self, fn1, channel=0, video=True, verbose=True): """ + Initialize a Playback class for playing back audio + Args: + fn1: Filename of the measurement file + channel: channel index to play back + video: if True and video is available in the measurement file, + video will also be shown + verbose: print out status messages to stdout """ ext = '.h5' - if not ext in fn1: + if fn1[-3:] != ext: fn = fn1 + ext else: fn = fn1 diff --git a/lasp/lasp_reverb.py b/lasp/lasp_reverb.py index 2de03df..fecd991 100644 --- a/lasp/lasp_reverb.py +++ b/lasp/lasp_reverb.py @@ -25,7 +25,7 @@ class ReverbTime: """ assert level.ndim == 2, 'Invalid number of dimensions in level' - assert level.shape[1] == 1,'Invalid number of channels, should be 1' + assert level.shape[1] == 1, 'Invalid number of channels, should be 1' self._level = level # Number of time samples self._N = self._level.shape[0] diff --git a/lasp/lasp_slm.py b/lasp/lasp_slm.py index ce2e02e..644a328 100644 --- a/lasp/lasp_slm.py +++ b/lasp/lasp_slm.py @@ -22,7 +22,7 @@ class Dummy: class SLM: """ - Multi-channel sound Level Meter. Input data: time data with a certain + Multi-channel Sound Level Meter. Input data: time data with a certain sampling frequency. Output: time-weighted (fast/slow) sound pressure levels in dB(A/C/Z). @@ -39,6 +39,8 @@ class SLM: """ if tw[0] is not TimeWeighting.none[0]: + # Initialize the single-pole low-pass filter for given time- + # weighting value. self._lp = SPLowpass(fs, tw[0]) else: self._lp = Dummy() @@ -69,6 +71,8 @@ class SLM: Args: data: + returns: + level values as a function of time """ assert data.ndim == 2 assert data.shape[1] == 1 diff --git a/lasp/lasp_weighcal.py b/lasp/lasp_weighcal.py index da24332..2be8bd2 100644 --- a/lasp/lasp_weighcal.py +++ b/lasp/lasp_weighcal.py @@ -19,7 +19,7 @@ class WeighCal: """ Frequency weighting and calibration FIR filter """ - + def __init__(self, fw=FreqWeighting.default, nchannels=1, fs=48000., diff --git a/lasp/tools/plot.py b/lasp/tools/plot.py index fe8f11b..0782c95 100644 --- a/lasp/tools/plot.py +++ b/lasp/tools/plot.py @@ -15,7 +15,6 @@ from lasp.lasp_common import (PLOT_COLORS_LIST, PLOT_NOCOLORS_LIST, DEFAULT_FIGSIZE_H, DEFAULT_FIGSIZE_W) - class Figure: def __init__(self, **kwargs): ncols = kwargs.pop('ncols', 1) diff --git a/lasp/wrappers.pyx b/lasp/wrappers.pyx index 134af1d..c2cfea9 100644 --- a/lasp/wrappers.pyx +++ b/lasp/wrappers.pyx @@ -1,3 +1,6 @@ +""" +This file contains the Cython wrapper functions to +""" include "config.pxi" setTracerLevel(15) @@ -208,14 +211,14 @@ cdef class AvPowerSpectra: us window=Window.hann, d[:] weighting = np.array([])): - + cdef vd weighting_vd cdef vd* weighting_ptr = NULL if(weighting.size != 0): weighting_vd = dmat_foreign_data(weighting.size,1, &weighting[0],False) weighting_ptr = &weighting_vd - + self.aps = AvPowerSpectra_alloc(nfft, nchannels, overlap_percentage, @@ -323,11 +326,11 @@ cdef extern from "lasp_decimation.h": ctypedef struct c_Decimator "Decimator" ctypedef enum DEC_FAC: DEC_FAC_4 - + c_Decimator* Decimator_create(us nchannels,DEC_FAC d) nogil dmat Decimator_decimate(c_Decimator* dec,const dmat* samples) nogil void Decimator_free(c_Decimator* dec) nogil - + cdef class Decimator: cdef: c_Decimator* dec diff --git a/tools/__init__.py b/tools/__init__.py deleted file mode 100644 index 0ed8b07..0000000 --- a/tools/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -from .config import report_quality -from .report_tools import * - -__all__ = ['report_quality']