示例#1
0
def main(argv):

    #create a command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)
    
    #define command line arguments
    #
    parser.add_argument("args", type = str, nargs='*')
    parser.add_argument("-rdir", "-r", type = str)
    parser.add_argument("-odir", "-o", type = str)
    parser.add_argument("-ext", "-e", type = str)
    parser.add_argument("-help", action="help")

    #parse the command line
    #
    args = parser.parse_args()

     # check if the proper number of lists has been provided
    #
    if len(args.args) != NUM_ARGS:
        parser.print_usage()
        exit(-1)

    # set the replace file
    #
    if args.rdir is not None:
        rdir_a = args.rdir

    # set the output file
    #
    if args.odir is not None:
        odir_a = args.odir
    
    #set the input file lists names
    #
    rec_list_name = args.args[0]

    #load the lists
    #
    rec_list = nft.get_flist(rec_list_name)

    # create the output directory
    #
    if nft.make_dir(odir_a) == False:
        print  "%s: error creating output directory (%s)" \
            % (sys.argv[0], odir)
        exit (-1)

    #loop through the files
    #
    for rec_file in rec_list:

        # convert rec file name to full path
        #
        rec_file = os.path.abspath(os.path.realpath(
            os.path.expanduser(rec_file)))

        # set the ofile extension
        #
        if args.ext is not None:
            ext = args.ext

        else:
            if (rec_file.endswith("t")):
                ext = "rect_bi"
            else:
                ext = "rec_bi"

        # get name of the output file and make it
        #
        ofile = nft.make_ofile(rec_file, ext, odir_a, rdir_a)
        nft.make_dir(os.path.abspath(ofile).strip(os.path.basename(ofile)))
        
        # read the rec filepyth
        #
        lines = [line.rstrip('\n') for line in open(rec_file)]
        
        # list to store events
        #
        events = []
        
        # loop over lines in the rec file
        #
        for line in lines:
        
            #remove spaces and newline chars, split the line
            #
            line = line.replace(nft.DELIM_SPACE, nft.DELIM_NULL)                                    .replace(nft.DELIM_NEWLINE, nft.DELIM_NULL)
            parts = line.split(nft.DELIM_COMMA)

            # append lines to events
            #
            events.append([int(parts[0]), float(parts[1]),                                              float(parts[2]), int(parts[3])])

        # end for

        # list to store converted events
        #
        bi_events = []
        
        # loop through all annotations
        #
        for event in events:

            # check if the label is a seizure label
            #
            label =  event[3]
            
            if label >= 8 and label <= 18:

                # convert to bi_class and add to bi_events
                #
                bi_label = 7
                bi_events.append([event[0],event[1],event[2],bi_label])
                
            # if this is not a seizure, just copy the event to bi_events
            #
            else:
                
                bi_events.append(event)

        # overwrite the rec file with the non bckg array
        #
        replace_file(bi_events,ofile)

    #end for (file)
    # exit
    #
    return True
示例#2
0
def main(argv):

    #create a command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)
    
    #define command line arguments
    #
    parser.add_argument("args", type = str, nargs='*')
    parser.add_argument("-odir", "-o", type = str)
    parser.add_argument("-rdir", "-r", type = str)
    parser.add_argument("-mfile", "-m", type = str)
    parser.add_argument("-ext", "-e", type = str)
    parser.add_argument("-help", action="help")

    #parse the command line
    #
    args = parser.parse_args()

    # check if the proper number of lists has been provided
    #
    if len(args.args) != NUM_ARGS:
        parser.print_usage()
        exit(-1)

    # set the output directory
    #
    if args.odir is not None:
        odir = args.odir

    # set the replace directory
    #
    if args.rdir is not None:
        rdir = args.rdir

    # set the map
    #
    if args.mfile is not None:
        mfile = args.mfile
    
    #set the input file lists names
    #
    rec_list_name = args.args[0]

    #load the lists
    #
    rec_list = nft.get_flist(rec_list_name)

    # create the output directory
    #
    nft.make_dir(odir)

    # load the map
    #
    tmpmap = nft.load_parameters(mfile, "REC_MAP")
    lmap = nft.generate_map(tmpmap)
    
    #loop through the files
    #
    for rec_file in rec_list:

        # convert rec_file name to absolute path
        #
        abs_rec_file = os.path.abspath(os.path.realpath(
            os.path.expanduser(rec_file)))
        
        if DSK_RAID_STR in abs_rec_file:
            rec_file = abs_rec_file.split(DSK_RAID_STR)[1]

        # set the ofile extension
        #
        if args.ext is not None:
            ext = args.ext

        else:
            ext = "tse"
        
        # read the rec file
        #
        lines = [line.rstrip('\n') for line in open(rec_file)]
        
        # list to store events
        #
        events = []
        
        # loop over lines in the rec file
        #
        for line in lines:
        
            #remove spaces and newline chars, split the line
            #
            line = line.replace(nft.DELIM_SPACE, nft.DELIM_NULL)                                    .replace(nft.DELIM_NEWLINE, nft.DELIM_NULL)
            parts = line.split(nft.DELIM_COMMA)

            # append lines to events
            #
            events.append([int(parts[0]), float(parts[1]),                                              float(parts[2]), parts[3]])

    
        #find out how many channels there should be based on montage
        #
        file_dirs=rec_file.split("/")
        montage=file_dirs[len(file_dirs)-5]
        
        if montage == ("01_tcp_ar" or "02_tcp_le"):
            chan_num = 22
        elif montage == ("03_tcp_ar_a" or "04_tcp_le_a"):
            chan_num = 20

        #loop through each channel and associated annotations
        #
        for channel in range(0,chan_num):
            
            #list to store events for this channel
            #
            chan_events = []

            # make tse file that these events will be stored in
            #
            rec_filename = os.path.basename(rec_file).split(".")[0] +\
                           "_ch" + format(channel, '03') + ".rec"
            rec_dirname = os.path.dirname(rec_file)

            chan_file = os.path.join(rec_dirname, rec_filename)

            ofile = nft.make_ofile(chan_file, ext, odir, rdir)

            nft.make_dir(os.path.abspath(ofile).strip(os.path.basename(ofile)))

            #loop through events
            #
            for event in events:
                
                ann_chan = event[0]

                if ann_chan == channel:

                    start = event[1]
                    stop = event[2]
                    key = event[3]
                   
                    # translate numerical label to string label
                    #
                    label = str(lmap.get(key))[2:-2]
                    
                    # add event to chan_events
                    #
                    chan_events.append([start, stop, label, "1.0000"])
                    
            # write chan_events to ofile, if there are any
            #
            if len(chan_events) != 0:
                
                gen_file(chan_events,ofile)

    # exit
    #
    return True
示例#3
0
def main(argv):

    # create a command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)

    # define command line arguments
    #
    parser.add_argument("args", type = str, nargs='*')
    parser.add_argument("-rdir", "-r", type = str)
    parser.add_argument("-odir", "-o", type = str)
    parser.add_argument("-ext", "-e", type = str)
    parser.add_argument("-help", action="help")

    # parse the command line
    #
    args = parser.parse_args()

    # check if the proper number of lists has been provided
    #
    if len(args.args) != NUM_ARGS:
        parser.print_usage()
        exit(-1)

    # set the replace file
    #
    if args.rdir is not None:
        rdir_a = args.rdir

    # set the output file
    #
    if args.odir is not None:
        odir_a = args.odir

    # set the input file lists names
    #
    rec_list_name = args.args[0]

    # load the list of rec files
    #
    rec_list = nft.get_flist(rec_list_name)

    if (rec_list == None):
        print  "%s (%s: %s): error loading input list (%s)" % \
            (sys.argv[0], __name__, "main", rec_list_name)
        exit (-1)

    # create the output directory
    #
    if nft.make_dir(odir_a) == False:
        print  "%s: error creating output directory (%s)" \
            % (sys.argv[0], odir)
        exit (-1)

    # loop through the files

    for ifile in rec_list:

        # convert rec file name to full path
        #
        ifile = os.path.abspath(os.path.realpath(
            os.path.expanduser(ifile)))

        # get name of the output file and make it
        #
        if args.ext is not None:
            ext = args.ext
            
        else:

            if (ifile.endswith("bi")):
                ext = "rect_bi"
            else:
                ext = "rect"

        ofile = nft.make_ofile(ifile, ext, odir_a, rdir_a)
        nft.make_dir(os.path.abspath(ofile).strip(os.path.basename(ofile)))

        # find out how many channels there should be based on montage
        #
        file_dirs=ifile.split("/")
        montage=file_dirs[len(file_dirs)-5]

        if montage == ("01_tcp_ar" or "02_tcp_le"):
            chan_num = 22
        elif montage == ("03_tcp_ar_a" or "04_tcp_le_a"):
            chan_num = 20

        # list to store annotations
        #
        segments = {}
        
        # open file
        #
        with open(ifile, 'r') as fp:

            # iterate over labels in fp
            #
            for label in fp:

                channel = int(label.split(',')[0].replace(" ", ""))
                start = float(label.split(',')[1].replace(" ", ""))
                stop = float(label.split(',')[2].replace(" ", ""))
                symbol = int(label.split(',')[3].replace(" ", ""))
            
                if channel not in segments:
                    segments[channel] = []

                segments[channel].append([start, stop, symbol])

        #
        # end of with

        # aggregate the segments!
        #
        agg_segs = nedc_aggregate_segments(segments)

        # write to file
        #
        with open(ofile, 'w') as fp:

            for i in range(chan_num):
                for seg in agg_segs[0]:
                    fp.write("%d,%.4f,%.4f,%d\n" % (i, seg[0], seg[1], seg[2]))
示例#4
0
def main(argv):

    # define local variables
    #
    odir = ODIR
    params = PARAM_FILE
    rdir = RDIR
    ifile = IFILE

    # defines the default file extension to print to (i.e. raw rgb pixel data)
    #
    ext = 'raw'

    # create command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)

    # define command line arguments
    #
    parser.add_argument("files", type=str, nargs='*')
    parser.add_argument("--version", action='version', version=__version__)
    parser.add_argument("-parameters", "-p", "-pfile", type=str)
    parser.add_argument("-odir", "-o", "-output", type=str)
    parser.add_argument("-rdir", "-r", "-replace", type=str)
    parser.add_argument("-ofmt", "-format", type=str)
    parser.add_argument("-help", action="help")

    # parse the command line
    #
    args = parser.parse_args()

    # print usage file if no cmdl arguments provided and exit
    #
    if not args.files:
        parser.print_usage()
        exit(-1)

    # set options and argument values provided
    #
    if args.parameters:
        pfile = args.parameters

    if args.odir:
        odir = args.odir

    if args.rdir:
        rdir = args.rdir

    if args.ofmt:
        image_format = args.ofmt

        # set extension to image format, strip period for formatting
        #
        ext = image_format.replace(".", "")

        # check if image format is valid, prompts user with valid
        # formats. Exits script if invalid.
        #
        if image_format not in valid_image_formats:
            print  "%s (%s: %s): format %s not supported" \
                % (sys.argv[0], __name__, "main", image_format)
            sys.exit(-1)

    # load parameters from param file
    #
    parameters = nft.load_parameters(pfile, "")
    if (parameters == None):
        print  "%s (%s: %s): error loading the parameter file (%s)" % \
          (sys.argv[0], __name__, "main", pfile)
        sys.exit(-1)

    parameters.update(nft.load_parameters(params, "RESCALE_IMAGE"))

    parameters.update(nft.load_parameters(params, "SVS_SLIDE_LEVEL"))

    parameters.update(nft.load_parameters(params, "GAUSSIAN_VARS"))

    parameters.update(nft.load_parameters(params, "OUTPUT_FORMAT"))

    # load file list
    #
    flist = nft.get_flist(args.files[0])

    # check if file list was was provided
    #
    if flist is None:

        # if not, set flist to default global variable
        #
        flist = ifile

    # create class instance to be modified
    #
    ppi = nit.Preprocess(parameters)

    # create dictionary to store methods to be called
    #

    method_dict = {
        "grayscale": ppi.grayscale_image,
        "rescale": ppi.rescale,
        "display": ppi.display_image,
        "gaussian": ppi.gaussian_blur,
        "laplacian": ppi.laplacian_transform
    }

    # image flag, checks if images were passed or file list
    #
    image_flag = False

    # test if command line args are files or file lists
    # image_flags is a dictionary with the same length as the # of file arguments
    # provided.  True means that the file is an image and 0 means that it isn't.
    # if ppi.is_image() returns False it will be treated as a .list
    #
    image_flags = {}

    for file in args.files:
        if ppi.is_image(file) == True:
            image_flags[file] = True
        else:
            image_flags[file] = False

    flist = args.files

    # generate list of methods to complete for each image from
    # the param file, comma delimited from param file.
    #
    method_list = parameters['methods'].split(',')

    # display to user methods invoked
    #
    print "methods invoked: ", method_list

    # create function for implementing preprocessing functions (this is here
    # so that it has access to the local variables in the main function)
    #
    def _preprocess_image(file):
        # Recreate instance of class
        #
        ppi = nit.Preprocess(parameters)

        # create a numpy array from image data for initialization
        # this allows methods to be applied since they operate on the array
        #
        image = ppi.create_np_array(file)

        # iterate over methods to apply to image from param file
        # map to dictionary
        #
        for method in method_list:
            image = method_dict[method](image)

        # display image in new window according to param file
        #
        if parameters['display_image'] == 'True':
            method_dict['display'](image)

        # create output file path name
        #
        ofile = nft.make_ofile(file, ext, odir, rdir)

        # create output directory
        #
        try:
            os.makedirs(os.path.dirname(ofile))
        except OSError as e:
            if e.errno != NEDC_DEF_MAKEDIRS_ERROR:
                raise

        # write file to output directory (odir) safely
        #
        with open(ofile, "w") as ofile_fp:

            # check if output format parameter provided and not an svs file
            #
            if parameters['output_format'] and not file.endswith('svs'):
                ofile_fp.write(
                    ppi.convert_image(parameters['output_format'], image))

            # write numpy array to binary file output
            #
            else:
                image.tofile(ofile)

    # iterate through the files in the file list or command line
    #

    for f in flist:
        # clause for handling image files passed through the command line
        #
        if image_flags[f]:
            _preprocess_image(f)
        else:
            img_list = nft.get_flist(nft.get_fullpath(f))
            for img in img_list:
                _preprocess_image(img)

    # exit gracefully
    #
    return
示例#5
0
def main(argv):

    # define local variables
    #
    odir = ODIR
    rdir_tse = RDIRTSE
    rdir_edf = RDIREDF
    header_prefix = HEADER_PREFIX
    null_insert = NULL_INSERT
    tse_ext = TSE_EXT
    edf_ext = EDF_EXT

    # create command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)

    # define command line arguments
    #
    parser.add_argument("ilist", type=str, nargs='*')
    parser.add_argument("-odir", "-o", "-output", type=str)
    parser.add_argument("-rdirtse", "-rtse", "-replacetse", type=str)
    parser.add_argument("-rdiredf", "-redf", "-replaceedf", type=str)
    parser.add_argument("-channel", "-chan", "-ch", action="store_true")

    # parse the command line
    #
    args = parser.parse_args()

    # print usage file if no cmdl arguments provided and exit
    #
    if not args.ilist:
        parser.print_usage()
        exit(-1)

    # set options and argument values provided and strip trailing ('/')
    #
    if args.odir:
        odir = args.odir.rstrip('/')

    if args.rdirtse:
        rdir_tse = args.rdirtse.rstrip('/')
    else:
        print  "%s (%s: %s): error loading the rdir for tse files" % \
            (sys.argv[0], __name__, "main")
        exit(-1)

    if args.rdiredf:
        rdir_edf = args.rdiredf.rstrip('/')
    else:
        print  "%s (%s: %s): error loading the rdir for edf files" % \
            (sys.argv[0], __name__, "main")
        exit(-1)

    # if channel argument provided with any input, channel flag is set
    # to true
    #
    if args.channel == True:
        channel_flag = True
        CHANNEL_FLAG = True
    else:
        channel_flag = False

    # load tse file list
    #
    flist = nft.get_flist(args.ilist[0])

    # loop over file lists provided on command line
    #
    for tse_file in flist:

        # break down tse file into header/data
        #
        tse_header, tse_data = get_tse_data(tse_file, header_prefix)

        # check if channel flag is true and act accordingly
        #
        if channel_flag == True:

            # get and remove channel info from tse filepath for
            # odir/rdir manipulations
            #
            channel_info = tse_file[-10:-4]
            tse_file = tse_file.replace(channel_info, '')

        else:
            channel_info = ''

        # get file name and manipulate for rdir/odir
        #
        fname = os.path.basename(tse_file)
        edf_file = fname.replace(tse_ext, edf_ext)
        fdir = os.path.dirname(tse_file)
        edf_fdir = fdir.replace(rdir_tse, rdir_edf)
        edf_file = os.path.join(edf_fdir, edf_file)
        ofile = tse_file.replace(rdir_tse, odir)
        of_dir = os.path.dirname(ofile)

        # check if directory of ofile exists, create if not
        #
        if not os.path.exists(of_dir):
            os.makedirs(of_dir)

        # try to get edf duration, if error, print edf to error file
        #
        try:
            end_duration = get_edf_duration(edf_file)

            # insert null data where gaps in time exist
            #
            tse_data = insert_null_data(tse_data, end_duration)

            # write tse data to output file
            #
            merge_write_tse(tse_header, tse_data, ofile, channel_info,
                            channel_flag)

        except:
            # write edf to a error file path
            #
            write_edf_to_err(edf_file)

    # exit gracefully
    #
    return
示例#6
0
def main(argv):

    #create a command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)

    #define command line arguments
    #
    parser.add_argument("args", type=str, nargs='*')
    parser.add_argument("-rdir", "-r", type=str)
    parser.add_argument("-odir", "-o", type=str)
    parser.add_argument("-ext", "-e", type=str)
    parser.add_argument("-label", "-l", type=str)
    parser.add_argument("-help", action="help")

    #parse the command line
    #
    args = parser.parse_args()

    # check if the proper number of lists has been provided
    #
    if len(args.args) != NUM_ARGS:
        parser.print_usage()
        exit(-1)

    # set the label to remove
    #
    if args.label is not None:
        label = args.label

    else:

        label = "6"

    # set the replace file
    #
    if args.rdir is not None:
        rdir_a = args.rdir

    # set the output file
    #
    if args.odir is not None:
        odir_a = args.odir

    #set the input file lists names
    #
    rec_list_name = args.args[0]

    # convert label to an integer
    #
    rm_label = int(label)

    #load the lists
    #
    rec_list = nft.get_flist(rec_list_name)

    if (rec_list == None):
        print  "%s (%s: %s): error loading input list (%s)" % \
            (sys.argv[0], __name__, "main", rec_list_name)
        exit(-1)

    # create the output directory
    #
    if nft.make_dir(odir_a) == False:
        print  "%s: error creating output directory (%s)" \
            % (sys.argv[0], odir)
        exit(-1)

    #loop through the files
    #
    for rec_file in rec_list:

        # convert rec file name to full path
        #
        rec_file = os.path.abspath(
            os.path.realpath(os.path.expanduser(rec_file)))

        # set the ofile ext
        #
        if args.ext is not None:
            ext = args.ext

        else:
            ext = rec_file.split(".")[1]

        # get name of the output file and make it
        #
        ofile = nft.make_ofile(rec_file, ext, odir_a, rdir_a)
        nft.make_dir(os.path.abspath(ofile).strip(os.path.basename(ofile)))

        # read the rec file
        #
        lines = [line.rstrip('\n') for line in open(rec_file)]

        # list to store events
        #
        events = []

        # loop over lines in the rec file
        #
        for line in lines:

            #remove spaces and newline chars, split the line
            #
            line = line.replace(nft.DELIM_SPACE, nft.DELIM_NULL) \
                       .replace(nft.DELIM_NEWLINE, nft.DELIM_NULL)
            parts = line.split(nft.DELIM_COMMA)

            # append lines to events
            #
            events.append([int(parts[0]), float(parts[1]), \
                           float(parts[2]), int(parts[3])])

        # end for

        non_bckg = []

        for event in events:
            label = event[3]

            if label != rm_label:
                non_bckg.append(event)

        # end for event in events

        # overwrite the rec file with the non bckg array
        #
        replace_file(non_bckg, ofile)

    #end for (file)
    # exit
    #
    return True
示例#7
0
def main(argv):

    # create a command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)

    # define command line arguments
    #
    parser.add_argument("args", type=str, nargs='*')
    parser.add_argument("-rdir", "-r", type=str)
    parser.add_argument("-odir", "-o", type=str)
    parser.add_argument("-ext", "-e", type=str)
    parser.add_argument("-label", "-l", type=str)
    parser.add_argument("-help", action="help")

    # parse the command line
    #
    args = parser.parse_args()

    # check if the proper number of lists has been provided
    #
    if len(args.args) != NUM_ARGS:
        parser.print_usage()
        exit(-1)

    # set the label used to fully annotate
    #
    if args.label is not None:
        fill_label = args.label

    # set default label to bckg
    #
    else:

        fill_label = 0

    # set the replace file
    #
    if args.rdir is not None:
        rdir_a = args.rdir

    # set the output file
    #
    if args.odir is not None:
        odir_a = args.odir

    # set the input file lists names
    #
    rec_list_name = args.args[0]
    edf_list_name = args.args[1]

    # load the list of rec files
    #
    rec_list = nft.get_flist(rec_list_name)

    if (rec_list == None):
        print  "%s (%s: %s): error loading input list (%s)" % \
            (sys.argv[0], __name__, "main", rec_list_name)
        exit(-1)

    # load the list of edf files
    #
    edf_list = nft.get_flist(edf_list_name)

    if (edf_list == None):
        print  "%s (%s: %s): error loading input list (%s)" % \
            (sys.argv[0], __name__, "main", edf_list_name)
        exit(-1)

    # create the output directory
    #
    if nft.make_dir(odir_a) == False:
        print  "%s: error creating output directory (%s)" \
            % (sys.argv[0], odir)
        exit(-1)

    # loop through the files
    #
    for rec_file, edf_file in zip(rec_list, edf_list):

        # convert rec file name to full path
        #
        rec_file = os.path.abspath(
            os.path.realpath(os.path.expanduser(rec_file)))

        # set ofile extension
        #
        if args.ext is not None:
            ext = args.ext

        else:
            ext = rec_file.split(".")[1]

        # get name of the output file and make it
        #
        ofile = nft.make_ofile(rec_file, ext, odir_a, rdir_a)
        nft.make_dir(os.path.abspath(ofile).strip(os.path.basename(ofile)))

        # get duration of the edf file
        #
        cmd = "nedc_print_header %s | grep secs | awk '{printf $6}'" % edf_file
        edf_dur = os.popen('%s' % cmd).read()
        edf_dur = int(float(edf_dur))

        # read the rec file
        #
        lines = [line.rstrip('\n') for line in open(rec_file)]

        # list to store events
        #
        events = []

        # loop over lines in the rec file
        #
        for line in lines:

            #remove spaces and newline chars, split the line
            #
            line = line.replace(nft.DELIM_SPACE, nft.DELIM_NULL)\
                       .replace(nft.DELIM_NEWLINE, nft.DELIM_NULL)
            parts = line.split(nft.DELIM_COMMA)

            # append lines to events
            #
            events.append([int(parts[0]), float(parts[1]), \
                           float(parts[2]), int(parts[3])])

        # end for

        # find out how many channels there should be based on montage
        #
        file_dirs = rec_file.split("/")
        montage = file_dirs[len(file_dirs) - 5]

        if montage == ("01_tcp_ar" or "02_tcp_le"):
            chan_num = 22
        elif montage == ("03_tcp_ar_a" or "04_tcp_le_a"):
            chan_num = 20

        # list to store fully annotated events
        #
        full_events = []

        # fully annotate each channel
        #
        for chan in range(0, chan_num):

            # list to store events for each channel
            #
            chan_events = []

            # loop through events
            #
            i = 0
            for event in events:
                channel = event[0]
                label = event[3]

                # events to chan_events if channel # matches
                # exclue fill label annotations. default will
                # exclude bckg annotations
                #
                if channel == chan and label != fill_label:
                    chan_events.append(event)

            #end for

            # if there are no annotations for this channel: fill with bckg
            #
            if len(chan_events) == 0:

                chan_events.append([chan, 0, edf_dur, fill_label])

            # if there are annotations for this channel: call fill_blanks
            #
            else:

                chan_events = fill_blanks(chan_events, edf_dur, fill_label)

            # loop through the events for this channel and add then to
            # full_events list
            #
            for event in chan_events:

                full_events.append(event)

            #end for (event)
        #end for (chan)

        # overwrite the rec file with the fully annotated event list
        #
        replace_file(full_events, ofile)

    #end for (file)
    # exit
    #
    return True
示例#8
0
文件: get_list.py 项目: EvavW/isip
def main(argv):

    #create a command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)

    #define command line arguments
    #
    parser.add_argument("args", type=str, nargs='*')
    parser.add_argument("-rdir", type=str)
    parser.add_argument("-odir", type=str)
    parser.add_argument("-ext", type=str)
    parser.add_argument("-help", action="help")

    #parse the command line
    #
    args = parser.parse_args()

    # check if the proper number of lists has been provided
    #
    if len(args.args) != NUM_ARGS:
        parser.print_usage()
        exit(-1)

    # set the output extension
    #
    if args.ext is not None:

        ext = args.ext

    else:  # set default

        ext = "edf"

    # set the output directory
    #
    if args.odir is not None:
        odir = args.odir

    # set the replace directory
    #
    if args.rdir is not None:
        rdir = args.rdir

    #set the input file lists names
    #
    file_list_name = args.args[0]

    #load the lists
    #
    file_list = nft.get_flist(file_list_name)

    #loop through the files
    #
    for this_file in file_list:

        # convert input to full path
        #
        this_file = os.path.abspath(
            os.path.realpath(os.path.expanduser(this_file)))

        # check to see if this is a channel based file
        #
        file_parts = this_file.split("/")

        if len(file_parts[len(file_parts) - 1].split("_")) == 4:

            # if channel based, strip channel name
            #
            cut_file = this_file[:-10] + ".ext"

            # create edf file name from striped input
            #
            ofile = nft.make_ofile(cut_file, ext, odir, rdir)

        # if not channel based, use original file to make ofile
        #
        else:
            ofile = nft.make_ofile(this_file, ext, odir, rdir)

        # print to console. direct output from command line
        #
        print ofile

    # exit
    #
    return True
示例#9
0
文件: gen_sheet.py 项目: EvavW/isip
def main(argv):

    #create a command line parser
    #
    parser = ncp.CommandLineParser(USAGE_FILE, HELP_FILE)

    #define command line arguments
    #
    parser.add_argument("args", type=str, nargs='*')
    parser.add_argument("-info", "-o", type=str)
    parser.add_argument("-help", action="help")

    #parse the command line
    #
    args = parser.parse_args()

    # check if the proper number of lists has been provided
    #
    if len(args.args) != NUM_ARGS:
        parser.print_usage()
        exit(-1)

    # set the info array
    #
    if args.info is not None:
        info_file = args.info

    # set the input file lists names
    #
    tse_list_name = args.args[0]

    #load the lists
    #
    tse_list = nft.get_flist(tse_list_name)

    if (tse_list == None):
        print  "%s (%s: %s): error loading input list (%s)" % \
            (sys.argv[0], __name__, "main", tse_list_name)
        exit(-1)

    # translate session_info.csv into an array
    #
    info_list_array = [line.rstrip('\n') for line in open(info_file)]

    # list to store processed session info
    #
    info_array = []

    #loop through each line in the info file
    #
    for session_info in info_list_array:

        # remove new line characters, split the line
        #
        session_info = session_info.replace(nft.DELIM_NEWLINE, nft.DELIM_NULL)
        parts = session_info.split(nft.DELIM_COMMA)

        # remove carriage return characters that appear after normal/abnormal
        #
        #        parts[5] = parts[5].replace('\r','')

        # append each session to session_info
        #
        info_array.append(
            [parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]])

    # print sheet headers before looping through filelist
    #
    print ",,,,,,,,,,,,Seizure Time,"
    print "Index,File No.,Patient,Session,File,EEG Type,EEG SubType,LTM -or- Routine,Normal/Abnormal,No. Seizures/File,No. Seizures/Session,Filename,Start,Stop,Seizure_Type"

    #loop through the files
    #
    file_index = 1
    global print_index
    print_index = 1

    done_sessions = []
    for tse_file in tse_list:

        # read the tse file
        #
        lines = [line.rstrip('\n') for line in open(tse_file)]

        # list to store events
        #
        events = []

        # loop over lines in the tse file
        #
        for line in lines:

            # make sure that we are looking at an acutal label
            #
            if (not line.startswith("#")) and \
               (not line.startswith("version")) and \
               "bckg" not in line and \
               line:

                # remove spaces and newline chars, split the line
                #
                line = line.replace(nft.DELIM_NEWLINE, nft.DELIM_NULL)
                parts = line.split(nft.DELIM_SPACE)

                # append lines to events
                #
                events.append([float(parts[0]), float(parts[1]),\
                               parts[2], float(parts[3])])

        # get basic info about this tse file
        #
        file_info = get_file_info(tse_file, tse_list_name, events)

        # print relevant info from this file to the console
        # update local print_index
        #
        #print_index=print_sheet(file_info,events,done_sessions,info_array,\
        #                        file_index)#,print_index)
        print_sheet(file_info, events, done_sessions, info_array, file_index)

        # iterate the file index number
        #
        file_index += 1

        # add this session to the list of accessed sessions
        #
        done_sessions.append(file_info[3])

    # end for tse_file in tse_list

    return True