def _load_owl(self, owl_file):
        if owl_file in self.owl_readers:
            self.owl = self.owl_readers[owl_file]
        else:
            # Retreive owl file for NIDM-Results
            # owl_file = os.path.join(RELPATH, 'terms', 'nidm-results.owl')

            # check the file exists
            assert os.path.exists(owl_file)
            # Read owl (turtle) file

            owl_path = os.path.dirname(owl_file)

            if not "extension" in os.path.dirname(owl_file):
                import_files = glob.glob(os.path.join(owl_path, \
                    os.pardir, os.pardir, "imports", '*.ttl'))
            else:
                import_files = glob.glob(os.path.join(owl_path, \
                    os.pardir, os.pardir, os.pardir, os.pardir, "imports", '*.ttl'))
                # Main ontology file
                import_files += glob.glob(os.path.join(owl_path, \
                    os.pardir, os.pardir, os.pardir, "terms", '*.owl'))

            self.owl = OwlReader(owl_file, import_files)
            self.owl_readers[owl_file] = self.owl
Example #2
0
    def __init__(self,
                 owl_file,
                 import_files,
                 spec_name,
                 subcomponents=None,
                 used_by=None,
                 generated_by=None,
                 derived_from=None,
                 attributed_to=None,
                 prefix=None,
                 commentable=False,
                 intro=None):
        self.owl = OwlReader(owl_file, import_files)
        self.owl.graph.bind('dct', 'http://purl.org/dc/terms/')
        self.owl.graph.bind('dicom', 'http://purl.org/nidash/dicom#')
        self.owl.graph.bind('nidm', 'http://purl.org/nidash/nidm#')
        self.owl.graph.bind('bids', 'http://purl.org/nidash/bids#')
        self.owl.graph.bind(
            'onli',
            'http://neurolog.unice.fr/ontoneurolog/v3.0/instrument.owl#')
        self.owl.graph.bind('pato', 'http://purl.obolibrary.org/obo/pato#')
        self.owl.graph.bind('prov', 'http://www.w3.org/ns/prov')
        self.owl.graph.bind(
            'qibo', 'http://www.owl-ontologies.com/Ontology1298855822.owl')
        self.owl.graph.bind('sio', 'http://semanticscience.org/resource/')
        self.name = spec_name
        self.component = self.name.lower().replace("-", "_")
        self.section_open = 0
        self.already_defined_classes = list()
        self.commentable = commentable

        self.attributes_done = set()
        self.text = ""
        self.create_specification(subcomponents, used_by, generated_by,
                                  derived_from, attributed_to, prefix, intro)
Example #3
0
    def setUp(self):
        self.my_execption = ""

        owl_file = os.path.join(os.path.dirname(
            os.path.dirname(
                os.path.realpath(__file__))),
                'nidmresults', 'owl',
                'nidm-results_130.owl')
        self.owl = OwlReader(owl_file)

        pwd = os.path.dirname(
            os.path.abspath(inspect.getfile(inspect.currentframe())))

        # Store test data in a 'data' folder until 'test'
        data_dir = os.path.join(pwd, 'data')

        if not os.path.exists(data_dir):
            os.makedirs(data_dir)

        # Collection containing examples of NIDM-Results packs (1.3.0)
        req = Request(
            "http://neurovault.org/api/collections/2210/nidm_results")
        rep = urlopen(req)

        response = rep.read()
        data = json.loads(response.decode('utf-8'))

        # Download the NIDM-Results packs from NeuroVault if not available
        # locally
        self.packs = list()
        for nidm_res in data["results"]:
            url = nidm_res["zip_file"]
            study = nidm_res["name"]

            nidmpack = os.path.join(data_dir, study + ".zip")
            if not os.path.isfile(nidmpack):
                f = urlopen(url)
                print("downloading " + url + " at " + nidmpack)
                with open(nidmpack, "wb") as local_file:
                    local_file.write(f.read())
            self.packs.append(nidmpack)

        self.packs = glob.glob(os.path.join(data_dir, '*.nidm.zip'))
        self.out_dir = os.path.join(data_dir, 'recomputed')

        if os.path.isdir(self.out_dir):
            shutil.rmtree(self.out_dir)

        os.mkdir(self.out_dir)
Example #4
0
    def __init__(self, owl_file, import_files, spec_name, subcomponents=None,
                 used_by=None, generated_by=None, derived_from=None,
                 attributed_to=None, prefix=None, commentable=False,
                 intro=None):
        self.owl = OwlReader(owl_file, import_files)
        self.owl.graph.bind('nidm', 'http://purl.org/nidash/nidm#')
        self.name = spec_name
        self.component = self.name.lower().replace("-", "_")
        self.section_open = 0
        self.already_defined_classes = list()
        self.commentable = commentable

        self.attributes_done = set()
        self.text = ""
        self.create_specification(subcomponents, used_by, generated_by,
                                  derived_from, attributed_to, prefix, intro)
    def __init__(self,
                 nidm_classes,
                 example_file,
                 one_file_per_class=False,
                 owl_file=None,
                 remove_att=None):
        self.nidm_classes = nidm_classes
        self.one_file_per_class = one_file_per_class
        self.remove_att = remove_att

        self.owl = None
        if owl_file is None:
            import_files = glob.glob(os.path.join(NIDMPATH, "imports",
                                                  '*.ttl'))
            owl_file = os.path.join(NIDM_TERMS_DIR, 'nidm-results.owl')

        self.owl = OwlReader(owl_file, import_files)

        if not one_file_per_class:
            self.file = example_file
        else:
            self.dir = example_file
Example #6
0
    def __init__(self, name, ttl_file, gt_ttl_files, exact_comparison,
                 version):
        self.name = name
        self.ttl_file = ttl_file

        self.gt_ttl_files = gt_ttl_files
        self.exact_comparison = exact_comparison
        self.graph = Graph()

        print(ttl_file)

        self.graph.parse(ttl_file, format='turtle')

        # Get NIDM-Results version for each example
        self.version = version

        if self.version != "dev":
            self.gt_ttl_files = [
                x.replace(os.path.join("nidm", "nidm"),
                          os.path.join("nidm_releases", self.version, "nidm"))
                for x in self.gt_ttl_files
            ]

        # Owl file corresponding to version
        owl_file = os.path.join(
            os.path.dirname(os.path.dirname(__file__)), 'owl',
            "nidm-results_" + version.replace(".", "") + ".owl")

        self.owl_file = owl_file

        owl_imports = None
        if self.version == "dev":
            owl_imports = glob.glob(
                os.path.join(os.path.dirname(owl_file), os.pardir, os.pardir,
                             "imports", '*.ttl'))
        self.owl = OwlReader(self.owl_file, owl_imports)
Example #7
0
 def __init__(self, owl_file):
     self.owl = OwlReader(owl_file)
def main(sid, aid, owl_file, template_files, script_files, constants_file):
    owl_txt = get_file_text(owl_file)

    templates_txt = dict()
    for template_file in template_files:
        templates_txt[template_file] = get_file_text(template_file)

    scripts_txt = dict()
    for script_file in script_files:
        scripts_txt[script_file] = get_file_text(script_file)

    cst_txt = get_file_text(constants_file)

    sid_name = sid.split(":")[1]
    sid_namespace = sid.split(":")[0]
    if sid_namespace == "nidm":
        uri = NIDM[sid_name]
        pref = "NIDM"
    elif sid_namespace == "fsl":
        uri = FSL[sid_name]
        pref = "FSL"
    elif sid_namespace == "spm":
        uri = SPM[sid_name]
        pref = "SPM"

    # If alphanumeric identifier was not defined, find the next available
    if aid is None:
        before_alnum = sid_namespace + ":" + pref + "_"

        # Find all alphanumeric identifiers in the owl file
        alphanum_ids = set(re.findall("(" + before_alnum + '\d+)\s+', owl_txt))

        # Get identifier number for next alphanumeric identifier
        last_id = sorted(list(alphanum_ids))[-1]
        new_id_num = int(last_id.replace(before_alnum, "")) + 1

        aid = before_alnum + "{0:0>7}".format(new_id_num)

    owl = OwlReader

    owl = OwlReader(owl_file)
    label = owl.get_label(uri).split(":")[1].replace("'", "")

    # Replace all occurences of semantic id
    owl_txt = owl_txt.replace(sid + " ", aid + " ")
    # Replace ids in templates
    for tpl, tpl_txt in templates_txt.items():
        templates_txt[tpl] = tpl_txt.replace(sid + " ", aid + " ")
    for scr, scr_txt in scripts_txt.items():
        scripts_txt[scr] = scr_txt.replace('"' + sid + '"', '"' + aid + '"')

    new_constant = pref + "_" + \
        label.upper().replace(" ", "_").replace("-", "_") + \
        " = " + pref + "['"+aid.replace(sid_namespace + ":", "")+"']"
    cst_txt = cst_txt.replace("# NIDM constants",
                              "# NIDM constants\n" + new_constant)

    replace_file_txt(owl_file, owl_txt)
    replace_file_txt(constants_file, cst_txt)
    for tpl, tpl_txt in templates_txt.items():
        replace_file_txt(tpl, tpl_txt)
    for scr, scr_txt in scripts_txt.items():
        replace_file_txt(scr, scr_txt)