Skip to content

Commit

Permalink
fixed rowcol issue in tma export figure
Browse files Browse the repository at this point in the history
  • Loading branch information
c-arthurs committed Sep 17, 2020
1 parent 0549f19 commit 47d3601
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DAB_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from skimage import color
from PIL import Image

# sys._MEIPASS = '.' # for running locally
sys._MEIPASS = '.' # for running locally

class MyWindow(QMainWindow):
def __init__(self):
Expand Down
36 changes: 26 additions & 10 deletions scripts/Cut_Application_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
have been applied and then reapply them when the image is needed - for the remove small objects phase
"""

# sys._MEIPASS = '.' # for running locally
sys._MEIPASS = '.' # for running locally


# setup the Graphics scene to detect clicks
Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(self):
self.load_excel.clicked.connect(lambda: self.read_excel())
self.overlay.clicked.connect(lambda: self.overlaystart())
self.export_2.clicked.connect(lambda: self.export_images())

self.export_again.clicked.connect(lambda: self.export_images(meta_only=True))
self.current_image = None

# threshold buttons
Expand Down Expand Up @@ -394,7 +394,7 @@ def read_excel(self):
def info(self, text):
self.label.setText(text)

def export_images(self):
def export_images(self, meta_only=False):
self.progressBar.setMaximum(len(self.scene.centroid))
self.activate([self.nameLabel, self.nameLineEdit, self.formatLabel, self.formatLineEdit,
self.magnificationLabel, self.magnificationLineEdit, self.scanDateLabel,
Expand All @@ -403,12 +403,17 @@ def export_images(self):
self.numberOfCoresLineEdit, self.diameterLabel, self.diamiterLineEdit,
self.export_2, self.overlay, self.excel_btn, self.load_ndpi, self.load_excel, self.overlaySave,
self.tabWidget], action=False)
try:
resolution = int(self.resolution_edit.text())
print("new resolution applied")
except ValueError as E:
resolution = 0

self.export = Export(image=self.image, centroid=self.scene.centroid, cores=self.cores,
scale_index=self.scale_index,
core_diameter=self.core_diameter, output=self.output, name=self.name, lvl=self.lvl,
path=self.path, arrayshape=self.arrayshape, pathology=self.pathology)

path=self.path, arrayshape=self.arrayshape, pathology=self.pathology, resolution=resolution,
meta_only=meta_only)

self.thread = QThread()
self.export.info.connect(self.info)
Expand Down Expand Up @@ -438,7 +443,7 @@ class Export(QObject):
done = pyqtSignal(bool)

def __init__(self, image, centroid, cores, scale_index, core_diameter, output, name, lvl, path, arrayshape,
pathology):
pathology, resolution, meta_only=False):
super().__init__()
self.image = image
self.centroid = centroid
Expand All @@ -451,10 +456,16 @@ def __init__(self, image, centroid, cores, scale_index, core_diameter, output, n
self.path = path
self.arrayshape = arrayshape
self.pathology = pathology
self.resolution = resolution
self.meta_only = meta_only

@pyqtSlot()
def run(self):
self.export_images(self.centroid, self.cores)
if self.meta_only:
self.json_write()
self.wsifigure(higher_resolution=False, pathology=self.pathology)
else:
self.export_images(self.centroid, self.cores)
print('exporting')

def export_images(self, centroid, cores):
Expand Down Expand Up @@ -489,10 +500,12 @@ def json_write(self):

def wsifigure(self, higher_resolution=False, pathology=None):
"""
TODO: theres an error here that creates a blank line and col on row 1 col 1
takes the metadata from the json
makes a fig of the locations on the tissue array and saves it
higher resolution = int start at 1 and move up to improve resolution - will slow code
"""
higher_resolution = self.resolution

def overly_path(im, overlay, pathology):
if pathology == "N":
Expand All @@ -512,13 +525,13 @@ def overly_path(im, overlay, pathology):
lvl = data['lowlevel']
scale_index = image.level_downsamples[lvl]
diameter = int(data["diameter"] / scale_index)
arrayshape = [a + 1 for a in data['arrayshape']]
arrayshape = data['arrayshape']
arrayshape = [(a * diameter) + 1 for a in arrayshape]
arrayshape.append(3)
figarray = np.ones(arrayshape)
cindex = [coordinate_from_string(i) for i in data['cores']] # returns ('A',4)
cindex = [(b, column_index_from_string(a)) for (a, b) in cindex] # returns index tuples for each core
maskcoord = [[y * diameter if y > 1 else y for y in x] for x in cindex]
maskcoord = [[y * diameter - diameter if y > 1 else y for y in x] for x in cindex]

if pathology:
overlay = np.load(sys._MEIPASS + os.sep + "scripts" + os.sep + "outline.npy")
Expand All @@ -528,7 +541,10 @@ def overly_path(im, overlay, pathology):
im = image.read_region(location=data["coordinates"][i], level=lvl, size=(diameter, diameter))
im = np.array(im)[:, :, :3]
if pathology:
im = overly_path(im, overlay, pathology[i])
try:
im = overly_path(im, overlay, pathology[i])
except:
continue
figarray[maskcoord[i][0]:maskcoord[i][0] + diameter, maskcoord[i][1]:maskcoord[i][1] + diameter, :] = im
figarray[figarray == [1, 1, 1]] = 255 # make background white
savepath = self.output + os.sep + self.name + '_layoutfig.tiff'
Expand Down
40 changes: 38 additions & 2 deletions scripts/DAB_CUT.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1236</width>
<width>1134</width>
<height>708</height>
</rect>
</property>
Expand Down Expand Up @@ -421,6 +421,42 @@
</property>
</widget>
</item>
<item row="11" column="1">
<widget class="QLineEdit" name="resolution_edit">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item row="11" column="0">
<widget class="QLabel" name="label_4">
<property name="font">
<font>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>Fig resolution</string>
</property>
</widget>
</item>
<item row="12" column="0" colspan="2">
<widget class="QPushButton" name="export_again">
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
<property name="text">
<string>Re-export metadata and figures</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down Expand Up @@ -914,7 +950,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1120</width>
<width>1018</width>
<height>77</height>
</rect>
</property>
Expand Down

0 comments on commit 47d3601

Please sign in to comment.