Package: locushandler
LocusHandlerThis package was created to automate Locus coded data processing steps. It provides functions to parse and validate
Locus codes. It also allows to process entire data files that use the Locus classification system or other common economic
classification systems such as NAICS or SOC.Getting StartedInstallationThe package can be downloaded and installed using pip.pip install locushandlerYou will then be able to import the package in your Python scripts.import locushandler as lhYou can also import specific parts of the package.import locushandler.string_parser as sp
import locushandler.file_handler as fhPrerequisitesThe LocusHandler is coded in Python 3.
Using pip install, all Python libraries required to use the LocusHandler should be automatically installed on your machine.How to use the LocusHandlerGranularityThe granularity parameters takes in a string with the format <a>x<r> with <a>
in [1,4,12,36] and <r> in [1,6,6x4,6x4x3]Activity'1' : no activity'4' : 4 phases activty cycle'12' : 12 phases activity cycle'36' : 36 phases activity cycleResource'1' : no resource'6' : resource categories'6x4' : resource categories staged'6x4x3' : resource categories sub-stagedExamplesWork Locus : - 4x6 : 1 B, 3 F - 12x6x4 : (B4) 1.2 B4, 2.2 C3 - 36x6x4x3 : 3.3.1 E3ii, (B4ii) 3.1.2 DivResource Locus : - 1x6 : B, F - 12x6x4 : B4 2.2 B4, A4 1.3 C3 - 36x6x4x3 : B4ii 1.2.2 E3ii, B4ii 2.2.2 DivInput syntaxThe LocusHandler assumes that all work loci contain parentheses around the Distinguishing Resources and the Information Outputs.
Please make sure that the input data follows that syntax.Parsing a Locus codeThe string_parser module allows you to parse any work or resource Loci, in a string format, such as'(B4ii) 1.2.2 B4i'
'B4ii 2.2.2 F'and get a list, a dictionary or a string of that code at the required granularity.Parsing to a dictionarysp.string_parser('(B4ii) 1.2.2 B4Div', 'dict', '36x6x4x3', show_dr=True, show_io=True))will return the following dictionary{'dr': {'r1': 'B', 'r2': '4', 'r3': 'ii'},
 'act': {'a1': '1', 'a2': '2', 'a3': '2'},
 'obj': {'r1': 'B',  'r2': '4', 'r3': 'V'},
 'io':  {'r1': '',  'r2': '', 'r3': ''}
 }make use of themerge_fieldsparameter to return (field, field value) pairs:sp.string_parser('(B4ii) 1.2.2 B4Div', 'dict', '36x6x4x3', show_dr=True, show_io=True))will return the following dictionary{'dr': 'B4ii',
 'act': '1.2.2',
 'obj': 'B4V',
 'io': ''
}Parsing to a listsp.string_parser('(B4ii) 1.2.2 B4Div', 'list', '4x6', show_dr=True, show_io=True))will return the following list['B','2','B','']Parsing to a stringsp.string_parser('(B4ii) 1.2.2 B4Div', 'string', '12x6x4', show_dr=False, show_io=False))will return the following string'1.2 B4'Parsing a Locus code column in a fileThe file_handler module allows you to parse an entire column of a .csv file. If the input file contains Locus code as strings
the function parse_file can parse it and return a Dataframe or a name of a new saved .csv file with columns containing
each element the Locus code at the required granularity.
An example of input data would be the following table.Enterprise_LocusEmployment level...AreaYear'(B4ii) 1.2.2 B4i'1366...NY2010(A4iii) 3.3.2 B3ii235...VA2008...............'(B4ii) 1.3.2 F'78...KS2010Parsing to a Pandas dataframefh.parse_file('file.csv', 'Enterprise_Locus', 'df', '12x6x4x3', show_dr=False, show_io=False)would return a Pandas dataframe with the following informationa1a2r1r2r3Employment level...AreaYear12B4i1366...NY201033B3ii235...VA2008...........................13F78...KS2010Parsing to a new .csv filefh.parse_file('file.csv', 'Enterprise_Locus', 'path', '12x6x4x3', show_dr=False, show_io=False)would return the path of the new .csv file that contain the same information as above. In this case, the path would be
'file_parsed_12x6x4x3_Enterprise_Locus.csv'Mapping to Locus codesIf the input data uses a common classification system, the LocusHandler provide a function to map the input data
to Locus code at the desired granularity.
An example of input data would be the following table.NaicsEmployment level...AreaYear135893468735...USA20107862187685...FRANCE2008...............3469734786...NIGERIA2010To call the map_to_locus you need the path of your input file (file_naics.csv), the name of the column with the classification system you map from (Naics)
, the name of the column of the classification sytem in the crosswalk file (NAICS5), the path to the crosswalk file (naics2locus.csv),
the barcode field you are interested in (enterprise_locus), the granularity (4x6) and the need for dr and io.fh.map_to_locus('file_naics.csv', 'Naics', 'NAICS5', 'naics2locus.csv', 'entreprise_locus','4x6', show_dr=True, show_io=True)would return a Pandas dataframe with the following information*.dr1a1r1io1Employment level...AreaYearB1B3468735...USA20102AC87685...FRANCE2008........................A3B34786...NIGERIA2010*Naics to Locus mapping not accurate hereDocumentationPlease read theTechnical Brieffor details on the project.Please read theSpecsfor details on the functions available.AuthorsPhillip Nguyen-String parsingOlivia Dalglish-Code validationEmeline Floc'h-File handlingContributorsVinharng Chew-Stefanie Bourland-Chris Haack-Aaron Lee-Atul Prasad-Reviewers