Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic for max_studies > 100 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions pytrials/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def __api_info(self):

return api_version, last_updated

def get_full_studies(self, search_expr, max_studies=50):
"""Returns all content for a maximum of 100 study records.
def get_full_studies(self, search_expr, max_studies=None):
"""Returns all content for a maximum of `max_studies` study records.

Retrieves information from the full studies endpoint, which gets all study fields.
This endpoint can only output JSON (Or not-supported XML) format and does not allow
Expand All @@ -51,21 +51,51 @@ def get_full_studies(self, search_expr, max_studies=50):
Args:
search_expr (str): A string containing a search expression as specified by
`their documentation <https://clinicaltrials.gov/api/gui/ref/syntax#searchExpr>`_.
max_studies (int): An integer indicating the maximum number of studies to return.
Defaults to 50.
max_studies (int; optional): An integer indicating the maximum number of studies to return.
Defaults to None, resulting in all studies being returned.

Returns:
dict: Object containing the information queried with the search expression.
list: List of responses containing the information queried with the search expression.

Raises:
ValueError: The number of studies can only be between 1 and 100
"""
if max_studies > 100 or max_studies < 1:
raise ValueError("The number of studies can only be between 1 and 100")
if max_studies is not None and max_studies < 1:
raise ValueError("The number of studies must be at least 1")

min_rnk = 1
max_rnk = 100 if max_studies is None else min(100, max_studies)
req = "full_studies?expr={}&min_rnk={}&max_rnk={}&{}"

full_studies = list()

req = f"full_studies?expr={search_expr}&max_rnk={max_studies}&{self._JSON}"
reqf = req.format(search_expr, min_rnk, max_rnk, self._JSON)
full_studies.append(json_handler(f"{self._BASE_URL}{self._QUERY}{reqf}"))

full_studies = json_handler(f"{self._BASE_URL}{self._QUERY}{req}")
if max_studies is None or max_studies > 100:

n_studies_found = full_studies[0]["FullStudiesResponse"]["NStudiesFound"]

min_rnk += 100
max_rnk += 100

while_stop = (
n_studies_found
if max_studies is None
else min(max_studies, n_studies_found)
)
max_rnk = min(n_studies_found, max_rnk, while_stop)

print(while_stop)
while min_rnk <= while_stop:
print(max_rnk)
reqf = req.format(search_expr, min_rnk, max_rnk, self._JSON)
full_studies.append(
json_handler(f"{self._BASE_URL}{self._QUERY}{reqf}")
)
min_rnk += 100
max_rnk += 100
max_rnk = min(while_stop, max_rnk)

return full_studies

Expand Down
19 changes: 17 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
def test_full_studies():
fifty_studies = ct.get_full_studies(search_expr="Coronavirus+COVID", max_studies=50)

assert len(fifty_studies) == 1

fifty_studies = fifty_studies[0]

assert [*fifty_studies["FullStudiesResponse"].keys()] == [
"APIVrs",
"DataVrs",
Expand All @@ -30,6 +34,10 @@ def test_full_studies_max():
search_expr="Coronavirus+COVID", max_studies=100
)

assert len(hundred_studies) == 1

hundred_studies = hundred_studies[0]

assert [*hundred_studies["FullStudiesResponse"].keys()] == [
"APIVrs",
"DataVrs",
Expand All @@ -51,8 +59,15 @@ def test_full_studies_below():


def test_full_studies_above():
with raises(ValueError):
ct.get_full_studies(search_expr="Coronavirus+COVID", max_studies=150)
hundred_fifty_studies = ct.get_full_studies(
search_expr="Coronavirus+COVID", max_studies=150
)

n = 0
for r in hundred_fifty_studies:
n += len(r["FullStudiesResponse"]["FullStudies"])

assert n == 150


def test_study_fields_csv():
Expand Down