Techniques that load court cases into an amos project.
Loaders make a project's data from a source named in its settings, so a
project with a loader needs no item. They are usually the first step of the
wrangler, and they work through the project's clerk (its
nagata.FileManager): a folder or file named by a relative path is looked
for in the current folder and then in the clerk's input folder (the
"input_folder" of the "files" section), and downloads are saved in the input
folder.
courtpy's loaders collect court cases (downloading or dividing them first, if
asked), parse them with rulebooks, and code them, so the label of a study
(such as "outcome_reversal", which a coder makes) exists when the data is
loaded. As with reference data, load_courtlistener parses the cases that
were saved before, and download_courtlistener downloads them first (from
CourtListener's bulk data, unless "method" is "api"):
[general]
seed = 43
label = outcome_reversal
[files]
input_folder = data
[appeals_project]
appeals_workers = wrangler, analyst, critic
[wrangler]
techniques = download_courtlistener, drop_text_columns
[download_courtlistener_parameters]
source = courtlistener
method = bulk
courts = federal_geographic_circuits
start_date = 2019-01-01
end_date = 2019-12-31
save = cases.csv
reuse = true
Every loader takes these parameters, set in its "{name}_parameters" section:
| Parameter | Meaning |
|---|---|
source |
The folder of saved cases (or, for load_cases, a saved table). |
coders |
Techniques to apply after parsing. Defaults to "code_parties, code_case_type, code_outcome" ("none" for load_cases). Use "none" for none. |
save |
File to save the coded table to (".csv", ".parquet", or ".xlsx"), with the whole dataset beside it in a joblib file of the same name (see Cases.save). A relative path is in the clerk's interim folder. |
reuse |
Whether to load the saved dataset, if there is one, instead of collecting, parsing, and coding the cases again. Defaults to false. |
label, task, groups |
As in amos: they default to those in the "general" section. |
Other modules add reference loaders (see ReferenceLoader), which load a
table that describes something other than cases. Each "load_" technique
loads the files that courtpy keeps (or a file named as its "source"), and
each "download_" technique downloads the files first, again if "refresh" is
true:
| Technique | Table | Module |
|---|---|---|
load_roster, download_biographies |
A roster of judges. | courtpy.judges |
load_scdb, download_scdb |
The Supreme Court Database's cases or votes. | courtpy.scdb |
load_idb, download_idb |
The Integrated Database's appeals. | courtpy.fjc_idb |
load_politics, download_politics |
The political context of each year. | courtpy.politics |
download_jcs |
The Judicial Common Space scores. | courtpy.jcs |
Contents
CaseLoader: base class for techniques that load court cases.
LoadCases: loads a dataset of cases saved by Cases.save, or a table of
one.
DownloadCourtlistener: downloads CourtListener cases, and parses and
codes them.
LoadCourtlistener: parses and codes saved CourtListener cases.
LoadLexisNexis: divides, parses, and codes Lexis-Nexis cases.
ReferenceLoader: base class for techniques that load a table of
reference data, such as load_scdb and download_biographies.
SourceLoader: base class for loaders that parse a folder of a source's
saved cases.
CaseLoader(contents=None, name='', parameters=dict(), clerk=None)
dataclass
¶
Bases: Loader, ABC
Base class for techniques that load court cases.
A subclass writes load, which collects and parses the cases at a source
(a folder or file, already found with the clerk) and returns a table of
them, one row for each case. apply then codes the table with the
loader's "coders", saves it if "save" is set, and makes it the dataset
that the rest of the workflow works on, with the label, task, and groups
from the loader's parameters (which default to the "general" section of
the settings). The label can be a column that a coder makes. If no coder
makes it, it is pending, as in amos: a later technique (such as
"derive_columns") can still make it, and using it before then raises an
error. The dataset's history records the loader and then each coder.
The dataset is Cases, or the kind that the last of the coders makes
(such as Votes, if "cases_to_votes" is the last). If "save" is set, it
is saved with CourtData.save: whole, in a joblib file beside the table
(such as "cases.joblib" beside "cases.csv"), and as the table. If
"reuse" is true and it was saved before, the saved dataset is loaded
instead (from the joblib file, or else from the table), and nothing is
collected, parsed, or coded again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name used to refer to the technique in a workflow. Defaults to
|
''
|
contents
|
str | Callable[..., Any] | None
|
not used. Defaults to |
None
|
parameters
|
GenericDict
|
"source", "coders", "save", "reuse", "label", "task", and
"groups" (see the module's documentation), and parameters for
|
dict()
|
clerk
|
FileManager | None
|
the file manager that finds folders and files. Defaults to
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
default_coders |
tuple[str, ...]
|
the coders applied when "coders" is not set. |
default_source |
str | None
|
the source used when "source" is not set, or |
default_source = None
class-attribute
¶
Public Methods
apply(item, **kwargs)
¶
Loads, codes, and saves the cases, as a dataset continuing item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Dataset
|
the dataset so far, whose seed and history are kept. |
required |
**kwargs
|
Any
|
"source", "coders", "save", "reuse", "label", "task", and
"groups", and parameters for |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if there is no "source" and the loader has no default source. |
Returns:
| Type | Description |
|---|---|
Dataset
|
The dataset of the coded cases. |
Source code in src/courtpy/wrangler/loaders.py
DownloadCourtlistener(contents=None, name='', parameters=dict(), clerk=None)
dataclass
¶
Bases: LoadCourtlistener
Downloads cases from CourtListener, and parses and codes them.
The cases are downloaded into the source folder (by default,
"courtlistener" in the clerk's input folder) with
courtpy.CourtListener.download, and then the folder is parsed as
load_courtlistener parses it. With "bulk" (the default), they are
extracted from CourtListener's bulk data, which needs no API key. With
"api", they are downloaded with the API, with the key from
courtpy.secrets. Cases that were saved before are not downloaded again
unless "refresh" is true.
It takes the parameters of SourceLoader, and these:
| Parameter | Meaning |
|---|---|
method |
"bulk" (the default) or "api". |
courts |
CourtListener court ids (such as "ca1, ca2") or groups (such as "federal_appellate") to download. |
start_date, end_date |
Dates filed to download (YYYY-MM-DD). |
max_cases |
Most cases to download: in all with "bulk", and in each court with "api". |
refresh |
Whether to download cases that were already saved. |
dockets |
For "api", whether to download each case's docket too (the bulk data always has it). |
bulk_folder, bulk_date, stream |
For "bulk", where to keep the bulk files (found with the clerk), which date's files to use, and whether to read them without saving them. |
load(source, *, method='bulk', courts=None, start_date=None, end_date=None, max_cases=None, refresh=False, dockets=False, bulk_folder=None, bulk_date=None, stream=False, **kwargs)
¶
Downloads the cases into the folder source and parses it.
Settings can be text (as a settings file gives them), so each is converted to the type it needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
the folder to save the cases in, found with the clerk. |
required |
method
|
Any
|
"bulk" or "api". Defaults to "bulk". |
'bulk'
|
courts
|
Any
|
CourtListener court ids or groups to download. Defaults
to |
None
|
start_date
|
Any
|
earliest date filed to download (YYYY-MM-DD).
Defaults to |
None
|
end_date
|
Any
|
latest date filed to download (YYYY-MM-DD). Defaults
to |
None
|
max_cases
|
Any
|
most cases to download. Defaults to |
None
|
refresh
|
Any
|
whether to download cases that were already saved.
Defaults to |
False
|
dockets
|
Any
|
with "api", whether to download each case's docket too.
Defaults to |
False
|
bulk_folder
|
Any
|
with "bulk", the folder to keep the bulk files in,
found with the clerk. Defaults to |
None
|
bulk_date
|
Any
|
with "bulk", the date of the files to use. Defaults to
|
None
|
stream
|
Any
|
with "bulk", whether to read the files without saving
them. Defaults to |
False
|
**kwargs
|
Any
|
the parameters of |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if "method" is not "bulk" or "api", if no courts are named, or if no cases are found. |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table of parsed cases, labeled by "case_id". |
Source code in src/courtpy/wrangler/loaders.py
LoadCases(contents=None, name='', parameters=dict(), clerk=None)
dataclass
¶
Bases: CaseLoader
Loads a dataset of cases saved by Cases.save, or a table of one.
A joblib file holds the whole dataset, of the kind that it was (so a
saved Votes is Votes again), with its history. A table (such as a
CSV or parquet file) becomes Cases, labeled by "case_id", and columns
of booleans and whole numbers with missing values get types that allow
them (see CourtData.load). A saved dataset is usually coded already,
so no coders are applied unless "coders" names some.
load(source, **kwargs)
¶
Loads the dataset at source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
path of a joblib, CSV, or parquet file saved by
|
required |
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if there is no file at |
Returns:
| Type | Description |
|---|---|
Dataset
|
The dataset. |
Source code in src/courtpy/wrangler/loaders.py
LoadCourtlistener(contents=None, name='', parameters=dict(), clerk=None)
dataclass
¶
Bases: SourceLoader
Parses and codes cases saved from CourtListener.
The source is a folder of cases saved by courtpy.CourtListener (by
default, "courtlistener" in the clerk's input folder), from its bulk
data or its API. To download them first, use download_courtlistener.
It takes the parameters of SourceLoader.
LoadLexisNexis(contents=None, name='', parameters=dict(), clerk=None)
dataclass
¶
Bases: SourceLoader
Divides, parses, and codes cases downloaded from Lexis-Nexis.
The source is a folder of Lexis-Nexis cases, one per text file (by
default, "lexis_nexis" in the clerk's input folder). If "batches" names
files (or folders) of many cases, as Lexis-Nexis downloads them, they are
divided into the source folder first (see courtpy.lexis.split).
It takes the parameters of SourceLoader, and:
| Parameter | Meaning |
|---|---|
batches |
Lexis-Nexis downloads of many cases to divide first. |
reads = 'lexis_nexis'
class-attribute
¶
Required Methods
load(source, *, batches=None, **kwargs)
¶
Divides any batches into the folder source and parses it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
the folder of Lexis-Nexis cases, found with the clerk. |
required |
batches
|
Any
|
files or folders of downloads of many cases, found with
the clerk. The ".txt" files in a folder are used. Defaults to
|
None
|
**kwargs
|
Any
|
the parameters of |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table of parsed cases, labeled by "case_id". |
Source code in src/courtpy/wrangler/loaders.py
ReferenceLoader(contents=None, name='', parameters=dict(), clerk=None)
dataclass
¶
Bases: Loader, ABC
Base class for techniques that load a table of reference data.
Reference data describes something other than parsed cases: the judges,
the Supreme Court Database, the Integrated Database, or the political
context of each year. Unlike a CaseLoader, a reference loader needs no
"source". Without one, it loads the files that courtpy keeps outside of
projects (in courtpy.utilities.data_folder()), which are downloaded
first if they are missing, and the dataset's history records the source
as "current". A relative "source" is looked for in the current folder and
then in the clerk's input folder.
Like every loader, it starts a new dataset, so it usually begins a study
whose rows are what the table describes (such as each justice's vote in
each case). To add a table's columns to parsed cases, use a merger (such
as add_scdb) instead.
Attributes:
| Name | Type | Description |
|---|---|---|
default_source |
str
|
the source that is recorded when "source" is not set. |
default_source = 'current'
class-attribute
¶
Public Methods
apply(item, **kwargs)
¶
Loads the table into a dataset that continues item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Dataset
|
the dataset so far, whose seed and history are kept. |
required |
**kwargs
|
Any
|
"source", "label", "task", and "groups", and parameters
for |
{}
|
Returns:
| Type | Description |
|---|---|
Dataset
|
The dataset of the loaded table. |
Source code in src/courtpy/wrangler/loaders.py
SourceLoader(contents=None, name='', parameters=dict(), clerk=None)
dataclass
¶
Bases: CaseLoader, ABC
Base class for loaders that parse a folder of a source's saved cases.
The source is a folder of saved cases of the courtpy.sources.Source
named by reads. A subclass sets reads and default_source, and can
extend load to collect the cases first (as DownloadCourtlistener
downloads them and LoadLexisNexis divides files of many cases).
| Parameter | Meaning |
|---|---|
rulebooks |
Rules to parse with (such as "civil, criminal"), after the source's header rules and "general", which are always applied. Defaults to "federal". |
keep_text |
Whether to keep the text of the cases' sections (such as "party", "history", and "opinion") in the table, which parse_all can parse again. |
limit |
Most cases to parse, for trying out rules. |
workers |
Number of processes to parse with. |
Attributes:
| Name | Type | Description |
|---|---|---|
reads |
str
|
the name of the source of the saved cases, such as "courtlistener". |
reads = ''
class-attribute
¶
Required Methods
load(source, *, rulebooks=None, keep_text=False, limit=None, workers=None, **kwargs)
¶
Parses the folder source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
the folder of saved cases, found with the clerk. |
required |
rulebooks
|
Any
|
rules to parse with. Defaults to |
None
|
keep_text
|
Any
|
whether to keep the text of the cases' sections.
Defaults to |
False
|
limit
|
Any
|
most cases to parse. Defaults to |
None
|
workers
|
Any
|
number of processes to parse with. Defaults to |
None
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if no cases are found. |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table of parsed cases, labeled by "case_id". |