We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
find_contours returns (y,x) instead of (x,y) pairs
The API doc says for the return value "Each contour is an ndarray of shape `(n, 2)`, consisting of n `(x, y)` coordinates along the contour."

A small test case, with input:
![figure_4](https://cloud.githubusercontent.com/assets/530988/4217168/2ceec576-38e4-11e4-8b27-62839077635e.png)

```
def testImage():
    n = 10
    coord = np.ones((n,n))

    r = n*0.4
    y,x = np.ogrid[-r: r+1, -r: r+1]
    mask = x**2+y**2 <= r**2

    coord[mask] = -coord[mask]
    coord[coord>0] = np.nan
    # make it non-symmetric
    coord[4,0] = np.nan

    r = ~np.isnan(coord)
    return np.hstack((r,r))

import skimage.measure
contours = skimage.measure.find_contours(testImage(), 0.99)
print contours
# output:
 [array([[ 0.,  4.],
       [ 1.,  5.],
       [ 1.,  6.],
       [ 1.,  6.],
       [ 2.,  7.],
       [ 2.,  7.],
       [ 3.,  7.],
       [ 4.,  8.],
       [ 4.,  8.],
       [ 4.,  8.],
       [ 5.,  7.],
       [ 6.,  7.],
       [ 6.,  7.],
       [ 7.,  6.],
       [ 7.,  6.],
       [ 7.,  5.],
       [ 8.,  4.],
       [ 8.,  4.],
       [ 8.,  4.],
       [ 7.,  3.],
       [ 7.,  2.],
       [ 7.,  2.],
       [ 6.,  1.],
       [ 6.,  1.],
       [ 5.,  1.],
       [ 4.,  1.],
       [ 3.,  1.],
       [ 2.,  1.],
       [ 2.,  1.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  3.],
       [ 0.,  4.]]), array([[  0.,  14.],
       [  1.,  15.],
       [  1.,  16.],
       [  1.,  16.],
       [  2.,  17.],
       [  2.,  17.],
       [  3.,  17.],
       [  4.,  18.],
       [  4.,  18.],
       [  4.,  18.],
       [  5.,  17.],
       [  6.,  17.],
       [  6.,  17.],
       [  7.,  16.],
       [  7.,  16.],
       [  7.,  15.],
       [  8.,  14.],
       [  8.,  14.],
       [  8.,  14.],
       [  7.,  13.],
       [  7.,  12.],
       [  7.,  12.],
       [  6.,  11.],
       [  6.,  11.],
       [  5.,  11.],
       [  4.,  11.],
       [  3.,  11.],
       [  2.,  11.],
       [  2.,  11.],
       [  1.,  12.],
       [  1.,  12.],
       [  1.,  13.],
       [  0.,  14.]])]
```

You can see clearly that the x-coordinate is actually in the second column and not the first.

Apart from that, why are there consecutive duplicates? Should I open a separate issue for that?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `skimage/measure/_find_contours.py`
Content:
```
1 import numpy as np
2 from . import _find_contours_cy
3 
4 from collections import deque
5 
6 _param_options = ('high', 'low')
7 
8 
9 def find_contours(array, level,
10                   fully_connected='low', positive_orientation='low'):
11     """Find iso-valued contours in a 2D array for a given level value.
12 
13     Uses the "marching squares" method to compute a the iso-valued contours of
14     the input 2D array for a particular level value. Array values are linearly
15     interpolated to provide better precision for the output contours.
16 
17     Parameters
18     ----------
19     array : 2D ndarray of double
20         Input data in which to find contours.
21     level : float
22         Value along which to find contours in the array.
23     fully_connected : str, {'low', 'high'}
24          Indicates whether array elements below the given level value are to be
25          considered fully-connected (and hence elements above the value will
26          only be face connected), or vice-versa. (See notes below for details.)
27     positive_orientation : either 'low' or 'high'
28          Indicates whether the output contours will produce positively-oriented
29          polygons around islands of low- or high-valued elements. If 'low' then
30          contours will wind counter- clockwise around elements below the
31          iso-value. Alternately, this means that low-valued elements are always
32          on the left of the contour. (See below for details.)
33 
34     Returns
35     -------
36     contours : list of (n,2)-ndarrays
37         Each contour is an ndarray of shape ``(n, 2)``,
38         consisting of n ``(x, y)`` coordinates along the contour.
39 
40     Notes
41     -----
42     The marching squares algorithm is a special case of the marching cubes
43     algorithm [1]_.  A simple explanation is available here::
44 
45       http://www.essi.fr/~lingrand/MarchingCubes/algo.html
46 
47     There is a single ambiguous case in the marching squares algorithm: when
48     a given ``2 x 2``-element square has two high-valued and two low-valued
49     elements, each pair diagonally adjacent. (Where high- and low-valued is
50     with respect to the contour value sought.) In this case, either the
51     high-valued elements can be 'connected together' via a thin isthmus that
52     separates the low-valued elements, or vice-versa. When elements are
53     connected together across a diagonal, they are considered 'fully
54     connected' (also known as 'face+vertex-connected' or '8-connected'). Only
55     high-valued or low-valued elements can be fully-connected, the other set
56     will be considered as 'face-connected' or '4-connected'. By default,
57     low-valued elements are considered fully-connected; this can be altered
58     with the 'fully_connected' parameter.
59 
60     Output contours are not guaranteed to be closed: contours which intersect
61     the array edge will be left open. All other contours will be closed. (The
62     closed-ness of a contours can be tested by checking whether the beginning
63     point is the same as the end point.)
64 
65     Contours are oriented. By default, array values lower than the contour
66     value are to the left of the contour and values greater than the contour
67     value are to the right. This means that contours will wind
68     counter-clockwise (i.e. in 'positive orientation') around islands of
69     low-valued pixels. This behavior can be altered with the
70     'positive_orientation' parameter.
71 
72     The order of the contours in the output list is determined by the position
73     of the smallest ``x,y`` (in lexicographical order) coordinate in the
74     contour.  This is a side-effect of how the input array is traversed, but
75     can be relied upon.
76 
77     .. warning::
78 
79        Array coordinates/values are assumed to refer to the *center* of the
80        array element. Take a simple example input: ``[0, 1]``. The interpolated
81        position of 0.5 in this array is midway between the 0-element (at
82        ``x=0``) and the 1-element (at ``x=1``), and thus would fall at
83        ``x=0.5``.
84 
85     This means that to find reasonable contours, it is best to find contours
86     midway between the expected "light" and "dark" values. In particular,
87     given a binarized array, *do not* choose to find contours at the low or
88     high value of the array. This will often yield degenerate contours,
89     especially around structures that are a single array element wide. Instead
90     choose a middle value, as above.
91 
92     References
93     ----------
94     .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High
95            Resolution 3D Surface Construction Algorithm. Computer Graphics
96            (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).
97 
98     Examples
99     --------
100     >>> a = np.zeros((3, 3))
101     >>> a[0, 0] = 1
102     >>> a
103     array([[ 1.,  0.,  0.],
104            [ 0.,  0.,  0.],
105            [ 0.,  0.,  0.]])
106     >>> find_contours(a, 0.5)
107     [array([[ 0. ,  0.5],
108            [ 0.5,  0. ]])]
109     """
110     array = np.asarray(array, dtype=np.double)
111     if array.ndim != 2:
112         raise ValueError('Only 2D arrays are supported.')
113     level = float(level)
114     if (fully_connected not in _param_options or
115        positive_orientation not in _param_options):
116         raise ValueError('Parameters "fully_connected" and'
117         ' "positive_orientation" must be either "high" or "low".')
118     point_list = _find_contours_cy.iterate_and_store(array, level,
119                                                      fully_connected == 'high')
120     contours = _assemble_contours(_take_2(point_list))
121     if positive_orientation == 'high':
122         contours = [c[::-1] for c in contours]
123     return contours
124 
125 
126 def _take_2(seq):
127     iterator = iter(seq)
128     while(True):
129         n1 = next(iterator)
130         n2 = next(iterator)
131         yield (n1, n2)
132 
133 
134 def _assemble_contours(points_iterator):
135     current_index = 0
136     contours = {}
137     starts = {}
138     ends = {}
139     for from_point, to_point in points_iterator:
140         # Ignore degenerate segments.
141         # This happens when (and only when) one vertex of the square is
142         # exactly the contour level, and the rest are above or below.
143         # This degnerate vertex will be picked up later by neighboring squares.
144         if from_point == to_point:
145             continue
146 
147         tail_data = starts.get(to_point)
148         head_data = ends.get(from_point)
149 
150         if tail_data is not None and head_data is not None:
151             tail, tail_num = tail_data
152             head, head_num = head_data
153             # We need to connect these two contours.
154             if tail is head:
155                 # We need to closed a contour.
156                 # Add the end point, and remove the contour from the
157                 # 'starts' and 'ends' dicts.
158                 head.append(to_point)
159                 del starts[to_point]
160                 del ends[from_point]
161             else:  # tail is not head
162                 # We need to join two distinct contours.
163                 # We want to keep the first contour segment created, so that
164                 # the final contours are ordered left->right, top->bottom.
165                 if tail_num > head_num:
166                     # tail was created second. Append tail to head.
167                     head.extend(tail)
168                     # remove all traces of tail:
169                     del starts[to_point]
170                     del ends[tail[-1]]
171                     del contours[tail_num]
172                     # remove the old end of head and add the new end.
173                     del ends[from_point]
174                     ends[head[-1]] = (head, head_num)
175                 else:  # tail_num <= head_num
176                     # head was created second. Prepend head to tail.
177                     tail.extendleft(reversed(head))
178                     # remove all traces of head:
179                     del starts[head[0]]
180                     del ends[from_point]
181                     del contours[head_num]
182                     # remove the old start of tail and add the new start.
183                     del starts[to_point]
184                     starts[tail[0]] = (tail, tail_num)
185         elif tail_data is None and head_data is None:
186             # we need to add a new contour
187             current_index += 1
188             new_num = current_index
189             new_contour = deque((from_point, to_point))
190             contours[new_num] = new_contour
191             starts[from_point] = (new_contour, new_num)
192             ends[to_point] = (new_contour, new_num)
193         elif tail_data is not None and head_data is None:
194             tail, tail_num = tail_data
195             # We've found a single contour to which the new segment should be
196             # prepended.
197             tail.appendleft(from_point)
198             del starts[to_point]
199             starts[from_point] = (tail, tail_num)
200         elif tail_data is None and head_data is not None:
201             head, head_num = head_data
202             # We've found a single contour to which the new segment should be
203             # appended
204             head.append(to_point)
205             del ends[from_point]
206             ends[to_point] = (head, head_num)
207     # end iteration over from_ and to_ points
208 
209     return [np.array(contour) for (num, contour) in sorted(contours.items())]
210
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/skimage/measure/_find_contours.py b/skimage/measure/_find_contours.py
--- a/skimage/measure/_find_contours.py
+++ b/skimage/measure/_find_contours.py
@@ -35,7 +35,7 @@
     -------
     contours : list of (n,2)-ndarrays
         Each contour is an ndarray of shape ``(n, 2)``,
-        consisting of n ``(x, y)`` coordinates along the contour.
+        consisting of n ``(row, column)`` coordinates along the contour.
 
     Notes
     -----

{"golden_diff": "diff --git a/skimage/measure/_find_contours.py b/skimage/measure/_find_contours.py\n--- a/skimage/measure/_find_contours.py\n+++ b/skimage/measure/_find_contours.py\n@@ -35,7 +35,7 @@\n     -------\n     contours : list of (n,2)-ndarrays\n         Each contour is an ndarray of shape ``(n, 2)``,\n-        consisting of n ``(x, y)`` coordinates along the contour.\n+        consisting of n ``(row, column)`` coordinates along the contour.\n \n     Notes\n     -----\n", "issue": "find_contours returns (y,x) instead of (x,y) pairs\nThe API doc says for the return value \"Each contour is an ndarray of shape `(n, 2)`, consisting of n `(x, y)` coordinates along the contour.\"\n\nA small test case, with input:\n![figure_4](https://cloud.githubusercontent.com/assets/530988/4217168/2ceec576-38e4-11e4-8b27-62839077635e.png)\n\n```\ndef testImage():\n    n = 10\n    coord = np.ones((n,n))\n\n    r = n*0.4\n    y,x = np.ogrid[-r: r+1, -r: r+1]\n    mask = x**2+y**2 <= r**2\n\n    coord[mask] = -coord[mask]\n    coord[coord>0] = np.nan\n    # make it non-symmetric\n    coord[4,0] = np.nan\n\n    r = ~np.isnan(coord)\n    return np.hstack((r,r))\n\nimport skimage.measure\ncontours = skimage.measure.find_contours(testImage(), 0.99)\nprint contours\n# output:\n [array([[ 0.,  4.],\n       [ 1.,  5.],\n       [ 1.,  6.],\n       [ 1.,  6.],\n       [ 2.,  7.],\n       [ 2.,  7.],\n       [ 3.,  7.],\n       [ 4.,  8.],\n       [ 4.,  8.],\n       [ 4.,  8.],\n       [ 5.,  7.],\n       [ 6.,  7.],\n       [ 6.,  7.],\n       [ 7.,  6.],\n       [ 7.,  6.],\n       [ 7.,  5.],\n       [ 8.,  4.],\n       [ 8.,  4.],\n       [ 8.,  4.],\n       [ 7.,  3.],\n       [ 7.,  2.],\n       [ 7.,  2.],\n       [ 6.,  1.],\n       [ 6.,  1.],\n       [ 5.,  1.],\n       [ 4.,  1.],\n       [ 3.,  1.],\n       [ 2.,  1.],\n       [ 2.,  1.],\n       [ 1.,  2.],\n       [ 1.,  2.],\n       [ 1.,  3.],\n       [ 0.,  4.]]), array([[  0.,  14.],\n       [  1.,  15.],\n       [  1.,  16.],\n       [  1.,  16.],\n       [  2.,  17.],\n       [  2.,  17.],\n       [  3.,  17.],\n       [  4.,  18.],\n       [  4.,  18.],\n       [  4.,  18.],\n       [  5.,  17.],\n       [  6.,  17.],\n       [  6.,  17.],\n       [  7.,  16.],\n       [  7.,  16.],\n       [  7.,  15.],\n       [  8.,  14.],\n       [  8.,  14.],\n       [  8.,  14.],\n       [  7.,  13.],\n       [  7.,  12.],\n       [  7.,  12.],\n       [  6.,  11.],\n       [  6.,  11.],\n       [  5.,  11.],\n       [  4.,  11.],\n       [  3.,  11.],\n       [  2.,  11.],\n       [  2.,  11.],\n       [  1.,  12.],\n       [  1.,  12.],\n       [  1.,  13.],\n       [  0.,  14.]])]\n```\n\nYou can see clearly that the x-coordinate is actually in the second column and not the first.\n\nApart from that, why are there consecutive duplicates? Should I open a separate issue for that?\n\n", "before_files": [{"content": "import numpy as np\nfrom . import _find_contours_cy\n\nfrom collections import deque\n\n_param_options = ('high', 'low')\n\n\ndef find_contours(array, level,\n                  fully_connected='low', positive_orientation='low'):\n    \"\"\"Find iso-valued contours in a 2D array for a given level value.\n\n    Uses the \"marching squares\" method to compute a the iso-valued contours of\n    the input 2D array for a particular level value. Array values are linearly\n    interpolated to provide better precision for the output contours.\n\n    Parameters\n    ----------\n    array : 2D ndarray of double\n        Input data in which to find contours.\n    level : float\n        Value along which to find contours in the array.\n    fully_connected : str, {'low', 'high'}\n         Indicates whether array elements below the given level value are to be\n         considered fully-connected (and hence elements above the value will\n         only be face connected), or vice-versa. (See notes below for details.)\n    positive_orientation : either 'low' or 'high'\n         Indicates whether the output contours will produce positively-oriented\n         polygons around islands of low- or high-valued elements. If 'low' then\n         contours will wind counter- clockwise around elements below the\n         iso-value. Alternately, this means that low-valued elements are always\n         on the left of the contour. (See below for details.)\n\n    Returns\n    -------\n    contours : list of (n,2)-ndarrays\n        Each contour is an ndarray of shape ``(n, 2)``,\n        consisting of n ``(x, y)`` coordinates along the contour.\n\n    Notes\n    -----\n    The marching squares algorithm is a special case of the marching cubes\n    algorithm [1]_.  A simple explanation is available here::\n\n      http://www.essi.fr/~lingrand/MarchingCubes/algo.html\n\n    There is a single ambiguous case in the marching squares algorithm: when\n    a given ``2 x 2``-element square has two high-valued and two low-valued\n    elements, each pair diagonally adjacent. (Where high- and low-valued is\n    with respect to the contour value sought.) In this case, either the\n    high-valued elements can be 'connected together' via a thin isthmus that\n    separates the low-valued elements, or vice-versa. When elements are\n    connected together across a diagonal, they are considered 'fully\n    connected' (also known as 'face+vertex-connected' or '8-connected'). Only\n    high-valued or low-valued elements can be fully-connected, the other set\n    will be considered as 'face-connected' or '4-connected'. By default,\n    low-valued elements are considered fully-connected; this can be altered\n    with the 'fully_connected' parameter.\n\n    Output contours are not guaranteed to be closed: contours which intersect\n    the array edge will be left open. All other contours will be closed. (The\n    closed-ness of a contours can be tested by checking whether the beginning\n    point is the same as the end point.)\n\n    Contours are oriented. By default, array values lower than the contour\n    value are to the left of the contour and values greater than the contour\n    value are to the right. This means that contours will wind\n    counter-clockwise (i.e. in 'positive orientation') around islands of\n    low-valued pixels. This behavior can be altered with the\n    'positive_orientation' parameter.\n\n    The order of the contours in the output list is determined by the position\n    of the smallest ``x,y`` (in lexicographical order) coordinate in the\n    contour.  This is a side-effect of how the input array is traversed, but\n    can be relied upon.\n\n    .. warning::\n\n       Array coordinates/values are assumed to refer to the *center* of the\n       array element. Take a simple example input: ``[0, 1]``. The interpolated\n       position of 0.5 in this array is midway between the 0-element (at\n       ``x=0``) and the 1-element (at ``x=1``), and thus would fall at\n       ``x=0.5``.\n\n    This means that to find reasonable contours, it is best to find contours\n    midway between the expected \"light\" and \"dark\" values. In particular,\n    given a binarized array, *do not* choose to find contours at the low or\n    high value of the array. This will often yield degenerate contours,\n    especially around structures that are a single array element wide. Instead\n    choose a middle value, as above.\n\n    References\n    ----------\n    .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High\n           Resolution 3D Surface Construction Algorithm. Computer Graphics\n           (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).\n\n    Examples\n    --------\n    >>> a = np.zeros((3, 3))\n    >>> a[0, 0] = 1\n    >>> a\n    array([[ 1.,  0.,  0.],\n           [ 0.,  0.,  0.],\n           [ 0.,  0.,  0.]])\n    >>> find_contours(a, 0.5)\n    [array([[ 0. ,  0.5],\n           [ 0.5,  0. ]])]\n    \"\"\"\n    array = np.asarray(array, dtype=np.double)\n    if array.ndim != 2:\n        raise ValueError('Only 2D arrays are supported.')\n    level = float(level)\n    if (fully_connected not in _param_options or\n       positive_orientation not in _param_options):\n        raise ValueError('Parameters \"fully_connected\" and'\n        ' \"positive_orientation\" must be either \"high\" or \"low\".')\n    point_list = _find_contours_cy.iterate_and_store(array, level,\n                                                     fully_connected == 'high')\n    contours = _assemble_contours(_take_2(point_list))\n    if positive_orientation == 'high':\n        contours = [c[::-1] for c in contours]\n    return contours\n\n\ndef _take_2(seq):\n    iterator = iter(seq)\n    while(True):\n        n1 = next(iterator)\n        n2 = next(iterator)\n        yield (n1, n2)\n\n\ndef _assemble_contours(points_iterator):\n    current_index = 0\n    contours = {}\n    starts = {}\n    ends = {}\n    for from_point, to_point in points_iterator:\n        # Ignore degenerate segments.\n        # This happens when (and only when) one vertex of the square is\n        # exactly the contour level, and the rest are above or below.\n        # This degnerate vertex will be picked up later by neighboring squares.\n        if from_point == to_point:\n            continue\n\n        tail_data = starts.get(to_point)\n        head_data = ends.get(from_point)\n\n        if tail_data is not None and head_data is not None:\n            tail, tail_num = tail_data\n            head, head_num = head_data\n            # We need to connect these two contours.\n            if tail is head:\n                # We need to closed a contour.\n                # Add the end point, and remove the contour from the\n                # 'starts' and 'ends' dicts.\n                head.append(to_point)\n                del starts[to_point]\n                del ends[from_point]\n            else:  # tail is not head\n                # We need to join two distinct contours.\n                # We want to keep the first contour segment created, so that\n                # the final contours are ordered left->right, top->bottom.\n                if tail_num > head_num:\n                    # tail was created second. Append tail to head.\n                    head.extend(tail)\n                    # remove all traces of tail:\n                    del starts[to_point]\n                    del ends[tail[-1]]\n                    del contours[tail_num]\n                    # remove the old end of head and add the new end.\n                    del ends[from_point]\n                    ends[head[-1]] = (head, head_num)\n                else:  # tail_num <= head_num\n                    # head was created second. Prepend head to tail.\n                    tail.extendleft(reversed(head))\n                    # remove all traces of head:\n                    del starts[head[0]]\n                    del ends[from_point]\n                    del contours[head_num]\n                    # remove the old start of tail and add the new start.\n                    del starts[to_point]\n                    starts[tail[0]] = (tail, tail_num)\n        elif tail_data is None and head_data is None:\n            # we need to add a new contour\n            current_index += 1\n            new_num = current_index\n            new_contour = deque((from_point, to_point))\n            contours[new_num] = new_contour\n            starts[from_point] = (new_contour, new_num)\n            ends[to_point] = (new_contour, new_num)\n        elif tail_data is not None and head_data is None:\n            tail, tail_num = tail_data\n            # We've found a single contour to which the new segment should be\n            # prepended.\n            tail.appendleft(from_point)\n            del starts[to_point]\n            starts[from_point] = (tail, tail_num)\n        elif tail_data is None and head_data is not None:\n            head, head_num = head_data\n            # We've found a single contour to which the new segment should be\n            # appended\n            head.append(to_point)\n            del ends[from_point]\n            ends[to_point] = (head, head_num)\n    # end iteration over from_ and to_ points\n\n    return [np.array(contour) for (num, contour) in sorted(contours.items())]\n", "path": "skimage/measure/_find_contours.py"}], "after_files": [{"content": "import numpy as np\nfrom . import _find_contours_cy\n\nfrom collections import deque\n\n_param_options = ('high', 'low')\n\n\ndef find_contours(array, level,\n                  fully_connected='low', positive_orientation='low'):\n    \"\"\"Find iso-valued contours in a 2D array for a given level value.\n\n    Uses the \"marching squares\" method to compute a the iso-valued contours of\n    the input 2D array for a particular level value. Array values are linearly\n    interpolated to provide better precision for the output contours.\n\n    Parameters\n    ----------\n    array : 2D ndarray of double\n        Input data in which to find contours.\n    level : float\n        Value along which to find contours in the array.\n    fully_connected : str, {'low', 'high'}\n         Indicates whether array elements below the given level value are to be\n         considered fully-connected (and hence elements above the value will\n         only be face connected), or vice-versa. (See notes below for details.)\n    positive_orientation : either 'low' or 'high'\n         Indicates whether the output contours will produce positively-oriented\n         polygons around islands of low- or high-valued elements. If 'low' then\n         contours will wind counter- clockwise around elements below the\n         iso-value. Alternately, this means that low-valued elements are always\n         on the left of the contour. (See below for details.)\n\n    Returns\n    -------\n    contours : list of (n,2)-ndarrays\n        Each contour is an ndarray of shape ``(n, 2)``,\n        consisting of n ``(row, column)`` coordinates along the contour.\n\n    Notes\n    -----\n    The marching squares algorithm is a special case of the marching cubes\n    algorithm [1]_.  A simple explanation is available here::\n\n      http://www.essi.fr/~lingrand/MarchingCubes/algo.html\n\n    There is a single ambiguous case in the marching squares algorithm: when\n    a given ``2 x 2``-element square has two high-valued and two low-valued\n    elements, each pair diagonally adjacent. (Where high- and low-valued is\n    with respect to the contour value sought.) In this case, either the\n    high-valued elements can be 'connected together' via a thin isthmus that\n    separates the low-valued elements, or vice-versa. When elements are\n    connected together across a diagonal, they are considered 'fully\n    connected' (also known as 'face+vertex-connected' or '8-connected'). Only\n    high-valued or low-valued elements can be fully-connected, the other set\n    will be considered as 'face-connected' or '4-connected'. By default,\n    low-valued elements are considered fully-connected; this can be altered\n    with the 'fully_connected' parameter.\n\n    Output contours are not guaranteed to be closed: contours which intersect\n    the array edge will be left open. All other contours will be closed. (The\n    closed-ness of a contours can be tested by checking whether the beginning\n    point is the same as the end point.)\n\n    Contours are oriented. By default, array values lower than the contour\n    value are to the left of the contour and values greater than the contour\n    value are to the right. This means that contours will wind\n    counter-clockwise (i.e. in 'positive orientation') around islands of\n    low-valued pixels. This behavior can be altered with the\n    'positive_orientation' parameter.\n\n    The order of the contours in the output list is determined by the position\n    of the smallest ``x,y`` (in lexicographical order) coordinate in the\n    contour.  This is a side-effect of how the input array is traversed, but\n    can be relied upon.\n\n    .. warning::\n\n       Array coordinates/values are assumed to refer to the *center* of the\n       array element. Take a simple example input: ``[0, 1]``. The interpolated\n       position of 0.5 in this array is midway between the 0-element (at\n       ``x=0``) and the 1-element (at ``x=1``), and thus would fall at\n       ``x=0.5``.\n\n    This means that to find reasonable contours, it is best to find contours\n    midway between the expected \"light\" and \"dark\" values. In particular,\n    given a binarized array, *do not* choose to find contours at the low or\n    high value of the array. This will often yield degenerate contours,\n    especially around structures that are a single array element wide. Instead\n    choose a middle value, as above.\n\n    References\n    ----------\n    .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High\n           Resolution 3D Surface Construction Algorithm. Computer Graphics\n           (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).\n\n    Examples\n    --------\n    >>> a = np.zeros((3, 3))\n    >>> a[0, 0] = 1\n    >>> a\n    array([[ 1.,  0.,  0.],\n           [ 0.,  0.,  0.],\n           [ 0.,  0.,  0.]])\n    >>> find_contours(a, 0.5)\n    [array([[ 0. ,  0.5],\n           [ 0.5,  0. ]])]\n    \"\"\"\n    array = np.asarray(array, dtype=np.double)\n    if array.ndim != 2:\n        raise ValueError('Only 2D arrays are supported.')\n    level = float(level)\n    if (fully_connected not in _param_options or\n       positive_orientation not in _param_options):\n        raise ValueError('Parameters \"fully_connected\" and'\n        ' \"positive_orientation\" must be either \"high\" or \"low\".')\n    point_list = _find_contours_cy.iterate_and_store(array, level,\n                                                     fully_connected == 'high')\n    contours = _assemble_contours(_take_2(point_list))\n    if positive_orientation == 'high':\n        contours = [c[::-1] for c in contours]\n    return contours\n\n\ndef _take_2(seq):\n    iterator = iter(seq)\n    while(True):\n        n1 = next(iterator)\n        n2 = next(iterator)\n        yield (n1, n2)\n\n\ndef _assemble_contours(points_iterator):\n    current_index = 0\n    contours = {}\n    starts = {}\n    ends = {}\n    for from_point, to_point in points_iterator:\n        # Ignore degenerate segments.\n        # This happens when (and only when) one vertex of the square is\n        # exactly the contour level, and the rest are above or below.\n        # This degnerate vertex will be picked up later by neighboring squares.\n        if from_point == to_point:\n            continue\n\n        tail_data = starts.get(to_point)\n        head_data = ends.get(from_point)\n\n        if tail_data is not None and head_data is not None:\n            tail, tail_num = tail_data\n            head, head_num = head_data\n            # We need to connect these two contours.\n            if tail is head:\n                # We need to closed a contour.\n                # Add the end point, and remove the contour from the\n                # 'starts' and 'ends' dicts.\n                head.append(to_point)\n                del starts[to_point]\n                del ends[from_point]\n            else:  # tail is not head\n                # We need to join two distinct contours.\n                # We want to keep the first contour segment created, so that\n                # the final contours are ordered left->right, top->bottom.\n                if tail_num > head_num:\n                    # tail was created second. Append tail to head.\n                    head.extend(tail)\n                    # remove all traces of tail:\n                    del starts[to_point]\n                    del ends[tail[-1]]\n                    del contours[tail_num]\n                    # remove the old end of head and add the new end.\n                    del ends[from_point]\n                    ends[head[-1]] = (head, head_num)\n                else:  # tail_num <= head_num\n                    # head was created second. Prepend head to tail.\n                    tail.extendleft(reversed(head))\n                    # remove all traces of head:\n                    del starts[head[0]]\n                    del ends[from_point]\n                    del contours[head_num]\n                    # remove the old start of tail and add the new start.\n                    del starts[to_point]\n                    starts[tail[0]] = (tail, tail_num)\n        elif tail_data is None and head_data is None:\n            # we need to add a new contour\n            current_index += 1\n            new_num = current_index\n            new_contour = deque((from_point, to_point))\n            contours[new_num] = new_contour\n            starts[from_point] = (new_contour, new_num)\n            ends[to_point] = (new_contour, new_num)\n        elif tail_data is not None and head_data is None:\n            tail, tail_num = tail_data\n            # We've found a single contour to which the new segment should be\n            # prepended.\n            tail.appendleft(from_point)\n            del starts[to_point]\n            starts[from_point] = (tail, tail_num)\n        elif tail_data is None and head_data is not None:\n            head, head_num = head_data\n            # We've found a single contour to which the new segment should be\n            # appended\n            head.append(to_point)\n            del ends[from_point]\n            ends[to_point] = (head, head_num)\n    # end iteration over from_ and to_ points\n\n    return [np.array(contour) for (num, contour) in sorted(contours.items())]\n", "path": "skimage/measure/_find_contours.py"}]}