def coordinatorRandConvFactory(
        nbFilters=5,
        filterPolicy=(Const.FGEN_ZEROPERT,
                      {"minSize": 2, "maxSize": 32, "minVal": -1, "maxVal": 1,
                       "valGen": Const.RND_RU,
                       "normalization": FilterGenerator.NORMALISATION_MEANVAR}),
        poolings=[(3, 3, Const.POOLING_AGGREG_AVG)],
        extractor=(Const.FEATEXT_ALL, {}),
        nbSubwindows=10,
        subwindowMinSizeRatio=0.5, subwindowMaxSizeRatio=1.,
        subwindowTargetWidth=16, subwindowTargetHeight=16,
        subwindowInterpolation=SubWindowExtractor.INTERPOLATION_BILINEAR,
        includeOriginalImage=False,
        nbJobs=-1, verbosity=10, tempFolder=None,
        random=True):
    """
    Factory method to create :class:`RandConvCoordinator` tuned for RGB images

    Parameters
    ----------
    nbFilters : int >= 0 (default : 5)
        The number of filter

    filterPolicy : pair (policyType, parameters)
        policyType : one of Const.FGEN_*
            The type of filter generation policy to use
        parameters : dict
            The parameter dictionnary to forward to :func:`getFilterGenerator`

    poolings : iterable of triple (height, width, policy) (default :
    [(3, 3, Const.POOLING_AGGREG_AVG)])
        A list of parameters to instanciate the according :class:`Pooler`
        height : int > 0
            the height of the neighborhood window
        width : int > 0
            the width of the neighborhood window
        policy : int in {Const.POOLING_NONE, Const.POOLING_AGGREG_MIN,
    Const.POOLING_AGGREG_AVG, Const.POOLING_AGGREG_MAX,
    Const.POOLING_CONV_MIN, Const.POOLING_CONV_AVG, Const.POOLING_CONV_MAX}

    nbSubwindows : int >= 0 (default : 10)
        The number of subwindow to extract
    subwindowMinSizeRatio : float > 0 (default : 0.5)
        The minimum size of a subwindow expressed as the ratio of the size
        of the original image
    subwindowMaxSizeRatio : float : subwindowMinSizeRatio
    <= subwindowMaxSizeRatio <= 1 (default : 1.)
        The maximim size of a subwindow expressed as the ratio of the size
        of the original image
    subwindowTargetWidth : int > 0 (default : 16)
        The width of the subwindows after reinterpolation
    subwindowTargetHeight : int > 0 (default : 16)
        The height of the subwindows after reinterpolation
    subwindowInterpolation : int (default :
    SubWindowExtractor.INTERPOLATION_BILINEAR)
        The subwindow reinterpolation algorithm. For more information, see
        :class:`SubWindowExtractor`

    includeOriginalImage : boolean (default : False)
        Whether or not to include the original image in the subwindow
        extraction process

    nbJobs : int >0 or -1 (default : -1)
        The number of process to spawn for parallelizing the computation.
        If -1, the maximum number is selected. See also :mod:`Joblib`.
    verbosity : int >= 0 (default : 10)
        The verbosity level
    tempFolder : string (directory path) (default : None)
            The temporary folder used for memmap. If none, some default folder
            will be use (see the :class:`ParallelCoordinator`)

    random : bool (default : True)
        Whether to use randomness or use a predefined seed

    Return
    ------
        coordinator : :class:`Coordinator`
            The RandConvCoordinator corresponding to the
            set of parameters

    Notes
    -----
    - Filter generator
        Base instance of :class:`Finite3SameFilter` with a base instance of
        :class:`NumberGenerator` for the values and
        :class:`OddUniformGenerator` for the sizes
    - Filter size
        The filter are square (same width as height)
    - Convolver
        Base instance of :class:`RGBConvolver`
    - Subwindow random generator
        The subwindow random generator is a :class:`NumberGenerator` base
        instance (generate real nubers uniformely).
    - Feature extractor
        Base instance of :class:`ImageLinearizationExtractor`
    """
    #RANDOMNESS
    swngSeed = None
    if random is False:
        swngSeed = 0

    #CONVOLUTIONAL EXTRACTOR
    #Filter generator
    #Type/policy parameters, #filters, random
    filterPolicyType, filterPolicyParam = filterPolicy
    filterGenerator = getFilterGenerator(filterPolicyType, filterPolicyParam,
                                         nbFilters, random)

    #Convolver
    convolver = RGBConvolver()

    #Aggregator
    multiPooler = getMultiPoolers(poolings, subwindowTargetHeight,
                                  subwindowTargetWidth)

    #SubWindowExtractor
    swNumGenerator = NumberGenerator(seed=swngSeed)
    swExtractor = SubWindowExtractor(subwindowMinSizeRatio,
                                     subwindowMaxSizeRatio,
                                     subwindowTargetWidth,
                                     subwindowTargetHeight,
                                     subwindowInterpolation, swNumGenerator)

    multiSWExtractor = MultiSWExtractor(swExtractor, nbSubwindows, False)

    #ConvolutionalExtractor
    convolutionalExtractor = ConvolutionalExtractor(filterGenerator,
                                                    convolver,
                                                    multiSWExtractor,
                                                    multiPooler,
                                                    includeOriginalImage)
    #FEATURE EXTRACTOR
    featureExtractor = getFeatureExtractor(extractor[0], extractor[1])

    #LOGGER
    autoFlush = verbosity >= 40
    logger = ProgressLogger(StandardLogger(autoFlush=autoFlush,
                                           verbosity=verbosity))
    #COORDINATOR
    coordinator = RandConvCoordinator(convolutionalExtractor, featureExtractor,
                                      logger, verbosity)

    if nbJobs != 1:
        coordinator.parallelize(nbJobs, tempFolder)
    return coordinator
def customRandConvFactory(
        poolings=[(3, 3, Const.POOLING_AGGREG_AVG)],
        nbSubwindows=10,
        subwindowMinSizeRatio=0.5, subwindowMaxSizeRatio=1.,
        subwindowTargetWidth=16, subwindowTargetHeight=16,
        subwindowInterpolation=SubWindowExtractor.INTERPOLATION_BILINEAR,
        includeOriginalImage=False,
        nbJobs=-1, verbosity=10, tempFolder=None,
        random=True):
    """
    Factory method to create :class:`RandConvCoordinator` tuned for RGB images
    using predefined well-known filters

    Parameters
    ----------
    poolings : iterable of triple (height, width, policy) (default :
    [(3, 3, Const.POOLING_AGGREG_AVG)])
        A list of parameters to instanciate the according :class:`Pooler`
        height : int > 0
            the height of the neighborhood window
        width : int > 0
            the width of the neighborhood window
        policy : int in {Const.POOLING_NONE, Const.POOLING_AGGREG_MIN,
    Const.POOLING_AGGREG_AVG, Const.POOLING_AGGREG_MAX,
    Const.POOLING_CONV_MIN, Const.POOLING_CONV_AVG, Const.POOLING_CONV_MAX}

    nbSubwindows : int >= 0 (default : 10)
        The number of subwindow to extract
    subwindowMinSizeRatio : float > 0 (default : 0.5)
        The minimum size of a subwindow expressed as the ratio of the size
        of the original image
    subwindowMaxSizeRatio : float : subwindowMinSizeRatio
    <= subwindowMaxSizeRatio <= 1 (default : 1.)
        The maximim size of a subwindow expressed as the ratio of the size
        of the original image
    subwindowTargetWidth : int > 0 (default : 16)
        The width of the subwindows after reinterpolation
    subwindowTargetHeight : int > 0 (default : 16)
        The height of the subwindows after reinterpolation
    subwindowInterpolation : int (default :
    SubWindowExtractor.INTERPOLATION_BILINEAR)
        The subwindow reinterpolation algorithm. For more information, see
        :class:`SubWindowExtractor`

    includeOriginalImage : boolean (default : False)
        Whether or not to include the original image in the subwindow
        extraction process

    nbJobs : int >0 or -1 (default : -1)
        The number of process to spawn for parallelizing the computation.
        If -1, the maximum number is selected. See also :mod:`Joblib`.
    verbosity : int >= 0 (default : 10)
        The verbosity level
    tempFolder : string (directory path) (default : None)
            The temporary folder used for memmap. If none, some default folder
            will be use (see the :class:`ParallelCoordinator`)

    random : bool (default : True)
        Whether to use randomness or use a predefined seed

    Return
    ------
        coordinator : :class:`Coordinator`
            The RandConvCoordinator corresponding to the
            set of parameters

    Notes
    -----
    - Convolver
        Base instance of :class:`RGBConvolver`
    - Subwindow random generator
        The subwindow random generator is a :class:`NumberGenerator` base
        instance (generate real nubers uniformly).
    - Feature extractor
        Base instance of :class:`ImageLinearizationExtractor`
    """
    #RANDOMNESS
    swngSeed = 0
    if random is None:
        swngSeed = None

    #CONVOLUTIONAL EXTRACTOR
    filterGenerator = customFinite3sameFilter()

    #Convolver
    convolver = RGBConvolver()

    #Aggregator
    poolers = []
    for height, width, policy in poolings:
        if policy == Const.POOLING_NONE:
            poolers.append(IdentityPooler())
        elif policy == Const.POOLING_AGGREG_AVG:
            poolers.append(AverageAggregator(width, height,
                                             subwindowTargetWidth,
                                             subwindowTargetHeight))
        elif policy == Const.POOLING_AGGREG_MAX:
            poolers.append(MaximumAggregator(width, height,
                                             subwindowTargetWidth,
                                             subwindowTargetHeight))
        elif policy == Const.POOLING_AGGREG_MIN:
            poolers.append(MinimumAggregator(width, height,
                                             subwindowTargetWidth,
                                             subwindowTargetHeight))
        elif policy == Const.POOLING_CONV_MIN:
            poolers.append(ConvMinPooler(height, width))
        elif policy == Const.POOLING_CONV_AVG:
            poolers.append(ConvAvgPooler(height, width))
        elif policy == Const.POOLING_CONV_MAX:
            poolers.append(ConvMaxPooler(height, width))

    multiPooler = getMultiPoolers(subwindowTargetHeight, subwindowTargetWidth)

    #SubWindowExtractor
    swNumGenerator = NumberGenerator(seed=swngSeed)
    swExtractor = SubWindowExtractor(subwindowMinSizeRatio,
                                     subwindowMaxSizeRatio,
                                     subwindowTargetWidth,
                                     subwindowTargetHeight,
                                     subwindowInterpolation, swNumGenerator)

    multiSWExtractor = MultiSWExtractor(swExtractor, nbSubwindows, False)

    #ConvolutionalExtractor
    convolutionalExtractor = ConvolutionalExtractor(filterGenerator,
                                                    convolver,
                                                    multiSWExtractor,
                                                    multiPooler,
                                                    includeOriginalImage)
    #FEATURE EXTRACTOR
    featureExtractor = ImageLinearizationExtractor()

    #LOGGER
    autoFlush = verbosity >= 45
    logger = ProgressLogger(StandardLogger(autoFlush=autoFlush,
                                           verbosity=verbosity))
    #COORDINATOR
    coordinator = RandConvCoordinator(convolutionalExtractor, featureExtractor,
                                      logger, verbosity)

    if nbJobs != 1:
        coordinator.parallelize(nbJobs, tempFolder)
    return coordinator
def coordinatorRandConvFactory(
        nbFilters=5,
        filterGenConfiguration=(Const.GEN_REAL, (-1, 1)),
        filterMinSize=1, filterMaxSize=17,
        filterNormalisation=FilterGenerator.NORMALISATION_MEANVAR,
        poolings=[(3, 3, Const.POOLING_AGGREG_AVG)],
        nbSubwindows=10,
        subwindowMinSizeRatio=0.5, subwindowMaxSizeRatio=1.,
        subwindowTargetWidth=16, subwindowTargetHeight=16,
        subwindowInterpolation=SubWindowExtractor.INTERPOLATION_BILINEAR,
        includeOriginalImage=False,
        nbJobs=-1, verbosity=10, tempFolder=None,
        random=True):
    """
    Factory method to create :class:`RandConvCoordinator` tuned for RGB images

    Parameters
    ----------
    nbFilters : int >= 0 (default : 5)
        The number of filter
    filterGenConfiguration : pair (policy, parameters)
        The value filter generation parameters
        policy : int in {Const.GEN_REAL, Const.GEN_SET}
            The filter value generation policy.
            - Const.GEN_REAL (default):
                generate real number between two bounds. The parameters must
                specify theses bounds :
                parameters : pairs (filterMinVal, filterMaxVal)
                    filterMinVal : float (default : -1)
                        The minimum value of a filter component
                    filterMaxVal : float : filterMinVal <= filterMaxVal
                    (default : 1)
                        The maximum value of a filter component
            - Const.GEN_SET :
                generate number from a predefine set with a given probability
                parameters : iterable of pairs (number, probability)
                    number : number
                        an element of the set from which to draw
                    probability : float
                        the probability of the element being chosen at each
                        draw
        seed : int or None (default : None)
            if seed is int : initiate the random generator with this seed

    filterMinSize : int >= 0 : odd number (default : 1)
        The minimum size of a filter
    filterMaxSize : int >= 0 : odd number s.t.  filterMinSize <= filterMaxSize
    (default : 1)
        The maximum size of a filter
    filterNormalisation : int (default : FilterGenerator.NORMALISATION_MEANVAR)
        The filter normalisation policy. See also :class:`FilterGenerator`

    poolings : iterable of triple (height, width, policy) (default :
    [(3, 3, Const.POOLING_AGGREG_AVG)])
        A list of parameters to instanciate the according :class:`Pooler`
        height : int > 0
            the height of the neighborhood window
        width : int > 0
            the width of the neighborhood window
        policy : int in {Const.POOLING_NONE, Const.POOLING_AGGREG_MIN,
    Const.POOLING_AGGREG_AVG, Const.POOLING_AGGREG_MAX,
    Const.POOLING_CONV_MIN, Const.POOLING_CONV_AVG, Const.POOLING_CONV_MAX}

    nbSubwindows : int >= 0 (default : 10)
        The number of subwindow to extract
    subwindowMinSizeRatio : float > 0 (default : 0.5)
        The minimum size of a subwindow expressed as the ratio of the size
        of the original image
    subwindowMaxSizeRatio : float : subwindowMinSizeRatio
    <= subwindowMaxSizeRatio <= 1 (default : 1.)
        The maximim size of a subwindow expressed as the ratio of the size
        of the original image
    subwindowTargetWidth : int > 0 (default : 16)
        The width of the subwindows after reinterpolation
    subwindowTargetHeight : int > 0 (default : 16)
        The height of the subwindows after reinterpolation
    subwindowInterpolation : int (default :
    SubWindowExtractor.INTERPOLATION_BILINEAR)
        The subwindow reinterpolation algorithm. For more information, see
        :class:`SubWindowExtractor`

    includeOriginalImage : boolean (default : False)
        Whether or not to include the original image in the subwindow
        extraction process

    nbJobs : int >0 or -1 (default : -1)
        The number of process to spawn for parallelizing the computation.
        If -1, the maximum number is selected. See also :mod:`Joblib`.
    verbosity : int >= 0 (default : 10)
        The verbosity level
    tempFolder : string (directory path) (default : None)
            The temporary folder used for memmap. If none, some default folder
            will be use (see the :class:`ParallelCoordinator`)

    random : bool (default : True)
        Whether to use randomness or use a predefined seed

    Return
    ------
        coordinator : :class:`Coordinator`
            The RandConvCoordinator corresponding to the
            set of parameters

    Notes
    -----
    - Filter generator
        Base instance of :class:`Finite3SameFilter` with a base instance of
        :class:`NumberGenerator` for the values and
        :class:`OddUniformGenerator` for the sizes
    - Filter size
        The filter are square (same width as height)
    - Convolver
        Base instance of :class:`RGBConvolver`
    - Subwindow random generator
        The subwindow random generator is a :class:`NumberGenerator` base
        instance (generate real nubers uniformely).
    - Feature extractor
        Base instance of :class:`ImageLinearizationExtractor`
    """
    #RANDOMNESS
    swngSeed = 0
    filtValGenSeed = 1
    filtSizeGenSeed = 2
    if random is None:
        swngSeed = None
        filtValGenSeed = None
        filtSizeGenSeed = None

    #CONVOLUTIONAL EXTRACTOR
    #Filter generator
    #--Value
    filterGenPolicy, filterGenParam = filterGenConfiguration
    if filterGenPolicy == Const.GEN_REAL:
        filterMinVal, filterMaxVal = filterGenParam
        filterValGenerator = NumberGenerator(filterMinVal, filterMaxVal,
                                             seed=filtValGenSeed)
    if filterGenPolicy == Const.GEN_SET:
        filterValGenerator = CustomDiscreteNumberGenerator(filterGenParam,
                                                           seed=filtValGenSeed)
    #--Size
    filterSizeGenerator = OddUniformGenerator(filterMinSize, filterMaxSize,
                                              seed=filtSizeGenSeed)
    baseFilterGenerator = FilterGenerator(filterValGenerator,
                                          filterSizeGenerator,
                                          normalisation=filterNormalisation)
    filterGenerator = Finite3SameFilter(baseFilterGenerator, nbFilters)

    #Convolver
    convolver = RGBConvolver()

    #Aggregator
    poolers = []
    for height, width, policy in poolings:
        if policy == Const.POOLING_NONE:
            poolers.append(IdentityPooler())
        elif policy == Const.POOLING_AGGREG_AVG:
            poolers.append(AverageAggregator(width, height,
                                             subwindowTargetWidth,
                                             subwindowTargetHeight))
        elif policy == Const.POOLING_AGGREG_MAX:
            poolers.append(MaximumAggregator(width, height,
                                             subwindowTargetWidth,
                                             subwindowTargetHeight))
        elif policy == Const.POOLING_AGGREG_MIN:
            poolers.append(MinimumAggregator(width, height,
                                             subwindowTargetWidth,
                                             subwindowTargetHeight))
        elif policy == Const.POOLING_CONV_MIN:
            poolers.append(ConvMinPooler(height, width))
        elif policy == Const.POOLING_CONV_AVG:
            poolers.append(ConvAvgPooler(height, width))
        elif policy == Const.POOLING_CONV_MAX:
            poolers.append(ConvMaxPooler(height, width))

    multiPooler = MultiPooler(poolers)

    #SubWindowExtractor
    swNumGenerator = NumberGenerator(seed=swngSeed)
    swExtractor = SubWindowExtractor(subwindowMinSizeRatio,
                                     subwindowMaxSizeRatio,
                                     subwindowTargetWidth,
                                     subwindowTargetHeight,
                                     subwindowInterpolation, swNumGenerator)

    multiSWExtractor = MultiSWExtractor(swExtractor, nbSubwindows, False)

    #ConvolutionalExtractor
    convolutionalExtractor = ConvolutionalExtractor(filterGenerator,
                                                    convolver,
                                                    multiSWExtractor,
                                                    multiPooler,
                                                    includeOriginalImage)
    #FEATURE EXTRACTOR
    featureExtractor = ImageLinearizationExtractor()

    #LOGGER
    autoFlush = verbosity >= 45
    logger = ProgressLogger(StandardLogger(autoFlush=autoFlush,
                                           verbosity=verbosity))
    #COORDINATOR
    coordinator = RandConvCoordinator(convolutionalExtractor, featureExtractor,
                                      logger, verbosity)

    if nbJobs != 1:
        coordinator.parallelize(nbJobs, tempFolder)
    return coordinator