from adj_filst import adjfilst
import cpsmar
from cps_rets import Returns
from soi_rets import create_soi
from phase1 import phaseone
from phase2 import phasetwo
from add_cps_vars import add_cps
from add_nonfilers import add_nonfiler
import pandas as pd
import argparse

"""
    Script to run each phase of the matching process
"""


def match(mar_cps_path='asec2016_pubuse_v3.dat',
          puf_path='puf2011.csv'):
    # Add arguments for specifying path to CPS file in CSV format
    # this will allow the program to skip the process of creating the CPS from
    # a .DAT file.
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--cps', help='path to CPS file in CSV format')
    parser.add_argument('-d', '--dat', help='path to CPS file in DAT format')
    parser.add_argument('-p', '--puf', help='path to PUF file in CSV format')
    args = parser.parse_args()

    # Create CPS file either from a CPS or through create_cps method
    if args.cps is not None:
        mar_cps = pd.read_csv(args.cps)
    else:
        if args.dat is not None:
            mar_cps_path = args.dat
        mar_cps = cpsmar.create_cps(mar_cps_path)

    # If you already have the CPS in CSV format, comment out the line above and
    # uncomment the line bellow to skip creation from the DAT file and use CSV

    # do this initially in an effort to fix warning:
    # "A value is trying to be set on a copy of a slice from a DataFrame"
    if args.puf is not None:
        puf_path = args.puf
    puf = pd.read_csv(puf_path)
    # Change PUF columns to lowercase
    puf.columns = map(str.lower, puf.columns)
    # Remove aggregated variables from the PUF
    puf = puf[(puf['recid'] != 999996) & (puf['recid'] != 999997) &
              (puf['recid'] != 999998) & (puf['recid'] != 999999)]

    print('CPS Created')
    rets = Returns(mar_cps)
    cps = rets.computation()

    print('CPS Tax Units Created')
    filers, nonfilers = adjfilst(cps)

    print('Adjustment Complete')
    soi = create_soi(puf.copy())

    print('PUF Created')
    print('Start Phase One')
    filers = filers.fillna(0)
    soi = soi.fillna(0)
    soi_final, cps_final, counts = phaseone(filers, soi)

    print('Start Phase Two')
    match = phasetwo(
                soi_final.loc[:, ['cellid', 'soiseq', 'wt', 'factor', 'yhat']],
                cps_final.loc[:, ['cellid', 'cpsseq', 'wt', 'factor', 'yhat']])

    print('Creating final file')
    cpsrets = add_cps(filers, match, puf)
    cps_matched = add_nonfiler(cpsrets, nonfilers)
    # add age range variable
    cps_matched['agerange'] = 0
    # Rename variables for use in PUF data prep
    renames = {'icps1': 'age_head',
               'icps2': 'age_spouse',
               'wasp': 'wage_head',
               'wass': 'wage_spouse'}
    cps_matched = cps_matched.rename(columns=renames)

    return cps_matched


if __name__ == "__main__":
    cps_matched = match()
    cps_matched.to_csv('../../cps-matched-puf.csv', index=False)

@@ -254,24 +254,18 @@
 %0Aimport 
-argparse
+os
 %0A%0A%22%22%22%0A  
@@ -335,776 +335,436 @@
 tch(
-mar_cps_path='asec2016_pubuse_v3.dat',%0A          puf_path='puf2011.csv'):%0A    # Add arguments for specifying path to CPS file in CSV format%0A    # this will allow the program to skip the process of creating the CPS from%0A    # a .DAT file.%0A    parser = argparse.ArgumentParser()%0A    parser.add_argument('-c', '--cps', help='path to CPS file in CSV format')%0A    parser.add_argument('-d', '--dat', help='path to CPS file in DAT format')%0A    parser.add_argument('-p', '--puf', help='path to PUF file 
+):%0A    # If there is a .CSV version of the CPS, simply read that in. Otherwise%0A    # convert the .DAT file to a .CSV%0A    cps_csv_path = 'cpsmar2016.csv'%0A    if os.path.isfile(cps_csv_path):%0A        print('Read
 in
+g
  C
-SV form
+PS D
 at
+a
 ')%0A    
+ 
-args = parser.parse_args()%0A%0A    # Create CPS file either from a CPS or through create_cps method%0A    if args.cps is not None:%0A        mar_cps = pd.read_csv(args.cps)%0A    else:%0A        if args.dat is not None:%0A            mar_cps_path = args.dat%0A
+   mar_cps = pd.read_csv('cpsmar2016.csv')%0A    else:%0A        cps_dat_path = 'asec2016_pubuse_v3.dat'%0A        if os.path.isfile(cps_dat_path):%0A            print('Converting .DAT to .CSV')%0A     
        
- 
 mar_
@@ -791,347 +791,221 @@
 cps(
-mar_cps
+cps_dat
 _path)%0A
-%0A
     
-# If you already have the CPS in CSV format, comment out the line above and%0A    # uncomment the line bellow to skip creation from the DAT file and use CSV%0A%0A    # do this initially in an effort to fix warning:%0A    # %22A value is trying to be set on a copy of a slice from a DataFrame%22%0A    if args.puf is not None:%0A   
+    else:%0A            m = ('You must have either the .DAT or .CSV version of the 2016' +%0A                 ' CPS in your directory')%0A            raise IOError(m)%0A    print('Reading PUF Data')%0A
     
- 
 puf_
@@ -1015,16 +1015,21 @@
 h = 
-args.puf
+'puf2011.csv'
 %0A   
@@ -1340,18 +1340,29 @@
 t('C
-PS Created
+reating CPS Tax Units
 ')%0A 
@@ -1563,33 +1563,8 @@
 ))%0A%0A
-    print('PUF Created')%0A
     

update runmatch.py