Beispiel #1
0
def searchAction(keyword, page):
    """ 搜索行为 """
    (soup, htmlText) = searchAnimation(keyword=keyword, pageNum=page)

    animationsNums = getAllPageListCount(soup)
    if animationsNums == 0 or animationsNums == None:
        print("搜索的动画结果为0或为空,请确认动画名称是否正确.")
        return

    # 获取列表中的详细信息是在主线程中解析的
    detailUrls = getDetailUrls(soup=soup, htmlText=htmlText)

    # 使用信号量控制并发的数量 并发线程太多也不是好事
    sem = Semaphore(value=10)

    # 多线程获取详细网址列表中的信息
    detailUrlThread = DetailUrlThread(detailUrls=detailUrls, sem=sem)
    detailUrlThread.start()

    detailUrlThread.join()
    print("所有的下载完成")

    # 在主线程中进行最后的写入,这样更安全,其实并不是这样,只是这样写了而已
    writer = CSVWriter(keyword=keyword)
    writer.write(detailUrlThread.downloadInfos)

    # 下载完成后打开下载种子的文件夹
    os.system(r"open {}".format(Constant.seedFilePath))
Beispiel #2
0
    def show(self): 
        if not self.can_update: 
            self.can_update = True
            self.can_play = True
            
            # Initialize labels and buttons
            self.label_trial.config(text = 'Trial: 1/' + str(len(self.trial_labels)))

            self.button_skip = tk.Button(self, text="Skip", command=self.abortAndSkip)
            self.button_skip.pack()

            self.button_back = tk.Button(self, text="Abort", command=self.abortAndGoToStart, fg='red')
            self.button_back.pack()

            # Initialize recording streams
            self.eeg_recorder = EEGRecorder(LIVE_DATA, LIVE_EEG_OUTPUT_FILENAME, channels=CHANNELS_USED)
            self.event_csv_writer = CSVWriter(LIVE_EVENT_OUTPUT_FILENAME, column_headers=["timestamp", "event"]) #initializes csv writer
            
            # Initialize the event recording variables
            self.trial_index = 0
            self.trial_time = 0
            self.extra_time = 0
            self.extra_time_computed = False
            self.render_cross = True
            self.render_left = False
            self.render_right = False
            self.last_event_start_recorded = -1
            self.last_event_end_recorded = -1

            # Begin the update loops for timer and prompt
            self.counter = 28800        
            self.label_timer.after(100, self.update_label_timer)
            self.label_trial.after(100, self.update)
            self.canvas.after(100, self.render)
Beispiel #3
0
class EEGRecorder:
    def __init__(self, live, output_filename, channels=DEFAULT_CHANNELS):
        self.fs = DEFAULT_FS
        self.output_filename = output_filename
        self.started = False
        self.live = live
        self.channels = channels
        atexit.register(self.end)

    ############################
    ## STREAM CONTROL METHODS ##
    ############################
    def start(self):
        print("start recording called")
        if self.live:
            from pyOpenBCI import OpenBCICyton
            print("LIVE: started eeg recording")
            header = ["timestamp"] + self.channels
            self.csv_writer = CSVWriter(self.output_filename,
                                        column_headers=header)
            self.board = OpenBCICyton()
            self.eeg_thread = threading.Thread(
                target=self.board.start_stream,
                args=(self.record_data_sample, ))
            self.eeg_thread.start()
            self.started = True

    def end(self):
        print("end recording called")
        if self.started:
            print("LIVE: ended eeg recording")
            if self.live:
                self.board.stop_stream()
            self.started = False

    ####################
    ## HELPER METHODS ##
    ####################
    def record_data_sample(self, sample):
        # Get timestamp
        now = time.time()

        # Get the scaled channel data
        if self.live:
            raw_eeg_data = np.array(sample.channels_data) * SCALE_FACTOR_EEG
        else:
            raw_eeg_data = np.array(sample)

        # Record to CSV
        row_data = [now]
        row_data.extend(raw_eeg_data[self.channels])
        self.csv_writer.writerow(row_data)
Beispiel #4
0
def write_csv_summary(ops_analyzes, keywords):
    csvWriter = CSVWriter('./data/summary.csv')
    csvWriter.open()

    # Write headers
    headers = ['keyword']
    for analysis in ops_analyzes:
        headers += list(analysis.keys())
    headers.append('category')

    csvWriter.write_row(headers)

    # Collect categories
    categories = {}
    for keyword in keywords:
        category = keywords[keyword]['category']

        if not category in categories:
            categories[category] = []

        categories[category].append(keyword)

    # Sort keywords by category - alphabets
    category_list = list(categories.keys())
    category_list.sort()

    keyword_list = []
    for category in category_list:
        category_keywords = categories[category]
        category_keywords.sort()
        keyword_list += category_keywords

    # Write data
    for keyword in keyword_list:
        row = []
        for header in headers:
            if header == 'keyword':
                formated = keyword
                formated = re.sub('\\-\n', '', formated)
                formated = formated.replace('\n', ' ')
                row.append(formated)
            elif header == 'category':
                row.append(keywords[keyword]['category'])
            else:
                for analysis in ops_analyzes:
                    if header in analysis:
                        row.append(len(analysis[header].word_counts[keyword]))

        csvWriter.write_row(row)

    csvWriter.close()
Beispiel #5
0
 def start(self):
     print("start recording called")
     if self.live:
         from pyOpenBCI import OpenBCICyton
         print("LIVE: started eeg recording")
         header = ["timestamp"] + self.channels
         self.csv_writer = CSVWriter(self.output_filename,
                                     column_headers=header)
         self.board = OpenBCICyton()
         self.eeg_thread = threading.Thread(
             target=self.board.start_stream,
             args=(self.record_data_sample, ))
         self.eeg_thread.start()
         self.started = True
Beispiel #6
0
    def buildPlayerStats(self, careerURL, playerName):
        try:
            htmlSource = self.getHTMLSource(careerURL)
        except requests.exceptions.RequestException as err:
            self.logger.printLn(err)
        else:
            tableSet = htmlSource.find_all("table")
            newFolder = self.PARENT_FOLDER + "/" + playerName

            if not OSWorker.isExistingPath(newFolder):
                OSWorker.createFolder(newFolder)

            writer = CSVWriter(newFolder)
            for table in tableSet:
                writer.writeTableToCSV(table)
Beispiel #7
0
def main(args):
    parser = createParser()
    args = parser.parse_args(args)

    useDefaultLogFilesPath = True if not isinstance(
        args.logfiles, list) else False
    if not useDefaultLogFilesPath:
        args.logfiles = args.logfiles[0]

    if folderExist(args.logfiles) and containsFiles(args.logfiles):

        for logfile in listLogFilesByFolderPath(args):
            logfile_path = f'{args.logfiles}{logfile}'
            LogFiles.append(LogFile(logfile_path))

        if args.list:
            print(f"Available logfiles:")
            for logfile in LogFiles:
                print(logfile.path)

        if args.o:
            if folderExist(args.o):
                print(f"Sanitizing...")
                for i, logfile in enumerate(LogFiles):
                    LogFiles[i] = GameStartedTimeStampNormalizer.sanitize(logfile)
                    LogFiles[i] = MutationTimeStampNormalizer.sanitize(logfile)

                print(f"Writing files... ({args.o})")
                for logFile in LogFiles:
                    CSVWriter.write(logFile, args.o)
                    CSVWriter.write_mutation(logFile, args.o)
                CSVWriter.write_mutation_report(LogFiles, args.o)
Beispiel #8
0
def RunQueryFile(fileName, comment, options):

    doc = Parse(fileName)
    #doc.xpath('string(ham//em[1])')

    # read overall benchmark settings
    root = doc.rootNode.childNodes[0]
    repeatTimes = int(root.attributes[(None, 'repeatTimes')].value)
    clearCache = root.attributes[(None, 'clearCache')].value == "True"

    if options.runQueries:
        # setup output
        w = CSVWriter()
        w.Open(ResultsColumns, fileName + '.out.txt')

        # for each store
        stores = root.xpath('./Store')
        for store in stores:
            RunStore(w, fileName, store, repeatTimes, clearCache, comment)

        w.Close()

    print "Done."
Beispiel #9
0
 def _createWriters(self):
     self.writers = []
     for filePattern in self.filePatterns:
         writer = CSVWriter()
         writer.open(self.outputDir + filePattern + '.csv')
         self.writers.append(writer)
Beispiel #10
0
    def modeling(self, country_name):

        # create dest dir
        if not exists(self.destdir):
            makedirs(self.destdir)

        root.info('Infer for %s', country_name)

        time = datetime.now()

        # build location where clause for succeeding queries
        boundary = None
        if self.poly:
            boundary = PolyParser.poly_to_polygon(self.poly)
            where_clause = "st_intersects(l.way, st_transform(st_geomfromtext('" + boundary.wkt + "',4269),3857))"
        elif self.bpoly:
            boundary = wkt.loads(self.bpoly)
            where_clause = "st_intersects(l.way, st_transform(st_geomfromtext('" + boundary.wkt + "',4269),3857))"
        else:
            where_clause = "st_distance(l.way, (select way from planet_osm_polygon where osm_id = " + str(
                self.ssid) + ")) <= 300000"

        # do inference for each voltage level
        all_circuits = []
        all_substations = dict()
        all_generators = dict()
        equipment_points = []
        length_found_lines = 0

        for voltage_level in self.voltage_levels.split('|'):
            (length_found_lines, equipment_points, generators, substations, circuits) = self.inference_for_voltage(
                voltage_level, where_clause, length_found_lines, equipment_points,
                all_substations, all_generators, boundary)
            all_generators.update(generators)
            all_substations.update(substations)
            all_circuits.extend(circuits)

        root.info('Total length of all found lines is %s meters', str(length_found_lines))
        equipments_multipoint = MultiPoint(equipment_points)
        map_centroid = equipments_multipoint.centroid
        logging.debug('Centroid lat:%lf, lon:%lf', map_centroid.x, map_centroid.y)
        all_circuits = Transnet.remove_duplicates(all_circuits)
        root.info('Inference took %s millies', str(datetime.now() - time))

        transnet_instance.export_to_json(all_circuits)

        partition_by_station_dict = None
        population_by_station_dict = None
        cities = None
        if self.load_estimation:
            root.info('Start partitioning into Voronoi-portions')
            load_estimator = LoadEstimator(all_substations, boundary)
            partition_by_station_dict, population_by_station_dict = load_estimator.partition()
            cities = load_estimator.cities

        if self.topology:
            root.info('Plot inferred transmission system topology')
            plotter = Plotter(self.voltage_levels)
            plotter.plot_topology(all_circuits, equipments_multipoint, partition_by_station_dict, cities, self.destdir)

        root.info('CSV generation started ...')
        csv_writer = CSVWriter(all_circuits)
        csv_writer.publish(self.destdir + '/csv')

        root.info('CIM model generation started ...')
        cim_writer = CimWriter(all_circuits, map_centroid, population_by_station_dict, self.voltage_levels)
        cim_writer.publish(self.destdir + '/cim')

        # for circuit in all_circuits:
        #     for line in circuit.members[1:-1]:
        #         if line.id not in self.all_lines:
        #             self.length_all += line.length
        #             self.all_lines[line.id] = line.id
        #
        # root.info('All lines length without duplicates %s', str(self.length_all / 1000))
        #
        # self.length_all = 0
        # for circuit in all_circuits:
        #     for line in circuit.members[1:-1]:
        #         self.length_all += line.length
        #
        # root.info('All lines length with duplicates %s', str(self.length_all / 1000))
        #
        # for circuit in all_circuits:
        #     sts = [circuit.members[0], circuit.members[-1]]
        #     for st in sts:
        #         if st.id not in self.all_stations:
        #             self.all_stations[st.id] = 1
        #         else:
        #             self.all_stations[st.id] += 1
        #
        # root.info('All Stations %s', str(self.all_stations))

        if validate:
            validator = InferenceValidator(self.cur)
            if boundary:
                all_stations = all_substations.copy()
                all_stations.update(all_generators)
                validator.validate2(all_circuits, all_stations, boundary, self.voltage_levels)
            else:
                validator.validate(self.ssid, all_circuits, None, self.voltage_levels)

        root.info('Took %s in total', str(datetime.now() - time))
logger = Logger()
scraper = nflScraper(logger)
for i in range(97, 97 + 26):
    scraper.scrapeUrlForLinks(
        "http://www.nfl.com/players/search?category=lastName&playerType=current&d-447263-p=1&filter=%s"
        % chr(i))
allPlayerURLs = scraper.getPlayerLinks()

for playerURL in allPlayerURLs:
    playerURL = "http://www.nfl.com" + playerURL
    splitPlayerURL = playerURL.split("/")
    playerName = splitPlayerURL[4]
    print(playerName)
    playerCareerURL = splitPlayerURL[:-1] + ["careerstats"]
    playerCareerURL = "/".join(playerCareerURL)

    try:
        code = requests.get(playerCareerURL)
        htmlInPlainText = code.text
        htmlSoup = BeautifulSoup(htmlInPlainText, "html.parser")
    except requests.exceptions.RequestException as err:
        logger.printLn(err)
    else:
        tableSet = htmlSoup.find_all("table")
        newFolder = "PlayerStats/" + playerName
        if not OSWorker.isExistingPath(newFolder):
            OSWorker.createFolder(newFolder)

        writer = CSVWriter()
        for table in tableSet:
            writer.writeTableToCSV(table)
Beispiel #12
0
    def publish(self, file_name):
        self.region.UUID = str(CimWriter.uuid())
        self.cimobject_by_uuid_dict[self.region.UUID] = self.region

        self.add_location(self.centroid.x, self.centroid.y, is_center=True)

        total_line_length = 0
        voltages = set()
        cables = set()
        wires = set()
        types = set()
        line_length = 0

        for circuit in self.circuits:
            station1 = circuit.members[0]
            station2 = circuit.members[-1]

            try:
                for line_part in circuit.members[1:-1]:
                    tags_list = ast.literal_eval(str(line_part.tags))
                    line_tags = dict(zip(tags_list[::2], tags_list[1::2]))
                    line_tags_keys = line_tags.keys()
                    voltages.update([
                        CSVWriter.try_parse_int(v)
                        for v in line_part.voltage.split(';')
                    ])
                    if 'cables' in line_tags_keys:
                        cables.update(
                            [CSVWriter.try_parse_int(line_tags['cables'])])
                    if 'wires' in line_tags_keys:
                        wires.update(
                            CSVWriter.convert_wire_names_to_numbers(
                                CSVWriter.sanitize_csv(line_tags['wires'])))
                    types.update([line_part.type])

                    line_length += line_part.length
            except Exception as ex:
                print('Error line_to_cim_param_extraction')

            if 'station' in station1.type:
                connectivity_node1 = self.substation_to_cim(
                    station1, circuit.voltage)
            elif 'plant' in station1.type or 'generator' in station1.type:
                connectivity_node1 = self.generator_to_cim(
                    station1, circuit.voltage)
            else:
                self.root.error('Invalid circuit! - Skip circuit')
                circuit.print_circuit()
                continue

            if 'station' in station2.type:
                connectivity_node2 = self.substation_to_cim(
                    station2, circuit.voltage)
            elif 'plant' in station2.type or 'generator' in station2.type:
                connectivity_node2 = self.generator_to_cim(
                    station2, circuit.voltage)
            else:
                self.root.error('Invalid circuit! - Skip circuit')
                circuit.print_circuit()
                continue

            lines_wsg84 = []
            line_length = 0
            for line_wsg84 in circuit.members[1:-1]:
                lines_wsg84.append(line_wsg84.geom)
                line_length += line_wsg84.length
            line_wsg84 = linemerge(lines_wsg84)
            total_line_length += line_length
            self.root.debug(
                'Map line from (%lf,%lf) to (%lf,%lf) with length %s meters',
                station1.geom.centroid.y, station1.geom.centroid.x,
                station2.geom.centroid.y, station2.geom.centroid.x,
                str(line_length))
            self.line_to_cim(connectivity_node1, connectivity_node2,
                             line_length, circuit.name, circuit.voltage,
                             line_wsg84.centroid.y, line_wsg84.centroid.x,
                             line_length, cables, voltages, wires)

            # self.root.info('The inferred net\'s length is %s meters', str(total_line_length))

        self.attach_loads()

        cimwrite(self.cimobject_by_uuid_dict,
                 file_name + '.xml',
                 encoding='utf-8')
        cimwrite(self.cimobject_by_uuid_dict,
                 file_name + '.rdf',
                 encoding='utf-8')

        # pretty print cim file
        xml = parse(file_name + '.xml')
        pretty_xml_as_string = xml.toprettyxml(encoding='utf-8')
        matches = re.findall('#x[0-9a-f]{4}', pretty_xml_as_string)
        for match in matches:
            pretty_xml_as_string = pretty_xml_as_string.replace(
                match, unichr(int(match[2:len(match)], 16)))
        pretty_file = io.open(file_name + '_pretty.xml', 'w', encoding='utf8')
        pretty_file.write(unicode(pretty_xml_as_string))
        pretty_file.close()
Beispiel #13
0
    def line_to_cim(self, connectivity_node1, connectivity_node2, length, name,
                    circuit_voltage, lat, lon, line_length, cables, voltages,
                    wires):

        r = 0.3257
        x = 0.3153
        # r0 = 0.5336
        # x0 = 0.88025

        r0 = 0
        x0 = 0

        coeffs_of_voltage = {
            220000: dict(wires_typical=2.0, r=0.08, x=0.32, c=11.5, i=1.3),
            380000: dict(wires_typical=4.0, r=0.025, x=0.25, c=13.7, i=2.6)
        }

        length_selected = round(line_length)
        cables_selected = CSVWriter.convert_max_set_to_string(cables)
        voltage_selected = CSVWriter.convert_max_set_to_string(voltages)
        wires_selected = CSVWriter.convert_max_set_to_string(wires)

        voltage_selected_round = 0
        if 360000 <= int(voltage_selected) <= 400000:
            voltage_selected_round = 380000
        elif 180000 <= int(voltage_selected) <= 260000:
            voltage_selected_round = 220000
        try:
            if length_selected and cables_selected and int(
                    voltage_selected_round
            ) in coeffs_of_voltage and wires_selected:
                coeffs = coeffs_of_voltage[int(voltage_selected_round)]
                # Specific resistance of the transmission lines.
                if coeffs['wires_typical']:
                    r = coeffs['r'] / (int(wires_selected) /
                                       coeffs['wires_typical']) / (
                                           int(cables_selected) / 3.0)
                    # Specific reactance of the transmission lines.
                    x = coeffs['x'] / (int(wires_selected) /
                                       coeffs['wires_typical']) / (
                                           int(cables_selected) / 3.0)
        except Exception as ex:
            print('Error line_to_cim')

        line = ACLineSegment(
            name=CimWriter.escape_string(name) + '_' +
            connectivity_node1.name + '_' + connectivity_node2.name,
            bch=0,
            r=r,
            x=x,
            r0=r0,
            x0=x0,
            length=length,
            BaseVoltage=self.base_voltage(int(circuit_voltage)),
            Location=self.add_location(lat, lon))
        line.UUID = str(CimWriter.uuid())
        self.cimobject_by_uuid_dict[line.UUID] = line
        terminal1 = Terminal(ConnectivityNode=connectivity_node1,
                             ConductingEquipment=line,
                             sequenceNumber=1)
        terminal1.UUID = str(CimWriter.uuid())
        self.cimobject_by_uuid_dict[terminal1.UUID] = terminal1
        terminal2 = Terminal(ConnectivityNode=connectivity_node2,
                             ConductingEquipment=line,
                             sequenceNumber=2)
        terminal2.UUID = str(CimWriter.uuid())
        self.cimobject_by_uuid_dict[terminal2.UUID] = terminal2
Beispiel #14
0
 def processMultiLevel(fp, calibrationMap, level=4, windFile=None):
     print("Process Multi Level: " + fp)
     Controller.processL1a(fp, calibrationMap)
     Controller.processL1b(fp, calibrationMap)
     #if level >= 1:
     Controller.processL2(fp)
     if level >= 2:
         Controller.processL2s(fp)
     if level >= 3:
         Controller.processL3a(fp)
     if level >= 4:
         windSpeedData = Controller.processWindData(windFile)
         Controller.processL4(fp, windSpeedData)
         #Controller.outputCSV_L4(fp)
     print("Output CSV: " + fp)
     CSVWriter.outputTXT_L1a(fp)
     CSVWriter.outputTXT_L1b(fp)
     CSVWriter.outputTXT_L2(fp)
     CSVWriter.outputTXT_L2s(fp)
     CSVWriter.outputTXT_L3a(fp)
     CSVWriter.outputTXT_L4(fp)
     print("Process Multi Level: " + fp + " - DONE")
Beispiel #15
0
 def processSingleLevel(fp, calibrationMap, level, windFile=None):
     print("Process Single Level: " + fp)
     if level == "1a":
         Controller.processL1a(fp, calibrationMap)
     elif level == "1b":
         fp = fp.replace("_L1a.hdf", ".hdf")
         Controller.processL1b(fp, calibrationMap)
     elif level == "2":
         fp = fp.replace("_L1b.hdf", ".hdf")
         Controller.processL2(fp)
     elif level == "2s":
         fp = fp.replace("_L2.hdf", ".hdf")
         Controller.processL2s(fp)
     elif level == "3a":
         fp = fp.replace("_L2s.hdf", ".hdf")
         Controller.processL3a(fp)
     elif level == "4":
         fp = fp.replace("_L3a.hdf", ".hdf")
         windSpeedData = Controller.processWindData(windFile)
         Controller.processL4(fp, windSpeedData)
     print("Output CSV: " + fp)
     CSVWriter.outputTXT_L1a(fp)
     CSVWriter.outputTXT_L1b(fp)
     CSVWriter.outputTXT_L2(fp)
     CSVWriter.outputTXT_L2s(fp)
     CSVWriter.outputTXT_L3a(fp)
     CSVWriter.outputTXT_L4(fp)
     print("Process Single Level: " + fp + " - DONE")
def parse_genes(query, sets_list, start, end, max_missing, max_heteros, maf,
                tmp_files_path):
    variants_dict = {}

    if query == "" or query == None:
        raise Exception("Query is empty.")

    if "." in query:
        query_list = [query]
        query_type = 1  # ISOF
    else:
        query_list = [query]
        query_type = 0  # GENE

    if sets_list == None or len(sets_list) == 0:
        raise Exception("Sets list is empty.")

    # all the sets in sets_list are from the same set_id and set_name
    # they only differ in the set_path
    set_id = sets_list[0][0]
    set_name = sets_list[0][1]

    sys.stderr.write("Set: " + str(set_id) + " - " + str(set_name) + "\n")

    vcf_filenames = [x[2] for x in sets_list]

    for vcf_filename in vcf_filenames:
        sys.stderr.write("Filename: " + str(vcf_filename) + "\n")

    #### Parse queries file
    ####
    # if query_file != "":
    #     sys.stderr.write("Processing queries list...\n")
    #     query_list = parse_queries_file(query_file)

    #### Variants to show
    ####
    # if variants_file != "":
    #     sys.stderr.write("Processing variants list...\n")
    #     variants_list = parse_queries_file(variants_file, keys=(1,2))
    variants_list = []

    #### Parse samples translation
    ####
    samples_translation = ""
    if (set_id == "ibsc2012"):
        samples_translation = "/home/cantalapiedra/SNP_panels/exome_sbcc/IBSC2012_as_ref/samples.translation"
    elif (set_id == "climbar"):
        samples_translation = ""
    else:
        raise Exception("Unknown set id: " + str(set_id))

    sys.stderr.write("Processing samples translation list...\n")
    samples_trans_dict = parse_samples_translation(samples_translation)

    #### Parse samples list
    ####
    #sys.stderr.write("Parsing samples list...\n")
    #samples_list = parse_samples_list(samples_filename)
    samples_list = []

    #### Parse headers file
    ####
    #header_found = parse_vcf_header_file(vcf_header, genotypes_dict, names_dict, \
    #                                     samples_filename, samples_list, samples_translation, samples_trans_dict)
    header_found = False

    genotypes_dict = {}
    names_dict = {}

    #### Parse VCF file
    ####
    total_records = 0
    total_variants = 0
    total_output = 0
    sys.stderr.write("Parsing VCF file...\n")
    for vcf_filename in vcf_filenames:
        sys.stderr.write("Processing " + vcf_filename + "\n")

        vcf_file = open(vcf_filename, 'r')
        for line in vcf_file:

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

            line_data = line.strip().split()

            # If a header is found, and if no header file was specified
            # record names of genotypes
            if header_found:
                if line.startswith("#"): continue
            elif not header_found:
                if line.startswith("#") and vcf_header == "":
                    parse_header(line_data, genotypes_dict, names_dict, \
                                 samples_filename, samples_list, samples_translation, samples_trans_dict)
                    header_found = True
                    continue

            if not header_found:
                raise Exception("No header found nor provided for VCF data.")

            total_records += 1

            contig = line_data[VCF_CONTIG_COL]
            #sys.stderr.write(str(line_data)+"\n")
            pos = long(line_data[VCF_POS_COL])
            if (start != 0 and pos < start): continue
            if (end != 0 and pos > end): break
            if len(variants_list) > 0 and not [contig, pos] in variants_list:
                continue

            variant_dict = {'var_id':-1, 'contig':contig, 'pos':pos, \
                            'ref':line_data[VCF_REF_COL], 'alt':line_data[VCF_ALT_COL], \
                            'alleles':{}, 'effs':{}, 'eff_x':[]}

            ok_variant = load_effects_genes(line_data, variant_dict,
                                            query_type, query_list)

            if not ok_variant: continue

            total_variants += 1
            var_id = total_variants
            variant_dict["var_id"] = var_id

            alleles = parse_alleles(line_data, genotypes_dict, biallelic)

            variant_dict['alleles'] = alleles

            ok_variant = preprocess_variant(alleles, max_heteros, max_missing, show_monomorph, maf, \
                                            biallelic, het_to_miss)
            if ok_variant:
                variants_dict[var_id] = variant_dict
                total_output += 1
                for allele in alleles:
                    for j in alleles[allele]:
                        genotypes_dict[j][var_id] = allele

    csv_file = CSVWriter().get_csv_file(tmp_files_path)

    csv_fileo = os.fdopen(csv_file[0], 'wb')

    #### Output
    ####
    sys.stderr.write("Generating output...\n")
    print_variants_genes(variants_dict, genotypes_dict, names_dict, samples_list, \
                         output_format, \
                         biallelic, numeric, cluster_samples, csv_fileo)

    csv_fileo.close()

    sys.stderr.write("Total records read: " + str(total_records) + "\n")
    sys.stderr.write("Total variants parsed: " + str(total_variants) + "\n")
    sys.stderr.write("Total variants output: " + str(total_output) + "\n")
    sys.stderr.write("Finished.\n")

    return csv_file
Beispiel #17
0
class CalibrationPrompt(tk.Frame):

    def __init__(self, parent, controller, session = None):
        tk.Frame.__init__(self, parent)
        
        self.session = session 
        self.controller = controller

        # The sequence of class labels for the session
        self.trial_labels = a_123 

        # Timer label
        self.label_timer = tk.Label(self, text = '', font=("Halvetica", 40))
        self.label_timer.pack()

        # Create cross canvas 
        self.canvas = tk.Canvas(self)
        self.canvas.pack(fill=tk.BOTH, expand=1)

        # Initialize arrows dimensions
        self.left_arrowhead = [150+120, 250, 230+120, 300, 230+120, 200]
        self.right_arrowhead = [1000-150-120, 250, 1000-230-120, 300, 1000-230-120, 200]
        
        # Trial label
        self.label_trial = tk.Label(self, text = 'Trial: 1/' + str(len(self.trial_labels)),\
                    font=("Halvetica", 25))
        self.label_trial.pack()

        # Define buttons that may be used
        self.button_skip = None
        self.button_back = None
        self.button_train = None

        # Load the beep noise
        pygame.mixer.init()
        pygame.mixer.music.load(BEEP_FILENAME)

        # Set the update mode to false
        self.can_update = False
        

    def show(self): 
        if not self.can_update: 
            self.can_update = True
            self.can_play = True
            
            # Initialize labels and buttons
            self.label_trial.config(text = 'Trial: 1/' + str(len(self.trial_labels)))

            self.button_skip = tk.Button(self, text="Skip", command=self.abortAndSkip)
            self.button_skip.pack()

            self.button_back = tk.Button(self, text="Abort", command=self.abortAndGoToStart, fg='red')
            self.button_back.pack()

            # Initialize recording streams
            self.eeg_recorder = EEGRecorder(LIVE_DATA, LIVE_EEG_OUTPUT_FILENAME, channels=CHANNELS_USED)
            self.event_csv_writer = CSVWriter(LIVE_EVENT_OUTPUT_FILENAME, column_headers=["timestamp", "event"]) #initializes csv writer
            
            # Initialize the event recording variables
            self.trial_index = 0
            self.trial_time = 0
            self.extra_time = 0
            self.extra_time_computed = False
            self.render_cross = True
            self.render_left = False
            self.render_right = False
            self.last_event_start_recorded = -1
            self.last_event_end_recorded = -1

            # Begin the update loops for timer and prompt
            self.counter = 28800        
            self.label_timer.after(100, self.update_label_timer)
            self.label_trial.after(100, self.update)
            self.canvas.after(100, self.render)
    
    ####################
    ## UPDATE METHODS ##
    ####################

    # Update loop for timer
    def update_label_timer(self):
        if self.controller.current_frame == self and self.can_update:
            tt = datetime.fromtimestamp(self.counter)
            string = tt.strftime("%H:%M:%S")
            self.label_timer.config(text = string)
            self.counter += 1
        if self.trial_index < len(self.trial_labels) and self.can_update:
            self.label_timer.after(1000,self.update_label_timer)
    
    # Update loop for prompt
    def update(self):
        if self.controller.current_frame == self and self.can_update:
            # Start EEG recording if it's the first trial
            if self.trial_index == 0 and self.trial_time == 0:
                self.eeg_recorder.start()
            
            # Return early if no more trials
            if self.trial_index >= len(self.trial_labels) : 
                self.eeg_recorder.end() 
                self.button_skip.pack_forget()

                self.button_train = tk.Button(self, text="Train Model", command=self.abortAndTrain)
                
                self.button_train.pack()
                return

            self.trial_time += 125

            if self.trial_time >= START_REST_TIME and self.trial_time < BEEP_FINISH_TIME and self.can_play:
                # Play sound
                pygame.mixer.music.play(loops = 0)
                self.can_play = False
            
            elif self.trial_time >= BEEP_FINISH_TIME and self.trial_time < ARROW_FINISH_TIME:
                # Show arrow
                label = self.trial_labels[self.trial_index]
                self.can_play = True
                if label == 1:
                    self.render_left = True
                    self.render_right = False
                elif label == 2:
                    self.render_right = True
                    self.render_left = False
            elif self.trial_time >= ARROW_FINISH_TIME and self.trial_time < TRIAL_FINISH_TIME:
                # Write event start data
                if self.last_event_start_recorded < self.trial_index: 
                    row_data = [time.time()] + ["start_" + str(self.trial_labels[self.trial_index])]
                    self.event_csv_writer.writerow(row_data)
                    self.last_event_start_recorded = self.trial_index
                # Stop rendering arrow and compute random extra time between trials
                self.render_left = False
                self.render_right = False
                if self.extra_time_computed == False:
                    self.extra_time = random.randint(0,MAX_EXTRA_TIME)
                    self.extra_time_computed = True
            elif self.trial_time >= TRIAL_FINISH_TIME and self.trial_time < TRIAL_BASEEXTRA_TIME + self.extra_time:
                # Write event end data
                if self.last_event_end_recorded < self.trial_index: 
                    row_data = [time.time()] + ["end_" + str(self.trial_labels[self.trial_index])]
                    self.event_csv_writer.writerow(row_data)
                    self.last_event_end_recorded = self.trial_index
                # Stop rendering cross
                self.render_cross = False
            elif self.trial_time >= TRIAL_BASEEXTRA_TIME + self.extra_time:
                # Update trial info
                self.trial_index+=1
                self.trial_time = 0
                self.render_cross = True
                self.extra_time_computed = False
                if self.trial_index < len(self.trial_labels):
                    self.label_trial.config(text='Trial: '+str(self.trial_index+1) + '/' + str(len(self.trial_labels)))
                else :
                    self.label_trial.config(text='Trials Completed! Please "Train Model" to continue')
            self.label_trial.after(125, self.update)

    # Create green rectangles instead that sits on the horizonal line
    # shifting left and right (on the x-axis)
    # During period where arrow shown, have square shift left/right every 100 ms depending 
    # on number of shift from random number generator 
    def render(self):
        if self.can_update:
            self.canvas.delete('all')
            if self.render_left == True:
                self.canvas.create_polygon(self.left_arrowhead, fill='#1f1', tags='left')
            if self.render_right == True:
                self.canvas.create_polygon(self.right_arrowhead, fill='#1f1', tags='right')
            if self.render_cross == True:
                self.canvas.create_line(1000/2-150+30, 250, 1000/2+150-30, 250, width = 4)
                self.canvas.create_line(500, 150+30, 500, 350-30, width = 4, dash=(4, 4))
            self.canvas.after(50, self.render)

    
    ###################
    ## ABORT METHODS ##
    ###################
    def abort(self):
        print("Abort calibration prompt")
        if self.can_update:
            # Stop the EEG recorder if aborting
            self.eeg_recorder.end()

            # Clear the buttons because they will be created again if returned to calibration prompt
            if self.button_back: 
                self.button_back.pack_forget()
            if self.button_skip: 
                self.button_skip.pack_forget()
            if self.button_train: 
                self.button_train.pack_forget()
            self.can_update = False

    def abortAndSkip(self): 
        self.abort()
        self.controller.show_frame(FeedbackPrompt)

    def abortAndGoToStart(self):
        self.abort()
        self.controller.show_frame(StartPage)

    def abortAndTrain(self):
        self.abort()
        self.controller.show_frame(TrainingPrompt)
Beispiel #18
0
#!/usr/bin/env python3
import argparse
import os
from LogFile import LogFile
from GameStartedTimeStampNormalizer import GameStartedTimeStampNormalizer
from MutationTimeStampNormalizer import MutationTimeStampNormalizer
from CSVWriter import CSVWriter

DEFAULT_IN = 'logs/'
DEFAULT_OUT = 'output/'
LogFiles = []
GameStartedTimeStampNormalizer = GameStartedTimeStampNormalizer()
MutationTimeStampNormalizer = MutationTimeStampNormalizer()
CSVWriter = CSVWriter()


def createParser():
    parser = argparse.ArgumentParser(
        description=""" Generates research relevant numbers
                          out of the gameclue-spacegame logs.

                          See gameclue-spacegame at: https://github.com/unioulu/gameclue"""
    )
    parser.add_argument("logfiles",
                        nargs='*',
                        help="Log files folder path.",
                        default=DEFAULT_IN)
    parser.add_argument("--list",
                        help="List logfiles found on logfiles path.",
                        action='store_true')
    parser.add_argument("-o",
Beispiel #19
0
def Stats2CSV(stats, cacheFolder):
    print 'Writing stats to CSV file...',
    startTime = time.time()

    basicValues = [
        'triples', 'subjects', 'predicates', 'objects', 'lit_triples',
        'lit_subjects', 'lit_predicates', 'lit_objects', 'rel_triples',
        'rel_subjects', 'rel_predicates', 'rel_objects', 'type_triples',
        'type_subjects', 'type_predicates', 'type_objects', 'distTime',
        'lit_distTime', 'lit_colDistTime', 'lit_patTime', 'rel_distTime',
        'rel_colDistTime', 'rel_patTime', 'type_distTime', 'type_colDistTime',
        'type_patTime', 'joinTime', 'elapsedTime'
    ]

    tables = ['lit', 'rel', 'type']

    joins = ['s-s', 's-o', 'o-s', 'o-o']

    filename = os.path.join(
        cacheFolder, stats['cacheName']
    ) + ".cache.txt"  # graph.store._internedId + "-" + normalizeValue(graph.store.configuration, "L") + ".cache.txt"

    if os.path.exists(filename):
        os.remove(filename)  # remove old version of file

    w = CSVWriter()

    w.Open(None, filename)
    w.WriteLine("-----------------------")
    w.WriteLine('Basic Stats')
    w.WriteLine("-----------------------")
    w.Close()

    w.Open(basicValues, filename)
    w.WriteEntry(stats)
    w.Close()

    for tableType in tables:
        for s in stats[tableType + '_colDist']:
            w.Open(None, filename)
            w.WriteLine("-----------------------")
            w.WriteLine("%s_colDist: %s\t%s" %
                        (tableType, s, len(stats[tableType + '_colDist'][s])))
            w.WriteLine("-----------------------")
            w.Close()

            w.Open(['ColValue', 'DistValues'], filename)
            for k in stats[tableType + '_colDist'][s]:
                w.WriteListEntry([k, stats[tableType + '_colDist'][s][k]])
            w.Close()

    for tableType in tables:
        for s in stats[tableType + '_pat']:
            w.Open(None, filename)
            w.WriteLine("-----------------------")
            w.WriteLine("%s_pat: %s\t%s" %
                        (tableType, s, len(stats[tableType + '_pat'][s])))
            w.WriteLine("-----------------------")
            w.Close()

            w.Open(['Pattern', 'Count'], filename)
            for tp in stats[tableType + '_pat'][s]:
                w.WriteListEntry([k, stats[tableType + '_pat'][s][tp]])
            w.Close()

    for join in joins:
        if not stats.has_key('join_' + join):
            continue
        w.Open(None, filename)
        w.WriteLine("-----------------------")
        w.WriteLine("join_%s\t%s" % (join, len(stats['join_' + join])))
        w.WriteLine("-----------------------")
        w.Close()

        w.Open(['Pred1', 'Pred2', 'Count'], filename)
        for (pred1, pred2) in stats['join_' + join]:
            w.WriteListEntry(
                [pred1, pred2, stats['join_' + join][(pred1, pred2)]])
        w.Close()

    print ' done in %s s' % (time.time() - startTime)