Skip to content

Commit

Permalink
Merge pull request #50 from QuanMPhm/43/alias_actual
Browse files Browse the repository at this point in the history
Validate combined dataframe with alias file
  • Loading branch information
knikolla authored May 30, 2024
2 parents e61e4fd + 3294bec commit 43d4341
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
48 changes: 46 additions & 2 deletions process_report/process_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
PI_S3_FILEPATH = "PIs/PI.csv"


ALIAS_S3_FILEPATH = "PIs/alias.csv"


def get_institution_from_pi(institute_map, pi_uname):
institution_key = pi_uname.split("@")[-1]
institution_name = institute_map.get(institution_key, "")
Expand All @@ -57,8 +60,7 @@ def load_old_pis(old_pi_file):
pi, first_month = pi_info.strip().split(",")
old_pi_dict[pi] = first_month
except FileNotFoundError:
print("Applying credit 0002 failed. Old PI file does not exist")
sys.exit(1)
sys.exit("Applying credit 0002 failed. Old PI file does not exist")

return old_pi_dict

Expand All @@ -69,6 +71,21 @@ def dump_old_pis(old_pi_file, old_pi_dict: dict):
f.write(f"{pi},{first_month}\n")


def load_alias(alias_file):
alias_dict = dict()

try:
with open(alias_file) as f:
for line in f:
pi_alias_info = line.strip().split(",")
alias_dict[pi_alias_info[0]] = pi_alias_info[1:]
except FileNotFoundError:
print("Validating PI aliases failed. Alias file does not exist")
sys.exit(1)

return alias_dict


def is_old_pi(old_pi_dict, pi, invoice_month):
first_invoice_month = old_pi_dict.get(pi, invoice_month)
if compare_invoice_month(first_invoice_month, invoice_month):
Expand Down Expand Up @@ -188,6 +205,11 @@ def main():
required=False,
help="Name of csv file listing previously billed PIs. If not provided, defaults to fetching from S3",
)
parser.add_argument(
"--alias-file",
required=False,
help="Name of alias file listing PIs with aliases (and their aliases). If not provided, defaults to fetching from S3",
)
parser.add_argument(
"--BU-subsidy-amount",
required=True,
Expand All @@ -202,11 +224,18 @@ def main():
csv_files = fetch_s3_invoices(invoice_month)
else:
csv_files = args.csv_files

if args.old_pi_file:
old_pi_file = args.old_pi_file
else:
old_pi_file = fetch_s3_old_pi_file()

if args.alias_file:
alias_file = args.alias_file
else:
alias_file = fetch_s3_alias_file()
alias_dict = load_alias(alias_file)

merged_dataframe = merge_csv(csv_files)

pi = []
Expand All @@ -224,6 +253,7 @@ def main():

projects = list(set(projects + timed_projects_list))

merged_dataframe = validate_pi_aliases(merged_dataframe, alias_dict)
merged_dataframe = add_institution(merged_dataframe)
export_lenovo(merged_dataframe, args.Lenovo_file)
remove_billables(merged_dataframe, pi, projects, args.nonbillable_file)
Expand Down Expand Up @@ -338,6 +368,20 @@ def validate_pi_names(dataframe):
return dataframe


def validate_pi_aliases(dataframe: pandas.DataFrame, alias_dict: dict):
for pi, pi_aliases in alias_dict.items():
dataframe.loc[dataframe[PI_FIELD].isin(pi_aliases), PI_FIELD] = pi

return dataframe


def fetch_s3_alias_file():
local_name = "alias.csv"
invoice_bucket = get_invoice_bucket()
invoice_bucket.download_file(ALIAS_S3_FILEPATH, local_name)
return local_name


def apply_credits_new_pi(dataframe, old_pi_file):
new_pi_credit_code = "0002"
new_pi_credit_amount = 1000
Expand Down
21 changes: 21 additions & 0 deletions process_report/tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ def test_get_pi_institution(self):
)


class TestAlias(TestCase):
def setUp(self):
self.alias_dict = {"PI1": ["PI1_1", "PI1_2"], "PI2": ["PI2_1"]}

self.data = pandas.DataFrame(
{
"Manager (PI)": ["PI1", "PI1_1", "PI1_2", "PI2_1", "PI2_1"],
}
)

self.answer = pandas.DataFrame(
{
"Manager (PI)": ["PI1", "PI1", "PI1", "PI2", "PI2"],
}
)

def test_validate_alias(self):
output = process_report.validate_pi_aliases(self.data, self.alias_dict)
self.assertTrue(self.answer.equals(output))


class TestCredit0002(TestCase):
def setUp(self):
data = {
Expand Down

0 comments on commit 43d4341

Please sign in to comment.