Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python3.10: numpy update and explicit int() #207

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion friture/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 13 additions & 7 deletions friture/plotting/coordinateTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,31 @@ def setBorders(self, start, end):
def setScale(self, scale):
self.scale = scale

@pyqtSlot(float, result=float)
@pyqtSlot(float, result=int)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's possible, I think this method should keep returning floats. It might sound strange that screen coordinates would not be integers, but with display scaling this might not be that unusual

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I think this one is not correct. The coordinates on the plot area are usually floats, they do not round nicely to integers.

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)
Expand All @@ -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)
return self.scale.inverse(trans_x)
17 changes: 10 additions & 7 deletions friture/plotting/scaleBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions friture/plotting/titleWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ 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)
# painter.setRenderHint(QtGui.QPainter.Antialiasing)

# 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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -100,17 +100,17 @@ 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)
# painter.setRenderHint(QtGui.QPainter.Antialiasing)

# 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)

Expand Down
20 changes: 11 additions & 9 deletions friture/spectrogram_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ 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
self.time_offset = (self.time_offset % oldWidth) * width / oldWidth
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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions friture/spectrogram_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ 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")
self.spinBox_minfreq.setSuffix(" Hz")

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")
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we pin to the precise version that's needed ?
I guess that 1.21.5 or 1.22.1 ?

In the future I might move to a different build tool that supports locking the whole dependency tree, but in the meantime I prefer to have pinned versions to avoid unexpected build failures when a package gets silently upgraded to a newer version.

"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"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, thanks !


with open(join(dirname(__file__), 'README.rst')) as f:
long_description = f.read()
Expand Down