diff --git a/.gitignore b/.gitignore index e02d976..24181f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ ._* tmp/ /.ipynb_checkpoints/ +*.pyc \ No newline at end of file diff --git a/.gitignore b/.gitignore index e02d976..24181f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ ._* tmp/ /.ipynb_checkpoints/ +*.pyc \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..53a2bd1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "cwd": "${workspaceFolder}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index e02d976..24181f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ ._* tmp/ /.ipynb_checkpoints/ +*.pyc \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..53a2bd1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "cwd": "${workspaceFolder}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 002fe32..5fbc413 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "python.pythonPath": "/Users/morteza/miniconda3/bin/python" + "python.pythonPath": "/Users/morteza/miniconda3/bin/python", + "python.condaPath": "~/miniconda3/bin/conda" } \ No newline at end of file diff --git a/.gitignore b/.gitignore index e02d976..24181f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ ._* tmp/ /.ipynb_checkpoints/ +*.pyc \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..53a2bd1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "cwd": "${workspaceFolder}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 002fe32..5fbc413 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "python.pythonPath": "/Users/morteza/miniconda3/bin/python" + "python.pythonPath": "/Users/morteza/miniconda3/bin/python", + "python.condaPath": "~/miniconda3/bin/conda" } \ No newline at end of file diff --git a/behaverse/preproc/NBackPreprocessingPipeline.py b/behaverse/preproc/NBackPreprocessingPipeline.py new file mode 100644 index 0000000..6cb2d0a --- /dev/null +++ b/behaverse/preproc/NBackPreprocessingPipeline.py @@ -0,0 +1,27 @@ +import pandas as pd +from pandas.io.json import json_normalize +import json + +import nback, generic + +class NBackPreprocessingPipeline(): + def __init__(self, json_path): + self.json_path = json_path + super().__init__() + + def run(self): + with open(self.json_path) as f: + js_data = json.load(f) + data = json_normalize(js_data,record_prefix=False,max_level=1) + + data = data.pipe(nback.extract_events). \ + pipe(generic.filter_instructions) + return data + + def __repr__(self): + return "Sample JSON" + +if __name__ == "__main__": + + json_file = "data/samples/nback_demo_v.json" + print(NBackPreprocessingPipeline(json_file).run()) \ No newline at end of file diff --git a/.gitignore b/.gitignore index e02d976..24181f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ ._* tmp/ /.ipynb_checkpoints/ +*.pyc \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..53a2bd1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "cwd": "${workspaceFolder}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 002fe32..5fbc413 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "python.pythonPath": "/Users/morteza/miniconda3/bin/python" + "python.pythonPath": "/Users/morteza/miniconda3/bin/python", + "python.condaPath": "~/miniconda3/bin/conda" } \ No newline at end of file diff --git a/behaverse/preproc/NBackPreprocessingPipeline.py b/behaverse/preproc/NBackPreprocessingPipeline.py new file mode 100644 index 0000000..6cb2d0a --- /dev/null +++ b/behaverse/preproc/NBackPreprocessingPipeline.py @@ -0,0 +1,27 @@ +import pandas as pd +from pandas.io.json import json_normalize +import json + +import nback, generic + +class NBackPreprocessingPipeline(): + def __init__(self, json_path): + self.json_path = json_path + super().__init__() + + def run(self): + with open(self.json_path) as f: + js_data = json.load(f) + data = json_normalize(js_data,record_prefix=False,max_level=1) + + data = data.pipe(nback.extract_events). \ + pipe(generic.filter_instructions) + return data + + def __repr__(self): + return "Sample JSON" + +if __name__ == "__main__": + + json_file = "data/samples/nback_demo_v.json" + print(NBackPreprocessingPipeline(json_file).run()) \ No newline at end of file diff --git a/behaverse/preproc/generic.py b/behaverse/preproc/generic.py new file mode 100644 index 0000000..7dd44c0 --- /dev/null +++ b/behaverse/preproc/generic.py @@ -0,0 +1,6 @@ +import pandas as pd + + +def filter_instructions(df): + f = (df['custom.type'] != 'Instructions') + return df[f] diff --git a/.gitignore b/.gitignore index e02d976..24181f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ ._* tmp/ /.ipynb_checkpoints/ +*.pyc \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..53a2bd1 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "cwd": "${workspaceFolder}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 002fe32..5fbc413 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "python.pythonPath": "/Users/morteza/miniconda3/bin/python" + "python.pythonPath": "/Users/morteza/miniconda3/bin/python", + "python.condaPath": "~/miniconda3/bin/conda" } \ No newline at end of file diff --git a/behaverse/preproc/NBackPreprocessingPipeline.py b/behaverse/preproc/NBackPreprocessingPipeline.py new file mode 100644 index 0000000..6cb2d0a --- /dev/null +++ b/behaverse/preproc/NBackPreprocessingPipeline.py @@ -0,0 +1,27 @@ +import pandas as pd +from pandas.io.json import json_normalize +import json + +import nback, generic + +class NBackPreprocessingPipeline(): + def __init__(self, json_path): + self.json_path = json_path + super().__init__() + + def run(self): + with open(self.json_path) as f: + js_data = json.load(f) + data = json_normalize(js_data,record_prefix=False,max_level=1) + + data = data.pipe(nback.extract_events). \ + pipe(generic.filter_instructions) + return data + + def __repr__(self): + return "Sample JSON" + +if __name__ == "__main__": + + json_file = "data/samples/nback_demo_v.json" + print(NBackPreprocessingPipeline(json_file).run()) \ No newline at end of file diff --git a/behaverse/preproc/generic.py b/behaverse/preproc/generic.py new file mode 100644 index 0000000..7dd44c0 --- /dev/null +++ b/behaverse/preproc/generic.py @@ -0,0 +1,6 @@ +import pandas as pd + + +def filter_instructions(df): + f = (df['custom.type'] != 'Instructions') + return df[f] diff --git a/behaverse/preproc/nback.py b/behaverse/preproc/nback.py new file mode 100644 index 0000000..f84a473 --- /dev/null +++ b/behaverse/preproc/nback.py @@ -0,0 +1,21 @@ +""" +This module includes pipeline elements and processors for the preprocessing of the n-back task. +""" + +import re +import pandas as pd + +def extract_events(df, drop_old_cols=False): + """ + Pipeline element to extract N-back events from a list of Behaverse json events. + """ + pattern = re.compile("N[Bb]ack\..*") + + def find_head_event(lst): + evts = [*map(lambda x: x.split('.',1)[1], filter(pattern.match, lst))] + return evts[0] + + df['nback_event_type'] = df.types.apply(lambda t: find_head_event(t)) + if drop_old_cols: df = df.drop(['types'],axis=1) + return df +