Module trase.tools.pcs.base.transform
Functions
def add_or_update_groups(base_object)def add_or_update_traders(base_object, random=True)-
Add traders after labels in paths when no traders are present OR Recompute traders based on labels using the current traders hierarchy if trader labels are present. OR Recompute traders based on names of traders using the current traders hierarchy if trader labels are not present.
When trader labels are not present, new traders are obtained from traders by getting the label with the same name, and retrieving the trader corresponding to that label in the traders hierarchy.
This allows to apply newly found synonyms to a dataset previously cleaned using
fix_supply_chain_tradersand with trader labels removed. For instance, a dataset with label "AGRO AGRO PTE LTD" is cleaned with the following hierarchy,[11111] AGRO AGRO PTE LTD [TRADER GROUP] | |– [22222] AGRO AGRO PTE LTD [TRADER] | | | |– [33333] AGRO AGRO PTE LTD
label 33333 is replaced by trader 22222.
Later, we realize that "AGRO AGRO PTE LTD" is a synonym of trader "AGRO AGRO", and update the traders hierarchy
[44444] AGRO AGRO [TRADER GROUP] | |– [55555] AGRO AGRO [TRADER] | | | |– [66666] AGRO AGRO | |– [33333] AGRO AGRO PTE LTD
When function
fix_supply_chain_tradersis run, trader node 22222 is replaced by the trader that is a parent of the label with the same name as 22222, i.e. trader node 55555.This operation is based on the assumption that the trader is still related to the original label (which was overwritten by
fix_supply_chain_tradersfollowed byremove_trader_labels) in the current trader hierarchy.The assumption breaks when a synonym is detached from a trader. For instance, if a dataset containing a flow/link with label "TRADES INC" (node 40404) is cleaned with the following traders hierarchy in the database,
[10101] TRADE INC [TRADER GROUP] | |– [20202] TRADE INC [TRADER] | | | |– [30303] TRADE INC | |– [40404] TRADES INC
label 40404 will be overwritten by trader 20202. If at a later date we realize that "TRADES INC" and "TRADE INC" are actually different companies, we would fix the trader hierarchy to
[10101] TRADE INC [TRADER GROUP] | |– [20202] TRADE INC [TRADER] | | | |– [30303] TRADE INC
[50505] TRADES INC [TRADER GROUP] | |– [60606] TRADES INC [TRADER] | | | |– [40404] TRADES INC
When
fix_supply_chain_tradersis run, trader 20202 will be preserved, as the function looks for the label with the same name (30303), and finds the trader of that label (20202). def add_parent_region(base_object, child_role, parent_region_role, parent_region_level_name)-
Insert parent region nodes to paths at position just before given role. There is an assumption that the unknown nodes have set parents.
Example usage:
add_parent_region(base_object, "MUNICIPALITY", "STATE OF PRODUCTION", "STATE"):param base_object: supply chain :type base_object: pcs.base_objects.base_objects.SupplyChain :param child_role: node role of node used to determine value of biome :type child_role: str :param parent_region_role: node role of parent node :type parent_region_role: str :param parent_region_level_name: node level name :type parent_region_level_name: str :param cur: database cursor object :type cur: psycopg2.extensions.cursor or pcs.connect.loggingCursor
def consolidate(base_object)def fix_traders(base_object, random=True)def remove_labels(base_object)