Skip to content

Commit

Permalink
Moved institution-processing functions from `add_institution_processo…
Browse files Browse the repository at this point in the history
…r` to `util`

Since these functions are now used in more than one location (`add_insti_proc`
and `prepay_proc`), it makes sense for them to be relocated to `util`

The test case checking institution-processing has changed slightly
  • Loading branch information
QuanMPhm committed Jan 14, 2025
1 parent 6e2cacf commit e95349a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
26 changes: 2 additions & 24 deletions process_report/processors/add_institution_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,6 @@

@dataclass
class AddInstitutionProcessor(processor.Processor):
@staticmethod
def _get_institute_mapping(institute_list: list):
institute_map = dict()
for institute_info in institute_list:
for domain in institute_info["domains"]:
institute_map[domain] = institute_info["display_name"]

return institute_map

@staticmethod
def _get_institution_from_pi(institute_map, pi_uname):
institution_domain = pi_uname.split("@")[-1]
for i in range(institution_domain.count(".") + 1):
if institution_name := institute_map.get(institution_domain, ""):
break
institution_domain = institution_domain[institution_domain.find(".") + 1 :]

if institution_name == "":
logger.warning(f"PI name {pi_uname} does not match any institution!")

return institution_name

def _add_institution(self):
"""Determine every PI's institution name, logging any PI whose institution cannot be determined
This is performed by `get_institution_from_pi()`, which tries to match the PI's username to
Expand All @@ -49,7 +27,7 @@ def _add_institution(self):
The list of mappings are defined in `institute_map.json`.
"""
institute_list = util.load_institute_list()
institute_map = self._get_institute_mapping(institute_list)
institute_map = util.get_institute_mapping(institute_list)
self.data = self.data.astype({invoice.INSTITUTION_FIELD: "str"})
for i, row in self.data.iterrows():
pi_name = row[invoice.PI_FIELD]
Expand All @@ -58,7 +36,7 @@ def _add_institution(self):
else:
self.data.at[
i, invoice.INSTITUTION_FIELD
] = self._get_institution_from_pi(institute_map, pi_name)
] = util.get_institution_from_pi(institute_map, pi_name)

def _process(self):
self._add_institution()
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from unittest import TestCase

from process_report.tests import util as test_utils
from process_report import util


class TestAddInstituteProcessor(TestCase):
class TestAddInstitute(TestCase):
def test_get_pi_institution(self):
institute_map = {
"harvard.edu": "Harvard University",
Expand Down Expand Up @@ -32,10 +32,7 @@ def test_get_pi_institution(self):
"[email protected]": "Beth Israel Deaconess Medical Center",
}

add_institute_proc = test_utils.new_add_institution_processor()

for pi_email, answer in answers.items():
self.assertEqual(
add_institute_proc._get_institution_from_pi(institute_map, pi_email),
answer,
util.get_institution_from_pi(institute_map, pi_email), answer
)

0 comments on commit e95349a

Please sign in to comment.