Skip to content

Commit

Permalink
Minor bugs in HumanClassify1 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Marsland committed Jul 17, 2017
1 parent eef1f87 commit a68ea85
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 210 deletions.
75 changes: 53 additions & 22 deletions AviaNZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ def addSegment(self,startpoint,endpoint,y1=0,y2=0,species=None,saveSeg=True):
if not saveSeg:
timeRangeStart = self.startRead #self.currentFileSection*self.config['maxFileShow']
timeRangeEnd = min(self.startRead + self.lenRead, float(self.fileLength) / self.sampleRate)
if startpoint > timeRangeStart and endpoint < timeRangeEnd:
if startpoint >= timeRangeStart and endpoint <= timeRangeEnd:
show = True
# Put the startpoint and endpoint in the right range
startpoint = startpoint - timeRangeStart
Expand Down Expand Up @@ -2136,11 +2136,14 @@ def humanClassifyDialog1(self):
"""

# Check there are segments to show on this page
self.box1id = 0
while self.listRectanglesa2[self.box1id] is None and self.box1id<len(self.segments):
self.box1id += 1

if self.box1id == len(self.segments):
if not self.config['showAllPages']:
if len(self.segments)>0:
self.box1id = 0
while self.box1id<len(self.segments) and self.listRectanglesa2[self.box1id] is None:
self.box1id += 1
else:
self.box1id = 0
if (self.config['showAllPages'] and len(self.segments)==0) or (not self.config['showAllPages'] and (self.box1id == len(self.segments) or len(self.listRectanglesa2)==0)):
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("No segments to check")
Expand Down Expand Up @@ -2173,19 +2176,23 @@ def humanClassifyDialog1(self):

# Check which page is first to have segments on
self.currentFileSection = 0
print self.segments
while self.segments[0][0] > (self.currentFileSection+1)*self.config['maxFileShow']:
self.currentFileSection += 1

# Load the first segment
self.startRead = self.currentFileSection * self.config['maxFileShow']
self.loadSegment()
print self.currentFileSection, self.startRead

# And show it
x1 = int(self.convertAmpltoSpec(self.segments[self.box1id][0]-self.startRead))
x2 = int(self.convertAmpltoSpec(self.segments[self.box1id][1]-self.startRead))
x3 = int((self.segments[self.box1id][0]-self.startRead)*self.sampleRate)
x4 = int((self.segments[self.box1id][1]-self.startRead)*self.sampleRate)

print self.segments[self.box1id][0]-self.startRead, x1, x2, x3, x4

self.humanClassifyDialog1 = Dialogs.HumanClassify1(self.sg[x1:x2,:],self.audiodata[x3:x4],self.sampleRate,self.segments[self.box1id][4],self.lut,self.colourStart,self.colourEnd,self.config['invertColourMap'], self.config['BirdList'])
self.humanClassifyDialog1.show()
self.humanClassifyDialog1.activateWindow()
Expand Down Expand Up @@ -2222,9 +2229,10 @@ def humanClassifyNextImage1(self):
self.segments[self.box1id][4])
else:
# Check if have moved to next segment, and if so load it
# If there was a section without segments this would be a bit inefficient
# If there was a section without segments this would be a bit inefficient, actually no, it was wrong!
if self.segments[self.box1id][0] > (self.currentFileSection+1)*self.config['maxFileShow']:
self.currentFileSection += 1
while self.segments[self.box1id][0] > (self.currentFileSection+1)*self.config['maxFileShow']:
self.currentFileSection += 1
self.startRead = self.currentFileSection * self.config['maxFileShow']
print "Loading next page", self.currentFileSection
self.loadSegment()
Expand All @@ -2249,7 +2257,25 @@ def humanClassifyNextImage1(self):
msg.exec_()
self.humanClassifyClose1()

def updateLabel(self,label):
""" Update the label on a segment that is currently shown in the display. """
if self.listRectanglesa2[self.box1id] is not None:
self.birdSelected(label, update=False)

self.listRectanglesa1[self.box1id].setBrush(self.prevBoxCol)
self.listRectanglesa1[self.box1id].update()
if self.dragRectTransparent.isChecked() and type(self.listRectanglesa2[self.box1id]) == self.ROItype:
col = self.prevBoxCol.rgb()
col = QtGui.QColor(col)
col.setAlpha(255)
self.listRectanglesa2[self.box1id].setPen(col, width=1)
else:
self.listRectanglesa2[self.box1id].setBrush(self.prevBoxCol)

self.listRectanglesa2[self.box1id].update()

def humanClassifyCorrect1(self):
""" Correct segment labels, save the old ones if necessary """
label, self.saveConfig, checkText = self.humanClassifyDialog1.getValues()
if len(checkText) > 0:
if label != checkText:
Expand All @@ -2268,23 +2294,28 @@ def humanClassifyCorrect1(self):

#self.updateText(label)
# Update the label on the box if it is in the current page
if self.listRectanglesa2[self.box1id] is not None:
self.birdSelected(label,update=False)

self.listRectanglesa1[self.box1id].setBrush(self.prevBoxCol)
self.listRectanglesa1[self.box1id].update()
if self.dragRectTransparent.isChecked() and type(self.listRectanglesa2[self.box1id]) == self.ROItype:
col = self.prevBoxCol.rgb()
col = QtGui.QColor(col)
col.setAlpha(255)
self.listRectanglesa2[self.box1id].setPen(col,width=1)
else:
self.listRectanglesa2[self.box1id].setBrush(self.prevBoxCol)

self.listRectanglesa2[self.box1id].update()
self.updateLabel(label)
# if self.listRectanglesa2[self.box1id] is not None:
# self.birdSelected(label,update=False)
#
# self.listRectanglesa1[self.box1id].setBrush(self.prevBoxCol)
# self.listRectanglesa1[self.box1id].update()
# if self.dragRectTransparent.isChecked() and type(self.listRectanglesa2[self.box1id]) == self.ROItype:
# col = self.prevBoxCol.rgb()
# col = QtGui.QColor(col)
# col.setAlpha(255)
# self.listRectanglesa2[self.box1id].setPen(col,width=1)
# else:
# self.listRectanglesa2[self.box1id].setBrush(self.prevBoxCol)
#
# self.listRectanglesa2[self.box1id].update()

if self.saveConfig:
self.config['BirdList'].append(label)
elif label[-1] == '?':
# Remove the question mark, since the user has agreed
self.updateLabel(label[:-1])
self.segments[self.box1id][4] = label[:-1]

self.humanClassifyDialog1.tbox.setText('')
self.humanClassifyDialog1.tbox.setEnabled(False)
Expand Down
254 changes: 127 additions & 127 deletions Dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,133 +1085,133 @@ def getValues(self):
return self.errors

# ======
class HumanClassify3(QDialog):
# TODO: Delete when other version works!
# This dialog is different to the others. The aim is to check (or ask for) classifications for segments.
# This version gets *12* at a time, and put them all out together on buttons, and their labels.
# It could be all the same species, or the ones that it is unsure about, or whatever.

# TODO: Work out how big the spect plots are, and make the right number of cols. Also have a min size?

def __init__(self, sg, segments, label, sampleRate, incr, lut, colourStart, colourEnd, cmapInverted,
parent=None):
QDialog.__init__(self, parent)
self.setWindowTitle('Check Classifications')
self.setWindowIcon(QIcon('img/Avianz.ico'))
self.frame = QWidget()

self.sampleRate = sampleRate
self.incr = incr
self.lut = lut
self.colourStart = colourStart
self.colourEnd = colourEnd
self.cmapInverted = cmapInverted

# Seems that image is backwards?
self.sg = np.fliplr(sg)
self.segments = segments
self.firstSegment = 0
self.errors = []

self.segments = [item for item in self.segments if item[4] == label or item[4][:-1] == label]
# print len(self.segments)
next = QPushButton("Next/Finish")
self.connect(next, SIGNAL("clicked()"), self.next)

if len(self.segments) > 0:

species = QLabel(label)

# TODO: Decide on these sizes
self.w = 3
self.h = 4
self.grid = QGridLayout()

self.makeButtons()

vboxFull = QVBoxLayout()
vboxFull.addWidget(QLabel('Click on the images that are incorrectly labelled'))
vboxFull.addWidget(species)
vboxFull.addLayout(self.grid)
vboxFull.addWidget(next)
else:
vboxFull = QVBoxLayout()
vboxFull.addWidget(QLabel('No images to show'))
vboxFull.addWidget(next)

self.setLayout(vboxFull)

def makeButtons(self):
positions = [(i, j) for i in range(self.h) for j in range(self.w)]
images = []
segRemain = len(self.segments) - self.firstSegment
# print segRemain, self.firstSegment

if segRemain < self.w * self.h:
for i in range(segRemain):
ind = i + self.firstSegment
x1 = int(self.convertAmpltoSpec(self.segments[ind][0]))
x2 = int(self.convertAmpltoSpec(self.segments[ind][1]))
images.append(self.setImage(self.sg[x1:x2, :]))
for i in range(segRemain, self.w * self.h):
images.append([None, None])
else:
for i in range(self.w * self.h):
ind = i + self.firstSegment
x1 = int(self.convertAmpltoSpec(self.segments[ind][0]))
x2 = int(self.convertAmpltoSpec(self.segments[ind][1]))
images.append(self.setImage(self.sg[x1:x2, :]))
self.buttons = []
for position, im in zip(positions, images):
if im[0] is not None:
self.buttons.append(SupportClasses.PicButton(position[0] * self.w + position[1], im[0], im[1]))
self.grid.addWidget(self.buttons[-1], *position)

def convertAmpltoSpec(self, x):
return x * self.sampleRate / self.incr

def setImage(self, seg):
if self.cmapInverted:
im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourEnd, self.colourStart])
else:
im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourStart, self.colourEnd])
im1 = fn.makeQImage(im, alpha)

if self.cmapInverted:
im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourStart, self.colourEnd])
else:
im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourEnd, self.colourStart])

im2 = fn.makeQImage(im, alpha)

return [im1, im2]

def next(self):
# Find out which buttons have been clicked (so are not correct)

for i in range(len(self.buttons)):
# print self.buttons[i].buttonClicked
if self.buttons[i].buttonClicked:
self.errors.append(i + self.firstSegment)
# print self.errors

# Now find out if there are more segments to check, and remake the buttons, otherwise close
if len(self.segments) > 0:
if (len(self.segments) - self.firstSegment) < self.w * self.h:
self.close()
else:
self.firstSegment += self.w * self.h
if self.firstSegment != len(self.segments):
for i in range(self.w * self.h):
self.grid.removeWidget(self.buttons[i])
self.makeButtons()
else:
self.close()
else:
self.close()

def getValues(self):
return self.errors
# class HumanClassify3(QDialog):
# # TODO: Delete when other version works!
# # This dialog is different to the others. The aim is to check (or ask for) classifications for segments.
# # This version gets *12* at a time, and put them all out together on buttons, and their labels.
# # It could be all the same species, or the ones that it is unsure about, or whatever.
#
# # TODO: Work out how big the spect plots are, and make the right number of cols. Also have a min size?
#
# def __init__(self, sg, segments, label, sampleRate, incr, lut, colourStart, colourEnd, cmapInverted,
# parent=None):
# QDialog.__init__(self, parent)
# self.setWindowTitle('Check Classifications')
# self.setWindowIcon(QIcon('img/Avianz.ico'))
# self.frame = QWidget()
#
# self.sampleRate = sampleRate
# self.incr = incr
# self.lut = lut
# self.colourStart = colourStart
# self.colourEnd = colourEnd
# self.cmapInverted = cmapInverted
#
# # Seems that image is backwards?
# self.sg = np.fliplr(sg)
# self.segments = segments
# self.firstSegment = 0
# self.errors = []
#
# self.segments = [item for item in self.segments if item[4] == label or item[4][:-1] == label]
# # print len(self.segments)
# next = QPushButton("Next/Finish")
# self.connect(next, SIGNAL("clicked()"), self.next)
#
# if len(self.segments) > 0:
#
# species = QLabel(label)
#
# # TODO: Decide on these sizes
# self.w = 3
# self.h = 4
# self.grid = QGridLayout()
#
# self.makeButtons()
#
# vboxFull = QVBoxLayout()
# vboxFull.addWidget(QLabel('Click on the images that are incorrectly labelled'))
# vboxFull.addWidget(species)
# vboxFull.addLayout(self.grid)
# vboxFull.addWidget(next)
# else:
# vboxFull = QVBoxLayout()
# vboxFull.addWidget(QLabel('No images to show'))
# vboxFull.addWidget(next)
#
# self.setLayout(vboxFull)
#
# def makeButtons(self):
# positions = [(i, j) for i in range(self.h) for j in range(self.w)]
# images = []
# segRemain = len(self.segments) - self.firstSegment
# # print segRemain, self.firstSegment
#
# if segRemain < self.w * self.h:
# for i in range(segRemain):
# ind = i + self.firstSegment
# x1 = int(self.convertAmpltoSpec(self.segments[ind][0]))
# x2 = int(self.convertAmpltoSpec(self.segments[ind][1]))
# images.append(self.setImage(self.sg[x1:x2, :]))
# for i in range(segRemain, self.w * self.h):
# images.append([None, None])
# else:
# for i in range(self.w * self.h):
# ind = i + self.firstSegment
# x1 = int(self.convertAmpltoSpec(self.segments[ind][0]))
# x2 = int(self.convertAmpltoSpec(self.segments[ind][1]))
# images.append(self.setImage(self.sg[x1:x2, :]))
# self.buttons = []
# for position, im in zip(positions, images):
# if im[0] is not None:
# self.buttons.append(SupportClasses.PicButton(position[0] * self.w + position[1], im[0], im[1]))
# self.grid.addWidget(self.buttons[-1], *position)
#
# def convertAmpltoSpec(self, x):
# return x * self.sampleRate / self.incr
#
# def setImage(self, seg):
# if self.cmapInverted:
# im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourEnd, self.colourStart])
# else:
# im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourStart, self.colourEnd])
# im1 = fn.makeQImage(im, alpha)
#
# if self.cmapInverted:
# im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourStart, self.colourEnd])
# else:
# im, alpha = fn.makeARGB(seg, lut=self.lut, levels=[self.colourEnd, self.colourStart])
#
# im2 = fn.makeQImage(im, alpha)
#
# return [im1, im2]
#
# def next(self):
# # Find out which buttons have been clicked (so are not correct)
#
# for i in range(len(self.buttons)):
# # print self.buttons[i].buttonClicked
# if self.buttons[i].buttonClicked:
# self.errors.append(i + self.firstSegment)
# # print self.errors
#
# # Now find out if there are more segments to check, and remake the buttons, otherwise close
# if len(self.segments) > 0:
# if (len(self.segments) - self.firstSegment) < self.w * self.h:
# self.close()
# else:
# self.firstSegment += self.w * self.h
# if self.firstSegment != len(self.segments):
# for i in range(self.w * self.h):
# self.grid.removeWidget(self.buttons[i])
# self.makeButtons()
# else:
# self.close()
# else:
# self.close()
#
# def getValues(self):
# return self.errors

# ======
class HumanClassify2a(QDialog):
Expand Down
Loading

0 comments on commit a68ea85

Please sign in to comment.