def get_tile(self, pos): if self.map[pos][1] is Tile_Status.unopened: return Misc.call_tile(None) elif self.map[pos][1] is Tile_Status.opened: return self.map[pos][0] elif self.map[pos][1] is Tile_Status.flagged: return Misc.call_tile(-1)
def predict_round(self, tourney_round, analysis): """ :method: Predict winner of a single round of the tournament. :param int tourney_round: round of the tournament to predict. :param object analysis: Analysis object """ Misc.check_input_data("Kaggle", "tourney_slots") last_round_id_2digit = "R%02d" % (tourney_round - 1) current_round_id_1digit = "R%d" % tourney_round current_round_id_2digit = "R%02d" % tourney_round df = Constants.INPUT_DATA['Kaggle']['tourney_slots'] tourney_slots = df[df.season == self.season_id] # slice of tourney_slots DataFrame for current season for _id, row in tourney_slots.iterrows(): if row.slot[:2] == current_round_id_1digit: if tourney_round == 6: strongseed = row.strongseed[-2:] weakseed = row.weakseed[-2:] else: strongseed = self.zero_pad_seed(row.strongseed) weakseed = self.zero_pad_seed(row.weakseed) team_1 = self.bracket[last_round_id_2digit][strongseed] team_2 = self.bracket[last_round_id_2digit][weakseed] if not team_1 or not team_2: raise Exception("Cannot predict round '%s', do not have results from prior round." % tourney_round) winner = analysis.predict_winner(team_1, team_2, self.season_id) if tourney_round == 5: slot = row.slot[-2:] else: slot = self.zero_pad_seed(row.slot[-2:]) self.bracket[current_round_id_2digit][slot] = winner
def SelectCell(self, col, row): """ Handle the selection of an individual cell in the Grid """ # If we're in the "Time" column ... if col == 0: # ... determine the time of the selected item times = self.gridClips.GetCellValue(row, col).split('-') # ... and move to that time self.ControlObject.SetVideoStartPoint(Misc.time_in_str_to_ms(times[0])) # Retain the program focus in the Data Window self.gridClips.SetFocus() # If we're in the "ID" or "Keywords" column ... else: # ... if we have a Clip ... if self.gridClips.GetCellValue(row, 4) == 'Clip': # Load the Clip # Switched to CallAfter because of crashes on the Mac. wx.CallAfter(self.ControlObject.LoadClipByNumber, int(self.gridClips.GetCellValue(row, 3))) # NOTE: LoadClipByNumber eliminates the DataItemsTab, so no further processing can occur!! # There used to be code here to select the Clip in the Database window, but it stopped # working when I added multiple transcripts, so I moved it to the ControlObject.LoadClipByNumber() # method. # ... if we have a Snapshot ... elif self.gridClips.GetCellValue(row, 4) == 'Snapshot': tmpSnapshot = Snapshot.Snapshot(int(self.gridClips.GetCellValue(row, 3))) # ... load the Snapshot self.ControlObject.LoadSnapshot(tmpSnapshot) # ... determine the time of the selected item times = self.gridClips.GetCellValue(row, 0).split('-') # ... and move to that time self.ControlObject.SetVideoStartPoint(Misc.time_in_str_to_ms(times[0])) # ... and set the program focus on the Snapshot self.ControlObject.SelectSnapshotWindow(tmpSnapshot.id, tmpSnapshot.number, selectInDataWindow=True)
def populate_1st_round(self, analysis): """ :method: Populate 1st round of bracket with teams. :param object analysis: Analysis object """ Misc.check_input_data("Kaggle", "tourney_slots") Misc.check_input_data("Kaggle", "tourney_seeds") #variable init round_1 = "R00" # predict winners for play-in games df = Constants.INPUT_DATA['Kaggle']['tourney_slots'] tourney_slots = df[df.season == self.season_id] # slice of tourney_slots DataFrame for current season for _id, row in tourney_slots.iterrows(): if row.slot[0] != "R": # play-in game team_1 = self.get_team_by_seed(row.strongseed) team_2 = self.get_team_by_seed(row.weakseed) winner = analysis.predict_winner(team_1, team_2, self.season_id) slot = self.zero_pad_seed(row.slot) self.bracket[round_1][slot] = winner # populate non-play-in teams df = Constants.INPUT_DATA['Kaggle']['tourney_seeds'] tourney_seeds = df[df.season == self.season_id] # slice of tourney_seeds DataFrame for current season for _id, row in tourney_seeds.iterrows(): if len(row.seed) == 3: self.bracket[round_1][row.seed] = self.get_team_by_seed(row.seed)
def get_input(self): """Custom input routine.""" # Inherit base input routine gen_input = Dialogs.GenForm.get_input(self) # If OK ... if gen_input: # Get main data values from the form, ID, Comment, and Image Filename self.obj.id = gen_input[_('Snapshot ID')] self.obj.image_filename = gen_input[_('Image Filename')] if gen_input[_('Episode Position')] == '': self.obj.episode_start = 0.0 else: self.obj.episode_start = Misc.time_in_str_to_ms( gen_input[_('Episode Position')]) if gen_input[_('Duration')] == '': self.obj.episode_duration = 0.0 else: self.obj.episode_duration = Misc.time_in_str_to_ms( gen_input[_('Duration')]) self.obj.comment = gen_input[_('Comment')] else: self.obj = None return self.obj
def tryNotify(self, msg:Dispatch): """ Implement me! :: To send the message """ # setup the parameters of the message to = msg.to message = msg.message sender = Misc.hasKey(self.ME_CONFIG, "FROM", '') subject = msg.replace_tokens(Misc.hasKey(self.ME_CONFIG, "SUBJECT", ''))
def showData(self, data: Data): """ To show data if this module start standalone """ if data.source_name == self.ME_NAME + '/Skeleton': person = data.data #Points of skeleton aux = data.strToJSon(data.aux) h = int(Misc.hasKey(aux, 'H', self.ME_CONFIG['FRAME_HEIGHT'])) w = int(Misc.hasKey(aux, 'W', self.ME_CONFIG['FRAME_WIDTH'])) rgbImage = np.zeros((h, w, 3), np.uint8) for idj, join in enumerate(person): if join[0] != None: cv2.circle(rgbImage, (join[0], join[1]), 5, self.colors[idj], thickness=-1) elif data.source_name == self.ME_NAME + '/Person': rgbImage = np.zeros((data.data.shape[0], data.data.shape[1], 3), dtype=np.uint8) rgbImage[:, :, 0] = np.where(data.data[:, :] == 1, 255, 0) rgbImage[:, :, 1] = np.where(data.data[:, :] == 1, 255, 0) rgbImage[:, :, 2] = np.where(data.data[:, :] == 1, 255, 0) else: rgbImage = data.data # For capture the image in RGB color space # Display the resulting frame cv2.imshow(data.source_name + '-' + data.source_item, cv2.cvtColor(rgbImage, cv2.COLOR_BGR2RGB)) if cv2.waitKey(1) & 0xFF == ord('q'): self.stop() cv2.destroyAllWindows()
def eval_cost(params01_vec_dst): global Cost_best, params_01_vec_best global num_iter, success num_iter += 1 params_dict = convert_param_func(params01_vec_dst) path_dst = '{0}/iter_{1:04d}'.format(folder_path, num_iter) img_dst_cv2, _, _ = render_and_load(params_dict, path_dst) G_dst, _ = get_feature_func(img_dst_cv2) Cost_this = calc_cost_func(G_ref, G_dst) img_text = "Cost: {0}\n#iter {1}".format(Cost_this, num_iter) Misc.show_text_on_image_cv2(img_dst_cv2, img_text, "find_step") ## change the best result if Cost_this < Cost_best: Cost_best = Cost_this params_01_vec_best = params01_vec_dst[:] ## show the progress bar here prog = num_iter + 1 cmds.progressWindow(edit=True, progress=prog) if cmds.progressWindow(query=1, isCancelled=1): success = False ## [ABORT] raise StopIteration return Cost_this
def setStack(self, filename): ''' Parse the structure file to set the self.stack. The self.stack is a list of material and thickness, [[mat1, d1], [mat2, d2], ...]. ''' fileObj = open(Misc.getScriptfilename(filename), mode="rt", newline='\n') for line in fileObj: line = line.strip() if((not len(line)) or ("#" in line)): continue line = line.expandtabs(1) while(" " * 2 in line): line = line.replace(" " * 2, " ") layers = line.partition("{")[2].partition("}")[0] repeat = line.partition("*")[2].strip() # discretize graded layers like [Al60%02%GaAs 120.0 21], if any. gradedLayerList = re.findall("\[.*?\]", layers) for gradedLayer in gradedLayerList: layers = layers.replace(gradedLayer, Misc.discretizeGradedLayer(gradedLayer)) # parse all the layers, get [[mat, d], ...] tmpStack = [] if(len(repeat)): repeat = int(repeat) else: repeat = 1 for lay in layers.split(","): lay = lay.strip().split(" ") lay[1] = float(lay[1]) tmpStack.append(lay) for lay in (tmpStack * repeat): self.stack.append(lay)
def SelectCell(self, col, row): """ Handle the selection of an individual cell in the Grid """ # If we're in the "Time" column ... if col == 0: # ... determine the time of the selected item times = self.gridClips.GetCellValue(row, col).split('-') # ... and move to that time self.ControlObject.SetVideoStartPoint(Misc.time_in_str_to_ms(times[0])) # Retain the program focus in the Data Window self.gridClips.SetFocus() # If we're in the "ID" or "Keywords" column ... else: # ... if we have a Clip ... if self.gridClips.GetCellValue(row, 4) == 'Clip': # Load the Clip # Switched to CallAfter because of crashes on the Mac. wx.CallAfter(self.ControlObject.LoadClipByNumber, int(self.gridClips.GetCellValue(row, 3))) # NOTE: LoadClipByNumber eliminates the EpisodeClipsTab, so no further processing can occur!! # There used to be code here to select the Clip in the Database window, but it stopped # working when I added multiple transcripts, so I moved it to the ControlObject.LoadClipByNumber() # method. # ... if we have a Snapshot ... elif self.gridClips.GetCellValue(row, 4) == 'Snapshot': tmpSnapshot = Snapshot.Snapshot(int(self.gridClips.GetCellValue(row, 3))) # ... load the Snapshot self.ControlObject.LoadSnapshot(tmpSnapshot) # ... determine the time of the selected item times = self.gridClips.GetCellValue(row, 0).split('-') # ... and move to that time self.ControlObject.SetVideoStartPoint(Misc.time_in_str_to_ms(times[0])) # ... and set the program focus on the Snapshot self.ControlObject.SelectSnapshotWindow(tmpSnapshot.id, tmpSnapshot.number, selectInDataWindow=True)
def generateData(self): radius = numpy.loadtxt(path + "/arc_circle_fit_parameters.txt", delimiter=",")[2] with open(path + "/frequency.txt", "w") as f: f.write("#frame,frequency(Hz)\n") data = numpy.loadtxt(path + "/arc_length.txt", delimiter=",") for i in range(0, data.shape[0]): freq = Misc.arcLengthToFrequency(data[i, 1], exposure_time, radius) s = str(int(data[i, 0])) + "," + str(freq) + "\n" f.write(s) with open(path + "/frequency_average.txt", "w") as f: length = numpy.loadtxt(path + "/arc_length_average.txt") f.write("#frequency_average(Hz)\n") freq = Misc.arcLengthToFrequency(length, exposure_time, radius) s = str(freq) + "\n" f.write(s) print "Average frequency =", freq
def toFile(self, path: str = "./"): """ Save data into file. aux var must have 't', 'ext' values Formats availables: image: image captured using cv2 of openCV """ if self.aux == None: return '' aux = self.strToJSon(self.aux) t = Misc.hasKey(aux, 't', '') ext = Misc.hasKey(aux, 'ext', '') dataType: str = str(type(self.data)) f = normpath(path + "/attachments/" + str(time()) + str(self.id) + "." + ext) if t == 'image_rgb': from cv2 import cv2 cv2.imwrite(f, cv2.cvtColor(self.data, cv2.COLOR_BGR2RGB)) elif t == 'image_bgr': from cv2 import cv2 cv2.imwrite(f, self.data) elif t == 'image_binary': from cv2 import cv2 img = np.zeros((self.data.shape[0], self.data.shape[1]), dtype=np.uint8) img[:, :] = np.where(self.data[:, :] == False, 0, 255) cv2.imwrite(f, img) elif 'numpy' in dataType: w = np.squeeze(self.data) np.savetxt(f, w, delimiter=",", fmt="%s") else: nf = open(f, "w") nf.write(self.data) nf.close() return f
def __init__(self): ''' Constructor ''' selectedColor = Misc.RED notSelectedColor = Misc.BLUE self.buttons = [] startButton = Misc.TextButton(Misc.DISPLAY_WIDTH / 2, Misc.DISPLAY_HEIGHT - 60, 'start', Misc.font, 20, selectedColor, notSelectedColor, self.start) returnButton = Misc.TextButton(Misc.DISPLAY_WIDTH / 2, Misc.DISPLAY_HEIGHT - 30, 'return', Misc.font, 20, selectedColor, notSelectedColor, self.ret) self.buttons.append(startButton) self.buttons.append(returnButton) global server if server is not None: server.terminate() server = Server(Misc.minPlayers, Misc.maxPlayers, Misc.host, Misc.hostPort) self.server = server print("Server started") threading.Thread(target=self.server.state_waitForPlayers).start() print("Waiting for players")
def __repr__(self): str = 'Transcript Object:\n' str = str + "Number = %s\n" % self.number str = str + "ID = %s\n" % self.id str = str + "Episode Number = %s\n" % self.episode_num str = str + "Source Transcript = %s\n" % self.source_transcript str = str + "Clip Number = %s\n" % self.clip_num str = str + "Sort Order = %s\n" % self.sort_order str = str + "Transcriber = %s\n" % self.transcriber str = str + "Clip Start = %s\n" % Misc.time_in_ms_to_str( self.clip_start) str = str + "Clip Stop = %s\n" % Misc.time_in_ms_to_str(self.clip_stop) str = str + "Comment = %s\n" % self.comment str += "MinTranscriptWidth = %s\n" % self.minTranscriptWidth str = str + "LastSaveTime = %s\n" % self.lastsavetime # str += "isLocked = %s\n" % self._isLocked # str += "recordlock = %s\n" % self.recordlock # str += "locktime = %s\n" % self.locktime if self.text != None: if len(self.text) > 250: str = str + "text[:50] = %s\n\n" % self.text[:50] else: str = str + "text = %s\n\n" % self.text else: str += "text = None\n\n" if self.plaintext != None: if len(self.plaintext) > 250: str = str + "plaintext[:50] = %s\n\n" % self.plaintext[:50] else: str = str + "plaintext = %s\n\n" % self.plaintext else: str += "plaintext = None\n\n" return str.encode('utf8')
def preLoad(self): """ Load knowledge for pre processing """ self.getRGB = 'RGB' in self.ME_CONFIG['FORMATS'] self.getGray = 'Gray' in self.ME_CONFIG['FORMATS'] self.getObject = 'Object' in self.ME_CONFIG['FORMATS'] self.getSkeleton = 'Skeleton' in self.ME_CONFIG['FORMATS'] if self.getObject: sys.path.insert(0, self.ME_PATH) from mrcnn.CamControllerExtractor import CamControllerExtractor cocoH5 = Misc.hasKey(self.ME_CONFIG, 'MODEL_MSK', 'model/objectsModel.h5') self.cce = CamControllerExtractor(Me_Component_Path=self.ME_PATH, coco_model_path=cocoH5) self.cce.CLASSES_TO_DETECT = [ x.lower() for x in self.ME_CONFIG['OBJECTS'] ] if self.getSkeleton: ModelPath = Misc.hasKey(self.ME_CONFIG, 'MODEL_SKl', 'model/poseModel.h5') ModelPath = os.path.normpath(os.path.join(self.ME_PATH, ModelPath)) self.log('Loadding model from {} ...'.format(ModelPath), LogTypes.DEBUG) self.joinsBodyNET = load_model(ModelPath) self.joinsBodyNET._make_predict_function()
def CompileCC(self, source_ppath): """Compiles a C++ source file into an object file. Args: source_ppath: .cc file project path. """ source_apath = self.GetSourceAbsolutePath(source_ppath) object_ppath = misc.SwitchSuffix(source_ppath, '.cc', '.o') object_apath = os.path.join(self._output_dir, object_ppath) source_ppaths = set([source_ppath]) source_ppaths.update(self.GetCCIncludesClosure(source_ppath)) target_ppaths = [object_ppath] if self.IsUpToDate(source_ppaths, target_ppaths): logging.info('%s -> %s is up to date', source_ppath, object_ppath) return misc.MakeDirectoriesFor(object_apath) command = (self.vars.CXX_COMPILER_COMMAND + self.vars.CXX_FLAGS + [ '-I' + self._source_dir, '-I' + self._output_dir, '-c', source_apath, '-o', object_apath, ]) logging.info('Compiling C++: %s', source_ppath) self.Exec(command) self.RecordMetadata(source_ppaths, target_ppaths)
def chain_rule(currentNode, currentValue, dStack): currentNode.key = "*" power = currentNode.getRightChild().key innerTree = currentNode.getLeftChild() derInnerTree = BinaryTree(innerTree.key) derInnerNode = derInnerTree iStack = Stack() Misc.build_tree(currentNode.getLeftChild(), dStack, derInnerNode, iStack) derivative(derInnerTree) # building tree based on chain rule that will be inserted into original tree chainTree = BinaryTree('*') chainNode = chainTree chainNode.insertRight(derInnerTree) chainNode.insertLeft('*') chainNode = chainNode.getLeftChild() chainNode.insertLeft(power) chainNode.insertRight('^') chainNode = chainNode.getRightChild() chainNode.insertRight(power - 1) chainNode.insertLeft(innerTree) # inserting chain tree's first kids into original tree currentNode.insertLeft(chainTree.getLeftChild()) currentNode.insertRight(chainTree.getRightChild())
def abbreviate(self): """ Try to abbreviate the 'worst' term. Return false if we ran out of options """ global MAXREPLACE stlist = [] for t in self.termlist: stlist += t.subterms() self.subterms = Misc.uniqstr(stlist) self.subtermcount = Misc.multiset(stlist) candlist = [[self.valAbbrevCandidate(term.replace(self.mapper)), term] for term in self.subterms] th = threshold(len(self.abbreviations.keys())) ## For debugging only #print "Candidate list for threshold %s" % th #candlist.sort() #for [val,term] in candlist: # print " %s:\t%s" % (val,str(term)) #print th vallist = [[val, term] for [val, term] in candlist if val > th] vallist.sort() if len(vallist) > 0: val = vallist[-1][0] bigterm = vallist[-1][1] bigtermString = str(bigterm) # New string abbreviationTerm = Term.TermConstant(self.newName(prefix=" TMP ")) abbreviationString = str(abbreviationTerm) # Store folding self.mapper[str(self.termUnfold(bigterm))] = abbreviationTerm # Store unfolding self.unfold[str(abbreviationTerm)] = bigterm # Local replacements abbrev = {} abbrev[bigtermString] = abbreviationTerm # Replace termlist ot = self.termlist[:] self.termlist = [t.replace(abbrev) for t in ot] ## Replace also in abbreviations for k in self.abbreviations.keys(): self.abbreviations[k] = self.abbreviations[k].replace(abbrev) self.abbreviations[abbreviationString] = bigterm ## Add to termlist too #self.termlist += [bigterm] return True else: return False
def initializeDevice(self, device): """ Initialize device """ capture = cv2.VideoCapture(device['id']) _width = Misc.hasKey(device, 'width', self.ME_CONFIG['FRAME_WIDTH']) _height = Misc.hasKey(device, 'height', self.ME_CONFIG['FRAME_HEIGHT']) capture.set(cv2.CAP_PROP_FRAME_WIDTH, _width) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, _height) return capture
def data_available(self, season_id): Misc.check_input_data("Kaggle", "tourney_seeds", raise_exception=True) tournament_year = Constants.SEASON_ID_TO_YEAR.get(season_id) file_name = 'summary%s' % str(tournament_year)[-2:] result = Misc.check_input_data("KenPomWithIds", file_name, raise_exception=False) return result
def animated_move(self, row, col): ox, oy=Misc.togridpoint(self.row, self.col) x,y=Misc.togridpoint(row,col) if x==ox: self.move_a_bit_inner(oy, y, x, 0) else: self.move_a_bit_inner(ox, x, y, 1) self.row=row self.col=col
def loggingConf(self, config): """ Prepare configuration of log """ LOGGINGLEVEL = Misc.hasKey(config, 'LOGGING_LEVEL', None) # Logging level to write LOGGINGFILE = Misc.hasKey(config, 'LOGGING_FILE', None) # Name of file where write log LOGGINGFORMAT = Misc.hasKey(config, 'LOGGING_FORMAT', None) # Format to show the log self.loggingSettings(LOGGINGLEVEL, LOGGINGFILE, LOGGINGFORMAT)
def tryNotify(self, msg: Dispatch): """ Implement me! :: To send the message """ # setup the parameters of the message Of = Misc.hasKey(self.CONFIG, "OF", '') if msg.of == '' else msg.of To = Misc.hasKey(self.CONFIG, "TO", '') if msg.to == '' else msg.to Subject = Misc.hasKey(self.CONFIG, "SUBJECT", '') if msg.subject == '' else msg.subject Message = Misc.hasKey(self.CONFIG, "MESSAGE", '') if msg.message == '' else msg.message
def postArticle(article, client): articleTitle = article['title'] articleAuthor = article['author'] articleTags = article['tags'] articleContent = article['content'] ######### post ################# postConent = '' for section in articleContent: if (section['type'] == 'htag'): value = section['value'] value = value.replace('<', '<') postConent = postConent + '<h2>' + value + '</h2>' elif (section['type'] == 'ptag'): value = section['value'] value = value.replace('<', '<') postConent = postConent + '<p>' + value + '</p>' elif (section['type'] == 'codetag'): value = section['value'] value = value.replace('<', '<') #value=value.replace('>','>') postConent = postConent + '<pre class="wp-block-code"><code>' + value + '</code></pre>' elif (section['type'] == 'tabletag'): value = section['value'] #value=value.replace('<','<') #value=value.replace('>','>') postConent = postConent + '<figure class="wp-block-table">' + value + '</figure>' elif (section['type'] == 'litag'): value = section['value'] #value=value.replace('<','<') #value=value.replace('>','>') postConent = postConent + '<li>' + value + '</li>' elif (section['type'] == 'separatorTag'): #postConent=postConent+'<hr class="wp-block-separator">' value = section['value'] postConent = postConent + '<p>' + value + '</p>' elif (section['type'] == 'imgtag'): value = Misc.downloadImg(section['value'], 'dnl') postConent = postConent + '<p><img style="max-width:100%;" alt="picture" src=' + value + '></p>' #postConent=postConent+'<p><img style="max-width:100%;" alt="picture" rel="noreferrer noopener sponsored nofollow" src='+value+'></p>' (tags, cats) = guessTagsCats(articleTitle, articleTags) print(tags) print(cats) newpost = WordPressPost() newpost.title = articleTitle + '-' + tags[0] newpost.content = postConent newpost.terms_names = {'category': cats, 'post_tag': tags} #newpost.thumbnail = picResponse['id'] newpost.post_status = 'publish' time.sleep(3) #try: aritcleId = client.call(posts.NewPost(newpost)) print(aritcleId) Misc.push2Baidu(aritcleId)
def perimeter_detect(action): zymkey.client.set_perimeter_event_actions(1, action_notify=True, action_self_destruct=False) while True: try: logInfo = "" #detect perimeter breach every 2 seconds zymkey.client.wait_for_perimeter_event(timeout_ms=2000) perimeter_status = "" plst = zymkey.client.get_perimeter_detect_info() #print(plst) now = datetime.now() #print warning, date and time of the breach print("\nPerimeter event detected at (%s %s):" % (datetime.date(now), datetime.time(now))) logInfo += "\nPerimeter event detected at " + str( datetime.date(now)) + " " + str(datetime.time(now)) #Detect in which loop the perimeter has been detected. for z in range(len(plst)): p = plst[z] if p: #first loop is broken (inner loop) if z == 0: print(" Breach in the inner loop has been detected!") logInfo += "\n Breach in the inner loop has been detected!" #second loop is broken (outer loop) elif z == 1: print(" Breach in the outer loop has been detected!") logInfo += "\n Breach in the outer loop has been detected!" for j in range(len(plst)): p = plst[j] #log perimeter detection info perimeter_status = " perimeter[%d] timestamp = %d" % (j, p) logInfo += "\n perimeter[" + str( j) + "] timestamp = " + str(p) print(perimeter_status) misc.logInfo_perim(logInfo) if action == '1': subject = "Security Alert - Perimeter Breach Detected: " + str( datetime.date(now)) + " " + str(datetime.time(now)) act.send_email(subject, logInfo) elif action == '2': #add self destruct code here. Actions.selfDestruct() print("selfDestruct") zymkey.client.clear_perimeter_detect_info() except zymkey.exceptions.ZymkeyTimeoutError: logInfo = "" now = datetime.now() #no perimeter breach has been detected print("Nothing going on at (%s %s):" % (datetime.date(now), datetime.time(now))) logInfo += "\nNothing going on at " + str( datetime.date(now)) + " " + str(datetime.time(now)) misc.logInfo_perim(logInfo)
def analyze(self, data): """ Exec analysis of activity """ d_current = self.Simulated_current_time if self.Simulating else datetime.fromtimestamp( time()) #""" # ONLY for test, remove for production aux_tmp = Data().strToJSon(data.aux) time_tmp = Misc.hasKey(aux_tmp['source_aux'], 'time', '') if time_tmp != '': d_current = datetime.strptime(time_tmp, '%Y-%m-%d %H:%M:%S') self.Simulated_current_time = d_current Simulating_tmp = self.Simulating self.Simulating = True #""" if self.Last_Cleaning_Day < d_current: self.remove_old_data() self.Last_Cleaning_Day = d_current + timedelta(days=1) dataReturn = [] event_name = Misc.hasKey(data.data, 'class', '') #if event_name in ['get out of bed',]: # print('atento') #print('No event' if event_name == '' else event_name, d_current, end=' \r') event_name = '' if event_name.lower() == 'none' else event_name.lower() if self.analyze_event( event_name ) < self.Threshold or event_name in self.ME_CONFIG['ALWAYS_ABNORMAL']: if not event_name in self.ME_CONFIG['ALWAYS_NORMAL']: dataInf = self.data_from_event(Event(d_current, event_name)) dataReturn.append(dataInf) backward_event_list = self.backward_events_detect() for be in backward_event_list: dataInf = self.data_from_event(be, occurred=False) dataInf.package = 'backward-' + str(time()) dataReturn.append(dataInf) expected_event_list = self.expected_events_predict() for ee in expected_event_list: dataInf = self.data_from_event(ee, occurred=False) dataInf.package = 'expected-' + str(time()) dataReturn.append(dataInf) #self.save_knowledge() #""" # ONLY for test, remove for production if time_tmp != '': self.Simulating = Simulating_tmp #""" return dataReturn
def simulateData(self, dataFilter: Data): """ Allows simulate input data """ _width = Misc.hasKey(self.ME_CONFIG, 'FRAME_WIDTH', 320) _height = Misc.hasKey(self.ME_CONFIG, 'FRAME_HEIGHT', 240) if str(self.SimulatingPath).index(".csv") > 0: if self.simulationStep == 0: self.file = np.loadtxt(self.SimulatingPath, dtype=np.object, delimiter=',') self.file_length = len(self.file) self.simulationStep = 1 #31085 if self.simulationStep < self.file_length: if len(self.file[self.simulationStep, 2]) < 3: self.simulationStep += 1 #print(' -- no -- ') return [] #print(' -- si -- ') self.simulationStep += 10 #t0 = time() frame = cv2.imread(self.file[self.simulationStep, 1]) #print('Imagen', self.simulationStep, time()) frame = self.image_resize(frame, _width) #print('Tiempo cargando imagen', (time() - t0)) time_event = self.file[self.simulationStep, 3] event = self.file[self.simulationStep, 2] return self.getData(dataFilter.source_item, frame=frame, time_event=time_event, event=event) else: self.simulationStep = 0 return self.simulateData(dataFilter) else: if self.simulationStep == 0: self.capture = cv2.VideoCapture(self.SimulatingPath) self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, _width) self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, _height) self.video_length = int( self.capture.get(cv2.CAP_PROP_FRAME_COUNT)) if self.capture.isOpened( ) and self.simulationStep < self.video_length: _, frame = self.capture.read() self.simulationStep += 1 #sleep(1) if frame is None: return [] return self.getData(dataFilter.source_item, frame=frame) else: self.simulationStep = 0 return self.simulateData(dataFilter)
def __init__(self, game): self.map = {} # save this for pos in game.map.xymap: if pos not in game.bomb.mines: self.map[pos] = [ Misc.call_tile( game.num.calculatepoint(game.num, pos, game.bomb)), Tile_Status.unopened ] else: self.map[pos] = [Misc.call_tile(9), Tile_Status.unopened]
def encodegradecollege(): while True: quarterGrades = [] i = 0 while i != 2: grade = input() if grade.isdigit(): quarterGrades.append(int(grade)) i += 1 else: Misc.error(type='input') return quarterGrades
def start(self, prod=False): """ Start api for data pool """ if prod: from os.path import dirname, abspath, exists, split, normpath self.CONFIG_FILE = normpath(abspath('.') + "/config.yaml") self.CONFIG = Misc.readConfig(self.CONFIG_FILE) self.initialize(self.CONFIG) app = Flask(__name__) log = logging.getLogger('werkzeug') log.setLevel(self.LOGGINGLEVEL + 10) app.logger = logging.getLogger() api = Api(app) if Misc.toBool(Misc.hasKey(self.CONFIG, 'RUN_IN_COLLAB', 'N')): from flask_ngrok import run_with_ngrok run_with_ngrok(app) #starts ngrok when the app is run @app.route("/") def home(): return "<h1>Running Home-Monitor on Google Colab!</h1>" from ApiServices import ApiAlerts, ApiEvents, ApiTickets, ApiLogs Binnacle().loggingConf(self.CONFIG) Binnacle().logFromCore('Starting /api/tickets', LogTypes.INFO, self.__class__.__name__) api.add_resource(ApiTickets, '/api/tickets', methods=['get', 'post', 'put']) Binnacle().logFromCore('Starting /api/events', LogTypes.INFO, self.__class__.__name__) api.add_resource(ApiEvents, '/api/events', methods=['get', 'post', 'put']) Binnacle().logFromCore('Starting /api/alerts', LogTypes.INFO, self.__class__.__name__) api.add_resource(ApiAlerts, '/api/alerts', methods=['get', 'post', 'put']) Binnacle().logFromCore('Starting /api/logs', LogTypes.INFO, self.__class__.__name__) api.add_resource(ApiLogs, '/api/logs', methods=['post']) if not prod: app.run() else: self.app = app Binnacle().logFromCore(Messages.pool_stoped, LogTypes.INFO, self.__class__.__name__)
def college(): studentName = str(input("Enter student name: ")) studentLevel = "College" while True: subjectslist = [] subjCount = input("Enter the total number of subjects: ").strip() if subjCount in '1234567890': subjCount = int(subjCount) for i in range(int(subjCount)): subjectslist.append(input("Enter subject {}: ".format(i + 1))) break else: Misc.error(type='input') subjgrades = [] quarterTotal = [] quarterAverage = [] for subjects in subjectslist: print("Enter " + subjects + " grades per semester") subjgrades.append(encodegradecollege()) for grades in subjgrades: quarterTotal.append(sum(grades)) for total in quarterTotal: average = total / 2 quarterAverage.append(average) quarterAverage = [float("{:.2f}".format(num)) for num in quarterAverage] generalTotal = sum(quarterAverage) generalAverage = generalTotal / subjCount Misc.load() reportCardcollege(studentName, studentLevel, subjgrades, subjectslist, quarterAverage, generalAverage) newsubgrades = [] for i in subjgrades: for x in i: newsubgrades.append(x) with open('College.txt', 'a') as col: col.write(studentName + '\n') for i in subjectslist: col.write(i.strip() + ' ') col.write('\n') for i in newsubgrades: col.write(str(i) + ' ') col.write('\n') print() input("Report card generated.\nPlease enter any key to continue.") print()
def init_standalone(self, path="./", config=None): """ Start the component isolated of system """ self.ME_PATH = path # Path of current component self.ME_STANDALONE = True if not Misc.existsFile("config.yaml", self.ME_PATH): raise ValueError(Messages.config_no_file) self.ME_CONFIG_PATH = normpath(self.ME_PATH + "/config.yaml") self.ME_CONFIG = Misc.readConfig( self.ME_CONFIG_PATH) if config == None else config self.CP = CommPool(self.ME_CONFIG, standAlone=True) self.load()
def tryNotify(self, msg: Dispatch): """ To send the message using SMS Channel """ # setup the parameters of the message to = msg.to message = msg.message sender = Misc.hasKey(self.ME_CONFIG, "FROM", '') # Your Account Sid and Auth Token from twilio.com/console account_sid = Misc.hasKey(self.ME_CONFIG, "ACCOUNT_SID", '') auth_token = Misc.hasKey(self.ME_CONFIG, "AUTH_TOKEN", '') client = Client(account_sid, auth_token) for number in to: sms = client.messages.create(to=number, from_=sender, body=message)
def OnSSLButton(self, event): """ Handle the Browse buttons for the SSL Client Certificate and the SSL CLient Key file fields """ # Define the File Type as *.pem files fileType = '*.pem' fileTypesString = _("SSL Certificate Files (*.pem)|*.pem|All files (*.*)|*.*") if event.GetId() == self.sslClientCertBrowse.GetId(): prompt = _("Select the SSL Client Certificate file") fileName = self.sslClientCert.GetValue() elif event.GetId() == self.sslClientKeyBrowse.GetId(): prompt = _("Select the SSL Client Key file") fileName = self.sslClientKey.GetValue() elif event.GetId() == self.sslMsgSrvCertBrowse.GetId(): prompt = _("Select the SSL Message Server Certificate file") fileName = self.sslMsgSrvCert.GetValue() (path, flnm) = os.path.split(fileName) if path != '': self.sslDir = path # Invoke the File Selector with the proper default directory, filename, file type, and style fs = wx.FileSelector(prompt, self.sslDir, fileName, fileType, fileTypesString, wx.OPEN | wx.FILE_MUST_EXIST) # If user didn't cancel .. if fs != "": # Mac Filenames use a different encoding system. We need to adjust the string returned by the FileSelector. # Surely there's an easier way, but I can't figure it out. if 'wxMac' in wx.PlatformInfo: import Misc fs = Misc.convertMacFilename(fs) if event.GetId() == self.sslClientCertBrowse.GetId(): self.sslClientCert.SetValue(fs) elif event.GetId() == self.sslClientKeyBrowse.GetId(): self.sslClientKey.SetValue(fs) elif event.GetId() == self.sslMsgSrvCertBrowse.GetId(): self.sslMsgSrvCert.SetValue(fs) (path, flnm) = os.path.split(fs) if path != '': self.sslDir = path
def get_team_by_seed(self, seed): """ :method: Get team object from tournament seed string. :param string seed: tournament seed string :returns: Team object :rtype: object """ Misc.check_input_data("Kaggle", "tourney_seeds") df = Constants.INPUT_DATA['Kaggle']['tourney_seeds'] team_df = df[(df.season == self.season_id) & (df.seed == seed)] if not team_df.empty: team_id = team_df.team.iloc[0] team = self.team_factory.get_team(team_id) else: team = None return team
def add_all_teams(self): """:method: Build objects for all teams from Kaggle data & add them to factory.""" Misc.check_input_data("Kaggle", "tourney_seeds") Misc.check_input_data("Kaggle", "teams") df_seeds = Constants.INPUT_DATA['Kaggle']['tourney_seeds'] for _id, row in df_seeds.iterrows(): # variable init season_id = row.season team_id = int(row.team) tourney_seed = row.seed df_teams = Constants.INPUT_DATA['Kaggle']['teams'] team_name = df_teams[df_teams.id == team_id].name.iloc[0] # create Team object team = self.get_team(team_id) if not team: team = self.add_team(team_id, team_name) season = team.add_season(season_id) season.set_tourney_seed(tourney_seed)
def testDot(fpath): try: cmd = "%s -V" % (fpath) (sts,sout,serr) = Misc.safeCommandOutput(cmd) if sts != -1: if "version" in sout + serr: return True except: pass return False
def __repr__(self): str = 'Transcript Object:\n' str = str + "Number = %s\n" % self.number str = str + "ID = %s\n" % self.id str = str + "Episode Number = %s\n" % self.episode_num str = str + "Source Transcript = %s\n" % self.source_transcript str = str + "Clip Number = %s\n" % self.clip_num str = str + "Sort Order = %s\n" % self.sort_order str = str + "Transcriber = %s\n" % self.transcriber str = str + "Clip Start = %s\n" % Misc.time_in_ms_to_str(self.clip_start) str = str + "Clip Stop = %s\n" % Misc.time_in_ms_to_str(self.clip_stop) str = str + "Comment = %s\n" % self.comment str += "MinTranscriptWidth = %s\n" % self.minTranscriptWidth str = str + "LastSaveTime = %s\n" % self.lastsavetime # str += "isLocked = %s\n" % self._isLocked # str += "recordlock = %s\n" % self.recordlock # str += "locktime = %s\n" % self.locktime if len(self.text) > 250: str = str + "text[:50] = %s\n\n" % self.text[:50] else: str = str + "text = %s\n\n" % self.text return str.encode('utf8')
def ScytherIcon(window): """ Set a nice Scyther icon """ import os,inspect # Determine base directory (taking symbolic links into account) cmd_file = os.path.realpath(os.path.abspath(inspect.getfile( inspect.currentframe() ))) basedir = os.path.split(cmd_file)[0] path = os.path.join(basedir,"Images") iconfile = Misc.mypath(os.path.join(path,"scyther-gui-32.ico")) if os.path.isfile(iconfile): icon = wx.Icon(iconfile,wx.BITMAP_TYPE_ICO) window.SetIcon(icon)
def findDot(): global DOTLOCATION # Cache the results if DOTLOCATION != None: return DOTLOCATION DOTLOCATION = scanLocations() if DOTLOCATION == None: Misc.panic(""" Could not find the required 'dot' program, which is part of the Graphviz suite. Please install it from http://www.graphviz.org/ Ubuntu users: install the 'graphviz' package. Windows users: make sure that Graphviz is installed and that the location of the 'dot' program is in the PATH environment variable. Restarting your system may be needed for Scyther to locate any newly installed programs. """) return DOTLOCATION
def get_input(self): """Custom input routine.""" # Inherit base input routine gen_input = Dialogs.GenForm.get_input(self) # If OK ... if gen_input: # Get main data values from the form, ID, Comment, and Image Filename self.obj.id = gen_input[_('Snapshot ID')] self.obj.image_filename = gen_input[_('Image Filename')] if gen_input[_('Episode Position')] == '': self.obj.episode_start = 0.0 else: self.obj.episode_start = Misc.time_in_str_to_ms(gen_input[_('Episode Position')]) if gen_input[_('Duration')] == '': self.obj.episode_duration = 0.0 else: self.obj.episode_duration = Misc.time_in_str_to_ms(gen_input[_('Duration')]) self.obj.comment = gen_input[_('Comment')] else: self.obj = None return self.obj
def _set_keyword(self, keyword): # Make sure parenthesis characters are not allowed in Keywords if (string.find(keyword, '(') > -1) or (string.find(keyword, ')') > -1): keyword = string.replace(keyword, '(', '') keyword = string.replace(keyword, ')', '') if 'unicode' in wx.PlatformInfo: # Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data. prompt = unicode(_('Keywords cannot contain parenthesis characters.\nYour Keyword has been renamed to "%s".'), 'utf8') % keyword else: prompt = _('Keywords cannot contain parenthesis characters.\nYour Keyword has been renamed to "%s".') % keyword dlg = Dialogs.ErrorDialog(None, prompt) dlg.ShowModal() dlg.Destroy() self._keyword = Misc.unistrip(keyword)
def BinaryGraph(): """ This calculates a graph of the whole binary. :return: a graph in NetworkX format """ FG = nx.DiGraph() for f_ea in Functions(): # TODO: Test a bit more the restrictions regarding # the types of XRefs for xref in XrefsTo(f_ea, 1): (s, e) = misc.function_boundaries(xref.frm) if s: FG.add_edge(s, f_ea) return FG
def _set_keywordGroup(self, keywordGroup): # ALSO SEE Dialogs.add_kw_group_ui(). The same errors are caught there. # Make sure parenthesis characters are not allowed in Keyword Group. Remove them if necessary. if (string.find(keywordGroup, '(') > -1) or (string.find(keywordGroup, ')') > -1): keywordGroup = string.replace(keywordGroup, '(', '') keywordGroup = string.replace(keywordGroup, ')', '') if 'unicode' in wx.PlatformInfo: # Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data. prompt = unicode(_('Keyword Groups cannot contain parenthesis characters.\nYour Keyword Group has been renamed to "%s".'), 'utf8') % keywordGroup else: prompt = _('Keyword Groups cannot contain parenthesis characters.\nYour Keyword Group has been renamed to "%s".') % keywordGroup dlg = Dialogs.ErrorDialog(None, prompt) dlg.ShowModal() dlg.Destroy() # Colons are not allowed in Keyword Groups. Remove them if necessary. if keywordGroup.find(":") > -1: keywordGroup = keywordGroup.replace(':', '') if 'unicode' in wx.PlatformInfo: msg = unicode(_('You may not use a colon (":") in the Keyword Group name. Your Keyword Group has been changed to\n"%s"'), 'utf8') else: msg = _('You may not use a colon (":") in the Keyword Group name. Your Keyword Group has been changed to\n"%s"') dlg = Dialogs.ErrorDialog(None, msg % keywordGroup) dlg.ShowModal() dlg.Destroy() # Let's make sure we don't exceed the maximum allowed length for a Keyword Group. # First, let's see what the max length is. maxLen = TransanaGlobal.maxKWGLength # Check to see if we've exceeded the max length if len(keywordGroup) > maxLen: # If so, truncate the Keyword Group keywordGroup = keywordGroup[:maxLen] # Display a message to the user describing the trunctions if 'unicode' in wx.PlatformInfo: # Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data. msg = unicode(_('Keyword Group is limited to %d characters. Your Keyword Group has been changed to\n"%s"'), 'utf8') else: msg = _('Keyword Group is limited to %d characters. Your Keyword Group has been changed to\n"%s"') dlg = Dialogs.ErrorDialog(None, msg % (maxLen, keywordGroup)) dlg.ShowModal() dlg.Destroy() # Remove white space from the Keyword Group. self._keywordGroup = Misc.unistrip(keywordGroup)
def __repr__(self): str = 'Episode Object:\n' str = str + "Number = %s\n" % self.number str = str + "id = %s\n" % self.id str = str + "comment = %s\n" % self.comment str = str + "Media file 1 = %s\n" % self.media_filename str = str + "Media File 1 Length = %s\n" % self.tape_length str += "Media File 1 Offset = %s\n" % self.offset str = str + "Additional media file:\n" for addFile in self.additional_media_files: str += ' %s %s %s %s\n' % (addFile['filename'], addFile['offset'], addFile['length'], addFile['audio']) str += "Total adjusted Episode Length = %d (%s)\n" % (self.episode_length(), Misc.time_in_ms_to_str(self.episode_length())) str = str + "Date = %s\n" % self.tape_date str = str + "Series ID = %s\n" % self.series_id str = str + "Series Num = %s\n" % self.series_num str += "Keywords:\n" for kw in self._kwlist: str += ' ' + kw.keywordPair + '\n' return str.encode('utf8')
def binary_graph(): """ This calculates a graph of the whole binary. :return: a *DiGraph* in NetworkX format """ fg = nx.DiGraph() for f_ea in Functions(): for xref in XrefsTo(f_ea, True): # NOTE: only code xrefs (that is, call sub_xxx or # alike but not data refs, mov [eax], sub_xxx if not xref.iscode: continue (s, e) = misc.function_boundaries(xref.frm) if s: fg.add_edge(s, f_ea) return fg
def killLines(self, rejectLines, killMatches, killBlockKeys, verbose=False): """ Delete the matching lines from self.data and return. @param rejectLines destination for killed lines, if to be saved. @param killMatches list of matching patterns. matching any pattern kills a line. @param killBlockKeys well-formed blocks matching any key in this list are killed. @return nothing. """ if killMatches == None and killBlockKeys == None: return # first kill block pass if not killBlockKeys == None and len(killBlockKeys) > 0: for key in killBlockKeys: status, span = self.findKeyRanges( key ) if not status == 0: continue if len(span) < 2: continue while len(span) > 1: if verbose: print "start span = ", span start = span[len(span)-2] stop = span[len(span)-1] if not rejectLines == None: for dead in self.data[start:stop+1]: rejectLines.append(dead); del self.data[start:stop+1] span = span[0:len(span)-1] if verbose: print "end span = ", span if not killMatches == None and len(killMatches) > 0: index = 0 for k in self.data: if Misc.symbolContainsListItem(k, killMatches): rejectLines.append(k) del self.data[index] else: index += 1
def DisplayCells(self, TimeCode): """ Get data from the database and populate the Episode Clips / Selected Clips Grid """ # Get clip data from the database clipData = DBInterface.list_of_clips_by_episode(self.episodeObj.number, TimeCode) if TransanaConstants.proVersion: # Get the snapshot data from the database snapshotData = DBInterface.list_of_snapshots_by_episode(self.episodeObj.number, TimeCode) # Combine the two lists cellData = {} # For each Clip ... for clip in clipData: # load the Clip tmpObj = Clip.Clip(clip['ClipNum']) # add the Clip to the cellData cellData[(clip['ClipStart'], clip['ClipStop'], tmpObj.GetNodeString(True).upper())] = tmpObj if TransanaConstants.proVersion: # for each Snapshot ... for snapshot in snapshotData: # load the Snapshot tmpObj = Snapshot.Snapshot(snapshot['SnapshotNum']) # add the Snapshot to the cellData cellData[(snapshot['SnapshotStart'], snapshot['SnapshotStop'], tmpObj.GetNodeString(True).upper())] = tmpObj # Get the Keys for the cellData sortedKeys = cellData.keys() # Sort the keys for the cellData into the right order sortedKeys.sort() # Add rows to the Grid to accomodate the amount of data returned, or delete rows if we have too many if len(cellData) > self.gridClips.GetNumberRows(): self.gridClips.AppendRows(len(cellData) - self.gridClips.GetNumberRows(), False) elif len(cellData) < self.gridClips.GetNumberRows(): self.gridClips.DeleteRows(numRows = self.gridClips.GetNumberRows() - len(cellData)) # Initialize the Row Counter loop = 0 # Add the data to the Grid for keyVals in sortedKeys: # If we have a Clip ... if isinstance(cellData[keyVals], Clip.Clip): # ... get the start, stop times and the object type startTime = cellData[keyVals].clip_start stopTime = cellData[keyVals].clip_stop objType = 'Clip' # Initialize the string for all the Keywords to blank kwString = unicode('', 'utf8') # Initialize the prompt for building the keyword string kwPrompt = '%s' # If we have a Snapshot ... elif isinstance(cellData[keyVals], Snapshot.Snapshot): # ... get the start, stop times and the object type startTime = cellData[keyVals].episode_start stopTime = cellData[keyVals].episode_start + cellData[keyVals].episode_duration objType = 'Snapshot' # if there are whole snapshot keywords ... if len(cellData[keyVals].keyword_list) > 0: # ... initialize the string for all the Keywords to indicate this kwString = unicode(_('Whole:'), 'utf8') + '\n' # If there are NOT whole snapshot keywords ... else: # ... initialize the string for all the Keywords to blank kwString = unicode('', 'utf8') # Initialize the prompt for building the keyword string kwPrompt = ' %s' # For each Keyword in the Keyword List ... for kws in cellData[keyVals].keyword_list: # ... add the Keyword to the Keyword List kwString += kwPrompt % kws.keywordPair # If we have a Clip ... if isinstance(cellData[keyVals], Clip.Clip): # After the first keyword, we need a NewLine in front of the Keywords. This accompishes that! kwPrompt = '\n%s' # If we have a Snapshot ... elif isinstance(cellData[keyVals], Snapshot.Snapshot): # After the first keyword, we need a NewLine in front of the Keywords. This accompishes that! kwPrompt = '\n %s' # If we have a Snapshot, we also want to display CODED Keywords in addition to the WHOLE Snapshot keywords # we've already included if isinstance(cellData[keyVals], Snapshot.Snapshot): # Keep a list of the coded keywords we've already displayed codedKeywords = [] # Modify the template for additional keywords kwPrompt = '\n %s : %s' # For each of the Snapshot's Coding Objects ... for x in range(len(cellData[keyVals].codingObjects)): # ... if the Coding Object is visible and if it is not already in the codedKeywords list ... if (cellData[keyVals].codingObjects[x]['visible']) and \ (not (cellData[keyVals].codingObjects[x]['keywordGroup'], cellData[keyVals].codingObjects[x]['keyword']) in codedKeywords): # ... if this is the FIRST Coded Keyword ... if len(codedKeywords) == 0: # ... and if there WERE Whole Snapshot Keywords ... if len(kwString) > 0: # ... then add a line break to the Keywords String ... kwString += '\n' # ... add the indicator to the Keywords String that we're starting to show Coded Keywords kwString += unicode(_('Coded:'), 'utf8') # ... add the coded keyword to the Keywords String ... kwString += kwPrompt % (cellData[keyVals].codingObjects[x]['keywordGroup'], cellData[keyVals].codingObjects[x]['keyword']) # ... add the keyword to the Coded Keywords list codedKeywords.append((cellData[keyVals].codingObjects[x]['keywordGroup'], cellData[keyVals].codingObjects[x]['keyword'])) # Insert the data values into the Grid Row # Start and Stop time in column 0 self.gridClips.SetCellValue(loop, 0, "%s -\n %s" % (Misc.time_in_ms_to_str(startTime), Misc.time_in_ms_to_str(stopTime))) # Node String (including Item name) in column 1 self.gridClips.SetCellValue(loop, 1, cellData[keyVals].GetNodeString(True)) # make the Collection / Item ID line auto-word-wrap self.gridClips.SetCellRenderer(loop, 1, grid.GridCellAutoWrapStringRenderer()) # Keywords in column 2 self.gridClips.SetCellValue(loop, 2, kwString) # Item Number (hidden) in column 3. Convert value to a string self.gridClips.SetCellValue(loop, 3, "%s" % cellData[keyVals].number) # Item Type (hidden) in column 4 self.gridClips.SetCellValue(loop, 4, "%s" % objType) # Auto-size THIS row self.gridClips.AutoSizeRow(loop, True) # Increment the Row Counter loop += 1 # Select the first cell self.gridClips.SetGridCursor(0, 0)
def data_available(self, season_id): Misc.check_input_data("Kaggle", "tourney_seeds", raise_exception=True) return True
def _set_id(self, id): self._id = Misc.unistrip(id)
def OnItemSelected(self, event): """ Process the selection of a clip to be merged with the original clip """ # Identify the selected item self.mergeItemIndex = event.GetIndex() # Get the Clip Data for the selected item mergeClip = Clip.Clip(self.mergeList[self.mergeItemIndex][0]) # re-initialize the TRANSCRIPT start and stop times self.transcript_clip_start = [] self.transcript_clip_stop = [] # Merge the clips. # First, determine whether the merging clip comes BEFORE or after the original if mergeClip.clip_start < self.obj.clip_start: # The start value comes from the merge clip self.clip_start_edit.SetValue(Misc.time_in_ms_to_str(mergeClip.clip_start)) # Update the merged clip Start Time self.clip_start = mergeClip.clip_start # The stop value comes from the original clip self.clip_stop_edit.SetValue(Misc.time_in_ms_to_str(self.obj.clip_stop)) # Update the Clip Length self.clip_length_edit.SetValue(Misc.time_in_ms_to_str(self.obj.clip_stop - mergeClip.clip_start)) # Update the merged clip Stop Time self.clip_stop = self.obj.clip_stop # For each of the original clip's Transcripts ... for x in range(len(self.obj.transcripts)): # We get the TRANSCRIPT start time from the merge clip self.transcript_clip_start.append(mergeClip.transcripts[x].clip_start) # We get the TRANSCRIPT end time from the original clip self.transcript_clip_stop.append(self.obj.transcripts[x].clip_stop) # If we're using the Rich Text Ctrl ... if TransanaConstants.USESRTC: # ... clear the transcript ... self.text_edit[x].ClearDoc(skipUnlock = True) # ... turn off read-only ... self.text_edit[x].SetReadOnly(0) # Create the Transana XML to RTC Import Parser. This is needed so that we can # pull XML transcripts into the existing RTC. Pass the RTC to be edited. handler = PyXML_RTCImportParser.XMLToRTCHandler(self.text_edit[x]) # Parse the merge clip transcript text, adding it to the RTC xml.sax.parseString(mergeClip.transcripts[x].text, handler) # Add a blank line self.text_edit[x].Newline() # ... trap exceptions here ... try: # ... insert a time code at the position of the clip break ... self.text_edit[x].insert_timecode(self.obj.clip_start) # If there were exceptions (duplicating time codes, for example), just skip it. except: pass # Parse the original transcript text, adding it to the RTC xml.sax.parseString(self.obj.transcripts[x].text, handler) # If we're using the Styled Text Ctrl else: # ... clear the transcript ... self.text_edit[x].ClearDoc() # ... turn off read-only ... self.text_edit[x].SetReadOnly(0) # ... insert the merge clip's text, skipping whitespace at the end ... self.text_edit[x].InsertRTFText(mergeClip.transcripts[x].text.rstrip()) # ... add a couple of line breaks ... self.text_edit[x].InsertStyledText('\n\n', len('\n\n')) # ... trap exceptions here ... try: # ... insert a time code at the position of the clip break ... self.text_edit[x].insert_timecode(self.obj.clip_start) # If there were exceptions (duplicating time codes, for example), just skip it. except: pass # ... now add the original clip's text ... self.text_edit[x].InsertRTFText(self.obj.transcripts[x].text) # ... signal that time codes will be visible, which they always are in the Clip Properties ... self.text_edit[x].codes_vis = 0 # ... scan transcript for Time Codes ... self.text_edit[x].load_timecodes() # ... display the time codes self.text_edit[x].show_codes() # If the merging clip comes AFTER the original ... else: # The start value comes from the original clip self.clip_start_edit.SetValue(Misc.time_in_ms_to_str(self.obj.clip_start)) # Update the merged clip Start Time self.clip_start = self.obj.clip_start # The stop value comes from the merge clip self.clip_stop_edit.SetValue(Misc.time_in_ms_to_str(mergeClip.clip_stop)) # Update the Clip Length self.clip_length_edit.SetValue(Misc.time_in_ms_to_str(mergeClip.clip_stop - self.obj.clip_start)) # Update the merged clip Stop Time self.clip_stop = mergeClip.clip_stop # For each of the original clip's Transcripts ... for x in range(len(self.obj.transcripts)): # We get the TRANSCRIPT start time from the original clip self.transcript_clip_start.append(self.obj.transcripts[x].clip_start) # We get the TRANSCRIPT end time from the merge clip self.transcript_clip_stop.append(mergeClip.transcripts[x].clip_stop) # If we're using the Rich Text Ctrl ... if TransanaConstants.USESRTC: # ... clear the transcript ... self.text_edit[x].ClearDoc(skipUnlock = True) # ... turn off read-only ... self.text_edit[x].SetReadOnly(0) # Create the Transana XML to RTC Import Parser. This is needed so that we can # pull XML transcripts into the existing RTC. Pass the RTC in. handler = PyXML_RTCImportParser.XMLToRTCHandler(self.text_edit[x]) # Parse the original clip transcript text, adding it to the reportText RTC xml.sax.parseString(self.obj.transcripts[x].text, handler) # Add a blank line self.text_edit[x].Newline() # ... trap exceptions here ... try: # ... insert a time code at the position of the clip break ... self.text_edit[x].insert_timecode(mergeClip.clip_start) # If there were exceptions (duplicating time codes, for example), just skip it. except: pass # ... now add the merge clip's text ... # Parse the merge clip transcript text, adding it to the RTC xml.sax.parseString(mergeClip.transcripts[x].text, handler) # If we're using the Styled Text Ctrl else: # ... clear the transcript ... self.text_edit[x].ClearDoc() # ... turn off read-only ... self.text_edit[x].SetReadOnly(0) # ... insert the original clip's text, skipping whitespace at the end ... self.text_edit[x].InsertRTFText(self.obj.transcripts[x].text.rstrip()) # ... add a couple of line breaks ... self.text_edit[x].InsertStyledText('\n\n', len('\n\n')) # ... trap exceptions here ... try: # ... insert a time code at the position of the clip break ... self.text_edit[x].insert_timecode(mergeClip.clip_start) # If there were exceptions (duplicating time codes, for example), just skip it. except: pass # ... now add the merge clip's text ... self.text_edit[x].InsertRTFText(mergeClip.transcripts[x].text) # ... signal that time codes will be visible, which they always are in the Clip Properties ... self.text_edit[x].codes_vis = 0 # ... scan transcript for Time Codes ... self.text_edit[x].load_timecodes() # ... display the time codes self.text_edit[x].show_codes() # Remember the Merged Clip's Clip Number self.obj.mergeNumber = mergeClip.number # Create a list object for merging the keywords kwList = [] # Add all the original keywords for kws in self.obj.keyword_list: kwList.append(kws.keywordPair) # Iterate through the merge clip keywords. for kws in mergeClip.keyword_list: # If they're not already in the list, add them. if not kws.keywordPair in kwList: kwList.append(kws.keywordPair) # Sort the keyword list kwList.sort() # Clear the keyword list box. self.ekw_lb.Clear() # Add the merged keywords to the list box. for kws in kwList: self.ekw_lb.Append(kws) # Get the Keyword Examples for the merged clip, if any. kwExamples = DBInterface.list_all_keyword_examples_for_a_clip(mergeClip.number) # Initialize a variable to hold merged keyword examples. (This clears the variable if the user changes merge clips.) self.mergedKeywordExamples = [] # Iterate through the keyword examples and add them to the list. for kw in kwExamples: self.mergedKeywordExamples.append(kw[:2])
for i in range(len(kaggle_output_options)): prompt += "%s) %s\n" % (i, kaggle_output_options[i]) user_input = raw_input(prompt) # check user input if user_input.isdigit() and (int(user_input) in range(num_kaggle_output_options)): kaggle_output = kaggle_output_options[int(user_input)] else: raise Exception("Invalid entry '%s', please enter one of following values %s." % (user_input, range(num_kaggle_output_options))) # create TeamFactory team_factory = Basketball.TeamFactory() team_factory.add_all_teams() # analyze each available season Misc.check_input_data("Kaggle", "seasons") for _, row in Constants.INPUT_DATA['Kaggle']['seasons'].iterrows(): year = Misc.get_tournament_year(row.years) print("Predicting %s NCAA Tournament" % year) season_id = Constants.SEASON_YEAR_TO_ID[year] if analysis.data_available(season_id): tournament = Basketball.Tournament(team_factory, season_id) tournament.generate_matchup_probabilities(analysis) tournament.generate_bracket(analysis) tournaments[row.season] = tournament
def OnSnapshot(self, event): """ Handle the Snapshot button press """ # Just because the media file is showing doesn't mean it's the CURRENT item in the Transana interface. # Let's bring the Transcript forward, making the media file part of the CURRENT interface self.parent.ControlObject.BringTranscriptToFront() # Create the Media Conversion dialog, including Clip Information so we export only the clip segment convertDlg = MediaConvert.MediaConvert(self, self.parent.ControlObject.currentObj.media_filename, self.parent.ControlObject.GetVideoPosition(), snapshot=True) # Show the Media Conversion Dialog convertDlg.ShowModal() # If the user took a snapshop and the image was successfully created ... if convertDlg.snapshotSuccess and os.path.exists(convertDlg.txtDestFileName.GetValue() % 1): imgFile = convertDlg.txtDestFileName.GetValue() % 1 # Set the field value to the file name selected self.fname_edit.SetValue(imgFile) # If the form's ID field is empty ... if self.id_edit.GetValue() == '': # ... get the base filename tempFilename = os.path.basename(imgFile) # ... separate the filename root and extension (self.obj.id, tempExt) = os.path.splitext(tempFilename) # ... and set the ID to match the base file name self.id_edit.SetValue(self.obj.id) # If the Snapshot comes from an Episode ... if isinstance(self.parent.ControlObject.currentObj, Episode.Episode): self.obj.series_num = self.parent.ControlObject.currentObj.series_num self.obj.series_id = self.parent.ControlObject.currentObj.series_id self.obj.episode_num = self.parent.ControlObject.currentObj.number self.obj.episode_id = self.parent.ControlObject.currentObj.id self.obj.transcript_num = self.parent.ControlObject.TranscriptWindow.dlg.editor.TranscriptObj.number self.series_cb.SetStringSelection(self.obj.series_id) self.PopulateEpisodeChoiceBasedOnSeries(self.obj.series_id) self.episode_cb.SetStringSelection(self.obj.episode_id) self.PopulateTranscriptChoiceBasedOnEpisode(self.obj.series_id, self.obj.episode_id) tmpTranscript = Transcript.Transcript(self.obj.transcript_num) self.transcript_cb.SetStringSelection(tmpTranscript.id) self.obj.episode_start = self.parent.ControlObject.GetVideoPosition() self.episode_start_edit.SetValue(Misc.time_in_ms_to_str(self.obj.episode_start)) self.episode_start_edit.Enable(True) self.obj.episode_duration = 10000 self.episode_duration_edit.SetValue(Misc.time_in_ms_to_str(self.obj.episode_duration)) self.episode_duration_edit.Enable(True) # If the Snapshot comes from a Clip ... elif isinstance(self.parent.ControlObject.currentObj, Clip.Clip): self.obj.episode_num = self.parent.ControlObject.currentObj.episode_num tmpEpisode = Episode.Episode(self.obj.episode_num) self.obj.series_num = tmpEpisode.series_num self.obj.series_id = tmpEpisode.series_id self.obj.episode_id = tmpEpisode.id # We need the Clip's SOURCE TRANSCRIPT for the Active Transcript self.obj.transcript_num = self.parent.ControlObject.currentObj.transcripts[self.parent.ControlObject.activeTranscript].source_transcript self.series_cb.SetStringSelection(self.obj.series_id) self.PopulateEpisodeChoiceBasedOnSeries(self.obj.series_id) self.episode_cb.SetStringSelection(self.obj.episode_id) self.PopulateTranscriptChoiceBasedOnEpisode(self.obj.series_id, self.obj.episode_id) tmpTranscript = Transcript.Transcript(self.obj.transcript_num) self.transcript_cb.SetStringSelection(tmpTranscript.id) self.obj.episode_start = self.parent.ControlObject.currentObj.clip_start self.episode_start_edit.SetValue(Misc.time_in_ms_to_str(self.obj.episode_start)) self.episode_start_edit.Enable(True) self.obj.episode_duration = self.parent.ControlObject.currentObj.clip_stop - self.parent.ControlObject.currentObj.clip_start - 0.1 self.episode_duration_edit.SetValue(Misc.time_in_ms_to_str(self.obj.episode_duration)) self.episode_duration_edit.Enable(True) # We need to explicitly Close the conversion dialog here to force cleanup of temp files in some circumstances convertDlg.Close() # Destroy the Media Conversion Dialog convertDlg.Destroy()
def __init__(self, parent, id, title, snapshot_object): # ... use the default Clip Properties Dialog size passed in from the config object. (This does NOT get saved.) size = TransanaGlobal.configData.clipPropertiesSize # Define the Help Context HelpContext='Snapshot Properties' # Make the Keyword Edit List resizable by passing wx.RESIZE_BORDER style. Signal that Propogation is included. Dialogs.GenForm.__init__(self, parent, id, title, size=size, style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, useSizers = True, HelpContext=HelpContext) # Define the minimum size for this dialog as the initial size minWidth = 750 minHeight = 570 # Remember the Parent Window self.parent = parent # Remember the original Snapshot Object passed in self.obj = snapshot_object # Create the form's main VERTICAL sizer mainSizer = wx.BoxSizer(wx.VERTICAL) # Create a HORIZONTAL sizer for the first row r1Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v1 = wx.BoxSizer(wx.VERTICAL) # Snapshot ID self.id_edit = self.new_edit_box(_("Snapshot ID"), v1, self.obj.id, maxLen=100) # Add the element to the sizer r1Sizer.Add(v1, 1, wx.EXPAND) # Add a horizontal spacer to the row sizer r1Sizer.Add((10, 0)) # Update the Snapshot's Collection ID based on the Snapshot's Collection Number self.obj._sync_snapshot() # Create a VERTICAL sizer for the next element v2 = wx.BoxSizer(wx.VERTICAL) # Collection ID collection_edit = self.new_edit_box(_("Collection ID"), v2, self.obj.GetNodeString(False)) # Add the element to the sizer r1Sizer.Add(v2, 2, wx.EXPAND) collection_edit.Enable(False) # Add the row sizer to the main vertical sizer mainSizer.Add(r1Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r2Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v3 = wx.BoxSizer(wx.VERTICAL) # Image Filename # If the image filename path is not empty, we should normalize the path specification if self.obj.image_filename == '': filePath = self.obj.image_filename else: filePath = os.path.normpath(self.obj.image_filename) self.fname_edit = self.new_edit_box(_("Image Filename"), v3, filePath) r2Sizer.Add(v3, 5, wx.EXPAND) r2Sizer.Add((10, 0)) # Browse button layout btnBrowse = wx.Button(self.panel, wx.ID_FILE1, _("Browse"), wx.DefaultPosition) r2Sizer.Add(btnBrowse, 0, wx.EXPAND | wx.TOP, 20) wx.EVT_BUTTON(self, wx.ID_FILE1, self.OnBrowse) # Figure out if the Snapshot Button should be displayed if ((isinstance(self.parent.ControlObject.currentObj, Episode.Episode)) or \ (isinstance(self.parent.ControlObject.currentObj, Clip.Clip))) and \ (len(self.parent.ControlObject.VideoWindow.mediaPlayers) == 1): # Add a horizontal spacer r2Sizer.Add((10, 0)) # Snapshot button layout # Create the Snapshot button btnSnapshot = wx.BitmapButton(self.panel, -1, TransanaImages.Snapshot.GetBitmap(), size=(48, 24)) # Set the Help String btnSnapshot.SetToolTipString(_("Capture Snapshot for Coding")) r2Sizer.Add(btnSnapshot, 0, wx.EXPAND | wx.TOP, 20) # Bind the Snapshot button to its event handler btnSnapshot.Bind(wx.EVT_BUTTON, self.OnSnapshot) # Add the row sizer to the main vertical sizer mainSizer.Add(r2Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r4Sizer = wx.BoxSizer(wx.HORIZONTAL) self.seriesList = DBInterface.list_of_series() seriesRecs = [''] serDefault = 0 for (seriesNum, seriesName) in self.seriesList: seriesRecs.append(seriesName) if self.obj.series_num == seriesNum: serDefault = len(seriesRecs) - 1 # Create a VERTICAL sizer for the next element v4 = wx.BoxSizer(wx.VERTICAL) # Library ID self.series_cb = self.new_choice_box(_("Library ID"), v4, seriesRecs, default = serDefault) # Add the element to the sizer r4Sizer.Add(v4, 1, wx.EXPAND) self.series_cb.Bind(wx.EVT_CHOICE, self.OnSeriesChoice) # Add a horizontal spacer to the row sizer r4Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v5 = wx.BoxSizer(wx.VERTICAL) # Episode ID self.episode_cb = self.new_choice_box(_("Episode ID"), v5, ['']) # Add the element to the sizer r4Sizer.Add(v5, 1, wx.EXPAND) self.episode_cb.Bind(wx.EVT_CHOICE, self.OnEpisodeChoice) self.episodeList = [] if self.obj.series_id != '': self.PopulateEpisodeChoiceBasedOnSeries(self.obj.series_id) # Add the row sizer to the main vertical sizer mainSizer.Add(r4Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r5Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v6 = wx.BoxSizer(wx.VERTICAL) # Episode ID self.transcript_cb = self.new_choice_box(_("Transcript ID"), v6, ['']) # Add the element to the sizer r5Sizer.Add(v6, 2, wx.EXPAND) self.transcript_cb.Bind(wx.EVT_CHOICE, self.OnTranscriptChoice) self.transcriptList = [] if self.obj.episode_id != '': self.PopulateTranscriptChoiceBasedOnEpisode(self.obj.series_id, self.obj.episode_id) # Add a horizontal spacer to the row sizer r5Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v7 = wx.BoxSizer(wx.VERTICAL) # Episode Time Code. Convert to HH:MM:SS.mm self.episode_start_edit = self.new_edit_box(_("Episode Position"), v7, Misc.time_in_ms_to_str(self.obj.episode_start)) # Add the element to the sizer r5Sizer.Add(v7, 1, wx.EXPAND) if self.episode_cb.GetStringSelection() == '': self.episode_start_edit.Enable(False) # Add a horizontal spacer to the row sizer r5Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v8 = wx.BoxSizer(wx.VERTICAL) # Episode Duration. Convert to HH:MM:SS.mm self.episode_duration_edit = self.new_edit_box(_("Duration"), v8, Misc.time_in_ms_to_str(self.obj.episode_duration)) # Add the element to the sizer r5Sizer.Add(v8, 1, wx.EXPAND) if self.episode_cb.GetStringSelection() == '': self.episode_duration_edit.Enable(False) # Add the row sizer to the main vertical sizer mainSizer.Add(r5Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r6Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v9 = wx.BoxSizer(wx.VERTICAL) # Comment comment_edit = self.new_edit_box(_("Comment"), v9, self.obj.comment, maxLen=255) # Add the element to the sizer r6Sizer.Add(v9, 1, wx.EXPAND) # Add the row sizer to the main vertical sizer mainSizer.Add(r6Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r7Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v10 = wx.BoxSizer(wx.VERTICAL) # Keyword Group [label] txt = wx.StaticText(self.panel, -1, _("Keyword Group")) v10.Add(txt, 0, wx.BOTTOM, 3) # Keyword Group [list box] # Create an empty Keyword Group List for now. We'll populate it later (for layout reasons) self.kw_groups = [] self.kw_group_lb = wx.ListBox(self.panel, -1, choices = self.kw_groups) v10.Add(self.kw_group_lb, 1, wx.EXPAND) # Add the element to the sizer r7Sizer.Add(v10, 1, wx.EXPAND) # Create an empty Keyword List for now. We'll populate it later (for layout reasons) self.kw_list = [] wx.EVT_LISTBOX(self, self.kw_group_lb.GetId(), self.OnGroupSelect) # Add a horizontal spacer r7Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v11 = wx.BoxSizer(wx.VERTICAL) # Keyword [label] txt = wx.StaticText(self.panel, -1, _("Keyword")) v11.Add(txt, 0, wx.BOTTOM, 3) # Keyword [list box] self.kw_lb = wx.ListBox(self.panel, -1, choices = self.kw_list, style=wx.LB_EXTENDED) v11.Add(self.kw_lb, 1, wx.EXPAND) wx.EVT_LISTBOX_DCLICK(self, self.kw_lb.GetId(), self.OnAddKW) # Add the element to the sizer r7Sizer.Add(v11, 1, wx.EXPAND) # Add a horizontal spacer r7Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v12 = wx.BoxSizer(wx.VERTICAL) # Keyword transfer buttons add_kw = wx.Button(self.panel, wx.ID_FILE2, ">>", wx.DefaultPosition) v12.Add(add_kw, 0, wx.EXPAND | wx.TOP, 20) wx.EVT_BUTTON(self, wx.ID_FILE2, self.OnAddKW) rm_kw = wx.Button(self.panel, wx.ID_FILE3, "<<", wx.DefaultPosition) v12.Add(rm_kw, 0, wx.EXPAND | wx.TOP, 10) wx.EVT_BUTTON(self, wx.ID_FILE3, self.OnRemoveKW) kwm = wx.BitmapButton(self.panel, wx.ID_FILE4, TransanaImages.KWManage.GetBitmap()) v12.Add(kwm, 0, wx.EXPAND | wx.TOP, 10) # Add a spacer to increase the height of the Keywords section v12.Add((0, 60)) kwm.SetToolTipString(_("Keyword Management")) wx.EVT_BUTTON(self, wx.ID_FILE4, self.OnKWManage) # Add the element to the sizer r7Sizer.Add(v12, 0) # Add a horizontal spacer r7Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v13 = wx.BoxSizer(wx.VERTICAL) # Whole Snapshot Keywords [label] txt = wx.StaticText(self.panel, -1, _("Whole Snapshot Keywords")) v13.Add(txt, 0, wx.BOTTOM, 3) # Clip Keywords [list box] # Create an empty ListBox. We'll populate it later for layout reasons. self.ekw_lb = wx.ListBox(self.panel, -1, style=wx.LB_EXTENDED) v13.Add(self.ekw_lb, 1, wx.EXPAND) self.ekw_lb.Bind(wx.EVT_KEY_DOWN, self.OnKeywordKeyDown) # Add the element to the sizer r7Sizer.Add(v13, 2, wx.EXPAND) # Add the row sizer to the main vertical sizer mainSizer.Add(r7Sizer, 5, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a sizer for the buttons btnSizer = wx.BoxSizer(wx.HORIZONTAL) # Add the buttons self.create_buttons(sizer=btnSizer) # Add the button sizer to the main sizer mainSizer.Add(btnSizer, 0, wx.EXPAND) ## # Because of the way Clips are created (with Drag&Drop / Cut&Paste functions), we have to trap the missing ## # ID error here. Therefore, we need to override the EVT_BUTTON for the OK Button. ## # Since we don't have an object for the OK Button, we use FindWindowById to find it based on its ID. ## self.Bind(wx.EVT_BUTTON, self.OnOK, self.FindWindowById(wx.ID_OK)) ## # We also need to intercept the Cancel button. ## self.Bind(wx.EVT_BUTTON, self.OnCancel, self.FindWindowById(wx.ID_CANCEL)) ## ## self.Bind(wx.EVT_SIZE, self.OnSize) ## # Set the main sizer self.panel.SetSizer(mainSizer) # Tell the panel to auto-layout self.panel.SetAutoLayout(True) # Lay out the Panel self.panel.Layout() # Lay out the form self.Layout() # Resize the form to fit the contents self.Fit() # Get the new size of the form (width, height) = self.GetSizeTuple() # Reset the form's size to be at least the specified minimum width self.SetSize(wx.Size(max(minWidth, width), max(minHeight, height))) # Define the minimum size for this dialog as the current size self.SetSizeHints(max(minWidth, width), max(minHeight, height)) # Center the form on screen TransanaGlobal.CenterOnPrimary(self) # We need to set some minimum sizes so the sizers will work right self.kw_group_lb.SetSizeHints(minW = 50, minH = 20) self.kw_lb.SetSizeHints(minW = 50, minH = 20) self.ekw_lb.SetSizeHints(minW = 50, minH = 20) # We populate the Keyword Groups, Keywords, and Clip Keywords lists AFTER we determine the Form Size. # Long Keywords in the list were making the form too big! self.kw_groups = DBInterface.list_of_keyword_groups() for keywordGroup in self.kw_groups: self.kw_group_lb.Append(keywordGroup) # Populate the Keywords ListBox # Load the parent Collection in order to determine the default Keyword Group tempCollection = Collection.Collection(self.obj.collection_num) # Select the Collection Default Keyword Group in the Keyword Group list if (tempCollection.keyword_group != '') and (self.kw_group_lb.FindString(tempCollection.keyword_group) != wx.NOT_FOUND): self.kw_group_lb.SetStringSelection(tempCollection.keyword_group) # If no Default Keyword Group is defined, select the first item in the list else: # but only if there IS a first item. if len(self.kw_groups) > 0: self.kw_group_lb.SetSelection(0) # If there's a selected keyword group ... if self.kw_group_lb.GetSelection() != wx.NOT_FOUND: # populate the Keywords list self.kw_list = \ DBInterface.list_of_keywords_by_group(self.kw_group_lb.GetStringSelection()) else: # If not, create a blank one self.kw_list = [] for keyword in self.kw_list: self.kw_lb.Append(keyword) # Populate the Snapshot Keywords ListBox # If the snapshot object has keywords ... for snapshotKeyword in self.obj.keyword_list: # ... add them to the keyword list self.ekw_lb.Append(snapshotKeyword.keywordPair) # Set initial focus to the Clip ID self.id_edit.SetFocus()
def three_pops(phi, xx, T, nu1=1, nu2=1, nu3=1, m12=0, m13=0, m21=0, m23=0, m31=0, m32=0, gamma1=0, gamma2=0, gamma3=0, h1=0.5, h2=0.5, h3=0.5, theta0=1, initial_t=0, frozen1=False, frozen2=False, frozen3=False): """ Integrate a 3-dimensional phi foward. phi: Initial 3-dimensional phi xx: 1-dimensional grid upon (0,1) overwhich phi is defined. It is assumed that this grid is used in all dimensions. nu's, gamma's, m's, and theta0 may be functions of time. nu1,nu2,nu3: Population sizes gamma1,gamma2,gamma3: Selection coefficients on *all* segregating alleles h1,h2,h3: Dominance coefficients. h = 0.5 corresponds to genic selection. m12,m13,m21,m23,m31,m32: Migration rates. Note that m12 is the rate *into 1 from 2*. theta0: Propotional to ancestral size. Typically constant. T: Time at which to halt integration initial_t: Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.) Note: Generalizing to different grids in different phi directions is straightforward. The tricky part will be later doing the extrapolation correctly. """ phi = phi.copy() if T - initial_t == 0: return phi elif T - initial_t < 0: raise ValueError('Final integration time T (%f) is less than ' 'intial_time (%f). Integration cannot be run ' 'backwards.' % (T, initial_t)) if (frozen1 and (m12 != 0 or m21 != 0 or m13 !=0 or m31 != 0))\ or (frozen2 and (m12 != 0 or m21 != 0 or m23 !=0 or m32 != 0))\ or (frozen3 and (m13 != 0 or m31 != 0 or m23 !=0 or m32 != 0)): raise ValueError('Population cannot be frozen and have non-zero ' 'migration to or from it.') vars_to_check = [nu1,nu2,nu3,m12,m13,m21,m23,m31,m32,gamma1,gamma2, gamma3,h1,h2,h3,theta0] if numpy.all([numpy.isscalar(var) for var in vars_to_check]): return _three_pops_const_params(phi, xx, T, nu1, nu2, nu3, m12, m13, m21, m23, m31, m32, gamma1, gamma2, gamma3, h1, h2, h3, theta0, initial_t, frozen1, frozen2, frozen3) zz = yy = xx nu1_f = Misc.ensure_1arg_func(nu1) nu2_f = Misc.ensure_1arg_func(nu2) nu3_f = Misc.ensure_1arg_func(nu3) m12_f = Misc.ensure_1arg_func(m12) m13_f = Misc.ensure_1arg_func(m13) m21_f = Misc.ensure_1arg_func(m21) m23_f = Misc.ensure_1arg_func(m23) m31_f = Misc.ensure_1arg_func(m31) m32_f = Misc.ensure_1arg_func(m32) gamma1_f = Misc.ensure_1arg_func(gamma1) gamma2_f = Misc.ensure_1arg_func(gamma2) gamma3_f = Misc.ensure_1arg_func(gamma3) h1_f = Misc.ensure_1arg_func(h1) h2_f = Misc.ensure_1arg_func(h2) h3_f = Misc.ensure_1arg_func(h3) theta0_f = Misc.ensure_1arg_func(theta0) current_t = initial_t nu1,nu2,nu3 = nu1_f(current_t), nu2_f(current_t), nu3_f(current_t) m12,m13 = m12_f(current_t), m13_f(current_t) m21,m23 = m21_f(current_t), m23_f(current_t) m31,m32 = m31_f(current_t), m32_f(current_t) gamma1,gamma2 = gamma1_f(current_t), gamma2_f(current_t) gamma3 = gamma3_f(current_t) h1,h2,h3 = h1_f(current_t), h2_f(current_t), h3_f(current_t) dx,dy,dz = numpy.diff(xx),numpy.diff(yy),numpy.diff(zz) dt = min(_compute_dt(dx,nu1,[m12,m13],gamma1,h1), _compute_dt(dy,nu2,[m21,m23],gamma2,h2), _compute_dt(dz,nu3,[m31,m32],gamma3,h3)) steps = 0 while current_t < T: dt = min(_compute_dt(dx,nu1,[m12,m13],gamma1,h1), _compute_dt(dy,nu2,[m21,m23],gamma2,h2), _compute_dt(dz,nu3,[m31,m32],gamma3,h3)) this_dt = min(dt, T - current_t) next_t = current_t + this_dt steps += 1 nu1,nu2,nu3 = nu1_f(next_t), nu2_f(next_t), nu3_f(next_t) m12,m13 = m12_f(next_t), m13_f(next_t) m21,m23 = m21_f(next_t), m23_f(next_t) m31,m32 = m31_f(next_t), m32_f(next_t) gamma1,gamma2 = gamma1_f(next_t), gamma2_f(next_t) gamma3 = gamma3_f(next_t) h1,h2,h3 = h1_f(next_t), h2_f(next_t), h3_f(next_t) theta0 = theta0_f(next_t) if numpy.any(numpy.less([T,nu1,nu2,nu3,m12,m13,m21,m23,m31,m32,theta0], 0)): raise ValueError('A time, population size, migration rate, or ' 'theta0 is < 0. Has the model been mis-specified?') if numpy.any(numpy.equal([nu1,nu2,nu3], 0)): raise ValueError('A population size is 0. Has the model been ' 'mis-specified?') _inject_mutations_3D(phi, this_dt, xx, yy, zz, theta0, frozen1, frozen2, frozen3) if not frozen1: phi = int_c.implicit_3Dx(phi, xx, yy, zz, nu1, m12, m13, gamma1, h1, this_dt, use_delj_trick) if not frozen2: phi = int_c.implicit_3Dy(phi, xx, yy, zz, nu2, m21, m23, gamma2, h2, this_dt, use_delj_trick) if not frozen3: phi = int_c.implicit_3Dz(phi, xx, yy, zz, nu3, m31, m32, gamma3, h3, this_dt, use_delj_trick) current_t = next_t return phi
def Export(self): """ Export the Analytic Data to a Tab-delimited file """ # Initialize values for data structures for this report # The Episode List is the list of Episodes to be sent to the Filter Dialog for the Library report episodeList = [] # The Document List is the list of Documents to be sent to the Filter Dialog for the Library report documentList = [] # The Quote List is the list of Quotes to be sent to the Filter Dialog quoteList = [] # The Quote Lookup allows us to find the Quote Number based on the data from the Quote List quoteLookup = {} # The Clip List is the list of Clips to be sent to the Filter Dialog clipList = [] # The Clip Lookup allows us to find the Clip Number based on the data from the Clip List clipLookup = {} # The Keyword List is the list of Keywords to be sent to the Filter Dialog keywordList = [] # Show a WAIT cursor. Assembling the data can take noticable time in some cases. TransanaGlobal.menuWindow.SetCursor(wx.StockCursor(wx.CURSOR_WAIT)) # If we have an Library Number, we set up the Library Analytic Data Export if self.libraryNum <> 0: # Get the Library record tempLibrary = Library.Library(self.libraryNum) # obtain a list of all Documents in the Library tempDocumentList = DBInterface.list_of_documents(tempLibrary.number) # initialize the temporary Quote List tempQuoteList = [] # iterate through the Document List ... for documentRecord in tempDocumentList: # ... and add each Document's Quotes to the Temporary Quote list tempQuoteList += DBInterface.list_of_quotes_by_document(documentRecord[0]) # Add the Document data to the Filter Dialog's Document List documentList.append((documentRecord[1], tempLibrary.id, True)) # For all the Quotes ... for quoteRecord in tempQuoteList: # ... add the Quote to the Quote List for filtering ... quoteList.append((quoteRecord['QuoteID'], quoteRecord['CollectNum'], True)) # ... retain a pointer to the Quote Number keyed to the Quote ID and Collection Number ... quoteLookup[(quoteRecord['QuoteID'], quoteRecord['CollectNum'])] = quoteRecord['QuoteNum'] # ... now get all the keywords for this Quote ... quoteKeywordList = DBInterface.list_of_keywords(Quote = quoteRecord['QuoteNum']) # ... and iterate through the list of Quote keywords. for quoteKeyword in quoteKeywordList: # If the keyword isn't already in the Keyword List ... if (quoteKeyword[0], quoteKeyword[1], True) not in keywordList: # ... add the keyword to the keyword list for filtering. keywordList.append((quoteKeyword[0], quoteKeyword[1], True)) # obtain a list of all Episodes in that Library tempEpisodeList = DBInterface.list_of_episodes_for_series(tempLibrary.id) # initialize the temporary clip List tempClipList = [] # iterate through the Episode List ... for episodeRecord in tempEpisodeList: # ... and add each Episode's Clips to the Temporary clip list tempClipList += DBInterface.list_of_clips_by_episode(episodeRecord[0]) # Add the Episode data to the Filter Dialog's Episode List episodeList.append((episodeRecord[1], tempLibrary.id, True)) # For all the Clips ... for clipRecord in tempClipList: # ... add the Clip to the Clip List for filtering ... clipList.append((clipRecord['ClipID'], clipRecord['CollectNum'], True)) # ... retain a pointer to the Clip Number keyed to the Clip ID and Collection Number ... clipLookup[(clipRecord['ClipID'], clipRecord['CollectNum'])] = clipRecord['ClipNum'] # ... now get all the keywords for this Clip ... clipKeywordList = DBInterface.list_of_keywords(Clip = clipRecord['ClipNum']) # ... and iterate through the list of clip keywords. for clipKeyword in clipKeywordList: # If the keyword isn't already in the Keyword List ... if (clipKeyword[0], clipKeyword[1], True) not in keywordList: # ... add the keyword to the keyword list for filtering. keywordList.append((clipKeyword[0], clipKeyword[1], True)) # If we have a Document Number, we set up the Document Analytic Data Export elif self.documentNum <> 0: # First, we get a list of all the Quotes for the Document specified tempQuoteList = DBInterface.list_of_quotes_by_document(self.documentNum) # For all the Quotes ... for quoteRecord in tempQuoteList: # ... add the Quote to the Quote List for filtering ... quoteList.append((quoteRecord['QuoteID'], quoteRecord['CollectNum'], True)) # ... retain a pointer to the Quote Number keyed to the Quote ID and Collection Number ... quoteLookup[(quoteRecord['QuoteID'], quoteRecord['CollectNum'])] = quoteRecord['QuoteNum'] # ... now get all the keywords for this Quote ... quoteKeywordList = DBInterface.list_of_keywords(Quote = quoteRecord['QuoteNum']) # ... and iterate through the list of Quote keywords. for quoteKeyword in quoteKeywordList: # If the keyword isn't already in the Keyword List ... if (quoteKeyword[0], quoteKeyword[1], True) not in keywordList: # ... add the keyword to the keyword list for filtering. keywordList.append((quoteKeyword[0], quoteKeyword[1], True)) # If we have an Episode Number, we set up the Episode Analytic Data Export elif self.episodeNum <> 0: # First, we get a list of all the Clips for the Episode specified tempClipList = DBInterface.list_of_clips_by_episode(self.episodeNum) # For all the Clips ... for clipRecord in tempClipList: # ... add the Clip to the Clip List for filtering ... clipList.append((clipRecord['ClipID'], clipRecord['CollectNum'], True)) # ... retain a pointer to the Clip Number keyed to the Clip ID and Collection Number ... clipLookup[(clipRecord['ClipID'], clipRecord['CollectNum'])] = clipRecord['ClipNum'] # ... now get all the keywords for this Clip ... clipKeywordList = DBInterface.list_of_keywords(Clip = clipRecord['ClipNum']) # ... and iterate through the list of clip keywords. for clipKeyword in clipKeywordList: # If the keyword isn't already in the Keyword List ... if (clipKeyword[0], clipKeyword[1], True) not in keywordList: # ... add the keyword to the keyword list for filtering. keywordList.append((clipKeyword[0], clipKeyword[1], True)) # If we don't have Library Number, Document Number, or Episode number, but DO have a Collection Number, we set # up the Clips for the Collection specified. If we have neither, it's the GLOBAL Analytic Data Export, # requesting ALL the Quotes and Clips in the database! We can handle both of these cases together. else: # If we have a specific collection specified ... if self.collectionNum <> 0: # ... load the specified collection. We need its data. tempCollection = Collection.Collection(self.collectionNum) # Put the selected Collection's data into the Collection List as a starting place. tempCollectionList = [(tempCollection.number, tempCollection.id, tempCollection.parent)] # If we don't have any selected collection ... else: # ... then we should initialise the Collection List with data for all top-level collections, with parent = 0 tempCollectionList = DBInterface.list_of_collections() # Iterate through the Collection List as long as it has entries while len(tempCollectionList) > 0: # Get the list of Quotes for the current Collection tempQuoteList = DBInterface.list_of_quotes_by_collectionnum(tempCollectionList[0][0]) # For all the Quotes ... for (quoteNo, quoteName, collNo, sourceDocNo) in tempQuoteList: # ... add the Quote to the Quote List for filtering ... quoteList.append((quoteName, collNo, True)) # ... retain a pointer to the Quote Number keyed to the Quote ID and Collection Number ... quoteLookup[(quoteName, collNo)] = quoteNo # ... now get all the keywords for this Quote ... quoteKeywordList = DBInterface.list_of_keywords(Quote = quoteNo) # ... and iterate through the list of Quote keywords. for quoteKeyword in quoteKeywordList: # If the keyword isn't already in the Keyword List ... if (quoteKeyword[0], quoteKeyword[1], True) not in keywordList: # ... add the keyword to the keyword list for filtering. keywordList.append((quoteKeyword[0], quoteKeyword[1], True)) # Get the list of Clips for the current Collection tempClipList = DBInterface.list_of_clips_by_collection(tempCollectionList[0][1], tempCollectionList[0][2]) # For all the Clips ... for (clipNo, clipName, collNo) in tempClipList: # ... add the Clip to the Clip List for filtering ... clipList.append((clipName, collNo, True)) # ... retain a pointer to the Clip Number keyed to the Clip ID and Collection Number ... clipLookup[(clipName, collNo)] = clipNo # ... now get all the keywords for this Clip ... clipKeywordList = DBInterface.list_of_keywords(Clip = clipNo) # ... and iterate through the list of clip keywords. for clipKeyword in clipKeywordList: # If the keyword isn't already in the Keyword List ... if (clipKeyword[0], clipKeyword[1], True) not in keywordList: # ... add the keyword to the keyword list for filtering. keywordList.append((clipKeyword[0], clipKeyword[1], True)) # Get the nested collections for the current collection and add them to the Collection List tempCollectionList += DBInterface.list_of_collections(tempCollectionList[0][0]) # Remove the current Collection from the list. We're done with it. del(tempCollectionList[0]) # Put the Quote List in alphabetical order in preparation for Filtering.. quoteList.sort() # Put the Clip List in alphabetical order in preparation for Filtering.. clipList.sort() # Put the Keyword List in alphabetical order in preparation for Filtering. keywordList.sort() # Prepare the Filter Dialog. # Set the title for the Filter Dialog title = unicode(_("Analytic Data Export Filter Dialog"), 'utf8') # If we have a Library-based report ... if self.libraryNum != 0: # ... reportType 14 indicates Library Analytic Data Export to the Filter Dialog reportType = 14 # ... the reportScope is the Library Number. reportScope = self.libraryNum # If we have a Document-based report ... elif self.documentNum != 0: # ... reportType 20 indicates Document Analytic Data Export to the Filter Dialog reportType = 20 # ... the reportScope is the Document Number. reportScope = self.documentNum # If we have an Episode-based report ... elif self.episodeNum != 0: # ... reportType 3 indicates Episode Analytic Data Export to the Filter Dialog reportType = 3 # ... the reportScope is the Episode Number. reportScope = self.episodeNum # If we have a Collection-based report ... else: # ... reportType 4 indicates Collection Analytic Data Export to the Filter Dialog reportType = 4 # ... the reportScope is the Collection Number. reportScope = self.collectionNum showDocuments = (len(documentList) > 0) showEpisodes = (len(episodeList) > 0) showQuotes = (len(quoteList) > 0) showClips = (len(clipList) > 0) # If we are basing the report on a Library ... if self.libraryNum != 0: # ... create a Filter Dialog, passing all the necessary parameters. We want to include the Episode List dlgFilter = FilterDialog.FilterDialog(None, -1, title, reportType=reportType, reportScope=reportScope, documentFilter=showDocuments, episodeFilter=showEpisodes, quoteFilter=showQuotes, clipFilter=showClips, keywordFilter=True) # If we are basing the report on a Collection (but not the Collection Root) ... elif self.collectionNum != 0: # ... create a Filter Dialog, passing all the necessary parameters. We want to be able to include Nested Collections dlgFilter = FilterDialog.FilterDialog(None, -1, title, reportType=reportType, reportScope=reportScope, quoteFilter=showQuotes, clipFilter=showClips, keywordFilter=True, reportContents=True, showNestedData=True) # If we are basing the report on a Document ... elif self.documentNum != 0: # ... create a Filter Dialog, passing all the necessary parameters. We DON'T need Nested Collections dlgFilter = FilterDialog.FilterDialog(None, -1, title, reportType=reportType, reportScope=reportScope, quoteFilter=True, keywordFilter=True) # If we are basing the report on an Episode ... elif self.episodeNum != 0: # ... create a Filter Dialog, passing all the necessary parameters. We DON'T need Nested Collections dlgFilter = FilterDialog.FilterDialog(None, -1, title, reportType=reportType, reportScope=reportScope, clipFilter=True, keywordFilter=True) # If we are doing the report on the Collection Root report, which MUST have nested data) ... else: # ... create a Filter Dialog, passing all the necessary parameters. We DON'T need Nested Collections dlgFilter = FilterDialog.FilterDialog(None, -1, title, reportType=reportType, reportScope=reportScope, quoteFilter=showQuotes, clipFilter=showClips, keywordFilter=True) # If we have a Library-based report ... if self.libraryNum != 0: if showDocuments: # ... populate the Document Data Structure dlgFilter.SetDocuments(documentList) if showEpisodes: # ... populate the Episode Data Structure dlgFilter.SetEpisodes(episodeList) # Populate the Quote, Clip and Keyword Data Structures if showQuotes: dlgFilter.SetQuotes(quoteList) if showClips: dlgFilter.SetClips(clipList) dlgFilter.SetKeywords(keywordList) # ... get the list of existing configuration names. profileList = dlgFilter.GetConfigNames() # If (translated) "Default" is in the list ... # (NOTE that the default config name is stored in English, but gets translated by GetConfigNames!) if unicode(_('Default'), 'utf8') in profileList: # ... set the Filter Dialog to use this filter dlgFilter.configName = unicode(_('Default'), TransanaGlobal.encoding) # Temporarily set loadDefault to True for the Filter Dialog. This disables the Filter Load dialog. # (We don't use the FilterDialog parameter, as that disables loading other Filters!) dlgFilter.loadDefault = True # We need to load the config dlgFilter.OnFileOpen(None) # Now we turn loadDefault back off so we can load other filters if we want. dlgFilter.loadDefault = False # restore the cursor, now that the data is set up for the filter dialog TransanaGlobal.menuWindow.SetCursor(wx.StockCursor(wx.CURSOR_ARROW)) # If the user clicks OK ... if dlgFilter.ShowModal() == wx.ID_OK: # Set the WAIT cursor. It can take a while to build the data file. TransanaGlobal.menuWindow.SetCursor(wx.StockCursor(wx.CURSOR_WAIT)) # If we have a Library-based report ... if self.libraryNum != 0: if showDocuments: documentList = dlgFilter.GetDocuments() if showEpisodes: # ... get the revised Episode data from the Filter Dialog episodeList = dlgFilter.GetEpisodes() # Get the revised Quote, Clip, and Keyword data from the Filter Dialog if showQuotes: quoteList = dlgFilter.GetQuotes() if showClips: clipList = dlgFilter.GetClips() keywordList = dlgFilter.GetKeywords() # If we have a Collection-based report ... if self.collectionNum != 0: # ... get the setting for including nested collections (not relevant for other reports) showNested = dlgFilter.GetShowNestedData() # If we have a report other than based on a Collection ... else: # ... nesting is meaningless, so we can just initialize this variable to False. showNested = False # Get the user-specified File Name fs = self.exportFile.GetValue() # Ensure that the file name has the proper extension. if fs[-4:].lower() != '.txt': fs = fs + '.txt' # On the Mac, if no path is specified, the data is exported to a file INSIDE the application bundle, # where no one will be able to find it. Let's put it in the user's HOME directory instead. # I'm okay with not handling this on Windows, where it will be placed in the Program's folder # but it CAN be found. (There's no easy way on Windows to determine the location of "My Documents" # especially if the user has moved it.) if "__WXMAC__" in wx.PlatformInfo: # if the specified file has no path specification ... if fs.find(os.sep) == -1: # ... then prepend the HOME folder fs = os.getenv("HOME") + os.sep + fs # Open the output file for writing. f = codecs.open(fs, 'w', 'utf8') # file(fs, 'w') prompt = unicode(_('Collection Name\tItem Type\tItem Name\tSource File\tStart\tStop\tLength'), 'utf8') # Write the Header line. We're creating a tab-delimited file, so we'll use tabs to separate the items. f.write(prompt) # Add keywords to the Header. Iterate through the Keyword List. for keyword in keywordList: # See if the user has left the keyword "checked" in the filter dialog. if keyword[2]: # Encode and write all "checked" keywords to the Header. kwg = keyword[0] kw = keyword[1] f.write('\t%s : %s' % (kwg, kw)) # Add a line break to signal the end of the Header line. f.write('\n') # Now iterate through the Quote List for quoteRec in quoteList: # See if the user has left the Quote "checked" in the filter dialog. # Also, if we are using a collection report, either Nested Data should be requested OR the current # Quote should be from the main collection if it is to be included in the report. if quoteRec[2] and ((self.collectionNum == 0) or (showNested) or (quoteRec[1] == self.collectionNum)): # Load the Quote data. The QuoteLookup dictionary allows this easily. # No need to load the Quote Text, which can be slow to load. quote = Quote.Quote(quoteLookup[quoteRec[0], quoteRec[1]], skipText=True) # Get the collection the Quote is from. collection = Collection.Collection(quote.collection_num) # Encode string values using the Export Encoding collectionID = collection.GetNodeString() quoteID = quote.id try: document = Document.Document(quote.source_document_num) documentID = document.id quoteSourceFilename = document.imported_file # If we're doing a Library report, we need the Quote's source document and Library for Document Filter comparison. if self.libraryNum != 0: library = Library.Library(document.library_num) libraryID = library.id # If we have an orphaned Quote ... except TransanaExceptions.RecordNotFoundError, e: # ... then we don't know these values! documentID = '' quoteSourceFilename = _('Source Document unknown') libraryID = 0 # Implement Document filtering if needed. If we have a Library Report, we need to confirm that the Source Document # is "checked" in the filter list. (If we don't have a Library Report, this check isn't needed.) if (self.libraryNum == 0) or ((documentID == '') and (libraryID == '')) or ((documentID, libraryID, True) in documentList): # Write the Quote's data values to the output file. We're creating a tab-delimited file, # so we'll use tabs to separate the items. f.write('%s\t%s\t%s\t%s\t%s\t%s\t%d' % (collectionID, '1', quoteID, quoteSourceFilename, quote.start_char, quote.end_char, (quote.end_char - quote.start_char))) # Now we iterate through the keyword list ... for keyword in keywordList: # ... looking only at those keywords the user left "checked" in the filter dialog ... if keyword[2]: # ... and check to see if the Quote HAS the keyword. if quote.has_keyword(keyword[0], keyword[1]): # If so, we write a "1", indicating True. f.write('\t1') else: # If not, we write a "0", indicating False. f.write('\t0') # Add a line break to signal the end of the Quote record f.write('\n') # Now iterate through the Clip List for clipRec in clipList: # See if the user has left the clip "checked" in the filter dialog. # Also, if we are using a collection report, either Nested Data should be requested OR the current # clip should be from the main collection if it is to be included in the report. if clipRec[2] and ((self.collectionNum == 0) or (showNested) or (clipRec[1] == self.collectionNum)): # Load the Clip data. The ClipLookup dictionary allows this easily. # No need to load the Clip Transcripts, which can be slow to load. clip = Clip.Clip(clipLookup[clipRec[0], clipRec[1]], skipText=True) # Get the collection the clip is from. collection = Collection.Collection(clip.collection_num) # Encode string values using the Export Encoding collectionID = collection.GetNodeString() clipID = clip.id clipMediaFilename = clip.media_filename # If we're doing a Library report, we need the clip's source episode and Library for Episode Filter comparison. if self.libraryNum != 0: episode = Episode.Episode(clip.episode_num) library = Library.Library(episode.series_num) # Implement Episode filtering if needed. If we have a Library Report, we need to confirm that the Source Episode # is "checked" in the filter list. (If we don't have a Library Report, this check isn't needed.) if (self.libraryNum == 0) or ((episode.id, library.id, True) in episodeList): # Write the Clip's data values to the output file. We're creating a tab-delimited file, # so we'll use tabs to separate the items. f.write('%s\t%s\t%s\t%s\t%s\t%s\t%10.4f' % (collectionID, '2', clipID, clipMediaFilename, Misc.time_in_ms_to_str(clip.clip_start), Misc.time_in_ms_to_str(clip.clip_stop), (clip.clip_stop - clip.clip_start) / 1000.0)) # Now we iterate through the keyword list ... for keyword in keywordList: # ... looking only at those keywords the user left "checked" in the filter dialog ... if keyword[2]: # ... and check to see if the Clip HAS the keyword. if clip.has_keyword(keyword[0], keyword[1]): # If so, we write a "1", indicating True. f.write('\t1') else: # If not, we write a "0", indicating False. f.write('\t0') # Add a line break to signal the end of the Clip record f.write('\n') # Flush the output file's buffer (probably unnecessary) f.flush() # Close the output file. f.close() # Restore the cursor when we're done. TransanaGlobal.menuWindow.SetCursor(wx.StockCursor(wx.CURSOR_ARROW)) # If so, create a prompt to inform the user and ask to overwrite the file. if 'unicode' in wx.PlatformInfo: # Encode with UTF-8 rather than TransanaGlobal.encoding because this is a prompt, not DB Data. prompt = unicode(_('Clip Data has been exported to file "%s".'), 'utf8') else: prompt = _('Clip Data has been exported to file "%s".') # Create the dialog to inform the user dlg2 = Dialogs.InfoDialog(self, prompt % fs) # Show the Info dialog. dlg2.ShowModal() # Destroy the Information dialog dlg2.Destroy()
def __init__(self, parent, id, title, clip_object, mergeList=None): # If no Merge List is passed in ... if mergeList == None: # ... use the default Clip Properties Dialog size passed in from the config object. (This does NOT get saved.) size = TransanaGlobal.configData.clipPropertiesSize # ... we can enable Clip Change Propagation propagateEnabled = True HelpContext='Clip Properties' # If we DO have a merge list ... else: # ... increase the size of the dialog to allow for the list of mergeable clips. size = (TransanaGlobal.configData.clipPropertiesSize[0], TransanaGlobal.configData.clipPropertiesSize[1] + 130) # ... and Clip Change Propagation should be disabled propagateEnabled = False HelpContext='Clip Merge' # Make the Keyword Edit List resizable by passing wx.RESIZE_BORDER style. Signal that Propogation is included. Dialogs.GenForm.__init__(self, parent, id, title, size=size, style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, propagateEnabled=propagateEnabled, useSizers = True, HelpContext=HelpContext) if mergeList == None: # Define the minimum size for this dialog as the initial size minWidth = 750 minHeight = 570 else: # Define the minimum size for this dialog as the initial size minWidth = 750 minHeight = 650 # Remember the Parent Window self.parent = parent # Remember the original Clip Object passed in self.obj = clip_object # Add a placeholder to the clip object for the merge clip number self.obj.mergeNumber = 0 # Remember the merge list, if one is passed in self.mergeList = mergeList # Initialize the merge item as unselected self.mergeItemIndex = -1 # if Keywords that server as Keyword Examples are removed, we will need to remember them. # Then, when OK is pressed, the Keyword Example references in the Database Tree can be removed. # We can't remove them immediately in case the whole Clip Properties Edit process is cancelled. self.keywordExamplesToDelete = [] # Initialize a variable to hold merged keyword examples. self.mergedKeywordExamples = [] # Create the form's main VERTICAL sizer mainSizer = wx.BoxSizer(wx.VERTICAL) # If we're merging Clips ... if self.mergeList != None: # ... display a label for the Merge Clips ... lblMergeClip = wx.StaticText(self.panel, -1, _("Clip to Merge")) mainSizer.Add(lblMergeClip, 0) # Add a vertical spacer to the main sizer mainSizer.Add((0, 3)) # Create a HORIZONTAL sizer for the merge information mergeSizer = wx.BoxSizer(wx.HORIZONTAL) # ... display a ListCtrl for the Merge Clips ... self.mergeClips = wx.ListCtrl(self.panel, -1, size=(300, 100), style=wx.LC_REPORT | wx.LC_SINGLE_SEL) # Add the element to the sizer mergeSizer.Add(self.mergeClips, 1, wx.EXPAND) # ... bind the Item Selected event for the List Control ... self.mergeClips.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected) # ... define the columns for the Merge Clips ... self.mergeClips.InsertColumn(0, _('Clip Name')) self.mergeClips.InsertColumn(1, _('Collection')) self.mergeClips.InsertColumn(2, _('Start Time')) self.mergeClips.InsertColumn(3, _('Stop Time')) self.mergeClips.SetColumnWidth(0, 244) self.mergeClips.SetColumnWidth(1, 244) # ... and populate the Merge Clips list from the mergeList data for (ClipNum, ClipID, CollectNum, CollectID, ClipStart, ClipStop, transcriptCount) in self.mergeList: index = self.mergeClips.InsertStringItem(sys.maxint, ClipID) self.mergeClips.SetStringItem(index, 1, CollectID) self.mergeClips.SetStringItem(index, 2, Misc.time_in_ms_to_str(ClipStart)) self.mergeClips.SetStringItem(index, 3, Misc.time_in_ms_to_str(ClipStop)) # Add the row sizer to the main vertical sizer mainSizer.Add(mergeSizer, 3, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the first row r1Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v1 = wx.BoxSizer(wx.VERTICAL) # Clip ID self.id_edit = self.new_edit_box(_("Clip ID"), v1, self.obj.id, maxLen=100) # Add the element to the sizer r1Sizer.Add(v1, 1, wx.EXPAND) # Add a horizontal spacer to the row sizer r1Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v2 = wx.BoxSizer(wx.VERTICAL) # Collection ID collection_edit = self.new_edit_box(_("Collection ID"), v2, self.obj.GetNodeString(False)) # Add the element to the sizer r1Sizer.Add(v2, 2, wx.EXPAND) collection_edit.Enable(False) # Add the row sizer to the main vertical sizer mainSizer.Add(r1Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r2Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v3 = wx.BoxSizer(wx.VERTICAL) # Library ID series_edit = self.new_edit_box(_("Library ID"), v3, self.obj.series_id) # Add the element to the sizer r2Sizer.Add(v3, 1, wx.EXPAND) series_edit.Enable(False) # Add a horizontal spacer to the row sizer r2Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v4 = wx.BoxSizer(wx.VERTICAL) # Episode ID episode_edit = self.new_edit_box(_("Episode ID"), v4, self.obj.episode_id) # Add the element to the sizer r2Sizer.Add(v4, 1, wx.EXPAND) episode_edit.Enable(False) # Add the row sizer to the main vertical sizer mainSizer.Add(r2Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Media Filename(s) [label] txt = wx.StaticText(self.panel, -1, _("Media Filename(s)")) mainSizer.Add(txt, 0) # Add a vertical spacer to the main sizer mainSizer.Add((0, 3)) # Create a HORIZONTAL sizer for the next row r3Sizer = wx.BoxSizer(wx.HORIZONTAL) # Media Filename(s) # If the media filename path is not empty, we should normalize the path specification if self.obj.media_filename == '': filePath = self.obj.media_filename else: filePath = os.path.normpath(self.obj.media_filename) # Initialize the list of media filenames with the first one. self.filenames = [filePath] # For each additional Media file ... for vid in self.obj.additional_media_files: # ... add it to the filename list self.filenames.append(vid['filename']) self.fname_lb = wx.ListBox(self.panel, -1, wx.DefaultPosition, wx.Size(180, 60), self.filenames) r3Sizer.Add(self.fname_lb, 1, wx.EXPAND) self.fname_lb.SetDropTarget(ListBoxFileDropTarget(self.fname_lb)) # Add the row sizer to the main vertical sizer mainSizer.Add(r3Sizer, 3, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r4Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v5 = wx.BoxSizer(wx.VERTICAL) # Clip Start. Convert to HH:MM:SS.mm self.clip_start_edit = self.new_edit_box(_("Clip Start"), v5, Misc.time_in_ms_to_str(self.obj.clip_start)) # Add the element to the sizer r4Sizer.Add(v5, 1, wx.EXPAND) # For merging, we need to remember the merged value of Clip Start self.clip_start = self.obj.clip_start self.clip_start_edit.Enable(False) # Add a horizontal spacer to the row sizer r4Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v6 = wx.BoxSizer(wx.VERTICAL) # Clip Stop. Convert to HH:MM:SS.mm self.clip_stop_edit = self.new_edit_box(_("Clip Stop"), v6, Misc.time_in_ms_to_str(self.obj.clip_stop)) # Add the element to the sizer r4Sizer.Add(v6, 1, wx.EXPAND) # For merging, we need to remember the merged value of Clip Stop self.clip_stop = self.obj.clip_stop self.clip_stop_edit.Enable(False) # Add a horizontal spacer to the row sizer r4Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v7 = wx.BoxSizer(wx.VERTICAL) # Clip Length. Convert to HH:MM:SS.mm self.clip_length_edit = self.new_edit_box(_("Clip Length"), v7, Misc.time_in_ms_to_str(self.obj.clip_stop - self.obj.clip_start)) # Add the element to the sizer r4Sizer.Add(v7, 1, wx.EXPAND) self.clip_length_edit.Enable(False) # Add the row sizer to the main vertical sizer mainSizer.Add(r4Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r5Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v8 = wx.BoxSizer(wx.VERTICAL) # Comment comment_edit = self.new_edit_box(_("Comment"), v8, self.obj.comment, maxLen=255) # Add the element to the sizer r5Sizer.Add(v8, 1, wx.EXPAND) # Add the row sizer to the main vertical sizer mainSizer.Add(r5Sizer, 0, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r6Sizer = wx.BoxSizer(wx.HORIZONTAL) self.text_edit = [] # Notebook for holding Transcripts # ... we need to display them within a notebook ... self.notebook = wx.Notebook(self.panel, -1) # Add the element to the sizer r6Sizer.Add(self.notebook, 2, wx.EXPAND) # Initialize a list of notebook pages self.notebookPage = [] # Initialize a counter for notebood pages counter = 0 # Initialize the TRANSCRIPT start and stop time variables self.transcript_clip_start = [] self.transcript_clip_stop = [] # Iterate through the clip transcripts for tr in self.obj.transcripts: # Initialize the transcript start and stop time values for the individual transcripts self.transcript_clip_start.append(tr.clip_start) self.transcript_clip_stop.append(tr.clip_stop) # Create a panel for each notebook page self.notebookPage.append(wx.Panel(self.notebook)) # Add the notebook page to the notebook ... self.notebook.AddPage(self.notebookPage[counter], _('Transcript') + " %d" % (counter + 1)) # ... and use this page as the transcript object's parent transcriptParent = self.notebookPage[counter] # Clip Text # Load the Transcript into an RTF Control so the RTF Encoding won't show. # We use a list of edit controls to handle multiple transcripts. if TransanaConstants.USESRTC: self.text_edit.append(TranscriptEditor_RTC.TranscriptEditor(transcriptParent)) else: self.text_edit.append(TranscriptEditor_STC.TranscriptEditor(transcriptParent)) ## DKW EXPERIMENT 4/5/2011 self.text_edit[len(self.text_edit) - 1].load_transcript(self.obj.transcripts[counter]) self.text_edit[len(self.text_edit) - 1].SetReadOnly(False) self.text_edit[len(self.text_edit) - 1].Enable(True) # Increment the counter counter += 1 # Add the row sizer to the main vertical sizer mainSizer.Add(r6Sizer, 6, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a HORIZONTAL sizer for the next row r7Sizer = wx.BoxSizer(wx.HORIZONTAL) # Create a VERTICAL sizer for the next element v9 = wx.BoxSizer(wx.VERTICAL) # Keyword Group [label] txt = wx.StaticText(self.panel, -1, _("Keyword Group")) v9.Add(txt, 0, wx.BOTTOM, 3) # Keyword Group [list box] # Create an empty Keyword Group List for now. We'll populate it later (for layout reasons) self.kw_groups = [] self.kw_group_lb = wx.ListBox(self.panel, -1, choices = self.kw_groups) v9.Add(self.kw_group_lb, 1, wx.EXPAND) # Add the element to the sizer r7Sizer.Add(v9, 1, wx.EXPAND) # Create an empty Keyword List for now. We'll populate it later (for layout reasons) self.kw_list = [] wx.EVT_LISTBOX(self, self.kw_group_lb.GetId(), self.OnGroupSelect) # Add a horizontal spacer r7Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v10 = wx.BoxSizer(wx.VERTICAL) # Keyword [label] txt = wx.StaticText(self.panel, -1, _("Keyword")) v10.Add(txt, 0, wx.BOTTOM, 3) # Keyword [list box] self.kw_lb = wx.ListBox(self.panel, -1, choices = self.kw_list, style=wx.LB_EXTENDED) v10.Add(self.kw_lb, 1, wx.EXPAND) wx.EVT_LISTBOX_DCLICK(self, self.kw_lb.GetId(), self.OnAddKW) # Add the element to the sizer r7Sizer.Add(v10, 1, wx.EXPAND) # Add a horizontal spacer r7Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v11 = wx.BoxSizer(wx.VERTICAL) # Keyword transfer buttons add_kw = wx.Button(self.panel, wx.ID_FILE2, ">>", wx.DefaultPosition) v11.Add(add_kw, 0, wx.EXPAND | wx.TOP, 20) wx.EVT_BUTTON(self, wx.ID_FILE2, self.OnAddKW) rm_kw = wx.Button(self.panel, wx.ID_FILE3, "<<", wx.DefaultPosition) v11.Add(rm_kw, 0, wx.EXPAND | wx.TOP, 10) wx.EVT_BUTTON(self, wx.ID_FILE3, self.OnRemoveKW) kwm = wx.BitmapButton(self.panel, wx.ID_FILE4, TransanaImages.KWManage.GetBitmap()) v11.Add(kwm, 0, wx.EXPAND | wx.TOP, 10) # Add a spacer to increase the height of the Keywords section ## v11.Add((0, 60)) kwm.SetToolTipString(_("Keyword Management")) wx.EVT_BUTTON(self, wx.ID_FILE4, self.OnKWManage) # Add the element to the sizer r7Sizer.Add(v11, 0) # Add a horizontal spacer r7Sizer.Add((10, 0)) # Create a VERTICAL sizer for the next element v12 = wx.BoxSizer(wx.VERTICAL) # Clip Keywords [label] txt = wx.StaticText(self.panel, -1, _("Clip Keywords")) v12.Add(txt, 0, wx.BOTTOM, 3) # Clip Keywords [list box] # Create an empty ListBox. We'll populate it later for layout reasons. self.ekw_lb = wx.ListBox(self.panel, -1, style=wx.LB_EXTENDED) v12.Add(self.ekw_lb, 1, wx.EXPAND) self.ekw_lb.Bind(wx.EVT_KEY_DOWN, self.OnKeywordKeyDown) # Add the element to the sizer r7Sizer.Add(v12, 2, wx.EXPAND) # Add the row sizer to the main vertical sizer mainSizer.Add(r7Sizer, 8, wx.EXPAND) # Add a vertical spacer to the main sizer mainSizer.Add((0, 10)) # Create a sizer for the buttons btnSizer = wx.BoxSizer(wx.HORIZONTAL) # Add the buttons self.create_buttons(sizer=btnSizer) # Add the button sizer to the main sizer mainSizer.Add(btnSizer, 0, wx.EXPAND) # Because of the way Clips are created (with Drag&Drop / Cut&Paste functions), we have to trap the missing # ID error here. Therefore, we need to override the EVT_BUTTON for the OK Button. # Since we don't have an object for the OK Button, we use FindWindowById to find it based on its ID. self.Bind(wx.EVT_BUTTON, self.OnOK, self.FindWindowById(wx.ID_OK)) # We also need to intercept the Cancel button. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.FindWindowById(wx.ID_CANCEL)) self.Bind(wx.EVT_SIZE, self.OnSize) # Set the PANEL's main sizer self.panel.SetSizer(mainSizer) # Tell the PANEL to auto-layout self.panel.SetAutoLayout(True) # Lay out the Panel self.panel.Layout() # Lay out the panel on the form self.Layout() # Resize the form to fit the contents self.Fit() # Get the new size of the form (width, height) = self.GetSizeTuple() # Determine which monitor to use and get its size and position if TransanaGlobal.configData.primaryScreen < wx.Display.GetCount(): primaryScreen = TransanaGlobal.configData.primaryScreen else: primaryScreen = 0 rect = wx.Display(primaryScreen).GetClientArea() # Reset the form's size to be at least the specified minimum width self.SetSize(wx.Size(max(minWidth, width), min(max(minHeight, height), rect[3]))) # Define the minimum size for this dialog as the current size self.SetSizeHints(max(minWidth, width), min(max(minHeight, height), rect[3])) # Center the form on screen TransanaGlobal.CenterOnPrimary(self) # We need to set some minimum sizes so the sizers will work right self.kw_group_lb.SetSizeHints(minW = 50, minH = 20) self.kw_lb.SetSizeHints(minW = 50, minH = 20) self.ekw_lb.SetSizeHints(minW = 50, minH = 20) # We populate the Keyword Groups, Keywords, and Clip Keywords lists AFTER we determine the Form Size. # Long Keywords in the list were making the form too big! self.kw_groups = DBInterface.list_of_keyword_groups() for keywordGroup in self.kw_groups: self.kw_group_lb.Append(keywordGroup) # Populate the Keywords ListBox # Load the parent Collection in order to determine the default Keyword Group tempCollection = Collection.Collection(self.obj.collection_num) # Select the Collection Default Keyword Group in the Keyword Group list if (tempCollection.keyword_group != '') and (self.kw_group_lb.FindString(tempCollection.keyword_group) != wx.NOT_FOUND): self.kw_group_lb.SetStringSelection(tempCollection.keyword_group) # If no Default Keyword Group is defined, select the first item in the list else: # but only if there IS a first item. if len(self.kw_groups) > 0: self.kw_group_lb.SetSelection(0) # If there's a selected keyword group ... if self.kw_group_lb.GetSelection() != wx.NOT_FOUND: # populate the Keywords list self.kw_list = \ DBInterface.list_of_keywords_by_group(self.kw_group_lb.GetStringSelection()) else: # If not, create a blank one self.kw_list = [] for keyword in self.kw_list: self.kw_lb.Append(keyword) # Populate the Clip Keywords ListBox # If the clip object has keywords ... for clipKeyword in self.obj.keyword_list: # ... add them to the keyword list self.ekw_lb.Append(clipKeyword.keywordPair) # If we have a Notebook of text controls ... if self.notebookPage: # ... interate through the text controls ... for textCtrl in self.text_edit: # ... and set them to the size of the notebook page. textCtrl.SetSize(self.notebookPage[0].GetSizeTuple()) # Set initial focus to the Clip ID self.id_edit.SetFocus()
def one_pop(phi, xx, T, nu=1, gamma=0, h=0.5, theta0=1.0, initial_t=0, frozen=False, beta=1): """ Integrate a 1-dimensional phi foward. phi: Initial 1-dimensional phi xx: Grid upon (0,1) overwhich phi is defined. nu, gamma, and theta0 may be functions of time. nu: Population size gamma: Selection coefficient on *all* segregating alleles h: Dominance coefficient. h = 0.5 corresponds to genic selection. Heterozygotes have fitness 1+2sh and homozygotes have fitness 1+2s. theta0: Propotional to ancestral size. Typically constant. beta: Breeding ratio, beta=Nf/Nm. T: Time at which to halt integration initial_t: Time at which to start integration. (Note that this only matters if one of the demographic parameters is a function of time.) frozen: If True, population is 'frozen' so that it does not change. In the one_pop case, this is equivalent to not running the integration at all. """ phi = phi.copy() # For a one population integration, freezing means just not integrating. if frozen: return phi if T - initial_t == 0: return phi elif T - initial_t < 0: raise ValueError('Final integration time T (%f) is less than ' 'intial_time (%f). Integration cannot be run ' 'backwards.' % (T, initial_t)) vars_to_check = (nu, gamma, h, theta0, beta) if numpy.all([numpy.isscalar(var) for var in vars_to_check]): return _one_pop_const_params(phi, xx, T, nu, gamma, h, theta0, initial_t, beta) nu_f = Misc.ensure_1arg_func(nu) gamma_f = Misc.ensure_1arg_func(gamma) h_f = Misc.ensure_1arg_func(h) theta0_f = Misc.ensure_1arg_func(theta0) beta_f = Misc.ensure_1arg_func(beta) current_t = initial_t nu, gamma, h = nu_f(current_t), gamma_f(current_t), h_f(current_t) beta = beta_f(current_t) dx = numpy.diff(xx) while current_t < T: dt = _compute_dt(dx,nu,[0],gamma,h) this_dt = min(dt, T - current_t) # Because this is an implicit method, I need the *next* time's params. # So there's a little inconsistency here, in that I'm estimating dt # using the last timepoints nu,gamma,h. next_t = current_t + this_dt nu, gamma, h = nu_f(next_t), gamma_f(next_t), h_f(next_t) beta = beta_f(next_t) theta0 = theta0_f(next_t) if numpy.any(numpy.less([T,nu,theta0], 0)): raise ValueError('A time, population size, migration rate, or ' 'theta0 is < 0. Has the model been mis-specified?') if numpy.any(numpy.equal([nu], 0)): raise ValueError('A population size is 0. Has the model been ' 'mis-specified?') _inject_mutations_1D(phi, this_dt, xx, theta0) # Do each step in C, since it will be faster to compute the a,b,c # matrices there. phi = int_c.implicit_1Dx(phi, xx, nu, gamma, h, beta, this_dt, use_delj_trick=use_delj_trick) current_t = next_t return phi
def tape_length_str(self): """Return a string representation (HH:MM:SS) of tape length.""" return Misc.time_in_ms_to_str(self.episode_length())