Ejemplo n.º 1
0
    def _parse_parameter_file(self):
        super()._parse_parameter_file()

        end = min(self.dimensionality + 1, 4)

        self.geometry = "spectral_cube"

        log_str = "Detected these axes: " + "%s " * len(self.ctypes)
        mylog.info(log_str, *self.ctypes)

        self.lat_axis = np.zeros((end - 1), dtype="bool")
        for p in lat_prefixes:
            self.lat_axis += np_char.startswith(self.ctypes, p)
        self.lat_axis = np.where(self.lat_axis)[0][0]
        self.lat_name = self.ctypes[self.lat_axis].split("-")[0].lower()

        self.lon_axis = np.zeros((end - 1), dtype="bool")
        for p in lon_prefixes:
            self.lon_axis += np_char.startswith(self.ctypes, p)
        self.lon_axis = np.where(self.lon_axis)[0][0]
        self.lon_name = self.ctypes[self.lon_axis].split("-")[0].lower()

        if self.lat_axis == self.lon_axis and self.lat_name == self.lon_name:
            self.lat_axis = 1
            self.lon_axis = 0
            self.lat_name = "Y"
            self.lon_name = "X"

        self.spec_axis = 2
        self.spec_name = "z"
        self.spec_unit = ""
Ejemplo n.º 2
0
    def function(famille, period):
        period = period.start.period(u'year').offset('first-of')

        depcom = famille('depcom', period)

        return period, np.logical_or(startswith(depcom, '97'),
                                     startswith(depcom, '98'))
Ejemplo n.º 3
0
    def function(self, simulation, period):
        period = period
        depcom_holder = simulation.compute('depcom', period)

        depcom = self.cast_from_entity_to_roles(depcom_holder)
        depcom = self.filter_role(depcom, role = CHEF)
        return period, startswith(depcom, '973')
Ejemplo n.º 4
0
    def formula(menage, period):
        depcom = menage('depcom', period.first_month)

        departements_idf = [b'75', b'77', b'78', b'91', b'92', b'93', b'94', b'95']
        in_idf = sum([startswith(depcom, departement_idf) for departement_idf in departements_idf])

        rfr_declarants_principaux_du_menage = menage.members.has_role(FoyerFiscal.DECLARANT_PRINCIPAL) * menage.members.foyer_fiscal('rfr', period.n_2)
        rfr = menage.sum(rfr_declarants_principaux_du_menage)

        nb_members = menage.nb_persons()

        bareme_idf = select(
            [nb_members == 1, nb_members == 2, nb_members == 3, nb_members == 4, nb_members >= 5],
            [select([rfr <= 19875, rfr <= 24194], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 29171, rfr <= 35510], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 35032, rfr <= 42648], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 40905, rfr <= 49799], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 46798 + ((nb_members - 5) * 5882), rfr <= 56970 + ((nb_members - 5) * 7162)], [2, 1], 0)])

        bareme_out = select(
            [nb_members == 1, nb_members == 2, nb_members == 3, nb_members == 4, nb_members >= 5],
            [select([rfr <= 14360, rfr <= 18409], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 21001, rfr <= 26923], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 25257, rfr <= 32377], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 29506, rfr <= 37826], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 33774 + ((nb_members - 5) * 4257), rfr <= 43297 + ((nb_members - 5) * 5454)], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier)])

        return where(in_idf, bareme_idf, bareme_out)
Ejemplo n.º 5
0
def mask_gaps(seqs, thresh=1.0):
    """Remove columns in an alignment when a certain fraction of sites
       are gaps
    Parameters
    ----------
    seqs : dict
        alignment
    thresh : float between 0 and 1, option
        fraction of gaps needed for column to be removed
    Returns
    -------
    algn : dict
        alignment with gapped columns removed
    """
    import numpy
    from numpy.core.defchararray import startswith

    nams = list(seqs)
    nseq = len(nams)

    data = []
    for n in nams:
        data.append(list(seqs[n]))
    data = numpy.array(data)

    perc = numpy.sum(startswith(data, '-'), axis=0) / float(nseq)
    cols = numpy.where(perc < thresh)[0]
    data = data[:, cols]

    algn = {}
    for i, n in enumerate(nams):
        algn[n] = ''.join(list(data[i, :]))

    return algn
    def formula(menage, period):
        depcom = menage('depcom', period.first_month)

        departements_idf = [b'75', b'77', b'78', b'91', b'92', b'93', b'94', b'95']
        in_idf = sum([startswith(depcom, departement_idf) for departement_idf in departements_idf])

        rfr_declarants_principaux_du_menage = menage.members.has_role(FoyerFiscal.DECLARANT_PRINCIPAL) * menage.members.foyer_fiscal('rfr', period.n_2)
        rfr = menage.sum(rfr_declarants_principaux_du_menage)

        nb_members = menage.nb_persons()

        bareme_idf = select(
            [nb_members == 1, nb_members == 2, nb_members == 3, nb_members == 4, nb_members >= 5],
            [select([rfr <= 19875, rfr <= 24194], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 29171, rfr <= 35510], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 35032, rfr <= 42648], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 40905, rfr <= 49799], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 46798 + ((nb_members - 5) * 5882), rfr <= 56970 + ((nb_members - 5) * 7162)], [2, 1], 0)])

        bareme_out = select(
            [nb_members == 1, nb_members == 2, nb_members == 3, nb_members == 4, nb_members >= 5],
            [select([rfr <= 14360, rfr <= 18409], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 21001, rfr <= 26923], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 25257, rfr <= 32377], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 29506, rfr <= 37826], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier),
             select([rfr <= 33774 + ((nb_members - 5) * 4257), rfr <= 43297 + ((nb_members - 5) * 5454)], [TypesEligibiliteANAH.tres_modeste, TypesEligibiliteANAH.modestes], TypesEligibiliteANAH.a_verifier)])

        return where(in_idf, bareme_idf, bareme_out)
    def formula(individu, period, parameters):
        params = parameters(
            period).regions.occitanie.msa_midi_pyrenees_sud.aide_permis

        montant = params.montant

        depcom = individu.menage('depcom', period)
        eligibilite_geographique = sum([
            startswith(depcom, code_departement)
            for code_departement in code_departements
        ])

        allocataire_msa = individu(
            'regime_securite_sociale',
            period) == RegimeSecuriteSociale.regime_agricole

        age = individu('age', period)
        eligibilite_age = (age >= params.age.minimum) * (age <=
                                                         params.age.maximum)

        quotient_familial = individu.foyer_fiscal(
            'rfr', period.this_year) / 12 / individu.foyer_fiscal(
                'nbptr', period.this_year)
        eligibilite_quotient_familial = quotient_familial <= params.plafond_quotient_familial

        return allocataire_msa * eligibilite_geographique * eligibilite_age * eligibilite_quotient_familial * montant
Ejemplo n.º 8
0
    def change_template(self, ch: Chromosome, cluster_id: int, template_index: int):
        # get the template at position index
        template = ch.templates[cluster_id][template_index]

        star_indexes = list()
        non_star_indexes = list()
        for index in range(0, template.get_length()):
            if template.token[index] == '*':
                star_indexes.append(index)
            elif not (startswith(template.token[index], "[") or template.token[index] == '#spec#' or
                            re.match("\B\W\Z", template.token[index])):
                non_star_indexes.append(index)

        if random.random() <= 0.50:
            if len(star_indexes) > 0:
                index = random.choice(star_indexes)
                message_index = random.choice(template.matched_lines)
                message = self.chGenerator.messages[cluster_id][message_index]
                template.token[index] = message.words[index]
        else:
            if len(non_star_indexes) > 0:
                index = random.choice(non_star_indexes)
                clone = Template(template.token[:])
                clone.token[index] = '*'
                compute_matched_lines(self.chGenerator.messages, clone)
                if len(clone.matched_lines) > len(template.matched_lines):
                    template.token[index] = '*'

        template.set_changed(True)
        compute_matched_lines(self.chGenerator.messages, template)
Ejemplo n.º 9
0
    def generate_template_from_line(self, cluster_id: int, rand_value: int):
        """ Generates a Template from a cluster
        """
        template = self.messages[cluster_id][rand_value].words[:]

        if len(template) > 1:
            contains_star = False
            for word in template:
                if word == "#spec#":
                    contains_star = True
                    break

            if not contains_star:
                modifiable_indexes = list()
                for index in range(0, len(template)):
                    if not (startswith(template[index], "[")
                            or re.match("\B\W\Z", template[index])):
                        modifiable_indexes.append(index)

                if len(modifiable_indexes) > 0:
                    index = random.choice(modifiable_indexes)
                    template[index] = '*'

        t = Template(template)
        compute_matched_lines(self.messages, t)
        return t
Ejemplo n.º 10
0
    def function(self, simulation, period):
        period = period
        depcom_holder = simulation.compute('depcom', period)

        depcom = self.cast_from_entity_to_roles(depcom_holder)
        depcom = self.filter_role(depcom, role=CHEF)
        return period, startswith(depcom, '976')
Ejemplo n.º 11
0
    def formula_2021_07_21(individu, period, parameters):
        age = individu('age', period)
        criteres_age = parameters(period).regions.hauts_de_france.aide_permis.age
        eligibilite_age = (criteres_age.minimum <= age) * (age <= criteres_age.maximum)

        depcom = individu.menage('depcom', period)
        eligibilite_geographique = sum([startswith(depcom, code_departement) for code_departement in code_departements])

        plafond_ressources = parameters(period).regions.hauts_de_france.aide_permis.plafond_ressources
        rfr = individu.foyer_fiscal('rfr', period.n_2)
        nbptr = individu.foyer_fiscal('nbptr', period.n_2)

        activite_eligible = (individu('activite', period) == TypesActivite.chomeur) + (individu('activite', period) == TypesActivite.inactif)
        stagiaire = individu('stagiaire', period)
        apprenti = individu('apprenti', period)
        eligibilite_situation = activite_eligible + stagiaire + apprenti

        statut_marital = individu('statut_marital', period)
        eligibilite_couple = (
            ((statut_marital == TypesStatutMarital.marie) + (statut_marital == TypesStatutMarital.pacse)) *
            plafond_ressources.base_personne_couple >= rfr
        )
        eligibilite_autonome = (
            (statut_marital == TypesStatutMarital.celibataire) *
            plafond_ressources.base_personne_autonome >= max_(0, rfr / nbptr)
        )

        return eligibilite_age * eligibilite_geographique * eligibilite_situation * (eligibilite_autonome + eligibilite_couple)
Ejemplo n.º 12
0
    def _parse_parameter_file(self):
        super(SpectralCubeFITSDataset, self)._parse_parameter_file()

        self.geometry = "spectral_cube"

        end = min(self.dimensionality+1,4)

        self.spec_axis = np.zeros(end-1, dtype="bool")
        for p in spec_names.keys():
            self.spec_axis += np_char.startswith(self.ctypes, p)
        self.spec_axis = np.where(self.spec_axis)[0][0]
        self.spec_name = spec_names[self.ctypes[self.spec_axis].split("-")[0][0]]

        # Extract a subimage from a WCS object
        self.wcs_2d = self.wcs.sub(["longitude", "latitude"])

        self._p0 = self.wcs.wcs.crpix[self.spec_axis]
        self._dz = self.wcs.wcs.cdelt[self.spec_axis]
        self._z0 = self.wcs.wcs.crval[self.spec_axis]
        self.spec_unit = str(self.wcs.wcs.cunit[self.spec_axis])

        if self.spectral_factor == "auto":
            self.spectral_factor = float(max(self.domain_dimensions[[self.lon_axis,
                                                                     self.lat_axis]]))
            self.spectral_factor /= self.domain_dimensions[self.spec_axis]
            mylog.info("Setting the spectral factor to %f" % (self.spectral_factor))
        Dz = self.domain_right_edge[self.spec_axis]-self.domain_left_edge[self.spec_axis]
        dre = self.domain_right_edge
        dre[self.spec_axis] = (self.domain_left_edge[self.spec_axis] +
                               self.spectral_factor*Dz)
        self.domain_right_edge = dre
        self._dz /= self.spectral_factor
        self._p0 = (self._p0-0.5)*self.spectral_factor + 0.5
def mask_gaps(seqs, thresh=1.0):
    """
    Removes columns in an alignment when a certain fraction
    of sites are gaps. Default is 100% gaps or 1.
    Parameters
    ----------
    seqs : python dictionary
    Returns
    -------
    algn : python dictionary
    """
    import numpy
    from numpy.core.defchararray import startswith

    nams = seqs.keys()
    nseq = len(nams)

    data = []
    for n in nams:
        data.append(list(seqs[n]))
    data = numpy.array(data)

    perc = numpy.sum(startswith(data, '-'), axis=0) / float(nseq)
    cols = numpy.where(perc < thresh)[0]
    data = data[:, cols]

    algn = {}
    for i, n in enumerate(nams):
        algn[n] = ''.join(list(data[i, :]))

    return algn
Ejemplo n.º 14
0
    def formula(individu, period):
        age = individu('age', period)
        depcom = individu.menage('depcom', period)

        eligibilite_geographique = startswith(depcom, b'85')

        eligible = eligibilite_geographique * (age >= 16) * (age <= 25)
        return 800 * eligible
Ejemplo n.º 15
0
 def formula(menage, period, parameters):
     depcom = menage('depcom', period)
     return sum(
         [
             startswith(depcom, str.encode(departement_idf))
             for departement_idf in parameters(
                 period).geopolitique.departements_idf
         ]
     )  # TOOPTIMIZE: string encoding into bytes array should be done at load time
Ejemplo n.º 16
0
    def formula(menage, period):
        code_departements = [
            b'16', b'17', b'19', b'23', b'24', b'33', b'40', b'47', b'64',
            b'79', b'86', b'87'
        ]

        depcom = menage('depcom', period)
        return sum([startswith(depcom, code)
                    for code in code_departements]) > 0
    def formula(individu, period):
        age = individu('age', period)
        activite = individu('activite', period)
        depcom = individu.menage('depcom', period)

        eligibilite_geographique = sum(
            [startswith(depcom, code) for code in code_departements]) > 0

        etudiant_eligible = (activite == TypesActivite.etudiant)
        autre_eligible = (age >= 15) * (age <= 19) * (activite !=
                                                      TypesActivite.etudiant)

        return eligibilite_geographique * (etudiant_eligible + autre_eligible)
Ejemplo n.º 18
0
    def formula(menage, period):
        depcom = menage('depcom', period)

        in_paris_communes_limitrophes = sum([
            depcom == commune_proche_paris
            for commune_proche_paris in paris_communes_limitrophes
        ])
        in_idf = sum([
            startswith(depcom, departement) for departement in departements_idf
        ])

        return select([in_paris_communes_limitrophes, in_idf], [
            ZoneLogementSocial.paris_communes_limitrophes,
            ZoneLogementSocial.ile_de_france,
        ],
                      default=ZoneLogementSocial.autres_regions)
Ejemplo n.º 19
0
    def formula(individu, period, parameters):
        params = parameters(period).regions.bretagne.msa_armorique.aide_permis

        montant = params.montant

        depcom = individu.menage('depcom', period)
        eligibilite_geographique = sum([startswith(depcom, code_departement) for code_departement in code_departements])

        allocataire_msa = individu('regime_securite_sociale', period) == RegimeSecuriteSociale.regime_agricole

        age = individu('age', period)
        eligibilite_age = age <= params.age.maximum

        smic = parameters(period).marche_travail.salaire_minimum.smic
        smic_brut_mensuel = smic.smic_b_horaire * smic.nb_heures_travail_mensuel
        eligibilite_salaire = individu('salaire_de_base', period.last_3_months, options = [ADD]) / 3 / smic_brut_mensuel * 100 <= params.pourcentage_ressources_maximum_jeune

        quotient_familial = individu.foyer_fiscal('rfr', period.this_year) / 12 / individu.foyer_fiscal('nbptr', period.this_year)
        enfant_a_charge = individu('enfant_a_charge', period.this_year)
        eligibilite_famillle = (quotient_familial <= params.plafond_quotient_familial) * enfant_a_charge

        return eligibilite_geographique * allocataire_msa * eligibilite_age * (eligibilite_salaire + eligibilite_famillle) * montant
    def formula(individu, period, parameters):
        params = parameters(
            period).regions.normandie.msa_haute_normandie.aide_permis
        ars_params = parameters(
            period
        ).prestations_sociales.prestations_familiales.education_presence_parentale.ars.ars_plaf
        smic = parameters(period).marche_travail.salaire_minimum.smic

        montant = params.montant

        allocataire_msa = individu(
            'regime_securite_sociale',
            period) == RegimeSecuriteSociale.regime_agricole
        enfant_a_charge = individu('enfant_a_charge', period.this_year)
        alternant = individu('alternant', period)

        depcom = individu.menage('depcom', period)
        eligibilite_geographique = sum([
            startswith(depcom, code_departement)
            for code_departement in code_departements
        ])

        age = individu('age', period)
        eligibilite_age = (age >= params.age.minimum) * (age <=
                                                         params.age.maximum)

        af_nbenf = individu.famille('af_nbenf', period)
        plafond_ressources = ars_params.plafond_ressources * (
            1 + af_nbenf * ars_params.majoration_par_enf_supp)
        eligibilite_plafond_ressources = individu.foyer_fiscal(
            'rfr', period.this_year) <= plafond_ressources
        smic_brut_mensuel = smic.smic_b_horaire * smic.nb_heures_travail_mensuel
        eligibilite_salaire = individu('salaire_de_base',
                                       period) <= smic_brut_mensuel

        return (
            allocataire_msa + (enfant_a_charge * alternant)
        ) * eligibilite_geographique * eligibilite_age * eligibilite_plafond_ressources * eligibilite_salaire * montant
Ejemplo n.º 21
0
 def formula(self, simulation, period):
     period = period.start.period('year').offset('first-of')
     city_code = simulation.calculate('city_code', period)
     return np.logical_or(startswith(city_code, '97'),
                          startswith(city_code, '98'))
Ejemplo n.º 22
0
def find_axes(axis_names, prefixes):
    x = 0
    for p in prefixes:
        y = np_char.startswith(axis_names, p)
        x += np.any(y)
    return x
Ejemplo n.º 23
0
 def formula(menage, period, parameters):
     depcom = menage('depcom', period)
     return startswith(depcom, b'971')
Ejemplo n.º 24
0
 def formula(menage, period):
     return startswith(menage('depcom', period), b'22')
Ejemplo n.º 25
0
 def formula(menage, period, parameters):
     depcom = menage('depcom', period)
     return startswith(depcom, '971')
 def function(self, depcom):
     return np.logical_or(startswith(depcom, '97'), startswith(depcom, '98'))
Ejemplo n.º 27
0
    def function(self, simulation, period):
        period = period.start.period(u'year').offset('first-of')
        depcom = simulation.calculate('depcom', period)

        return period, np.logical_or(startswith(depcom, '97'), startswith(depcom, '98'))
Ejemplo n.º 28
0
 def function(self, simulation, period):
     depcom = simulation.calculate('depcom', period)
     return period, startswith(depcom, '973')
Ejemplo n.º 29
0
    def function(famille, period):
        depcom = famille('depcom', period)

        return np.logical_or(startswith(depcom, '97'),
                             startswith(depcom, '98'))
Ejemplo n.º 30
0
 def formula(menage, period):
     depcom = menage('depcom', period)
     return startswith(depcom, b'67')
Ejemplo n.º 31
0
    def run(self) -> None:
        # Load metadata
        metadata: np.ndarray = None
        meta_attrs: np.ndarray = None
        metadata_file = os.path.join(am.paths().samples, "metadata",
                                     "metadata.xlsx")
        if os.path.exists(metadata_file):
            temp = pd.read_excel(metadata_file)
            meta_attrs = temp.columns.values
            metadata = temp.values

        with self.output().temporary_path() as out_file:
            attrs = {"title": self.tissue}
            valid_cells = []
            sample_files = [s.fn for s in self.input()]
            for sample in sorted(sample_files):
                # Connect and perform file-specific cell validation
                with loompy.connect(sample) as ds:
                    logging.info("Marking invalid cells")
                    (mols, genes) = ds.map([np.sum, np.count_nonzero], axis=1)
                    valid_cells.append(
                        np.logical_and(mols >= 600,
                                       (mols / genes) >= 1.2).astype('int'))
                    ds.ca.Total = mols
                    ds.ca.NGenes = genes

                    logging.info("Computing mito/ribo ratio for " + sample)
                    mito = np.where(npstr.startswith(ds.ra.Gene, "mt-"))[0]
                    ribo = np.where(npstr.startswith(ds.ra.Gene, "Rpl"))[0]
                    ribo = np.union1d(
                        ribo,
                        np.where(npstr.startswith(ds.ra.Gene, "Rps"))[0])
                    if len(ribo) > 0 and len(mito) > 0:
                        mitox = ds[mito, :]
                        ribox = ds[ribo, :]
                        ratio = (mitox.sum(axis=0) + 1) / (ribox.sum(axis=0) +
                                                           1)
                        ds.ca.MitoRiboRatio = ratio

            logging.info("Creating combined loom file")
            loompy.combine(sample_files,
                           out_file,
                           key="Accession",
                           file_attrs=attrs)

            # Validating genes
            logging.info("Marking invalid genes")
            with loompy.connect(out_file) as ds:
                vgpath = os.path.join(am.paths().build, "genes.txt")
                if os.path.exists(vgpath):
                    valids = np.zeros(ds.shape[0])
                    with open(vgpath, "r") as f:
                        line = f.readline()
                        items = line[:-1].split("\t")
                        valids[np.where(ds.Accession == items[0])] = int(
                            items[1])
                    ds.set_attr("_Valids", valids, axis=0)
                else:
                    nnz = ds.map([np.count_nonzero], axis=0)[0]
                    valid_genes = np.logical_and(nnz > 10,
                                                 nnz < ds.shape[1] * 0.6)
                    ds.set_attr("_Valid", valid_genes, axis=0)

                logging.info("Marking invalid cells")
                ds.set_attr("_Valid", np.concatenate(valid_cells), axis=1)
                n_valid = np.sum(ds.col_attrs["_Valid"] == 1)
                n_total = ds.shape[1]
                logging.info("%d of %d cells were valid", n_valid, n_total)

                classifier_path = os.path.join(am.paths().samples,
                                               "classified",
                                               "classifier.pickle")
                if os.path.exists(classifier_path):
                    logging.info("Classifying cells by major class")
                    with open(classifier_path, "rb") as f:
                        clf = pickle.load(f)  # type: cg.Classifier
                    np.random.seed(13)
                    (classes, probs,
                     class_labels) = clf.predict(ds, probability=True)

                    mapping = {
                        "Astrocyte": "Astrocytes",
                        "Astrocyte,Cycling": "Astrocytes",
                        "Astrocyte,Immune": None,
                        "Astrocyte,Neurons": None,
                        "Astrocyte,Oligos": None,
                        "Astrocyte,Vascular": None,
                        "Bergmann-glia": "Astrocytes",
                        "Blood": "Blood",
                        "Blood,Cycling": "Blood",
                        "Blood,Vascular": None,
                        "Enteric-glia": "PeripheralGlia",
                        "Enteric-glia,Cycling": "PeripheralGlia",
                        "Ependymal": "Ependymal",
                        "Ex-Astrocyte": None,
                        "Ex-Blood": None,
                        "Ex-Immune": None,
                        "Ex-Neurons": None,
                        "Ex-Oligos": None,
                        "Ex-Vascular": None,
                        "Immune": "Immune",
                        "Immune,Neurons": None,
                        "Immune,Oligos": None,
                        "Neurons": "Neurons",
                        "Neurons,Cycling": "Neurons",
                        "Neurons,Immune": None,
                        "Neurons,Oligos": None,
                        "Neurons,Satellite-glia": None,
                        "OEC": "Astrocytes",
                        "Oligos": "Oligos",
                        "Oligos,Cycling": "Oligos",
                        "Oligos,Immune": None,
                        "Oligos,Vascular": None,
                        "Satellite-glia": "PeripheralGlia",
                        "Satellite-glia,Cycling": "PeripheralGlia",
                        "Schwann": "PeripheralGlia",
                        "Schwann,Cycling": "PeripheralGlia",
                        "Satellite-glia,Schwann": None,
                        "Ttr": "Ependymal",
                        "Vascular": "Vascular",
                        "Vascular,Cycling": "Vascular",
                        "Neurons,Vascular": None,
                        "Vascular,Oligos": None,
                        "Satellite-glia,Vascular": None,
                        "Unknown": None,
                        "Outliers": None
                    }

                    classes_pooled = np.array(
                        [str(mapping[c]) for c in classes], dtype=np.object_)
                    # mask invalid cells
                    classes[ds.col_attrs["_Valid"] == 0] = "Excluded"
                    classes_pooled[ds.col_attrs["_Valid"] == 0] = "Excluded"
                    classes_pooled[classes_pooled == "None"] = "Excluded"
                    ds.set_attr("Class", classes_pooled.astype('str'), axis=1)
                    ds.set_attr("Subclass", classes.astype('str'), axis=1)
                    for ix, cls in enumerate(class_labels):
                        ds.set_attr("ClassProbability_" + str(cls),
                                    probs[:, ix],
                                    axis=1)
                else:
                    logging.info(
                        "No classifier found in this build directory - skipping."
                    )
                    ds.set_attr("Class",
                                np.array(["Unknown"] * ds.shape[1]),
                                axis=1)
                    ds.set_attr("Subclass",
                                np.array(["Unknown"] * ds.shape[1]),
                                axis=1)
Ejemplo n.º 32
0
 def formula(menage, period):
     depcom = menage('depcom', period)
     return sum([
         startswith(depcom, code_departement)
         for code_departement in DEPARTEMENTS_OCCITANIE
     ]) > 0
Ejemplo n.º 33
0
 def function(self, simulation, period):
     depcom = simulation.calculate('depcom', period)
     return period, startswith(depcom, '972')
    def _setup_spec_cube(self):

        self.spec_cube = True
        self.geometry = "spectral_cube"

        end = min(self.dimensionality+1,4)
        if self.events_data:
            ctypes = self.axis_names
        else:
            ctypes = np.array([self.primary_header["CTYPE%d" % (i)]
                               for i in range(1,end)])

        log_str = "Detected these axes: "+"%s "*len(ctypes)
        mylog.info(log_str % tuple([ctype for ctype in ctypes]))

        self.lat_axis = np.zeros((end-1), dtype="bool")
        for p in lat_prefixes:
            self.lat_axis += np_char.startswith(ctypes, p)
        self.lat_axis = np.where(self.lat_axis)[0][0]
        self.lat_name = ctypes[self.lat_axis].split("-")[0].lower()

        self.lon_axis = np.zeros((end-1), dtype="bool")
        for p in lon_prefixes:
            self.lon_axis += np_char.startswith(ctypes, p)
        self.lon_axis = np.where(self.lon_axis)[0][0]
        self.lon_name = ctypes[self.lon_axis].split("-")[0].lower()

        if self.lat_axis == self.lon_axis and self.lat_name == self.lon_name:
            self.lat_axis = 1
            self.lon_axis = 0
            self.lat_name = "Y"
            self.lon_name = "X"

        if self.wcs.naxis > 2:

            self.spec_axis = np.zeros((end-1), dtype="bool")
            for p in spec_names.keys():
                self.spec_axis += np_char.startswith(ctypes, p)
            self.spec_axis = np.where(self.spec_axis)[0][0]
            self.spec_name = spec_names[ctypes[self.spec_axis].split("-")[0][0]]

            self.wcs_2d = _astropy.pywcs.WCS(naxis=2)
            self.wcs_2d.wcs.crpix = self.wcs.wcs.crpix[[self.lon_axis, self.lat_axis]]
            self.wcs_2d.wcs.cdelt = self.wcs.wcs.cdelt[[self.lon_axis, self.lat_axis]]
            self.wcs_2d.wcs.crval = self.wcs.wcs.crval[[self.lon_axis, self.lat_axis]]
            self.wcs_2d.wcs.cunit = [str(self.wcs.wcs.cunit[self.lon_axis]),
                                     str(self.wcs.wcs.cunit[self.lat_axis])]
            self.wcs_2d.wcs.ctype = [self.wcs.wcs.ctype[self.lon_axis],
                                     self.wcs.wcs.ctype[self.lat_axis]]

            self._p0 = self.wcs.wcs.crpix[self.spec_axis]
            self._dz = self.wcs.wcs.cdelt[self.spec_axis]
            self._z0 = self.wcs.wcs.crval[self.spec_axis]
            self.spec_unit = str(self.wcs.wcs.cunit[self.spec_axis])

            if self.spectral_factor == "auto":
                self.spectral_factor = float(max(self.domain_dimensions[[self.lon_axis,
                                                                         self.lat_axis]]))
                self.spectral_factor /= self.domain_dimensions[self.spec_axis]
                mylog.info("Setting the spectral factor to %f" % (self.spectral_factor))
            Dz = self.domain_right_edge[self.spec_axis]-self.domain_left_edge[self.spec_axis]
            self.domain_right_edge[self.spec_axis] = self.domain_left_edge[self.spec_axis] + \
                                                     self.spectral_factor*Dz
            self._dz /= self.spectral_factor
            self._p0 = (self._p0-0.5)*self.spectral_factor + 0.5

        else:

            self.wcs_2d = self.wcs
            self.spec_axis = 2
            self.spec_name = "z"
            self.spec_unit = "code_length"
Ejemplo n.º 35
0
 def formula(self, simulation, period):
     depcom = simulation.calculate('depcom', period)
     return startswith(depcom, '971')
 def function(self, simulation, period):
     period = period.start.period('year').offset('first-of')
     depcom = simulation.calculate('depcom', period)
     return period, np.logical_or(startswith(depcom, '97'),
                                  startswith(depcom, '98'))
Ejemplo n.º 37
0
# Chapter 13 颜色空间转换
import cv2
from numpy.core.defchararray import startswith
import numpy as np

# 13.1 转换颜色空间
flag = 0
for i in dir(cv2):
    if startswith(i, 'COLOR_'):
        flag += 1
print(flag)  # 共有 274 种 cv2.COLOR_xxx

# 13.2 物体跟踪


def FindBlue():  # 识别视频中的蓝色物体并跟踪显示
    cap = cv2.VideoCapture(0)
    while cap.isOpened():
        # 获取每一帧
        ret, frame = cap.read()
        # 转换到 HSV
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        # 设定蓝色的阈值
        lower_blue = np.array([100, 43, 46])
        upper_blue = np.array([124, 255, 255])
        # 根据阈值构建 mask
        mask = cv2.inRange(hsv, lower_blue, upper_blue)
        # 对原图像和 mask 进行位运算
        res = cv2.bitwise_and(frame, frame, mask=mask)
        # 显示图片
        cv2.imshow('src', res)
    def _parse_parameter_file(self):

        if self.parameter_filename.startswith("InMemory"):
            self.unique_identifier = time.time()
        else:
            self.unique_identifier = \
                int(os.stat(self.parameter_filename)[stat.ST_CTIME])

        # Determine dimensionality

        self.dimensionality = self.naxis
        self.geometry = "cartesian"

        # Sometimes a FITS file has a 4D datacube, in which case
        # we take the 4th axis and assume it consists of different fields.
        if self.dimensionality == 4: self.dimensionality = 3

        self.domain_dimensions = np.array(self.dims)[:self.dimensionality]
        if self.dimensionality == 2:
            self.domain_dimensions = np.append(self.domain_dimensions,
                                               [int(1)])

        self.domain_left_edge = np.array([0.5]*3)
        self.domain_right_edge = np.array([float(dim)+0.5 for dim in self.domain_dimensions])

        if self.dimensionality == 2:
            self.domain_left_edge[-1] = 0.5
            self.domain_right_edge[-1] = 1.5

        # Get the simulation time
        try:
            self.current_time = self.parameters["time"]
        except:
            mylog.warning("Cannot find time")
            self.current_time = 0.0
            pass

        # For now we'll ignore these
        self.periodicity = (False,)*3
        self.current_redshift = self.omega_lambda = self.omega_matter = \
            self.hubble_constant = self.cosmological_simulation = 0.0

        if self.dimensionality == 2 and self.z_axis_decomp:
            mylog.warning("You asked to decompose along the z-axis, but this is a 2D dataset. " +
                          "Ignoring.")
            self.z_axis_decomp = False

        if self.events_data: self.specified_parameters["nprocs"] = 1

        # If nprocs is None, do some automatic decomposition of the domain
        if self.specified_parameters["nprocs"] is None:
            if self.z_axis_decomp:
                nprocs = np.around(self.domain_dimensions[2]/8).astype("int")
            else:
                nprocs = np.around(np.prod(self.domain_dimensions)/32**self.dimensionality).astype("int")
            self.parameters["nprocs"] = max(min(nprocs, 512), 1)
        else:
            self.parameters["nprocs"] = self.specified_parameters["nprocs"]

        # Check to see if this data is in some kind of (Lat,Lon,Vel) format
        self.spec_cube = False
        self.wcs_2d = None
        x = 0
        for p in lon_prefixes+lat_prefixes+list(spec_names.keys()):
            y = np_char.startswith(self.axis_names[:self.dimensionality], p)
            x += np.any(y)
        if x == self.dimensionality:
            if self.axis_names == ['LINEAR','LINEAR']:
                self.wcs_2d = self.wcs
                self.lat_axis = 1
                self.lon_axis = 0
                self.lat_name = "Y"
                self.lon_name = "X"
            else:
                self._setup_spec_cube()