コード例 #1
0
ファイル: test_utils.py プロジェクト: johnliu123/deid
    def test_write_read_files(self):
        '''test_write_read_files will test the functions 
           write_file and read_file
        '''
        print("Testing utils.write_file...")
        from deid.utils import write_file
        tmpfile = tempfile.mkstemp()[1]
        os.remove(tmpfile)
        write_file(tmpfile, "blaaahumbug")
        self.assertTrue(os.path.exists(tmpfile))

        print("Testing utils.read_file...")
        from deid.utils import read_file
        content = read_file(tmpfile)[0]
        self.assertEqual("blaaahumbug", content)

        from deid.utils import write_json
        print("Testing utils.write_json...")
        print("Case 1: Providing bad json")
        bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]}
        tmpfile = tempfile.mkstemp()[1]
        os.remove(tmpfile)
        with self.assertRaises(TypeError) as cm:
            write_json(bad_json, tmpfile)

        print("Case 2: Providing good json")
        good_json = {"Wakkawakkawakka": [True, "2", 3]}
        tmpfile = tempfile.mkstemp()[1]
        os.remove(tmpfile)
        write_json(good_json, tmpfile)
        content = json.load(open(tmpfile, 'r'))
        self.assertTrue(isinstance(content, dict))
        self.assertTrue("Wakkawakkawakka" in content)
コード例 #2
0
ファイル: utils.py プロジェクト: rjosest/deid
def load_deid(path=None):
    '''load_deid will return a loaded in (user) deid configuration file
    that can be used to update a default config.json. If a file path is
    specified, it is loaded directly. If a folder is specified, we look
    for a deid file in the folder. If nothing is specified, we assume
    the user wants to load a deid file in the present working directory.
    If the user wants to have multiple deid files in a directory, this
    can be done with an extension that specifies the module, eg;
   
             deid.dicom
             deid.nifti

    Parameters
    ==========
    path: a path to a deid file

    Returns
    =======
    config: a parsed deid (dictionary) with valid sections

    '''
    path = find_deid(path)

    # Read in spec, clean up extra spaces and newlines
    spec = [
        x.strip('\n').strip(' ') for x in read_file(path)
        if x.strip('\n').strip(' ') not in ['']
    ]

    spec = [x for x in spec if x not in ['', None]]
    config = OrderedDict()
    section = None

    while len(spec) > 0:

        # Clean up white trailing/leading space
        line = spec.pop(0).strip()

        # Comment
        if line.startswith("#"):
            continue

        # Starts with Format?
        elif bool(re.match('format', line, re.I)):
            fmt = re.sub('FORMAT|(\s+)', '', line).lower()
            if fmt not in formats:
                bot.error("%s is not a valid format." % fmt)
                sys.exit(1)
            # Set format
            config['format'] = fmt
            bot.debug("FORMAT set to %s" % fmt)

        # A new section?
        elif line.startswith('%'):

            # Remove any comments
            line = line.split('#', 1)[0].strip()

            # Is there a section name?
            section_name = None
            parts = line.split(' ')
            if len(parts) > 1:
                section_name = ' '.join(parts[1:])
            section = re.sub('[%]|(\s+)', '', parts[0]).lower()
            if section not in sections:
                bot.error("%s is not a valid section." % section)
                sys.exit(1)
            config = add_section(config=config,
                                 section=section,
                                 section_name=section_name)

        # An action (replace, blank, remove, keep, jitter)
        elif line.upper().startswith(actions):

            # Start of a filter group
            if line.upper().startswith('LABEL') and section == "filter":
                members = []
                keep_going = True
                while keep_going is True:
                    next_line = spec[0]
                    if next_line.upper().strip().startswith('LABEL'):
                        keep_going = False
                    elif next_line.upper().strip().startswith("%"):
                        keep_going = False
                    else:
                        new_member = spec.pop(0)
                        members.append(new_member)
                    if len(spec) == 0:
                        keep_going = False

                # Add the filter label to the config
                config = parse_label(config=config,
                                     section=section,
                                     label=line,
                                     section_name=section_name,
                                     members=members)
            # Parse the action
            else:
                config = parse_action(section=section,
                                      section_name=section_name,
                                      line=line,
                                      config=config)
        else:
            bot.debug("%s not recognized to be in valid format, skipping." %
                      line)
    return config
コード例 #3
0
def load_deid(path=None):
    """load_deid will return a loaded in (user) deid configuration file
       that can be used to update a default config.json. If a file path is
       specified, it is loaded directly. If a folder is specified, we look
       for a deid file in the folder. If nothing is specified, we assume
       the user wants to load a deid file in the present working directory.
       If the user wants to have multiple deid files in a directory, this
       can be done with an extension that specifies the module, eg;
   
             deid.dicom
             deid.nifti

       Parameters
       ==========
       path: a path to a deid file

       Returns
       =======
       config: a parsed deid (dictionary) with valid sections

    """
    path = find_deid(path)

    # Read in spec, clean up extra spaces and newlines
    spec = [
        x.strip("\n").strip(" ") for x in read_file(path)
        if x.strip("\n").strip(" ") not in [""]
    ]

    spec = [x for x in spec if x not in ["", None]]
    config = OrderedDict()
    section = None

    while spec:

        # Clean up white trailing/leading space
        line = spec.pop(0).strip()

        # Comment
        if line.startswith("#"):
            continue

        # Set format
        elif bool(re.match("^format", line, re.I)):
            config["format"] = parse_format(line)

        # A new section?
        elif line.startswith("%"):

            # Remove any comments
            line = line.split("#", 1)[0].strip()

            # Is there a section name?
            section_name = None
            parts = line.split(" ")
            if len(parts) > 1:
                section_name = " ".join(parts[1:])
            section = re.sub("[%]|(\s+)", "", parts[0]).lower()
            if section not in sections:
                bot.exit("%s is not a valid section." % section)

            config = add_section(config=config,
                                 section=section,
                                 section_name=section_name)

        # A %fields action (only field allowed), %values allows split
        elif line.upper().startswith(group_actions) and section in groups:
            config = parse_group_action(section=section,
                                        section_name=section_name,
                                        line=line,
                                        config=config)

        # An action (ADD, BLANK, JITTER, KEEP, REPLACE, REMOVE, LABEL)
        elif line.upper().startswith(actions):

            # Start of a filter group
            if line.upper().startswith("LABEL") and section == "filter":
                members = parse_filter_group(spec)

                # Add the filter label to the config
                config = parse_label(
                    config=config,
                    section=section,
                    label=line,
                    section_name=section_name,
                    members=members,
                )
            # Parse the action
            else:
                config = parse_config_action(section=section,
                                             section_name=section_name,
                                             line=line,
                                             config=config)
        else:
            bot.warning("%s not recognized to be in valid format, skipping." %
                        line)
    return config