<commit_before>'''Export of FSL results into NI-DM

@author: <NAME> <<EMAIL>>
@copyright: University of Warwick 2013-2014
'''

from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import re
from prov.model import ProvBundle, ProvRecord, ProvExceptionCannotUnifyAttribute, ProvEntity
# import prov.model.graph
import os
import numpy as np
import nibabel as nib
from NIDMStat import NIDMStat


''' Parse an FSL result directory to extract the pieces information stored in NI-DM (for statistical results)
'''
class FSL_NIDM():

    def __init__(self, *args, **kwargs):
        self.feat_dir = kwargs.pop('feat_dir')
        self.export_dir = os.path.join(self.feat_dir, 'nidm')
        
        self.design_file = os.path.join(self.feat_dir, 'design.fsf');
        self.find_reference_space();

        self.nidm = NIDMStat(export_dir=self.export_dir, standard_space=self.standard_space, custom_standard=self.custom_standard);

        
        self.parse_feat_dir()

    # Main function: parse a feat directory and build the corresponding NI-DM graph
    def parse_feat_dir(self):
        self.add_report_file(os.path.join(self.feat_dir, 'report_poststats.html'))
        self.add_model_fitting()
        self.maskFile = os.path.join(self.feat_dir, 'mask.nii.gz')
        self.add_search_space()

        # Find parameter estimates
        for file in os.listdir(os.path.join(self.feat_dir, 'stats')):
            if file.startswith("pe"):
                if file.endswith(".nii.gz"):
                    s = re.compile('pe\d+')
                    penum = s.search(file)
                    penum = penum.group()
                    penum = penum.replace('pe', '')
                    self.add_parameter_estimate(os.path.join(self.feat_dir, 'stats', file), penum)

        # Find excursion sets (in a given feat directory we have one excursion set per contrast)
        for file in os.listdir(self.feat_dir):
            if file.startswith("thresh_zstat"):
                if file.endswith(".nii.gz"):
                    s = re.compile('zstat\d+')
                    zstatnum = s.search(file)
                    zstatnum = zstatnum.group()
                    statnum = zstatnum.replace('zstat', '')
                    self.add_contrast(statnum)
                    self.add_clusters_peaks(statnum)        

    # Add model fitting, residuals map
    def add_model_fitting(self):
        residuals_file = os.path.join(self.feat_dir, 'stats', 'sigmasquareds.nii.gz')
        # FIXME: Check if there is an alternative file to use here
        if not os.path.isfile(residuals_file):
            residuals_file = None;

        gand_mean_file = os.path.join(self.feat_dir, 'stats', 'mean_func.nii.gz')
        # FIXME: Check if there is an alternative file to use here
        if not os.path.isfile(gand_mean_file):
            gand_mean_file = None;

        design_matrix_file = open(os.path.join(self.feat_dir, 'design.mat'), 'r')
        design_matrix = np.loadtxt(design_matrix_file, skiprows=5, ndmin=2)

        self.nidm.create_model_fitting(residuals_file, gand_mean_file, design_matrix)

    # For a parameter estimate, create the parameter estimate map emtity
    def add_parameter_estimate(self, pe_file, pe_num):
        self.nidm.create_parameter_estimate(pe_file, pe_num)

    # Find reference space 
    def find_reference_space(self):
        designFile = open(self.design_file, 'r')
        designTxt = designFile.read()
        standard_space_search = re.compile(r'.*set fmri\(regstandard_yn\) (?P<isStandard>[\d]+).*')<commit_msg>fix: grand mean file is not in stats directory<commit_after>'''Export of FSL results into NI-DM

@author: <NAME> <<EMAIL>>
@copyright: University of Warwick 2013-2014
'''

from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import re
from prov.model import ProvBundle, ProvRecord, ProvExceptionCannotUnifyAttribute, ProvEntity
# import prov.model.graph
import os
import numpy as np
import nibabel as nib
from NIDMStat import NIDMStat


''' Parse an FSL result directory to extract the pieces information stored in NI-DM (for statistical results)
'''
class FSL_NIDM():

    def __init__(self, *args, **kwargs):
        self.feat_dir = kwargs.pop('feat_dir')
        self.export_dir = os.path.join(self.feat_dir, 'nidm')
        
        self.design_file = os.path.join(self.feat_dir, 'design.fsf');
        self.find_reference_space();

        self.nidm = NIDMStat(export_dir=self.export_dir, standard_space=self.standard_space, custom_standard=self.custom_standard);

        
        self.parse_feat_dir()

    # Main function: parse a feat directory and build the corresponding NI-DM graph
    def parse_feat_dir(self):
        self.add_report_file(os.path.join(self.feat_dir, 'report_poststats.html'))
        self.add_model_fitting()
        self.maskFile = os.path.join(self.feat_dir, 'mask.nii.gz')
        self.add_search_space()

        # Find parameter estimates
        for file in os.listdir(os.path.join(self.feat_dir, 'stats')):
            if file.startswith("pe"):
                if file.endswith(".nii.gz"):
                    s = re.compile('pe\d+')
                    penum = s.search(file)
                    penum = penum.group()
                    penum = penum.replace('pe', '')
                    self.add_parameter_estimate(os.path.join(self.feat_dir, 'stats', file), penum)

        # Find excursion sets (in a given feat directory we have one excursion set per contrast)
        for file in os.listdir(self.feat_dir):
            if file.startswith("thresh_zstat"):
                if file.endswith(".nii.gz"):
                    s = re.compile('zstat\d+')
                    zstatnum = s.search(file)
                    zstatnum = zstatnum.group()
                    statnum = zstatnum.replace('zstat', '')
                    self.add_contrast(statnum)
                    self.add_clusters_peaks(statnum)        

    # Add model fitting, residuals map
    def add_model_fitting(self):
        residuals_file = os.path.join(self.feat_dir, 'stats', 'sigmasquareds.nii.gz')
        # FIXME: Check if there is an alternative file to use here
        if not os.path.isfile(residuals_file):
            residuals_file = None;

        grand_mean_file = os.path.join(self.feat_dir, 'mean_func.nii.gz')

        # FIXME: Check if there is an alternative file to use here
        if not os.path.isfile(grand_mean_file):
            grand_mean_file = None;

        design_matrix_file = open(os.path.join(self.feat_dir, 'design.mat'), 'r')
        design_matrix = np.loadtxt(design_matrix_file, skiprows=5, ndmin=2)

        self.nidm.create_model_fitting(residuals_file, grand_mean_file, design_matrix)

    # For a parameter estimate, create the parameter estimate map emtity
    def add_parameter_estimate(self, pe_file, pe_num):
        self.nidm.create_parameter_estimate(pe_file, pe_num)

    # Find reference space 
    def find_reference_space(self):
        designFile = open(self.design_file, 'r')
        designTxt = designFile.read()
        standard_space_search = re.compile(r'.*set fmri\(regstandard_yn\) (?P<isStandard>[\d]+).*')
{'fraction_non_alphanumeric': 0.10595287885124774, 'fraction_numerical': 0.003067057019378224, 'mean_word_length': 2.65461029037188, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1815398', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2200, 'n_words': 504}