<commit_before>import logging
import os

from dateutil import parser

import numpy as np
import pandas as pd

from sktracker.io import TiffFile
from sktracker.io import StackIO
from sktracker.io import ObjectsIO
from sktracker.detection import peak_detector
from sktracker.io.trackmate import trackmate_peak_import
from sktracker.utils import print_progress
from sktracker.trajectories import Trajectories

from ..utils.short_id import id_generator
from ..utils.path import check_extension
from ..utils.geometry import build_transformations_matrix

log = logging.getLogger(__name__)


class Tracker():
    """
    Generic container for particle tracking
    """

    MINIMUM_METADATA = []
    HDF5_EXTENSION = "h5"
    TIF_EXTENSION = "tif"
    XML_EXTENSION = "xml"

    def __init__(self,
                 sample_path,
                 base_dir,
                 verbose=True,
                 force_metadata=False,
                 json_discovery=True):
        """
        Parameters:
        -----------
        sample_path: string
            path to TIF file or to HDF5 file
        """

        if not verbose:
            log.disabled = True
        else:
            log.disabled = False
        self._verbose = verbose

        # Init paths
        self.sample_path = sample_path
        self.base_dir = base_dir
        self.full_path = os.path.join(self.base_dir, self.sample_path)

        self.tif_path = self.has_tif()
        self.h5_path = self.has_h5()

        self.st = None
        self.oio = None

        if self.h5_path and not force_metadata:
            try:
                self.oio = ObjectsIO.from_h5(self.h5_path,
                                             base_dir=self.base_dir,
                                             minimum_metadata_keys=self.__class__.MINIMUM_METADATA)
            except ValueError as e:
                log.error(e)

        if self.tif_path and not self.oio:
            self.st = StackIO(image_path=self.tif_path,
                              base_dir=self.base_dir,
                              json_discovery=json_discovery)

            self.h5_path = self.has_h5(force=True)
            self.oio = ObjectsIO(self.st.metadata,
                                 store_path=self.h5_path,
                                 base_dir=self.base_dir,
                                 minimum_metadata_keys=self.__class__.MINIMUM_METADATA)

        self.load_oio()

    @property
    def verbose(self):
        return self._verbose

    @verbose.setter
    def verbose(self, value):
        log.disabled = not value
        self._verbose = value

    @property
    def full_tif_path(self):
        if self.tif_path:
            return os.path.join(self.base_dir, self.tif_path)
        else:
            return None

    @property
    def full_h5_path(self):
        if self.h5_path:
            return os.path.join(self.base_dir, self.h5_path)
        else:
            return None

    @property
    def full_xml_path(self):
        if self.xml_path:
            return os.path.join(self.base_dir, self.xml_path)
        else:
            return None

    @property
    def stored_data(self):
        if not hasattr(self, '_stored_data'):
            self._stored_data = []
        else:
            self._stored_data = list(set(self._stored_data))
        return self._stored_data

    def has_h5(self, force=False):
        """
        """
        extension = self.__class__.HDF5_EXTENSION

        full_path = check_extension(self.full_path,
                                    extension=extension,
                                    force=force)

        if full_path:
            return os.path.relpath(full_path, self.base_dir)
        else:
            return None

    def has_tif(self, force=False):
        """
        """
        extension = self.__class__.TIF_EXTENSION

        full_path = check_extension(self.full_path,
                                    extension=extension,
                                    force=force)

        if full_path:
            return os.path.relpath(full_path, self.base_dir)
        else:
            return None

    def has_xml(self, force=False, suffix=None):
        """
        """
        extension = self.__class__.XML_EXTENSION

        full_path = check_extension(self.full_path,
                                    extension=extension,
                                    suffix=suffix,
                                    force=force)

        if full_path:
            self.xml_path = os.path.relpath(full_path, self.base_dir)
            return self.xml_path
        else:
            return None

    def load_oio(self):
        """
        """
        for key in self.oio.keys():
            key = key.replace('/', '')
            self.stored_data.append(key)
            obj = self.oio[key]<commit_msg>Raise error when no valid sample fiel in Tracker<commit_after>import logging
import os

from dateutil import parser

import numpy as np
import pandas as pd

from sktracker.io import TiffFile
from sktracker.io import StackIO
from sktracker.io import ObjectsIO
from sktracker.detection import peak_detector
from sktracker.io.trackmate import trackmate_peak_import
from sktracker.utils import print_progress
from sktracker.trajectories import Trajectories

from ..utils.short_id import id_generator
from ..utils.path import check_extension
from ..utils.geometry import build_transformations_matrix

log = logging.getLogger(__name__)


class Tracker():
    """
    Generic container for particle tracking
    """

    MINIMUM_METADATA = []
    HDF5_EXTENSION = "h5"
    TIF_EXTENSION = "tif"
    XML_EXTENSION = "xml"

    def __init__(self,
                 sample_path,
                 base_dir,
                 verbose=True,
                 force_metadata=False,
                 json_discovery=True):
        """
        Parameters:
        -----------
        sample_path: string
            path to TIF file or to HDF5 file
        """

        if not verbose:
            log.disabled = True
        else:
            log.disabled = False
        self._verbose = verbose

        # Init paths
        self.sample_path = sample_path
        self.base_dir = base_dir
        self.full_path = os.path.join(self.base_dir, self.sample_path)

        self.tif_path = self.has_tif()
        self.h5_path = self.has_h5()

        self.st = None
        self.oio = None

        if self.h5_path and not force_metadata:
            try:
                self.oio = ObjectsIO.from_h5(self.h5_path,
                                             base_dir=self.base_dir,
                                             minimum_metadata_keys=self.__class__.MINIMUM_METADATA)
            except ValueError as e:
                log.error(e)

        if self.tif_path and not self.oio:
            self.st = StackIO(image_path=self.tif_path,
                              base_dir=self.base_dir,
                              json_discovery=json_discovery)

            self.h5_path = self.has_h5(force=True)
            self.oio = ObjectsIO(self.st.metadata,
                                 store_path=self.h5_path,
                                 base_dir=self.base_dir,
                                 minimum_metadata_keys=self.__class__.MINIMUM_METADATA)

        if self.oio is None:
            raise IOError("Sample path should be a valid Tiff file or HDF5 file.")

        self.load_oio()

    @property
    def verbose(self):
        return self._verbose

    @verbose.setter
    def verbose(self, value):
        log.disabled = not value
        self._verbose = value

    @property
    def full_tif_path(self):
        if self.tif_path:
            return os.path.join(self.base_dir, self.tif_path)
        else:
            return None

    @property
    def full_h5_path(self):
        if self.h5_path:
            return os.path.join(self.base_dir, self.h5_path)
        else:
            return None

    @property
    def full_xml_path(self):
        if self.xml_path:
            return os.path.join(self.base_dir, self.xml_path)
        else:
            return None

    @property
    def stored_data(self):
        if not hasattr(self, '_stored_data'):
            self._stored_data = []
        else:
            self._stored_data = list(set(self._stored_data))
        return self._stored_data

    def has_h5(self, force=False):
        """
        """
        extension = self.__class__.HDF5_EXTENSION

        full_path = check_extension(self.full_path,
                                    extension=extension,
                                    force=force)

        if full_path:
            return os.path.relpath(full_path, self.base_dir)
        else:
            return None

    def has_tif(self, force=False):
        """
        """
        extension = self.__class__.TIF_EXTENSION

        full_path = check_extension(self.full_path,
                                    extension=extension,
                                    force=force)

        if full_path:
            return os.path.relpath(full_path, self.base_dir)
        else:
            return None

    def has_xml(self, force=False, suffix=None):
        """
        """
        extension = self.__class__.XML_EXTENSION

        full_path = check_extension(self.full_path,
                                    extension=extension,
                                    suffix=suffix,
                                    force=force)

        if full_path:
            self.xml_path = os.path.relpath(full_path, self.base_dir)
            return self.xml_path
        else:
            return None

    def load_oio(self):
        """
        """


        for key in self.oio.keys():
            key = key.replace('/', '')
            self.stored_data.append(key)
            obj = self.oio[key]
{'fraction_non_alphanumeric': 0.07796575621687729, 'fraction_numerical': 0.0033632286995515697, 'mean_word_length': 1.527820710973725, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1540182', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2705, 'n_words': 573}