Ejemplo n.º 1
0
 def __init__(self, cf):
     # type: (ConfigParser) -> None
     """Initialization."""
     SAConfig.__init__(self, cf)  # initialize base class first
     # 1. Check the required key and values
     requiredkeys = [
         'COLLECTION', 'DISTRIBUTION', 'SUBSCENARIO', 'ENVEVAL', 'BASE_ENV',
         'UNITJSON'
     ]
     for k in requiredkeys:
         if k not in self.bmps_info:
             raise ValueError(
                 '%s: MUST be provided in BMPs_cfg_units or BMPs_info!' % k)
     # 2. Slope position units information
     units_json = self.bmps_info.get('UNITJSON')
     unitsf = self.model.model_dir + os.sep + units_json
     if not FileClass.is_file_exists(unitsf):
         raise Exception('UNITJSON file %s is not existed!' % unitsf)
     with open(unitsf, 'r', encoding='utf-8') as updownfo:
         self.units_infos = json.load(updownfo)
     self.units_infos = UtilClass.decode_strs_in_dict(self.units_infos)
     if 'overview' not in self.units_infos:
         raise ValueError('overview MUST be existed in the UNITJSON file.')
     if 'all_units' not in self.units_infos['overview']:
         raise ValueError(
             'all_units MUST be existed in overview dict of UNITJSON.')
     self.units_num = self.units_infos['overview']['all_units']  # type: int
     # 3. Collection name and subscenario IDs
     self.bmps_coll = self.bmps_info.get('COLLECTION')  # type: str
     self.bmps_subids = self.bmps_info.get('SUBSCENARIO')  # type: List[int]
     # 4. Construct the dict of gene index to unit ID, and unit ID to gene index
     self.unit_to_gene = OrderedDict()  # type: OrderedDict[int, int]
     self.gene_to_unit = dict()  # type: Dict[int, int]
     # 5. Construct the upstream-downstream units of each unit if necessary
     self.updown_units = dict()  # type: Dict[int, Dict[AnyStr, List[int]]]
Ejemplo n.º 2
0
    def __init__(self, cf):
        """Initialization."""
        SAConfig.__init__(self, cf)  # initialize base class first
        # Handling self.bmps_info for specific application
        # 1. Check the required key and values
        requiredkeys = ['COLLECTION', 'DISTRIBUTION', 'SUBSCENARIO', 'UPDOWNJSON',
                        'ENVEVAL', 'BASE_ENV']
        for k in requiredkeys:
            if k not in self.bmps_info:
                raise ValueError('[%s]: MUST be provided!' % k)
        # 2. Slope position units information
        updownf = self.bmps_info.get('UPDOWNJSON')
        FileClass.check_file_exists(updownf)
        with open(updownf, 'r') as updownfo:
            self.units_infos = json.load(updownfo)
        self.units_infos = UtilClass.decode_strs_in_dict(self.units_infos)
        # 3. Get slope position sequence
        sptags = cf.get('BMPs', 'slppos_tag_name')
        self.slppos_tags = json.loads(sptags)
        self.slppos_tags = UtilClass.decode_strs_in_dict(self.slppos_tags)
        self.slppos_tagnames = sorted(list(self.slppos_tags.items()), key=operator.itemgetter(0))
        self.slppos_unit_num = self.units_infos['overview']['all_units']
        self.slppos_to_gene = OrderedDict()
        self.gene_to_slppos = dict()

        # method 1: (deprecated)
        #     gene index: 0, 1, 2, ..., n
        #     slppos unit: rdg1, rdg2,..., bks1, bks2,..., vly1, vly2...
        # idx = 0
        # for tag, sp in self.slppos_tagnames:
        #     for uid in self.units_infos[sp]:
        #         self.gene_to_slppos[idx] = uid
        #         self.slppos_to_gene[uid] = idx
        #         idx += 1
        # method 2:
        #     gene index: 0, 1, 2, ..., n
        #     slppos unit: rdg1, bks2, vly1,..., rdgn, bksn, vlyn
        idx = 0
        spname = self.slppos_tagnames[0][1]
        for uid, udict in self.units_infos[spname].items():
            spidx = 0
            self.gene_to_slppos[idx] = uid
            self.slppos_to_gene[uid] = idx
            idx += 1
            next_uid = udict['downslope']
            while next_uid > 0:
                self.gene_to_slppos[idx] = next_uid
                self.slppos_to_gene[next_uid] = idx
                idx += 1
                spidx += 1
                spname = self.slppos_tagnames[spidx][1]
                next_uid = self.units_infos[spname][next_uid]['downslope']

        assert (idx == self.slppos_unit_num)

        # 4. SubScenario IDs and parameters read from MongoDB
        self.bmps_subids = self.bmps_info.get('SUBSCENARIO')
        self.bmps_coll = self.bmps_info.get('COLLECTION')
        self.bmps_params = dict()
        self.slppos_suit_bmps = dict()

        self.read_bmp_parameters()
        self.get_suitable_bmps_for_slppos()