Skip to content

Commit

Permalink
added guessing of data part
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilipp committed Dec 12, 2018
1 parent 4458cbe commit 1f7e3a5
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 24 deletions.
62 changes: 49 additions & 13 deletions straditize/straditizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,45 @@ def draw_figure(self):
if self.magni is not None:
self.magni.ax.figure.canvas.draw()

def marks_for_data_selection(self, nums=2):
def guess_data_lims(self, fraction=0.7):
"""Guess the limits of the diagram part
Parameters
----------
fraction: float
The smallest fraction that has to be covered for a cell to be
considered as a corner
Returns
-------
np.array
xmin and xmax of the diagram part (see :attr:`data_xlim`)
np.array
ymin and ymax of the diagram part (see :attr:`data_ylim`)"""
arr = binary.DataReader.to_binary_pil(self.image)
mask = arr.astype(bool)
max_h = fraction * arr.sum(axis=1).max()
max_v = fraction * arr.sum(axis=0).max()
cum_x = np.where(mask, arr.cumsum(1), 0)
cum_y = np.where(mask, arr.cumsum(0), 0)
cum_xr = np.where(mask, arr[:, ::-1].cumsum(1)[:, ::-1], 0)
cum_yr = np.where(mask, arr[::-1].cumsum(0)[::-1], 0)

right = np.vstack(np.where((cum_x > max_h) & (cum_y > max_v)))
ymax, xmax = right[:, right.shape[1] - 1 - right.max(0)[::-1].argmax()]

left = np.vstack(np.where((cum_xr > max_h) & (cum_yr > max_v)))
ymin, xmin = left[:, left.min(0).argmin()]

# now check the right corner, whether the object extents further to the
# right
labeled = skim.label(arr, 8, return_num=False)
label = labeled[ymax, xmax]
xmax = np.where(labeled[ymin:ymax] == label)[1].max()

return np.array([xmin, xmax+1]), np.array([ymin, ymax+1])

def marks_for_data_selection(self, nums=2, fraction=0.7):
def new_mark(pos):
if len(self.marks) == nums:
raise ValueError("Cannot use more than %i marks!" % nums)
Expand All @@ -502,8 +540,7 @@ def new_mark(pos):
x0, x1 = self.data_xlim
y0, y1 = self.data_ylim
else:
x0 = x1 = np.mean(self.ax.get_xlim())
y0 = y1 = np.mean(self.ax.get_ylim())
(x0, x1), (y0, y1) = self.guess_data_lims(fraction)
Ny, Nx = np.shape(self.image)[:2]
xlim = (0, Nx)
ylim = (Ny, 0)
Expand All @@ -512,16 +549,15 @@ def new_mark(pos):
idx_h = indexes['x']
idx_v = indexes['y']
self.remove_marks()
if self.data_xlim is not None and self.data_ylim is not None:
self.marks = [
cm.CrossMarks(positions[i], ax=self.ax, idx_h=idx_h,
idx_v=idx_v, zorder=2, c='b',
xlim=xlim, ylim=ylim)
for i in range(nums)]
self.marks[0].connect_marks(self.marks)
self.create_magni_marks(self.marks)
else:
self.marks = []

self.marks = [
cm.CrossMarks(positions[i], ax=self.ax, idx_h=idx_h,
idx_v=idx_v, zorder=2, c='b',
xlim=xlim, ylim=ylim)
for i in range(nums)]
self.marks[0].connect_marks(self.marks)
self.create_magni_marks(self.marks)

self.mark_cids.add(self.fig.canvas.mpl_connect(
'button_press_event', self._add_mark_event(new_mark)))
self.mark_cids.add(self.fig.canvas.mpl_connect(
Expand Down
6 changes: 5 additions & 1 deletion straditize/widgets/tutorial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def get_doc_files(self):
str
The path to the tutorial introduction file
list of str
The paths of the remaining tutorialpdc files"""
The paths of the remaining tutorial files"""
files = glob.glob(osp.join(self.src_dir, '*.rst')) + \
glob.glob(osp.join(self.src_dir, '*.png'))
intro = files.pop(next(
Expand All @@ -324,11 +324,15 @@ def get_doc_files(self):
def show(self):
"""Show the documentation of the tutorial"""
from psyplot_gui.main import mainwindow
from straditize.colnames import tesserocr
intro, files = self.get_doc_files()
self.filename = osp.splitext(osp.basename(intro))[0]
mainwindow.help_explorer.set_viewer('HTML help')
with open(intro) as f:
rst = f.read()
if tesserocr is not None:
rst = rst.replace('straditize_tutorial_column_names',
'straditize_tutorial_column_names_ocr')
name = osp.splitext(osp.basename(intro))[0]
self.lock_viewer(False)
mainwindow.help_explorer.show_rst(rst, name, files=files)
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified straditize/widgets/tutorial/docs/straditize-tutorial.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ and we will do our best to assist you.
straditize_tutorial_select_data
straditize_tutorial_create_reader
straditize_tutorial_column_starts
straditize_tutorial_column_names
straditize_tutorial_remove_lines
straditize_tutorial_digitize
straditize_tutorial_samples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,25 @@ Being exact in this step significantly reduces the later work and simplifies
the automatic digitization.

1. Click the :guilabel:`Select data part` button in the digitization control.
2. :kbd:`Shift` - leftclick on the data diagram. This will create a mark at
your current mouse location.
3. Click on the cross to select it
2. Click on the upper left cross to select it

.. image:: select_mark.png
.. image:: select-data-ul0.png

4. Hold the left mouse button and drag the cross to the upper left corner of
3. Hold the left mouse button and drag the cross to the upper left corner of
the diagram. Make sure you don't include the y-axis and x-axis ticks.

You can also use the zoom and navigation tools in the figure toolbar for
navigation.

.. image:: drag_mark.png
.. image:: select-data-ul1.png

5. Now, create another mark by another :kbd:`Shift` - leftclick
6. Move the cross to the lower right corner of the diagram. Make sure you don't
5. Now select the cross at the lower right corner of the diagram and move it
such that you don't
include the x-axis ticks (i.e. the numbers on the x-axis)

.. image:: drag_mark2.png
.. image:: select-data-lr1.png

7. Click the :guilabel:`Apply` button at the bottom
6. Click the :guilabel:`Apply` button at the bottom

If you want to change the appearance of the marks, see the `Marker control`
section in the straditizer control panel.
7 changes: 7 additions & 0 deletions tests/test_straditizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def test_format_coord(self):
ax.format_xdata(1.)))
self.assertEqual(ax.format_coord(x, y), ref)

def test_guess_data_lims(self):
stradi = Straditizer(osp.join(test_dir, 'test_figures',
'basic_diagram.png'))
xlim, ylim = stradi.guess_data_lims()
self.assertEqual(list(xlim), [10, 27])
self.assertEqual(list(ylim), [10, 30])


if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions tests/widgets/_base_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ def set_data_lims(self, xlim=None, ylim=None):
self.straditizer_widgets.digitizer.btn_select_data.isEnabled())
QTest.mouseClick(self.straditizer_widgets.digitizer.btn_select_data,
Qt.LeftButton)
magni_marks = self.straditize.magni_marks or []
for m in self.straditizer.marks + magni_marks:
m.remove()
self.straditizer.marks.clear()
magni_marks.clear()
self.straditizer._new_mark(x0, y0)
self.straditizer._new_mark(x1, y1)
QTest.mouseClick(self.straditizer_widgets.apply_button,
Expand Down
1 change: 1 addition & 0 deletions tests/widgets/test_samples_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the straditize.widgets.samples_table module"""
import numpy as np
import _base_testing as bt
from itertools import chain
import unittest
from psyplot_gui.compat.qtcompat import QTest, Qt
from psyplot.utils import unique_everseen
Expand Down
4 changes: 4 additions & 0 deletions tests/widgets/test_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def test_page(self):
# Now click the correct button
self._test_hint(self.digitizer.btn_select_data.text())
QTest.mouseClick(self.digitizer.btn_select_data, Qt.LeftButton)
for m in self.straditizer.marks + self.straditizer.magni_marks:
m.remove()
self.straditizer.marks.clear()
self.straditizer.magni_marks.clear()
# this should display a hint to shift+leftclick one of the corners
self._test_hint('(?i)Shift\+leftclick')
mark = sw.straditizer._new_mark(*self.page.ref_lims[:, 0] + 20)[0]
Expand Down

0 comments on commit 1f7e3a5

Please sign in to comment.