From 54c4958c68bf4982e55da9e6f937d35a24b2e3e3 Mon Sep 17 00:00:00 2001 From: Ian Huang Date: Thu, 20 Jan 2022 00:41:32 -0500 Subject: [PATCH] python2.10: numpy update and explicit int() --- friture/levels.py | 2 +- friture/plotting/coordinateTransform.py | 20 +++++++++++++------- friture/plotting/scaleBar.py | 17 ++++++++++------- friture/plotting/titleWidget.py | 18 +++++++++--------- friture/spectrogram_image.py | 20 +++++++++++--------- friture/spectrogram_settings.py | 4 ++-- setup.py | 4 ++-- 7 files changed, 48 insertions(+), 37 deletions(-) diff --git a/friture/levels.py b/friture/levels.py index f8f95c20..1a05f0ed 100644 --- a/friture/levels.py +++ b/friture/levels.py @@ -105,7 +105,7 @@ def __init__(self, parent, engine): self.i = 0 def onWidthChanged(self): - self.quickWidget.setFixedWidth(self.qmlObject.width()) + self.quickWidget.setFixedWidth(int(self.qmlObject.width())) # method def set_buffer(self, buffer): diff --git a/friture/plotting/coordinateTransform.py b/friture/plotting/coordinateTransform.py index f775bd25..36d8b09a 100644 --- a/friture/plotting/coordinateTransform.py +++ b/friture/plotting/coordinateTransform.py @@ -58,25 +58,31 @@ def setBorders(self, start, end): def setScale(self, scale): self.scale = scale - @pyqtSlot(float, result=float) + @pyqtSlot(float, result=int) def toScreen(self, x): if self.scale is fscales.Logarithmic: if self.coord_clipped_min == self.coord_clipped_max: - return self.startBorder + 0. * x # keep x type (this can produce a RunTimeWarning if x contains inf) + return int(self.startBorder + 0. * x) # keep x type (this can produce a RunTimeWarning if x contains inf) x = (x < 1e-20) * 1e-20 + (x >= 1e-20) * x - return (np.log10(x / self.coord_clipped_min)) * (self.length - self.startBorder - self.endBorder) / self.coord_ratio_log + self.startBorder + return int((np.log10(x / self.coord_clipped_min)) + * (self.length - self.startBorder - self.endBorder) + / self.coord_ratio_log + + self.startBorder) else: if self.coord_max == self.coord_min: - return self.startBorder + 0. * x # keep x type (this can produce a RunTimeWarning if x contains inf) + return int(self.startBorder + 0. * x) # keep x type (this can produce a RunTimeWarning if x contains inf) trans_x = self.scale.transform(x) trans_min = self.scale.transform(self.coord_min) trans_max = self.scale.transform(self.coord_max) - return (trans_x - trans_min) * (self.length - self.startBorder - self.endBorder) / (trans_max - trans_min) + self.startBorder + return int((trans_x - trans_min) + * (self.length - self.startBorder - self.endBorder) + / (trans_max - trans_min) + + self.startBorder) - @pyqtSlot(float, result=float) + @pyqtSlot(int, result=float) def toPlot(self, x): if self.length == self.startBorder + self.endBorder: return self.coord_min + 0. * x # keep x type (this can produce a RunTimeWarning if x contains inf) @@ -88,4 +94,4 @@ def toPlot(self, x): trans_max = self.scale.transform(self.coord_max) trans_x = (x - self.startBorder) * (trans_max - trans_min) / (self.length - self.startBorder - self.endBorder) + trans_min - return self.scale.inverse(trans_x) \ No newline at end of file + return self.scale.inverse(trans_x) diff --git a/friture/plotting/scaleBar.py b/friture/plotting/scaleBar.py index a27e5d3d..ce070d0d 100644 --- a/friture/plotting/scaleBar.py +++ b/friture/plotting/scaleBar.py @@ -69,7 +69,7 @@ def paintEvent(self, event): # tick start xt = xb - self.tickLength - xtm = xb - self.tickLength / 2 + xtm = xb - self.tickLength // 2 # label end le = xt - self.labelSpacing @@ -90,7 +90,7 @@ def paintEvent(self, event): painter.drawLine(xt, y, xb, y) if self.coordinateTransform.startBorder < y < self.coordinateTransform.length - self.coordinateTransform.endBorder: tick_string = self.tickFormatter(tick, digits) - painter.drawText(le - fm.width(tick_string), y + lh / 2 - 2, tick_string) + painter.drawText(le - fm.width(tick_string), y + lh // 2 - 2, tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates @@ -160,7 +160,7 @@ def paintEvent(self, event): # tick start yt = yb + self.tickLength - ytm = yb + self.tickLength / 2 + ytm = yb + self.tickLength // 2 # label end le = yt + self.labelSpacing @@ -179,7 +179,7 @@ def paintEvent(self, event): x = self.coordinateTransform.toScreen(tick) painter.drawLine(x, yt, x, yb) tick_string = '{0:.{1}f}'.format(tick, digits) - painter.drawText(x - fm.width(tick_string) / 2, le + fm.height(), tick_string) + painter.drawText(x - fm.width(tick_string) // 2, le + fm.height(), tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates @@ -209,7 +209,10 @@ def __init__(self, parent, division, transform): # should be shared with spectrogram_image in a dedicated class cmap = generated_cmrmap.CMAP - self.colors = [QtGui.QColor(cmap[i, 0] * 255, cmap[i, 1] * 255, cmap[i, 2] * 255) for i in range(cmap.shape[0])] + self.colors = [QtGui.QColor(int(cmap[i, 0] * 255), + int(cmap[i, 1] * 255), + int(cmap[i, 2] * 255) + ) for i in range(cmap.shape[0])] # for vertical scale bar self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)) @@ -263,7 +266,7 @@ def paintEvent(self, event): # tick start xt = xb + self.tickLength - xtm = xb + self.tickLength / 2 + xtm = xb + self.tickLength // 2 # label start ls = xt + self.labelSpacing @@ -283,7 +286,7 @@ def paintEvent(self, event): y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(xt, y, xb, y) tick_string = '{0:.{1}f}'.format(tick, digits) - painter.drawText(ls, y + lh / 2 - 2, tick_string) + painter.drawText(ls, y + lh // 2 - 2, tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates diff --git a/friture/plotting/titleWidget.py b/friture/plotting/titleWidget.py index 651f02c7..aacb5764 100644 --- a/friture/plotting/titleWidget.py +++ b/friture/plotting/titleWidget.py @@ -25,7 +25,7 @@ def setTitle(self, title): def sizeHint(self): fm = QtGui.QFontMetrics(self.font()) # for vertical title - return QtCore.QSize(fm.height() * 1.2, fm.width(self.title)) + return QtCore.QSize(int(fm.height() * 1.2), fm.width(self.title)) def paintEvent(self, paintEvent): painter = QtGui.QPainter(self) @@ -33,9 +33,9 @@ def paintEvent(self, paintEvent): # for vertical title fm = painter.fontMetrics() - centeredTextShift = QtCore.QPoint(-fm.width(self.title) / 2, 0) + centeredTextShift = QtCore.QPoint(-fm.width(self.title) // 2, 0) - painter.translate(fm.height() / 1.2, self.height() / 2) + painter.translate(fm.height() // 1.2, self.height() / 2) painter.rotate(-90) painter.translate(centeredTextShift) @@ -63,16 +63,16 @@ def setTitle(self, title): def sizeHint(self): fm = QtGui.QFontMetrics(self.font()) # for vertical title - return QtCore.QSize(fm.width(self.title), fm.height() * 1.2) + return QtCore.QSize(fm.width(self.title), int(fm.height() * 1.2)) def paintEvent(self, paintEvent): painter = QtGui.QPainter(self) # painter.setRenderHint(QtGui.QPainter.Antialiasing) fm = painter.fontMetrics() - centeredTextShift = QtCore.QPoint(-fm.width(self.title) / 2, 0) + centeredTextShift = QtCore.QPoint(-fm.width(self.title) // 2, 0) - painter.translate(self.width() / 2, fm.height()) + painter.translate(self.width() // 2, fm.height()) painter.translate(centeredTextShift) painter.drawText(0, 0, self.title) @@ -100,7 +100,7 @@ def setTitle(self, title): def sizeHint(self): fm = QtGui.QFontMetrics(self.font()) # for vertical title - return QtCore.QSize(fm.height() * 1.5, fm.width(self.title)) + return QtCore.QSize(int(fm.height() * 1.5), fm.width(self.title)) def paintEvent(self, paintEvent): painter = QtGui.QPainter(self) @@ -108,9 +108,9 @@ def paintEvent(self, paintEvent): # for vertical title fm = painter.fontMetrics() - centeredTextShift = QtCore.QPoint(-fm.width(self.title) / 2, 0) + centeredTextShift = QtCore.QPoint(-fm.width(self.title) // 2, 0) - painter.translate(self.width() / 2, self.height() / 2) + painter.translate(self.width() // 2, self.height() / 2) painter.rotate(90) painter.translate(centeredTextShift) diff --git a/friture/spectrogram_image.py b/friture/spectrogram_image.py index eed8683d..7e016ae2 100644 --- a/friture/spectrogram_image.py +++ b/friture/spectrogram_image.py @@ -61,7 +61,7 @@ def erase(self): # resize the pixmap and update the offsets accordingly def resize(self, width, height): - oldWidth = self.pixmap.width() / 2 + oldWidth = int(self.pixmap.width() / 2) if width != oldWidth: self.offset = (self.offset % oldWidth) * width / oldWidth self.offset = self.offset % width # to handle negative values @@ -69,17 +69,17 @@ def resize(self, width, height): self.pixmap = self.pixmap.scaled(2 * width, height, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation) def setcanvas_height(self, canvas_height): - if self.canvas_height != canvas_height: - self.canvas_height = canvas_height + if self.canvas_height != int(canvas_height): + self.canvas_height = int(canvas_height) self.resize(self.canvas_width, self.canvas_height) - self.logger.info("Spectrogram image: canvas_height changed, now: %d", canvas_height) + self.logger.info("Spectrogram image: canvas_height changed, now: %d", int(canvas_height)) def setcanvas_width(self, canvas_width): - if self.canvas_width != canvas_width: - self.canvas_width = canvas_width + if self.canvas_width != int(canvas_width): + self.canvas_width = int(canvas_width) self.resize(self.canvas_width, self.canvas_height) - self.canvasWidthChanged.emit(canvas_width) - self.logger.info("Spectrogram image: canvas_width changed, now: %d", canvas_width) + self.canvasWidthChanged.emit(int(canvas_width)) + self.logger.info("Spectrogram image: canvas_width changed, now: %d", int(canvas_width)) def addPixelAdvance(self, pixel_advance): self.time_offset += pixel_advance @@ -148,7 +148,9 @@ def prepare_palette(self): self.colors = numpy.zeros((cmap.shape[0]), dtype=numpy.uint32) for i in range(cmap.shape[0]): - self.colors[i] = QtGui.QColor(cmap[i, 0] * 255, cmap[i, 1] * 255, cmap[i, 2] * 255).rgb() + self.colors[i] = QtGui.QColor(int(cmap[i, 0] * 255), + int(cmap[i, 1] * 255), + int(cmap[i, 2] * 255)).rgb() def color_from_float(self, v): # clip in [0..1] before using the fast lookup function diff --git a/friture/spectrogram_settings.py b/friture/spectrogram_settings.py index e93d0f38..d37a26e8 100644 --- a/friture/spectrogram_settings.py +++ b/friture/spectrogram_settings.py @@ -75,7 +75,7 @@ def __init__(self, parent): self.spinBox_minfreq = QtWidgets.QSpinBox(self) self.spinBox_minfreq.setMinimum(20) - self.spinBox_minfreq.setMaximum(SAMPLING_RATE / 2) + self.spinBox_minfreq.setMaximum(SAMPLING_RATE // 2) self.spinBox_minfreq.setSingleStep(10) self.spinBox_minfreq.setValue(DEFAULT_MINFREQ) self.spinBox_minfreq.setObjectName("spinBox_minfreq") @@ -83,7 +83,7 @@ def __init__(self, parent): self.spinBox_maxfreq = QtWidgets.QSpinBox(self) self.spinBox_maxfreq.setMinimum(20) - self.spinBox_maxfreq.setMaximum(SAMPLING_RATE / 2) + self.spinBox_maxfreq.setMaximum(SAMPLING_RATE // 2) self.spinBox_maxfreq.setSingleStep(1000) self.spinBox_maxfreq.setProperty("value", DEFAULT_MAXFREQ) self.spinBox_maxfreq.setObjectName("spinBox_maxfreq") diff --git a/setup.py b/setup.py index 7affd97d..35be9f49 100644 --- a/setup.py +++ b/setup.py @@ -53,14 +53,14 @@ def include_dirs(self, dirs): "sounddevice==0.4.2", "rtmixer==0.1.3", "docutils==0.17.1", - "numpy==1.21.1", + "numpy>=1.21.1", "PyQt5==5.15.4", "appdirs==1.4.4", "pyrr==0.10.3", ] # Cython and numpy are needed when running setup.py, to build extensions -setup_requires=["numpy==1.21.1", "Cython==0.29.24"] +setup_requires=["numpy>=1.21.1", "Cython==0.29.24"] with open(join(dirname(__file__), 'README.rst')) as f: long_description = f.read()