# Path: rivuletpy/utils/io.py
# def saveswc(filepath, swc):
#     if swc.shape[1] > 7:
#         swc = swc[:, :7]
# 
#     with open(filepath, 'w') as f:
#         for i in range(swc.shape[0]):
#             print('%d %d %.3f %.3f %.3f %.3f %d' %
#                   tuple(swc[i, :].tolist()), file=f)


import math
import numpy as np
from .utils.io import saveswc
from collections import Counter
from random import gauss
from random import random
from random import randrange
from scipy.spatial.distance import cdist
        from rivuletpy.utils.rendering3 import Viewer3, Line3
        # Pad the swc back
        tswc[:, 2] += crop_region[0, 0]
        tswc[:, 3] += crop_region[1, 0]
        tswc[:, 4] += crop_region[2, 0]
        self._data = tswc

    def get_id(self, idx):
        return self._data[idx, 0]

    def match(self, pos, radius):
        '''
        Find the closest ground truth node 
        '''

        nodes = self._data[:, 2:5]
        distlist = np.squeeze(cdist(pos.reshape(1, 3), nodes))
        if distlist.size == 0:
            return False, -2
        minidx = distlist.argmin()
        minnode = self._data[minidx, 2:5]

        # See if either of them can cover each other with a ball of their own
        # radius
        mindist = np.linalg.norm(pos - minnode)
        return radius > mindist or self._data[minidx, 5] > mindist, minidx

    def size(self):
        return self._data.shape[0]

    def save(self, fname):

# Path: rivuletpy/utils/io.py
# def saveswc(filepath, swc):
#     if swc.shape[1] > 7:
#         swc = swc[:, :7]
# 
#     with open(filepath, 'w') as f:
#         for i in range(swc.shape[0]):
#             print('%d %d %.3f %.3f %.3f %.3f %d' %
#                   tuple(swc[i, :].tolist()), file=f)


# Path: rivuletpy/swc.py
import math
import numpy as np
from .utils.io import saveswc
from collections import Counter
from random import gauss
from random import random
from random import randrange
from scipy.spatial.distance import cdist
        from rivuletpy.utils.rendering3 import Viewer3, Line3

        # Pad the swc back
        tswc[:, 2] += crop_region[0, 0]
        tswc[:, 3] += crop_region[1, 0]
        tswc[:, 4] += crop_region[2, 0]
        self._data = tswc

    def get_id(self, idx):
        return self._data[idx, 0]

    def match(self, pos, radius):
        '''
        Find the closest ground truth node 
        '''

        nodes = self._data[:, 2:5]
        distlist = np.squeeze(cdist(pos.reshape(1, 3), nodes))
        if distlist.size == 0:
            return False, -2
        minidx = distlist.argmin()
        minnode = self._data[minidx, 2:5]

        # See if either of them can cover each other with a ball of their own
        # radius
        mindist = np.linalg.norm(pos - minnode)
        return radius > mindist or self._data[minidx, 5] > mindist, minidx

    def size(self):
        return self._data.shape[0]

    def save(self, fname):

        saveswc(fname, self._data)