コード例 #1
0
    def __init__(self, dmax, nmax, emax, cmax, vmax, np_optimize):
        """PrimeGenerator __init__."""

        if not igraph:
            raise NotImplementedError('cannot use PrimeGenerator without igraph')
        
        self.ve = VariableElimination(np_optimize)

        # store parameters
        transfer(self, locals(), ['dmax', 'nmax', 'emax', 'cmax', 'vmax',])

        # setup N and e values to be used
        self.ns = list(range(1, self.nmax+1))
        self.emaxs = {n: min(self.emax, int(n/2*(n-1))) for n in self.ns}
        self.esbyn = {n: list(range(n-1, self.emaxs[n]+1)) for n in self.ns}

        # this could be more complicated than the same max for all (n,e)
        self.dmaxs = {(n,e): self.dmax for n in self.ns for e in self.esbyn[n]}

        # setup storage containers
        quantities = ['simple_graphs_d', 'edges_d', 'chis_d', 'einpaths_d',
                      'einstrs_d', 'weights_d']
        for q in quantities:
            setattr(self, q, {(n,e): [] for n in self.ns for e in self.esbyn[n]})

        # get simple connected graphs
        self._generate_simple()

        # get weighted connected graphs
        self._generate_weights()

        # flatten structures
        self._flatten_structures()
コード例 #2
0
    def __init__(self, measure, beta=1, kappa=1, normed=None, coords=None, check_input=True):
        r"""Processes inputs according to the measure choice and other options.

        **Arguments**

        - **measure** : _string_
            - The string specifying the energy and angular measures to use.
        - **beta** : _float_
            - The angular weighting exponent $\beta$. Must be positive.
        - **kappa** : {_float_, `'pf'`}
            - If a number, the energy weighting exponent $\kappa$. If `'pf'`,
            use $\kappa=v$ where $v$ is the valency of the vertex. `'pf'`
            cannot be used with measure `'hadr'`. Only IRC-safe for `kappa=1`.
        - **normed** : bool
            - Whether or not to use normalized energies/transverse momenta. A
            value of `None` defaults to `True`.
        - **coords** : {`'ptyphim'`, `'epxpypz'`, `None`}
            - Controls which coordinates are assumed for the input. If
            `'ptyphim'`, the fourth column (the masses) is optional and
            massless particles are assumed if it is not present. If `None`,
            coords with be `'ptyphim'` if using a hadronic measure and
            `'epxpypz'` if using the e+e- measure.
        - **check_input** : bool
            - Whether to check the type of input each time or assume the first
            input type.
        """

        # store parameters
        transfer(self, locals(), ['measure', 'kappa', 'normed', 'coords', 'check_input'])

        # check that coords is appropriate
        if self.coords not in [None, 'epxpypz', 'ptyphim']:
            raise ValueError('coords must be one of epxpypz, ptyphim, or None')

        # verify beta
        self.beta = float(beta)
        self.half_beta = self.beta/2
        assert self.beta > 0

        # measure function is not yet set
        self.need_meas_func = True

        # handle kappa options
        self._k_func = _kappa_func
        if self.kappa == PF_MARKER:

            # cannot subslice when kappa = pf
            self.subslicing = False

            # if normed was set to True, warn them about this
            if self.normed:
                warnings.warn('Normalization not supported when kappa=\'' + PF_MARKER + '\', '
                              'setting normed=False.')
                self.normed = False
            self._k_func = _pf_func

        # normed default to True if it's None at this point
        if self.normed is None:
            self.normed = True
コード例 #3
0
    def __init__(self, edges, weights=None, einstr=None, einpath=None, k=None):

        transfer(self, locals(), ['einstr', 'einpath', 'k'])

        self.process_edges(edges, weights)

        self.pow2d = 2**self.d
        self.ndk = (self.n, self.d, self.k)
コード例 #4
0
    def __init__(self,
                 measure,
                 beta=1,
                 kappa=1,
                 normed=True,
                 coords=None,
                 check_input=True):
        r"""Processes inputs according to the measure choice.

        **Arguments**

        - **measure** : _string_
            - The string specifying the energy and angular measures to use.
        - **beta** : _float_
            - The angular weighting exponent $\beta$. Must be positive.
        - **kappa** : {_float_, `'pf'`}
            - If a number, the energy weighting exponent $\kappa$. If `'pf'`, 
            use $\kappa=v$ where $v$ is the valency of the vertex. `'pf'` 
            cannot be used with measure `'hadr'`. Only IRC-safe for `kappa=1`.
        - **normed** : bool
            - Whether or not to use normalized energies.
        - **coords** : {`'ptyphim'`, `'epxpypz'`, `None`}
            - Controls which coordinates are assumed for the input. If `'ptyphim'`, the 
            fourth column (the masses) is optional and massless particles are assumed
            if it is not present. If `None`, coords with be `'ptyphim'` if using a 
            hadronic measure and `'epxpypz'` if using the e+e- measure.
        - **check_input** : bool
            - Whether to check the type of input each time or assume the first input type.
        """

        transfer(self, locals(),
                 ['measure', 'kappa', 'normed', 'coords', 'check_input'])

        self.beta = float(beta)
        self.half_beta = self.beta / 2
        assert self.beta > 0

        if self.coords not in [None, 'epxpypz', 'ptyphim']:
            raise ValueError('coords must be one of epxpypz, ptyphim, or None')

        self.need_meas_func = True
コード例 #5
0
ファイル: gen.py プロジェクト: zeusmail/EnergyFlow
    def __init__(self, dmax=None, nmax=None, emax=None, cmax=None, vmax=None, comp_dmaxs=None,
                      filename=None, gen_efms=True, np_optimize='greedy', verbose=False):
        r"""Doing a fresh generation of connected multigraphs (`filename=None`)
        requires that `igraph` be installed.

        **Arguments**

        - **dmax** : _int_
            - The maximum number of edges of the generated connected graphs.
        - **nmax** : _int_
            - The maximum number of vertices of the generated connected graphs.
        - **emax** : _int_
            - The maximum number of edges of the generated connected simple
            graphs.
        - **cmax** : _int_
            - The maximum VE complexity $\chi$ of the generated connected
            graphs.
        - **vmax** : _int_
            - The maximum valency of the generated connected graphs.
        - **comp_dmaxs** : {_dict_, _int_}
            - If an integer, the maximum number of edges of the generated
            disconnected graphs. If a dictionary, the keys are numbers of
            vertices and the values are the maximum number of edges of the
            generated disconnected graphs with that number of vertices.
        - **filename** : _str_
            - If `None`, do a complete generation from scratch. If set to a
            string, read in connected graphs from the file given, restrict them
            according to the various 'max' parameters, and do a fresh
            disconnected generation. The special value `filename='default'`
            means to read in graphs from the default file. This is useful when
            various disconnected graph parameters are to be varied since the
            generation of large simple graphs is the most computationlly
            intensive part.
        - **gen_efms** : _bool_
            - Controls whether EFM information is generated.
        - **np_optimize** : {`True`, `False`, `'greedy'`, `'optimal'`}
            - The `optimize` keyword of `numpy.einsum_path`.
        - **verbose** : _bool_
            - A flag to control printing.
        """

        start = time.time()

        # check for new generation
        if dmax is not None and filename is None:

            # set maxs
            self._set_maxs(dmax, nmax, emax, cmax, vmax)

            # set options
            self.np_optimize = np_optimize
            self.gen_efms = gen_efms

            # get prime generator instance
            self.pr_gen = PrimeGenerator(self.dmax, self.nmax, self.emax, self.cmax, self.vmax, 
                                         self.gen_efms, self.np_optimize, verbose, start)
            self.cols = self.pr_gen.cols
            self._set_col_inds()

            if verbose:
                print('Finished generating prime graphs in {:.3f}.'.format(time.time() - start))

            # store lists of important quantities
            transfer(self, self.pr_gen, self._prime_attrs())

        # if filename is set, read in file
        else:
            file = load_efp_file(filename)

            # setup cols and col inds
            self.cols = file['cols']
            self._set_col_inds()

            # get maxs from file and passed in options
            c_specs = np.asarray(file['c_specs'])
            for m in ['dmax','nmax','emax','cmax','vmax']:
                setattr(self, m, min(file[m], none2inf(locals()[m])))

            # select connected specs based on maxs
            mask = ((c_specs[:,self.d_ind] <= self.dmax) & 
                    (c_specs[:,self.n_ind] <= self.nmax) & 
                    (c_specs[:,self.e_ind] <= self.emax) & 
                    (c_specs[:,self.c_ind] <= self.cmax) & 
                    (c_specs[:,self.v_ind] <= self.vmax))

            # set ve options
            self.np_optimize = file['np_optimize']

            # get lists of important quantities
            self.gen_efms = file['gen_efms'] and gen_efms
            for attr in (self._prime_attrs()):
                setattr(self, attr, [x for x,m in zip(file[attr],mask) if m])
            self.c_specs = c_specs[mask]


        # setup generator of disconnected graphs
        self._set_comp_dmaxs(comp_dmaxs)
        self.comp_gen = CompositeGenerator(self.c_specs, self.cols, self.comp_dmaxs)

        if verbose:
            print('Finished generating composite graphs in {:.3f}.'.format(time.time() - start))

        # get results and store
        transfer(self, self.comp_gen, self._comp_attrs())
コード例 #6
0
ファイル: measure.py プロジェクト: diliamariap/EnergyFlow-1
    def __init__(self,
                 measure,
                 beta=1,
                 kappa=1,
                 normed=True,
                 coords=None,
                 check_input=True,
                 kappa_normed_behavior='new'):
        r"""Processes inputs according to the measure choice and other options.

        **Arguments**

        - **measure** : _string_
            - The string specifying the energy and angular measures to use.
        - **beta** : _float_
            - The angular weighting exponent $\beta$. Must be positive.
        - **kappa** : {_float_, `'pf'`}
            - If a number, the energy weighting exponent $\kappa$. If `'pf'`,
            use $\kappa=v$ where $v$ is the valency of the vertex. `'pf'`
            cannot be used with measure `'hadr'`. Only IRC-safe for `kappa=1`.
        - **normed** : bool
            - Whether or not to use normalized energies/transverse momenta.
        - **coords** : {`'ptyphim'`, `'epxpypz'`, `None`}
            - Controls which coordinates are assumed for the input. If
            `'ptyphim'`, the fourth column (the masses) is optional and
            massless particles are assumed if it is not present. If `None`,
            coords with be `'ptyphim'` if using a hadronic measure and
            `'epxpypz'` if using the e+e- measure.
        - **check_input** : bool
            - Whether to check the type of input each time or assume the first
            input type.
        - **kappa_normed_behavior** : {`'new'`, `'orig'`}
            - Determines how `'kappa'`!=1 interacts with normalization of the
            energies. A value of `'new'` will ensure that `z` is truly the
            energy fraction of a particle, so that $z_i=E_i^\kappa/\left(
            \sum_{i=1}^ME_i\right)^\kappa$. A value of `'orig'` will keep the
            behavior prior to version `1.1.0`, which used $z_i=E_i^\kappa/
            \sum_{i=1}^M E_i^\kappa$.
        """

        # store parameters
        transfer(self, locals(), [
            'measure', 'kappa', 'normed', 'coords', 'check_input',
            'kappa_normed_behavior'
        ])

        # check that options are appropriate
        if self.coords not in {None, 'epxpypz', 'ptyphim'}:
            raise ValueError(
                "coords must be one of 'epxpypz', 'ptyphim', or None")
        if self.kappa_normed_behavior not in {'new', 'orig'}:
            raise ValueError("kappa_normed_behavior must be 'new' or 'orig'")

        # verify beta
        self.beta = float(beta)
        self.half_beta = self.beta / 2
        assert self.beta > 0

        # measure function is not yet set
        self.need_meas_func = True

        # handle normed and kappa options
        self._z_func, self._phat_func = self._z_unnormed_func, _phat_func
        if self.kappa == PF_MARKER:
            self._phat_func = _pf_phat_func

            # cannot subslice when kappa = pf
            self.subslicing = False

            # if normed was set to True, warn them about this
            if self.normed:
                raise ValueError("Normalization not supported when kappa='pf'")

            self._z_func = self._pf_z_func

        # we're norming the correlators
        elif self.normed:
            if self.kappa_normed_behavior == 'new':
                self._z_func = self._z_normed_new_func
            else:
                self._z_func = self._z_normed_orig_func
コード例 #7
0
    def process_hps(self):
        """See [`ArchBase`](#archbase) for how to pass in hyperparameters.

        **Required CNN Hyperparameters**

        - **input_shape** : {_tuple_, _list_} of _int_
            - The shape of a single jet image. Assuming that `data_format`
            is set to `channels_first`, this is `(nb_chan,npix,npix)`.
        - **filter_sizes** : {_tuple_, _list_} of _int_
            - The size of the filters, which are taken to be square, in each 
            convolutional layer of the network. The length of the list will be
            the number of convolutional layers in the network.
        - **num_filters** : {_tuple_, _list_} of _int_
            - The number of filters in each convolutional layer. The length of 
            `num_filters` must match that of `filter_sizes`.

        **Default CNN Hyperparameters**

        - **dense_sizes**=`None` : {_tuple_, _list_} of _int_
            - The sizes of the dense layer backend. A value of `None` is 
            equivalent to an empty list.
        - **pool_sizes**=`None` : {_tuple_, _list_} of _int_
            - Size of maxpooling filter, taken to be a square. A value of 
            `None` will not use maxpooling.
        - **conv_acts**=`'relu'` : {_tuple_, _list_} of _str_
            - Activation function(s) for the conv layers. A single string 
            will apply the same activation to all conv layers. See the
            [Keras activations docs](https://keras.io/activations/) for 
            more detail.
        - **dense_acts**=`'relu'` : {_tuple_, _list_} of _str_
            - Activation functions(s) for the dense layers. A single string 
            will apply the same activation to all dense layers.
        - **conv_k_inits**=`'he_uniform'` : {_tuple_, _list_} of _str_
            - Kernel initializers for the convolutional layers. A single
            string will apply the same initializer to all layers. See the
            [Keras initializer docs](https://keras.io/initializers/) for 
            more detail.
        - **dense_k_inits**=`'he_uniform'` : {_tuple_, _list_} of _str_
            - Kernel initializers for the dense layers. A single string will 
            apply the same initializer to all layers.
        - **conv_dropouts**=`0` : {_tuple_, _list_} of _float_
            - Dropout rates for the convolutional layers. A single float will
            apply the same dropout rate to all conv layers. See the [Keras
            Dropout layer](https://keras.io/layers/core/#dropout) for more 
            detail.
        - **num_spatial2d_dropout**=`0` : _int_
            - The number of convolutional layers, starting from the beginning
            of the model, for which to apply [SpatialDropout2D](https://keras
            .io/layers/core/#spatialdropout2d) instead of Dropout.
        - **dense_dropouts**=`0` : {_tuple_, _list_} of _float_
            - Dropout rates for the dense layers. A single float will apply 
            the same dropout rate to all dense layers.
        - **paddings**=`'valid'` : {_tuple_, _list_} of _str_
            - Controls how the filters are convoled with the inputs. See
            the [Keras Conv2D layer](https://keras.io/layers/convolutional/#conv2d) 
            for more detail.
        - **data_format**=`'channels_first'` : {`'channels_first'`, `'channels_last'`}
            - Sets which axis is expected to contain the different channels.
        """

        # process generic NN hps
        super(CNN, self).process_hps()

        # required hyperparameters
        transfer(self, self.hps,
                 ['input_shape', 'filter_sizes', 'num_filters'])

        # required checks
        m = 'filter_sizes and num_filters must be the same length'
        assert len(self.filter_sizes) == len(self.num_filters), m

        # optional (but likely provided) hyperparameters with defaults
        self.pool_sizes = iter_or_rep(self.hps.get('pool_sizes', None))
        self.dense_sizes = self.hps.get('dense_sizes', None)

        # activations
        self.conv_acts = iter_or_rep(self.hps.get('conv_acts', 'relu'))
        self.dense_acts = iter_or_rep(self.hps.get('dense_acts', 'relu'))

        # initializations
        self.conv_k_inits = iter_or_rep(
            self.hps.get('conv_k_inits', 'he_uniform'))
        self.dense_k_inits = iter_or_rep(
            self.hps.get('dense_k_inits', 'he_uniform'))

        # regularization
        self.conv_dropouts = iter_or_rep(self.hps.get('conv_dropouts', 0))
        self.num_spatial2d_dropout = self.hps.get('num_spatial2d_dropout', 0)
        self.dense_dropouts = iter_or_rep(self.hps.get('dense_dropouts', 0))

        # padding
        self.paddings = iter_or_rep(self.hps.get('padding', 'valid'))
        self.data_format = self.hps.get('data_format', 'channels_first')