def main(): """Quick tests.""" a = Attribute('hour', ['0,...,23']) a2 = Attribute('minute', ['0,...,59']) r_ahead = Relation('R1(h1,m1,h2,m2) <=> h1 > h2 or (h1 = h2 and m1 > m2)', ['hour', 'minute', 'hour', 'minute'], 1) r_behind = Relation('R2(h1,m1,h2,m2) <=> h1 < h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 2) r_pm = Relation('R3(h1) <=> h1 > 12', ['hour'], 3) r_am = Relation('R4(h1) <=> h1 < 12', ['hour'], 4) attribute_structure = AttributeStructure(a, a2, r_ahead, r_behind, r_pm, r_am) ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) pm_rs = RelationSymbol('PM', 1) vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs], ['V1', 'V2']) profiles = [[ ahead_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2) ], [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)], [pm_rs, ('hour', 1)]] mapping = {ahead_rs: 1, behind_rs: 2, pm_rs: 3} ai = AttributeInterpretation(vocabulary, attribute_structure, mapping, profiles) print ai == ai
class Config: # 行情 quote = Attribute({ 'client_id': CONST_QUOTE_CLIENT_ID, 'save_file_path': CONST_QUOTE_SAVE_FILE_PATH, 'ip': CONST_QUOTE_IP, 'port': CONST_QUOTE_PORT, 'user': CONST_QUOTE_USER, 'password': CONST_QUOTE_PASSWORD, 'sock_type': CONST_QUOTE_SOCK_TYPE, 'auto_login': CONST_QUOTE_AUTO_LOGIN }) # 交易 trade = Attribute({ 'client_id': CONST_TRADE_CLIENT_ID, 'save_file_path': CONST_TRADE_SAVE_FILE_PATH, 'ip': CONST_TRADE_IP, 'port': CONST_TRADE_PORT, 'user': CONST_TRADE_USER, 'password': CONST_TRADE_PASSWORD, 'sock_type': CONST_TRADE_SOCK_TYPE, 'auto_login': CONST_TRADE_AUTO_LOGIN, 'key': CONST_TRADE_KEY }) # -------------------------------------------------- def __init__(self): """Constructor""" raise NotImplementedError()
def main(): """.""" color = Attribute("color", ['R', 'G', 'B']) size = Attribute("size", ['S', 'M', 'L']) a = AttributeStructure(color, size) o = ['s'] asys = AttributeSystem(a, o) s = State(asys) s1 = deepcopy(s) s1.set_ascription(('color', 's'), ['B', 'G']) s1.set_ascription(('size', 's'), ['S']) aes = s.get_alternate_extensions(s1) for ae in aes: print ae print s2 = deepcopy(s) s2.set_ascription(('color', 's'), ['R']) s2.set_ascription(('size', 's'), ['S', 'M', 'L']) s3 = deepcopy(s) s3.set_ascription(('color', 's'), ['R', 'B', 'G']) s3.set_ascription(('size', 's'), ['L', 'M'])
def main(): """Main method; quick testing.""" a, b, c = Attribute("a", []), Attribute("b", []), Attribute("c", []) r = Relation("R1(a,b) <=> ", ["a", "b"], 1) astr = AttributeStructure() print astr + a + b + r
def test_eq(self): t1 = Attribute(self.__statement[0]) for statement in self.__statement: t = Attribute(statement) self.assertEqual(t, t1) self.assertFalse(t1 == 'name') t2 = Attribute('attribute name2;') for statement in self.__statement: t = Attribute(statement) self.assertTrue(t != t2)
def main(): """quick dev tests.""" from interval import Interval from relationSymbol import RelationSymbol from vocabulary import Vocabulary from attribute_interpretation import AttributeInterpretation from formula import Formula from assumption_base import AssumptionBase from attribute import Attribute from relation import Relation from attribute_structure import AttributeStructure from attribute_system import AttributeSystem from constant_assignment import ConstantAssignment from named_state import NamedState from context import Context from variable_assignment import VariableAssignment a = Attribute('hour', [Interval(0, 23)]) a2 = Attribute('minute', [Interval(0, 59)]) r_pm = Relation('R1(h1) <=> h1 > 11', ['hour'], 1) r_am = Relation('R2(h1) <=> h1 <= 11', ['hour'], 2) r_ahead = Relation('R3(h1,m1,h2,m2) <=> h1 > h2 or (h1 = h2 and m1 > m2)', ['hour', 'minute', 'hour', 'minute'], 3) r_behind = Relation('R4(h1,m1,h2,m2) <=> h1 < h2 or (h1 = h2 and m1 < m2)', ['hour', 'minute', 'hour', 'minute'], 4) attribute_structure = AttributeStructure(a, a2, r_ahead, r_behind, r_pm, r_am) pm_rs = RelationSymbol('PM', 1) am_rs = RelationSymbol('AM', 1) ahead_rs = RelationSymbol('Ahead', 4) behind_rs = RelationSymbol('Behind', 4) vocabulary = Vocabulary(['C1', 'C2'], [pm_rs, am_rs, ahead_rs, behind_rs], ['V1', 'V2']) objs = ['s1', 's2', 's3'] asys = AttributeSystem(attribute_structure, objs) const_mapping_2 = {'C1': 's1'} p2 = ConstantAssignment(vocabulary, asys, const_mapping_2) ascriptions_1 = { ("hour", "s1"): [13, 15, 17], ("minute", "s1"): [10], ("hour", "s2"): [1, 3, 5], ("minute", "s2"): [10], ("hour", "s3"): [1, 3, 5], ("minute", "s3"): [10] } named_state_4 = NamedState(asys, p2, ascriptions_1)
def get_attribute(self, title): if title == "sizes": if len(self.sizes) == 1: return Attribute("Sample Sizes", None, None, None, self.sizes, None, None) else: return [ Attribute("Sample Sizes", None, None, self.get_xx()[ii], self.sizes[ii], None, None) for ii in range(len(self.sizes)) ] raise title + " not available"
def __init__(self): self.gender = random.choice("male" "female") self.possessive_pronoun = 'her' self.pronoun = 'she' if self.gender == 'male': self.possessive_pronoun = 'his' self.pronoun = 'he' self.full_name = names.get(self.gender) self.first_name = self.full_name.split(' ')[0] if len(self.full_name.split(' ')[0]) > 1: self.last_name = self.full_name.split(' ')[1] else: self.last_name = "" self.qualities = [generate_quality()] self.history = [] self.attributes = { 'combat': Attribute('combat'), 'lore': Attribute('lore'), 'survival': Attribute('survival') } self.memories = {} for i in range(5): topic = random.choice([ 'traveling', 'camping', 'hunting', 'fishing' 'searching for food', 'fighting' ]) relation = random.choice([ 'father', 'mother', 'sister', 'brother', 'best friend', 'rival' ]) when = random.choice([ 'as a child', 'when ' + self.pronoun + ' was a teenager', 'after leaving ' + self.possessive_pronoun + ' home' ]) self.add_memory( topic, self.get_name() + ' remembered ' + topic + ' with ' + self.possessive_pronoun + ' ' + relation + ' ' + when + '.') self.weapon = Weapon() self.dead = False self.injuries = []
def addAttribute(self, name=None, type=None, value=None): # pylint: disable=W0622 """ Add a user-defined attribute to this network. >>> network.addAttribute('Preliminary', Attribute.BOOLEAN_TYPE, True) The type parameter should be one of the :class:`Attribute.*_TYPE <Network.Attribute.Attribute>` values. Returns the attribute object that is created. """ if name is None or type is None or value is None: raise ValueError, gettext( 'The name, type and value parameters must be specified when adding an attribute.' ) if not isinstance(name, str): raise TypeError, 'The name parameter passed to addAttribute() must be a string.' if type not in Attribute.TYPES: raise TypeError, 'The type parameter passed to addAttribute() must be one of the Attribute.*_TYPE values.' # TODO: validate value based on the type? attribute = Attribute(self, name, type, value) self._attributes.append(attribute) dispatcher.send(('set', 'attributes'), self) return attribute
def setGlobal(name, data, description=""): global globals try: globals[name].data except KeyError: globals[name] = Attribute(data, description) globals[name].data = data
def test_validations_run(self): def greater_than(n, t): return n > t def less_than(n, t): return n < t attribute = Attribute("field", validations=[(greater_than, [0]), (less_than, [2])]) result = attribute.validate(1) self.assertEqual(result, None) result = attribute.validate(0) self.assertEqual( result, "field: Validation failed for function greater_than with args: [0]" ) result = attribute.validate(2) self.assertEqual( result, "field: Validation failed for function less_than with args: [2]")
def setUp(self): """Set up the attribute for testing.""" self.attribute_name = 'Test Attribute' self.attribute = Attribute(self.attribute_name, weightage_function_mock, valuation_function_mock, production_function_mock)
def predictAllExamplesInFile(self, predictFile): """ predicts all the training examples in a file, and returns a list of tuples where the 0th index of the tuple is the predicted value using our tree, and the 1st index of the tuple is the actual value from the data point. """ if self.rootNode == None: print( "You must build a tree from training data before running predictions" ) sys.exit(1) values = [] with open(predictFile, "r") as fh: for exampleLine in fh: if exampleLine[0] == ';': continue #provides easy way to comment out data exampleLineParts = exampleLine.split("D:")[1].strip().split() targetValue = exampleLineParts[-1] exampleAttributes = [] for j in range(len(self.attributesAndValues)): attrName = self.attributesAndValues[j].attrName attrValues = [exampleLineParts[j]] attribute = Attribute(attrName, attrValues) exampleAttributes.append(attribute) te = TrainingExample(exampleAttributes, targetValue) predictVsActualTuple = self.predictExamplePoint(te) values.append(predictVsActualTuple) return values
def new(self, machine_code, nickname): self.attrib = Attribute(self._table) yield self.attrib.new(machine_code=machine_code, nickname=nickname, max_weight=0, \ play_num=0, eat_num=0, be_eated_num=0) self.uid = self.attrib.attrib_id self.nickname = nickname self.machine_code = machine_code
def test_001_withmaxsize(self): a = Attribute('stat3', history_max_size=5) self.assertEqual(a.name, 'stat3') for i in xrange(0, 8): a.value = i self.assertEqual(a.history, [2, 3, 4, 5, 6])
def main(): """.""" from vocabulary import Vocabulary from attribute import Attribute from attribute_structure import AttributeStructure from attribute_system import AttributeSystem vocabulary = Vocabulary(['C'], [], ['V']) a = Attribute("a", []) b = Attribute("b", []) astr = AttributeStructure(a, b) objs = ['a', 'b', 'c'] attribute_system = AttributeSystem(astr, objs) C = ConstantAssignment(vocabulary, attribute_system, {'C': 'a'}) print C._vocabulary vocabulary.add_constant("C2") print C._vocabulary
def readFile(self, dataFilePath): """ @param dataFilePath: path to our dataSet Method reads in our data, and stores example points and attributes in approp fields """ targetNames = attributes = examples = None with open(dataFilePath, "r") as fh: nbrOfTargets = int(fh.readline().strip()) targetValues = fh.readline().strip().split("T:")[1].split() nbrOfAttributes = int(fh.readline().strip()) # build a list of attribute objects holding the attrName and attrValues attributes = [] for attrNum in range(nbrOfAttributes): attrLine = fh.readline().strip().split("A:")[1] attrLineParts = attrLine.split() attrName = attrLineParts[0] attrValues = [] for i in range(2, len(attrLineParts)): attrValues.append(attrLineParts[i]) attribute = Attribute(attrName, attrValues) attributes.append(attribute) # build a list of all the example data nbrOfExamples = int(fh.readline().strip()) examples = [] for i in range(nbrOfExamples): exampleLine = fh.readline().strip().split("D:")[1] exampleLineParts = exampleLine.split() targetValue = exampleLineParts[-1] exampleAttributes = [] for j in range(nbrOfAttributes): attrName = attributes[j].attrName attrValues = [exampleLineParts[j]] attribute = Attribute(attrName, attrValues) exampleAttributes.append(attribute) te = TrainingExample(exampleAttributes, targetValue) examples.append(te) self.targetNames = targetValues self.attributesAndValues = attributes self.trainingExamples = examples
def argsInit(): global state, starts, snake, set, enermylist, score, shield, attr, normal, special, timeIndex shield = 0 score = 0 state = starts timeIndex = 30 set.setInit() attr = Attribute(screen) normal = [] special = [] snake = MySnake(screen, set) enermylist.clear() for i in range(5): enermylist.append(OtherSnake(screen))
def __init__(self, sequence=5): self.scene = glob(PATH) self.scene.sort() self.sequence = sequence self.list = [] self.list_coordinate = [] self.all_data = [] for i, j in zip(self.scene, range(len(self.scene))): self.out = [] data_split = [] print i, "----------------------------" print j sub_path = os.path.join(i, "annotations_.txt") data = np.genfromtxt(sub_path, delimiter=' ') # Center coordinates from Bounding Box x_lim1, y_lim1 = data[:, 1], data[:, 2] x_lim2, y_lim2 = data[:, 3], data[:, 4] x = (x_lim1 + x_lim2) / 2 y = (y_lim1 + y_lim2) / 2 # Normalization x, y = test(x, y, j) data[:, 1], data[:, 2] = x, y # all target in the scene ID = np.unique(data[:, 0][-1]) for j in range(0, int(ID[0])): trajectory = data[data[:, 0] == j, :] # ID,x,y,frame,attribute trajectory = trajectory[:, [0, 1, 2, 5, 9]] # extract attribute trajectory = Attribute(trajectory) if len(trajectory) > 0: data_split.append(trajectory) for split in xrange(len(data_split)): data_split_coor = data_split[split] if data_split_coor.shape[0] > self.sequence + 1: self.out.append(data_split_coor[:, [1, 2, 4, 5, 6, 7, 8, 9]]) self.all_data.append( data_split_coor[:, [1, 2, 4, 5, 6, 7, 8, 9]]) self.list_coordinate.append(self.out) self.list.append(len(self.out))
def main(): # src = Path(getcwd(), 'default-values.txt') # dst = Path(getcwd(), 'AUTOGEN.md') writer = open('AUTOGEN.md', 'w') writer.write('# Default Values\n\n') with open('default-values.txt', 'r') as reader: for line in reader.readlines(): attribute = Attribute(line) description = Comment(line) entry = f'* `{attribute.name}` - {description.text}\n' writer.write(entry) writer.close() system('code AUTOGEN.md')
def generate_type(self, context, template): if self.has_attribute("type"): exist = self.get_attribute("type") attr = Attribute(self.name, "type", ConstValue(str(exist.value))) return attr attr = template.instantiate() attr.generate(context) _type = str(attr.value) if _type == "translate": self.value_class = svg.TransformTranslateValue elif _type == "scale": self.value_class = svg.TransformScaleValue elif _type == "rotate": self.value_class = svg.TransformRotateValue else: self.value_class = svg.TransformSkewXValue return attr
def test_002_withrate(self): a = Attribute('stat4', is_rate=True) self.assertEqual(a.name, 'stat4') a.value = 1 sleep(1) self.assertIsNone(a.value) a.value = 2 sleep(1) self.assertAlmostEqual(a.value, 1, places=2) a.value = 3 sleep(0.5) self.assertAlmostEqual(a.value, 1, places=2) a.value = 4 sleep(2) self.assertAlmostEqual(a.value, 2, places=2) a.value = 5 self.assertAlmostEqual(a.value, 0.5, places=2) self.assertAlmostEqual(a.history_mean(3), 6.0, places=1)
def test_000_nominal(self): a = Attribute('stat') self.assertEqual(a.name, 'stat') a.name = 'stat2' self.assertEqual(a.name, 'stat2') self.assertIsNone(a.value) self.assertEqual(a.history, []) a.value = 1 self.assertEqual(a.value, 1) self.assertEqual(a.history, []) a.value = 2 self.assertEqual(a.value, 2) self.assertEqual(a.history, [1]) a.value = 3 self.assertEqual(a.value, 3) self.assertEqual(a.history, [1, 2]) self.assertEqual(a.history_mean(3), 1.0)
def get_linear_params(self): params = {} params['data_name'] = Attribute(name='data_name', is_enabled=True, values=[]) params['cv'] = Attribute(name='cv', is_enabled=True, values=self.cvs) params['kernels'] = Attribute('kernels', is_enabled=True, values=['linear']) params['coef0'] = Attribute('coef0', is_enabled=True, values=self.coef0s) params['tol'] = Attribute('tol', is_enabled=True, values=self.tols) params['C'] = Attribute('C', is_enabled=True, values=self.cs) return params
def new(self, need_load=True, **dict_data):#Return { fn: value } if need_load: yield self.load(need_value=False) _attr = Attribute(self._table) _attrib_id = yield _attr.new(**dict_data) ''' try: _attrib_id = yield _attr.new(**dict_data) except Exception, e: raise AttribManagerException("[ %s ]new failed. table:%s, data: %s, error:%s." % ( self.__class__, self._table, dict_data, e)) ''' if self._multirow: if not isinstance(self.dict_attribs, dict): log.warn('[ %s.new ]property dict_attribs is not dict. %s' % ( self.__class__, self.dict_attribs )) self.dict_attribs = {} self.dict_attribs[_attrib_id] = _attr else: self.dict_attribs = _attr #returnValue( _attr.value ) returnValue( _attr.new_value() )
def addAttribute(self, name = None, type = None, value = None): """ Add a user-defined attribute to this object. >>> neuron1.addAttribute('Confirmed', Attribute.BOOLEAN_VALUE, True) It is allowable to have multiple attributes on the same object which have the same name. The type parameter should be one of the :class:`Attribute.*_TYPE <Network.Attribute.Attribute>` values. Returns the attribute object that is created. """ if name is None or type is None or value is None: raise ValueError, gettext('The name, type and value parameters must be specified when adding an attribute.') if not isinstance(name, str): raise TypeError, 'The name parameter passed to addAttribute() must be a string.' if type not in Attribute.TYPES: raise TypeError, 'The type parameter passed to addAttribute() must be one of the Attribute.*_TYPE values.' # TODO: validate value based on the type? attribute = Attribute(self, name, type, value) self._attributes.append(attribute) dispatcher.send(('set', 'attributes'), self) return attribute
def generate_attribute_name(self, context, template): if self.parent is None: return None if self.has_attribute("attributeName"): exist = self.get_attribute("attributeName") attr = Attribute(self.name, "attributeName", ConstValue(str(exist.value))) return attr attr = template.instantiate() if self.name == "SVGAnimateTransformElement": template = get_svg_animatable_transform_attribute(self.parent.name) else: template = get_svg_animatable_attribute(self.parent.name) # setup value class if self.name == "SVGAnimateElement" or self.name == "SVGSetElement": self.value_class = template.value_class attr.value = ConstValue(template.attr) attr.generate(context) return attr
def pretty_data(data): ret = Attribute() ret['data'] = {} for k in data: if type(data[k]) is dict: rows = data[k] if rows != {}: for p in rows: item = rows[p] if type(item) is dict: for m in item: if m == 'error_info': ret['error'] = item[m] elif m == k: if 'ticker' in item[m]: ret['data'][item[m]['ticker']] = item else: ret['data'][p] = item[m] else: ret['data'][p] = rows[p] else: ret['data'][k] = data[k] return ret
new_seq_item.append(item) expanded.append(new_seq_item) if (len(value["sequence"]) == 0): new_seq_item = [] for item in value["itemset"]: new_seq_item.append(item) expanded.append(new_seq_item) num_users = float(self.attributes["user"]["domain"]) * float( value["support"]) for i in range(int(num_users)): selected = random.randrange( 1, int(self.attributes["user"]["domain"])) selected_user = self.attributes["user"]["represent"] + str( selected) if selected_user not in self.exp_sequences: self.exp_sequences[selected_user] = {} self.exp_sequences[selected_user][key] = expanded if __name__ == '__main__': attribute = Attribute() attribute.load_dist_json() attribute.load_attr_cvs() pattern = Pattern(attribute.sem_to_rep) pattern.load() expanded_seq = Sequencepat(attribute.distribution, pattern.patterns) expanded_seq.expand() print expanded_seq.exp_sequences
def TrainData(): """ Trains the naive Bayes model against training data to create a model to use against test data Returns: list of w_0 and w_i weight values for each attribute to determine prediction against test data """ nb_model = dict() # Spin through each training row and aggregate data num_trained = 0 for row in training_file_data: split_row = row.split(",") for i in range(len(split_row)): key = str(i) if key not in nb_model: nb_model[key] = Attribute(attrib_dict[key]) nb_model[key].AddNewData(split_row[i], split_row[-1]) num_trained += 1 if num_trained >= train_limit: # The training limit has been met so break out of for loop break # Calculate the probabilities for each attribute for i in range(len(nb_model)): nb_model[str(i)].CalculateProbabilities(beta, beta) # w_0 = log( P(C=1)/P(C=0) ) + SUM(from i=1 to n) log( P(X_i=0|C=1) / P(X_i=0|C=0) ) if nb_model[str(attrib_cnt - 1)].prob_one == 0 or nb_model[str(attrib_cnt - 1)].prob_zero == 0: if nb_model[str(attrib_cnt - 1)].prob_one == 0: w_0 = -99 #simulate negative infinity without overflow error else: w_0 = 99 #simulate positive infinity without overflow error else: w_0 = math.log(nb_model[str(attrib_cnt - 1)].prob_one / nb_model[str(attrib_cnt - 1)].prob_zero) for i in range(len(nb_model) - 1): if nb_model[str(i)].prob_zero_given_one != 0 and nb_model[str( i)].prob_zero_given_zero != 0: w_0 += math.log(nb_model[str(i)].prob_zero_given_one / nb_model[str(i)].prob_zero_given_zero) # w_i = log( P(X_i=1|C=1)/P(X_i=1|C=0) ) - log( P(X_i=0|C=1)/P(X_i=0|C=0) ) weights = [] for i in range(len(nb_model) - 1): attrib = nb_model[str(i)] if (attrib.prob_one_given_one == 0 or attrib.prob_one_given_zero == 0) and (attrib.prob_zero_given_one == 0 or attrib.prob_zero_given_zero == 0): weights.append(0.0) elif attrib.prob_one_given_one == 0 or attrib.prob_one_given_zero == 0: weights.append(0.0 - math.log(attrib.prob_zero_given_one / attrib.prob_zero_given_zero)) elif attrib.prob_zero_given_one == 0 or attrib.prob_zero_given_zero == 0: weights.append( math.log(attrib.prob_one_given_one / attrib.prob_one_given_zero)) else: weights.append( math.log(attrib.prob_one_given_one / attrib.prob_one_given_zero) - math.log(attrib.prob_zero_given_one / attrib.prob_zero_given_zero)) # Prepend w_0 to model model = str(w_0) + "\n" # Append all attributes to model for i in range(len(weights)): model += nb_model[str(i)].name + "\t" + str(weights[i]) + "\n" # Write model to output file model_file.write(model) # Append w_0 to weights list. This contains all weights and is ordered by attribute index with w_0 being last. weights.append(w_0) return weights