Skip to content

Commit

Permalink
Better docstrings
Browse files Browse the repository at this point in the history
Better docstrings for all functions
  • Loading branch information
ReneSkukies committed Jun 3, 2024
1 parent f2e4608 commit 8c98442
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
Function to save unfold models in your BIDS root folder. Automatically creates a derivatives_subfolder (default = "Unfold") in the derivatives and subsequentely safes each model in results according to BIDS.
Example of path so saved file: bids_root/derivatives/Unfold/sub-XXX/eeg/sub-XXX_ses-XX_task-XXX_run-XX_unfold.jld2
If overwrite is false the function will check for already saved results in the same folder and with the same name i.e. same subject/session/task/run.
# Keywords
derivatives_subfolder (String::"Unfold"): Creates the named subfolder and saves Unfold models according to BIDS.
overwrite (Bool::false): Does not overwrite existing datasets; can be set to true.
"""
function save_results(results::DataFrame, bids_root::String;
derivatives_subfolder::String="Unfold",
Expand Down Expand Up @@ -64,8 +66,16 @@ end
task::Union{Nothing,AbstractString}=nothing,
run::Union{Nothing,AbstractString}=nothing)
Load Unfold models existing in a derivatives_subfolder in your BIDS root folder. If lazy is true (default) then data will not be loaded and a DataFrame containing all file paths + information will be returned (similar to bids_layout output).
generate_Xs reconstructs the designmatrix, can be set to false for faster loading times.
Load Unfold models existing in a derivatives_subfolder in your BIDS root folder.
# Keywords
derivatives_subfolder (String::"Unfold"): Defines in which subfolder of bids_root/derivatives to look for Unfold models.
lazy (Bool::false): Do not actually load the dataset into memore if true, only return a dataframe with paths
generate_Xs (Bool::true): Do not recreate the designmatrix; improves loading time.
ses (Union{Nothing,AbstractString}::nothing): Which session to load; loads all if nothing
task (Union{Nothing,AbstractString}::nothing): Which task to load; loads all if nothing
run (Union{Nothing,AbstractString}::nothing): Which run to load; loads all if nothing
"""
function load_results(bids_root::String;
derivatives_subfolder::String="Unfold",
Expand Down Expand Up @@ -123,7 +133,7 @@ end
"""
_load_results(files_df; generate_Xs::Bool = true)
Internal functino to load Unfold models into memory. Can also be used to load data after file information was loaded lazily (lazy=true) using load_results()
Internal function to load Unfold models into memory. Can also be used to load data after file information was loaded lazily (lazy=true) using load_results()
"""
function _load_results(files_df, generate_Xs::Bool = true)

Expand Down
42 changes: 41 additions & 1 deletion src/load.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
"""
bids_layout(bidsPath::AbstractString;
derivatives::Bool=true,
specificFolder::Union{Nothing,AbstractString}=nothing,
excludeFolder::Union{Nothing,AbstractString}=nothing,
ses::Union{Nothing,AbstractString}=nothing,
task::Union{Nothing,AbstractString}=nothing,
run::Union{Nothing,AbstractString}=nothing)
Main function to load paths of all subjects in one bids_root folder. Will return a DataFrame containing all found paths with specific subject information. Used before loading data into memore using @Ref(`load_bids_eeg_data`)
# Keywords
derivatives (Bool::true): Look for data in the derivatives folder
specificFolder (Union{Nothing,AbstractString}::nothing): Specify a specific folder name in either derivatives or bids_root to look for data.
excludeFolder (Union{Nothing,AbstractString}::nothing): Exclude a specific folder from data detection.
ses (Union{Nothing,AbstractString}::nothing): Which session to load; loads all if nothing
task (Union{Nothing,AbstractString}::nothing): Which task to load; loads all if nothing
run (Union{Nothing,AbstractString}::nothing): Which run to load; loads all if nothing
"""

function bids_layout(bidsPath::AbstractString;
derivatives::Bool=true,
specificFolder::Union{Nothing,AbstractString}=nothing,
Expand Down Expand Up @@ -69,7 +89,12 @@ function bids_layout(bidsPath::AbstractString;
return files_df
end

#
"""
get_info!(files_df, file)
Internal function to get subject information from dataframe.
"""

# get subject and file information
function get_info!(files_df, file)

Expand Down Expand Up @@ -114,6 +139,15 @@ function check_df(files_df, ses, task, run)
end
#-----------------------------------------------------------------------------------------------
# Function loading BIDS data given bidsLayout DataFrame
"""
load_bids_eeg_data(layout_df; verbose::Bool=true, kwargs...)
Load data found with @Ref('bids_layout') into memory.
verbose (Bool::true): Show ProgressBar
kwargs... : kwargs for CSV.read to load events from .tsv file; e.g. to specify delimeter
"""

function load_bids_eeg_data(layout_df; verbose::Bool=true, kwargs...)

# Initialize an empty dataframe
Expand Down Expand Up @@ -220,6 +254,12 @@ end

# Function to find and load all events files into a DataFrame

"""
load_events(layoutDF::DataFrame; kwargs...)
Internal function to load events based on paths in the layout Df
"""

function load_events(layoutDF::DataFrame; kwargs...)

all_events = DataFrame()
Expand Down
35 changes: 28 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
"""
run_unfold(dataDF, bfDict; eventcolumn="event",removeTimeexpandedXs=true, extract_data = raw_to_data, verbose::Bool=true, kwargs...)
- removeTimeexpandedXs (true): Removes the timeexpanded designmatrix which significantly reduces the memory-consumption. This Xs is rarely needed, but can be recovered (look into the Unfold.load function)
extractData (function) - specify the function that translate the MNE Raw object to an data array. Default is `rawToData` which uses get_data and allows to pick `channels` - see @Ref(`raw_to_data`). The optional kw- arguments (e.g. channels) need to be specified directly in the `run_unfold` function as kw-args
run_unfold(dataDF, bfDict;
eventcolumn="event",
removeTimeexpandedXs=true,
extract_data = raw_to_data,
verbose::Bool=true,
kwargs...)
Run Unfold analysis on all data in dataDF.
# Keywords
eventcolumn (String::"event"): Which collumn Unfold should use during the analysis.
removeTimeexpandedXs (Bool::true): Removes the timeexpanded designmatrix which significantly reduces the memory-consumption. This Xs is rarely needed, but can be recovered (look into the Unfold.load function)
extractData (functionraw_to_data): Specify the function that translate the MNE Raw object to an data array. Default is `rawToData` which uses get_data and allows to pick `channels` - see @Ref(`raw_to_data`). The optional kw- arguments (e.g. channels) need to be specified directly in the `run_unfold` function as kw-args
verbose (Bool::true): Show ProgressBar or not.
"""

function run_unfold(dataDF, bfDict; eventcolumn="event",removeTimeexpandedXs=true, extract_data = raw_to_data, verbose::Bool=true, kwargs...)
Expand Down Expand Up @@ -40,6 +48,13 @@ function run_unfold(dataDF, bfDict; eventcolumn="event",removeTimeexpandedXs=tru
return resultsDF
end

"""
raw_to_data(raw; channels::AbstractVector{<:Union{String,Integer}}=[])
Function to get data from MNE raw object. Can choose specific channels; default loads all channels.
"""

# Function to run Preprocessing functions on data
function raw_to_data(raw; channels::AbstractVector{<:Union{String,Integer}}=[])
return pyconvert(Array, raw.get_data(picks=pylist(channels), units="uV"))
Expand Down Expand Up @@ -69,6 +84,12 @@ function unpack_events(df::DataFrame)
return all_events
end

"""
bids_coeftable(model_df)
Turns all models found in model_df into tydy DataFrames and aggregates them in a new DataFrame.
"""

function bids_coeftable(model_df)

all_results = DataFrame()
Expand Down Expand Up @@ -116,7 +137,7 @@ end
"""
list_all_paths(path)
Internal functino to find pathfiles
Internal function to find pathfiles
"""
list_all_paths(path, file_ending, file_pattern; exclude=nothing) = @cont begin
if isfile(path)
Expand Down

0 comments on commit 8c98442

Please sign in to comment.