forked from asreview/asreview-datatools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import argparse | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
from asreview import ASReviewData | ||
from asreview.data.base import load_data | ||
|
||
|
||
def _check_suffix(input_files, output_file): | ||
# Also raises ValueError on URLs that do not end with a file extension | ||
suffixes = [Path(item).suffix for item in input_files if item is not None] | ||
suffixes.append(Path(output_file).suffix) | ||
|
||
set_ris = {".txt", ".ris"} | ||
set_tabular = {".csv", ".tab", ".tsv", ".xlsx"} | ||
set_suffixes = set(suffixes) | ||
|
||
if len(set(suffixes)) > 1: | ||
if not (set_suffixes.issubset(set_ris) or set_suffixes.issubset(set_tabular)): | ||
raise ValueError( | ||
"• Several file types were given; All input files, as well as the output file should be of the same " | ||
"type. " | ||
) | ||
|
||
|
||
def stack(output_file, input_files): | ||
_check_suffix(input_files, output_file) | ||
|
||
list_dfs = [load_data(item).df for item in input_files] | ||
df_stacked = pd.concat(list_dfs).reset_index(drop=True) | ||
as_stacked = ASReviewData(df=df_stacked) | ||
|
||
as_stacked.to_file(output_file) | ||
|
||
|
||
def _parse_arguments_stack(): | ||
parser = argparse.ArgumentParser(prog="ASReview dataset stacking") | ||
parser.add_argument("output_path", type=str, help="The output file path.") | ||
parser.add_argument( | ||
"datasets", type=str, nargs="+", help="Any number of datasets to stack." | ||
) | ||
|
||
return parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from pathlib import Path | ||
|
||
from asreview.data import ASReviewData | ||
from asreviewcontrib.datatools.stack import stack | ||
|
||
|
||
test_dir = Path(__file__).parent | ||
file_1 = Path(test_dir, "demo_data", "dataset_1.ris") | ||
file_2 = Path(test_dir, "demo_data", "dataset_2.ris") | ||
|
||
|
||
def test_stack(tmpdir): | ||
output_path = Path(tmpdir, "test_output.ris") | ||
stack(output_path, [file_1, file_2]) | ||
as_test = ASReviewData.from_file(output_path) | ||
|
||
assert len(as_test.df) == 14 | ||
assert as_test.df['included'].value_counts()[-1] == 9 | ||
assert as_test.df['included'].value_counts()[0] == 3 | ||
assert as_test.df['included'].value_counts()[1] == 2 |