def find_max_eigenpair(T, outer_iterations = 10, inner_iterations = 100): """ Run tensor power method (Algorithm 1 of Anandkumar/Ge/Hsu/Kakade/Telgarsky, 2012). """ D = T.shape[0] eps = 1e-10 best = (-np.inf, np.zeros(D)) # Outer iterations for tau in xrange(outer_iterations): # (1) Draw a random initialization θ_t theta = normalize( randn( D ) ) # Inner iterations for t in xrange(inner_iterations): # 2) Update θ ← T(I, θ, θ)/||T(I, θ, θ)|| theta_ = normalize( T.ttv( (theta, theta), modes = (1,2) ) ) if norm(theta - theta_) < eps: break # (3) Choose θ_t with max eigenvalue λ = T(θ, θ, θ) lbda = float( T.ttv( (theta, theta, theta), modes = (0,1,2) ) ) epair = lbda, theta if epair[0] > best[0]: best = epair _, theta = best for t in xrange(inner_iterations): # 2) Update θ ← T(I, θ, θ)/||T(I, θ, θ)|| theta = normalize( T.ttv( (theta, theta), modes = (1,2) ) ) # (4) Update θ lbda = float(T.ttv( (theta, theta, theta), modes = (0,1,2) )) # (5) Return λ, θ return lbda, theta
def isShowAsp(self, typ, lon1, lon2, p = -1): res = False if typ != chart.Chart.NONE and (not self.options.intables or self.options.aspect[typ]): val = True #check traditional aspects if self.options.intables: if self.options.traditionalaspects: if not(typ == chart.Chart.CONJUNCTIO or typ == chart.Chart.SEXTIL or typ == chart.Chart.QUADRAT or typ == chart.Chart.TRIGON or typ == chart.Chart.OPPOSITIO): val = False else: lona1 = lon1 lona2 = lon2 if self.options.ayanamsha != 0: lona1 -= self.chart.ayanamsha lona1 = util.normalize(lona1) lona2 -= self.chart.ayanamsha lona2 = util.normalize(lona2) sign1 = int(lona1/chart.Chart.SIGN_DEG) sign2 = int(lona2/chart.Chart.SIGN_DEG) signdiff = math.fabs(sign1-sign2) #check pisces-aries transition if signdiff > chart.Chart.SIGN_NUM/2: signdiff = chart.Chart.SIGN_NUM-signdiff#!? if self.arsigndiff[typ] != signdiff: val = False if not self.options.aspectstonodes and p == astrology.SE_MEAN_NODE: val = False res = val return res
def test_number_and_int_fields(): f = fields.NumberField(multiple_of=10) definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'number', 'multipleOf': 10, } f = fields.NumberField(minimum=0, maximum=10, exclusive_minimum=True, exclusive_maximum=True) definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'number', 'exclusiveMinimum': True, 'exclusiveMaximum': True, 'minimum': 0, 'maximum': 10, } f = fields.NumberField(enum=(1, 2, 3)) definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'number', 'enum': [1, 2, 3], } f = fields.IntField() definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'integer', }
def test_string_field(): f = fields.StringField() definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == {'type': 'string'} f = fields.StringField(min_length=1, max_length=10, pattern='^test$', enum=('a', 'b', 'c'), title='Pururum') expected_items = [ ('type', 'string'), ('title', 'Pururum'), ('enum', ['a', 'b', 'c']), ('pattern', '^test$'), ('minLength', 1), ('maxLength', 10), ] definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == dict(expected_items) definitions, ordered_schema = f.get_definitions_and_schema(ordered=True) assert isinstance(ordered_schema, OrderedDict) assert normalize(ordered_schema) == OrderedDict(expected_items) with pytest.raises(ValueError) as e: fields.StringField(pattern='(') assert str(e.value) == 'Invalid regular expression: unbalanced parenthesis'
def test_recursive_document_field(): class Tree(Document): node = fields.OneOfField([ fields.ArrayField(fields.DocumentField('self')), fields.StringField(), ]) expected_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'test_fields.Tree': { 'type': 'object', 'additionalProperties': False, 'properties': { 'node': { 'oneOf': [ { 'type': 'array', 'items': {'$ref': '#/definitions/test_fields.Tree'}, }, { 'type': 'string', }, ], }, }, }, }, '$ref': '#/definitions/test_fields.Tree', } assert normalize(Tree.get_schema()) == normalize(expected_schema)
def test_string_derived_fields(): f = fields.EmailField() definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'string', 'format': 'email', } f = fields.IPv4Field() definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'string', 'format': 'ipv4', } f = fields.DateTimeField() definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'string', 'format': 'date-time', } f = fields.UriField() definitions, schema = f.get_definitions_and_schema() assert normalize(schema) == { 'type': 'string', 'format': 'uri', }
def drawHouseNames(self, chrt, rHouseNames): (cx, cy) = self.center.Get() clr = self.options.clrhousenumbers if self.bw: clr = (0, 0, 0) pen = wx.Pen(clr, 1) self.bdc.SetPen(pen) asc = self.chartRadix.houses.ascmc[houses.Houses.ASC] if self.options.ayanamsha != 0 and self.options.hsys == "W": asc = util.normalize(self.chartRadix.houses.ascmc[houses.Houses.ASC] - self.chartRadix.ayanamsha) for i in range(1, houses.Houses.HOUSE_NUM + 1): width = 0.0 if i != houses.Houses.HOUSE_NUM: width = chrt.houses.cusps[i + 1] - chrt.houses.cusps[i] else: width = chrt.houses.cusps[1] - chrt.houses.cusps[houses.Houses.HOUSE_NUM] width = util.normalize(width) halfwidth = math.radians(width / 2.0) dif = math.radians(util.normalize(asc - chrt.houses.cusps[i])) x = cx + math.cos(math.pi + dif - halfwidth) * rHouseNames y = cy + math.sin(math.pi + dif - halfwidth) * rHouseNames if i == 1 or i == 2: xoffs = 0 yoffs = self.symbolSize / 4 if i == 2: xoffs = self.symbolSize / 8 else: xoffs = self.symbolSize / 4 yoffs = self.symbolSize / 4 self.draw.text((x - xoffs, y - yoffs), common.common.Housenames[i - 1], fill=clr, font=self.fntText)
def test(self, categories): for i in range(Constants.NUM_SUBJECTS): trainSubjects = [1, 2, 3, 4] testSubjects = [i + 1] trainSubjects.remove(i + 1) trainVoxelArrayMap = util.getVoxelArray(subjectNumbers = trainSubjects) testVoxelArrayMap = util.getVoxelArray(subjectNumbers = testSubjects) util.normalize(trainVoxelArrayMap) util.normalize(testVoxelArrayMap) util.filterData(trainVoxelArrayMap, categories=categories) util.filterData(testVoxelArrayMap, categories=categories) Xtrain = numpy.array([trainVoxelArrayMap[key] for key in trainVoxelArrayMap]) Ytrain = numpy.array([key[1] for key in trainVoxelArrayMap]) Xtest = numpy.array([testVoxelArrayMap[key] for key in testVoxelArrayMap]) Yanswer = numpy.array([key[1] for key in testVoxelArrayMap]) Yprediction = OneVsRestClassifier(LinearSVC()).fit(Xtrain, Ytrain).predict(Xtest) # Yprediction = OneVsOneClassifier(LinearSVC()).fit(Xtrain, Ytrain).predict(Xtest) correct = 0 for index in range(len(Yanswer)): if Yanswer[index] == Yprediction[index]: correct += 1 # correct = [1 if Yanswer[index] == Yprediction[index] else 0 for index in range(len(Yanswer))] print categories, "Correct Predictions: ", correct, "/", len(Yanswer) return float(correct) * 100 / len(Yanswer)
def correlationNearestNeighbor(): voxelArrayMap = util.getVoxelArray(False, False,True,False, [4]) util.normalize(voxelArrayMap) correct = [{ }]*3 totalCountCorrect = [0] *3 totalCountInCorrect = [0] *3 incorrect = [{ }]*3 voxelCopy = voxelArrayMap.keys() totalCorrect =0 totalIncorrect =0 count = 0 for key in voxelCopy: count +=1 testExample = voxelArrayMap[key] voxelArrayMap.pop(key, None) averageCategoryCorrelations = matrixify(calculateAverageCorrelations(voxelArrayMap)) exampleCorrelations = calculateSingleExampleCorrelations(testExample, voxelArrayMap) classifiedCategory = classifyByClosestCorrelation(exampleCorrelations,averageCategoryCorrelations) if classifiedCategory[0] == key[1]: totalCorrect +=1 else: totalIncorrect +=1 voxelArrayMap[key] = testExample print "Correct", totalCorrect, "Incorrect", totalIncorrect, "Percentage Correct ", totalCorrect/float((totalCorrect +totalIncorrect))
def toHCs(self, mundane, idprom, raprom, dsa, nsa, aspect, asp=0.0): #day-house, night-house length dh = dsa/3.0 nh = nsa/3.0 #ra rise, ra set rar = self.ramc+dsa ras = self.raic+nsa rar = util.normalize(rar) ras = util.normalize(ras) #ra housecusps rahcps = ((primdirs.PrimDir.HC2, rar+nh), (primdirs.PrimDir.HC3, rar+2*nh), (primdirs.PrimDir.HC5, self.raic+nh), (primdirs.PrimDir.HC6, self.raic+2*nh), (primdirs.PrimDir.HC8, ras+dh), (primdirs.PrimDir.HC9, ras+2*dh), (primdirs.PrimDir.HC11, self.ramc+dh), (primdirs.PrimDir.HC12, self.ramc+2*dh)) for h in range(len(rahcps)): rahcp = rahcps[h][1] rahcp = util.normalize(rahcp) arc = raprom-rahcp ok = True if idprom == astrology.SE_MOON and self.options.pdsecmotion: for itera in range(self.options.pdsecmotioniter+1): ok, arc = self.calcHArcWithSM(mundane, idprom, h, arc, aspect, asp) if not ok: break if ok: self.create(mundane, idprom, primdirs.PrimDir.NONE, rahcps[h][0], aspect, chart.Chart.CONJUNCTIO, arc)
def test_document_field(): document_cls_mock = mock.Mock() expected_schema = mock.Mock() attrs = { 'get_definitions_and_schema.return_value': ({}, expected_schema), 'get_definition_id.return_value': 'document.Document', 'is_recursive.return_value': False, } document_cls_mock.configure_mock(**attrs) f = fields.DocumentField(document_cls_mock) definitions, schema = f.get_definitions_and_schema() assert schema == expected_schema assert not definitions definitions, schema = f.get_definitions_and_schema(ref_documents=set([document_cls_mock])) assert normalize(schema) == {'$ref': '#/definitions/document.Document'} f = fields.DocumentField(document_cls_mock, as_ref=True) definitions, schema = f.get_definitions_and_schema() assert definitions == {'document.Document': expected_schema} assert normalize(schema) == {'$ref': '#/definitions/document.Document'} attrs = { 'get_definitions_and_schema.return_value': ({}, expected_schema), 'get_definition_id.return_value': 'document.Document', 'is_recursive.return_value': True, } document_cls_mock.reset_mock() document_cls_mock.configure_mock(**attrs) f = fields.DocumentField(document_cls_mock, as_ref=True) definitions, schema = f.get_definitions_and_schema() assert schema == expected_schema assert not definitions
def init_nmf(F, T, Z, q_init=None): if q_init is None: q = dict() q['f|z'] = normalize(1+numpy.random.exponential(size=(F, Z)), axis=0) q['zt'] = normalize(1+numpy.random.exponential(size=(Z, T))) else: q = copy.deepcopy(q_init) return q
def playlistinfo(self): with mpd.connect(self.host, self.port) as client: info = client.playlistinfo() info = [item for item in info if item] for item in info: normalize(item, {'title' : ('file',), 'artist' : tuple()}) item['duration'] = fmt_time(item.get('time', 0)) return info
def test_basics(): class User(Document): id = Var({ 'response': IntField(required=True) }) login = StringField(required=True) class Task(Document): class Options(object): title = 'Task' description = 'A task.' definition_id = 'task' id = IntField(required=Var({'response': True})) name = StringField(required=True, min_length=5) type = StringField(required=True, enum=['TYPE_1', 'TYPE_2']) created_at = DateTimeField(required=True) author = Var({'response': DocumentField(User)}) assert normalize(Task.get_schema()) == normalize({ '$schema': 'http://json-schema.org/draft-04/schema#', 'additionalProperties': False, 'description': 'A task.', 'properties': { 'created_at': {'format': 'date-time', 'type': 'string'}, 'id': {'type': 'integer'}, 'name': {'minLength': 5, 'type': 'string'}, 'type': {'enum': ['TYPE_1', 'TYPE_2'], 'type': 'string'} }, 'required': ['created_at', 'type', 'name'], 'title': 'Task', 'type': 'object' }) assert normalize(Task.get_schema(role='response')) == normalize({ '$schema': 'http://json-schema.org/draft-04/schema#', 'title': 'Task', 'description': 'A task.', 'type': 'object', 'additionalProperties': False, 'properties': { 'created_at': {'format': 'date-time', 'type': 'string'}, 'id': {'type': 'integer'}, 'name': {'minLength': 5, 'type': 'string'}, 'type': {'enum': ['TYPE_1', 'TYPE_2'], 'type': 'string'}, 'author': { 'additionalProperties': False, 'properties': { 'id': {'type': 'integer'}, 'login': {'type': 'string'} }, 'required': ['id', 'login'], 'type': 'object' }, }, 'required': ['created_at', 'type', 'name', 'id'], })
def test_multiple_inheritance(): class IntChild(Document): class Options(object): definition_id = 'int_child' foo = IntField() bar = IntField() class StringChild(Document): class Options(object): definition_id = 'string_child' foo = StringField() bar = StringField() class Parent(IntChild, StringChild): class Options(object): inheritance_mode = ONE_OF foo = BooleanField() bar = BooleanField() expected_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'oneOf': [ {'$ref': '#/definitions/int_child'}, {'$ref': '#/definitions/string_child'}, { 'type': 'object', 'properties': { 'foo': {'type': 'boolean'}, 'bar': {'type': 'boolean'} }, 'additionalProperties': False, } ], 'definitions': { 'int_child': { 'type': 'object', 'properties': { 'foo': {'type': 'integer'}, 'bar': {'type': 'integer'} }, 'additionalProperties': False, }, 'string_child': { 'type': 'object', 'properties': { 'foo': {'type': 'string'}, 'bar': {'type': 'string'} }, 'additionalProperties': False, } } } actual_schema = Parent.get_schema() assert normalize(actual_schema) == normalize(expected_schema)
def nn(test,train): test=util.normalize(test,gpuFlag=True); train=util.normalize(train,gpuFlag=True); distances=ca.dot(test,ca.transpose(train)); # print distances.shape distances_np=np.array(distances); indices=np.argsort(distances,axis=1)[:,::-1] distances=(1-np.sort(distances,axis=1))[:,::-1]; return indices,distances
def search(self, type, what): self.last_search = (type, what) with mpd.connect(self.host, self.port) as client: results = client.search(type, what) results = [x for x in results if 'file' in x and x['file'].strip() != ''] for result in results: normalize(result, {'title' : ('file',)}) self.last_search_results = results return results
def list(self, uri): with mpd.connect(self.host, self.port) as client: listing = client.lsinfo(uri) for d in listing: if 'directory' in d: d['dirname'] = d['directory'].split('/')[-1] if 'file' in d: normalize(d, {'title' : ('file',)}) return listing
def calcHArcWithSM(self, mundane, idprom, h, hcps, arc, aspect, asp=0.0): sm = secmotion.SecMotion(self.chart.time, self.chart.place, idprom, arc, self.chart.place.lat, self.chart.houses.ascmc2, self.options.topocentric) lonprom = sm.planet.speculums[primdirs.PrimDirs.REGIOSPECULUM][planets.Planet.LONG] pllat = sm.planet.speculums[primdirs.PrimDirs.REGIOSPECULUM][planets.Planet.LAT] raprom = sm.planet.speculums[primdirs.PrimDirs.REGIOSPECULUM][planets.Planet.RA] declprom = sm.planet.speculums[primdirs.PrimDirs.REGIOSPECULUM][planets.Planet.DECL] if not mundane: lonprom += asp lonprom = util.normalize(lonprom) latprom, raprom, declprom = 0.0, 0.0, 0.0 if self.options.subzodiacal == primdirs.PrimDirs.SZPROMISSOR or self.options.subzodiacal == primdirs.PrimDirs.SZBOTH: if self.options.bianchini: val = self.getBianchini(pllat, chart.Chart.Aspects[aspect]) if math.fabs(val) > 1.0: return False, 0.0 latprom = math.degrees(math.asin(val)) else: latprom = pllat #calc real(wahre)ra # raprom, declprom = util.getRaDecl(lonprom, latprom, self.chart.obl[0]) raprom, declprom, dist = astrology.swe_cotrans(lonprom, latprom, 1.0, -self.chart.obl[0]) else: raprom, declprom, distprom = astrology.swe_cotrans(lonprom, 0.0, 1.0, -self.chart.obl[0]) ID = 0 W = 1 MD = 2 UMD = 3 EASTERN = 4 pl = self.chart.planets.planets[0] #get zd of HC zdsig = pl.getZD(hcps[h][MD], self.chart.place.lat, 0.0, hcps[h][UMD]) val = math.sin(math.radians(self.chart.place.lat))*math.sin(math.radians(zdsig)) if math.fabs(val) > 1.0: return False, 0.0 polesig = math.degrees(math.asin(val)) val = math.tan(math.radians(declprom))*math.tan(math.radians(polesig)) if math.fabs(val) > 1.0: return False, 0.0 qprom = math.degrees(math.asin(val)) wprom = 0.0 if hcps[h][EASTERN]: wprom = raprom-qprom else: wprom = raprom+qprom wprom = util.normalize(wprom) return True, wprom-hcps[h][W]
def JSDDict(dict1, dict2, normalized = False): if not normalized: dict1 = util.normalize(dict1) dict2 = util.normalize(dict2) keys = set(dict1.keys() + dict2.keys()) vect1 = [dict1[k] if k in dict1 else 0.0 for k in keys] vect2 = [dict2[k] if k in dict2 else 0.0 for k in keys] return jsd.js_div(vect1, vect2)
def __init__(self, radix, y, m, d, t, cnt=0): #t is in GMT placelon = radix.place.lon placelat = radix.place.lat #negative on SH? ramc = radix.houses.ascmc2[houses.Houses.MC][houses.Houses.RA] declAsc = radix.houses.ascmc2[houses.Houses.ASC][houses.Houses.DECL] #radian!! oaAsc = util.normalize(ramc+90.0) val = math.tan(math.radians(declAsc))*math.tan(math.radians(placelat)) adlatAsc = 0.0 if math.fabs(val) <= 1.0: adlatAsc = math.degrees(math.asin(val)) dsalatAsc = 90.0+adlatAsc nsalatAsc = 90.0-adlatAsc dhlatAsc = dsalatAsc/3.0 #diurnal house nhlatAsc = nsalatAsc/3.0 #nocturnal house #placelon is negative in case of western long!! lon360 = placelon if placelon < 0.0: lon360 = 360.0+placelon jdbirth = astrology.swe_julday(y, m, d, t, astrology.SE_GREG_CAL) jd = jdbirth+cnt*365.2421904 #deltaYear diffYear = (jd-radix.time.jd)/365.2421904 #Profection cycle in Years cycInYears = diffYear-(int(diffYear/12.0))*12 #Number of diurnal steps (real) DCycInYears = cycInYears if cycInYears > 6.0: DCycInYears = 6.0 #Number of nocturnal steps (real) NCycInYears = 0.0 if cycInYears > 6.0: NCycInYears = cycInYears-DCycInYears # Delta geographical longitude for the fictious movement diffLon = DCycInYears*dhlatAsc+NCycInYears*nhlatAsc #New geographical long. to cast the fictious chart (range 0-360) lon360Z = util.normalize(lon360+diffLon) #Convert (0-360) --> E/W the longitude self.lonZ = lon360Z self.east = True if lon360Z > 180.0: self.lonZ = 360.0-lon360Z self.east = False
def calcFullAstronomicalProc(self, da, oblN, raN, declN, placelat, ascmc2, raequasc): # print '**** %s ****' % pl.name ksi = raN+da ksi = util.normalize(ksi) # print 'ksi=%f' % ksi # print 'declN=%f' % declN roblN = math.radians(oblN) rksi = math.radians(ksi) rdeclN = math.radians(declN) longSZ = 0.0 if ksi == 90.0: longSZ = 90.0 elif ksi == 270.0: longSZ = 270.0 else: # print 'obl=%f' % oblN Fd = 0.0 if math.cos(rksi) != 0.0: Fd = math.degrees(math.atan((math.cos(roblN)*math.sin(rksi)+math.sin(roblN)*math.tan(rdeclN))/math.cos(rksi))) # print 'rFd=%f' % math.radians(Fd) if ksi >= 0.0 and ksi < 90.0: longSZ = Fd # print 'First ksi' elif ksi > 90.0 and ksi < 270.0: longSZ = Fd+180.0 # print 'Second ksi' elif ksi > 270.0 and ksi < 360.0: longSZ = Fd+360.0 # print 'Third ksi' if longSZ <= 0.0: # print 'longSz<=0' longSZ = Fd+360.0 longSZ = util.normalize(longSZ)## # print 'longSz=%f' % longSZ roblN = math.radians(oblN) rksi = math.radians(ksi) rdeclN = math.radians(declN) latSZ = math.degrees(math.asin(math.sin(rdeclN)*math.cos(roblN)-math.cos(rdeclN)*math.sin(rksi)*math.sin(roblN))) raSZ, declSZ, distSZ = astrology.swe_cotrans(longSZ, latSZ, 1.0, -oblN) self.data = (longSZ, latSZ, self.data[Planet.DIST], self.data[Planet.SPLON], self.data[Planet.SPLAT], self.data[Planet.SPDIST]) self.dataEqu = (raSZ, declSZ, self.dataEqu[Planet.DISTEQU], self.dataEqu[Planet.SPRAEQU], self.dataEqu[Planet.SPDECLEQU], self.dataEqu[Planet.SPDISTEQU]) self.speculums = [] self.computePlacidianSpeculum(placelat, ascmc2) self.computeRegiomontanSpeculum(placelat, ascmc2, raequasc)
def extractFeature(img): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = imutils.resize(img, width=200) # Get dominant color scheme via k-means # reshape the image to be a list of pixels img = img.reshape((img.shape[0] * img.shape[1], 3)) # run kmeans clt = KMeans(n_clusters=NUM_CLUSTERS) clt.fit(img) # convert to hsv hsv = cv2.cvtColor(np.float32([clt.cluster_centers_]), cv2.COLOR_RGB2HSV) # create the palette histograms hue_palette = [0]*NUM_BINS sat_palette = [0]*NUM_BINS val_palette = [0]*NUM_BINS for c in hsv[0]: h, s, v = c ''' BINARY OR HISTOGRAM? ''' hue_palette[util.getBinIndex(h, NUM_BINS, util.MAX_HUE)] = 1 sat_palette[util.getBinIndex(s, NUM_BINS, util.MAX_SAT)] = 1 val_palette[util.getBinIndex(v, NUM_BINS, util.MAX_VAL)] = 1 num_unique_hues = util.normalize(sum(hue_palette), 0, NUM_BINS) num_unique_sat = util.normalize(sum(sat_palette), 0, NUM_BINS) num_unique_vals = util.normalize(sum(val_palette), 0, NUM_BINS) # return the features features = hue_palette + sat_palette + val_palette + \ [num_unique_hues, num_unique_sat, num_unique_vals] assert len(features) == len(getFeatureName()), \ "length of color palette features matches feature names" if IS_DEBUG: # build a histogram of clusters and then create a figuo each color hist = centroid_histogram(clt) # representing the number of pixels labeled t bar = plot_colors(hist, clt.cluster_centers_) # show our color bar plt.figure() plt.axis("off") plt.imshow(bar) plt.show() # value contrast, color contrast return features
def run(): voxelArrayMap = util.getVoxelArray(False, False, False, True, [2]) util.normalize(voxelArrayMap) centroids = [0] * 5 keys = list(voxelArrayMap.keys()) selected = [] for index in range(len(centroids)): while True: centroidKey = random.choice(keys) category = centroidKey[1] if category not in selected: selected.append(category) break print centroidKey centroids[index] = voxelArrayMap[centroidKey] numIters = 120 assignmentMap = {} for iteration in range(numIters): newCentroids = [ [0] * 1973 for i in range(len(centroids))] clusterCounts = [0] * len(newCentroids) for key in voxelArrayMap: value = voxelArrayMap[key] distances = [ util.getDistance(centroid, value) for centroid in centroids ] minDistance = min(distances) optCentroid = distances.index(minDistance) assignmentMap[key] = optCentroid for voxelIndex in range(len(newCentroids[optCentroid])): newCentroids[optCentroid][voxelIndex] += value[voxelIndex] clusterCounts[optCentroid] += 1 for index in range(len(newCentroids)): numPoints = clusterCounts[index] if numPoints != 0: centroid = newCentroids[index] centroid = [ centroid[voxelIndex] / numPoints for voxelIndex in range(len(centroid)) ] newCentroids[index] = centroid centroids = newCentroids # we want ClusterNum -> What type of image catMap = {} for key in assignmentMap: assignment = assignmentMap[key] # key[1] is the category (0-5 for face, body, etc) # assignment is the optimal centroid if assignment in catMap: arr = catMap[assignment] arr.append(key[1]) catMap[assignment] = arr else: catMap[assignment] = [key[1]] print catMap
def look_at(self, eye, center, up): up = normalize(up) f = normalize(sub(center, eye)) s = cross(f, up) u = cross(s, f) matrix = Matrix([ s[0], s[1], s[2], 0, u[0], u[1], u[2], 0, -f[0], -f[1], -f[2], 0, eye[0], eye[1], eye[2], 1, ]).inverse() return matrix * self
def init_ntf(F, T, R, Z, S, q_init=None): if q_init is None: q = dict() q['s'] = normalize(1+numpy.random.exponential(size=(S, ))) q['f|sz'] = normalize(1+numpy.random.exponential(size=(F, S, Z)), axis=0) q['zt|s'] = normalize(1+numpy.random.exponential(size=(Z, T, S)), axis=(0, 1)) q['r|ts'] = normalize(1+numpy.random.exponential(size=(R, T, S)), axis=0) q['ft|s'] = numpy.zeros((F, T, S)) # redundant but keep in for debugging q['ftr'] = numpy.zeros((F, T, R)) else: q = copy.deepcopy(q_init) return q
def getSimpleDot(test,train,gpuFlag=False,normalize=True): if normalize: test = util.normalize(test,gpuFlag=gpuFlag); train = util.normalize(train,gpuFlag=gpuFlag); if gpuFlag: distances=ca.dot(test,ca.transpose(train)); distances=np.array(distances); else: distances=np.dot(test,train.T); return distances
def initModel(self): self.numUsers, self.numItems = self.trainMatrix.shape() self.prediction = dok_matrix((self.numUsers, self.numItems)) self.MAX_Iterations = int(self.configHandler.getParameter('PLSA', 'MAX_Iterations')) self.numFactors = int(self.configHandler.getParameter('PLSA', 'numFactors')) self.X = np.random.uniform(0, 1, size=(self.numUsers, self.numFactors)) # P(z|x) self.X = normalize(self.X) self.Y = np.random.uniform(0, 1, size=(self.numItems, self.numFactors)) # P(y|z) self.Y = normalize(self.Y) self.Q = np.zeros((self.numUsers, self.numFactors, self.numItems)) # P(y,z|x)
def find_similar(sent, lst): dist = {} lst = lst + split_and_add(lst) for item in lst: item = util.normalize(item) sent = util.normalize(sent) d = jarow(item, sent) #print item , d if d > 0.75: dist[item] = d #print dist max_arr = util.get_max(dist) return max_arr
def toHCs(self, mundane, idprom, raprom, declprom, aspect, asp=0.0): '''Calculates directions of the Promissor to intermediate house cusps''' #aspects of proms to HCs in Zodiacal!? ID = 0 W = 1 MD = 2 UMD = 3 EASTERN = 4 #Regiomontan: W of housecusps (equator) HL = 30.0 HC11 = util.normalize(self.ramc+HL) HC12 = util.normalize(HC11+HL) HC2 = util.normalize(HC12+2*HL) HC3 = util.normalize(HC2+HL) HC5 = util.normalize(self.raic+HL) HC6 = util.normalize(HC5+HL) HC8 = util.normalize(HC6+2*HL) HC9 = util.normalize(HC8+HL) #housecusps hcps = ((primdirs.PrimDir.HC2, HC2, 2*HL, False, True), (primdirs.PrimDir.HC3, HC3, HL, False, True), (primdirs.PrimDir.HC5, HC5, HL, False, False), (primdirs.PrimDir.HC6, HC6, 2*HL, False, False), (primdirs.PrimDir.HC8, HC8, 2*HL, True, False), (primdirs.PrimDir.HC9, HC9, HL, True, False), (primdirs.PrimDir.HC11, HC11, HL, True, True), (primdirs.PrimDir.HC12, HC12, 2*HL, True, True)) pl = self.chart.planets.planets[0] for h in range(len(hcps)): #get zd of HC zdsig = pl.getZD(hcps[h][MD], self.chart.place.lat, 0.0, hcps[h][UMD]) val = math.sin(math.radians(self.chart.place.lat))*math.sin(math.radians(zdsig)) if math.fabs(val) > 1.0: continue polesig = math.degrees(math.asin(val)) val = math.tan(math.radians(declprom))*math.tan(math.radians(polesig)) if math.fabs(val) > 1.0: continue qprom = math.degrees(math.asin(val)) wprom = 0.0 if hcps[h][EASTERN]: wprom = raprom-qprom else: wprom = raprom+qprom wprom = util.normalize(wprom) arc = wprom-hcps[h][W] ok = True if idprom == astrology.SE_MOON and self.options.pdsecmotion: for itera in range(self.options.pdsecmotioniter+1): ok, arc = self.calcHArcWithSM(mundane, idprom, h, hcps, arc, aspect, asp) if not ok: break if ok: self.create(mundane, idprom, primdirs.PrimDir.NONE, hcps[h][ID], aspect, chart.Chart.CONJUNCTIO, arc)
def match_intensity(img1, img2): """ scale the intensity of img2 to img1 using least square img1 = A * img2 + b (element-wise) """ if img1.shape != img2.shape: raise ("Input Image 1 & 2 have different sizes!") img2 = normalize(img2, clip=True) img1_flat, img2_flat = img1.flatten(), img2.flatten() A = np.stack([img2_flat, np.ones((img2_flat.size))], axis=0) b = img1_flat At = A.transpose() x = np.linalg.lstsq(At, b, rcond=None) img2_scale = img2 * x[0][0] + x[0][1] return img2_scale
def sample(self, distribution, values=None): if type(distribution) == util.Counter: # items = distribution.items() items = list(distribution.items()) # Order possible actions in fixed order comparable to # Python2 order,which the autograder relies on. KS possibleActions = [i[0] for i in items] items.sort(key=lambda i: cmpGhostActions(i[0], possibleActions)) distribution = [i[1] for i in items] values = [i[0] for i in items] if sum(distribution) != 1: distribution = util.normalize(distribution) choice = random.random() i, total = 0, distribution[0] while choice > total: i += 1 total += distribution[i] return values[i]
def getData(channel, pop): nEvents = 50 #the maximum time value. maxt = 40. - .3125 # the evenly sampled times. eTimes = np.linspace(0., maxt, 128) # zero-crossing period (.5 period over frequency), currently unused zCPeriod = .5 / 1.2 #container to hold the scores scores = np.zeros((pop.shape[0])) # pop.shape[0] = popMax #container to hold the true sample times (from the gA) tVec = np.zeros(128) #fill a matrix of our data events, would be nice to do this outside of the fitness score loop. data = np.zeros((nEvents, 128)) phi0 = np.zeros(nEvents) amp = np.zeros(nEvents) for event in range(nEvents): #this is the data file. #the first entry is the initial phase #the second entry is the amplitude #the rest of the entries are the waveform data evstr = "00" chstr = str(channel) #print chstr if event < 10: evstr = "00" + str(event) elif event >= 10 and event < 100: evstr = "0" + str(event) # print evstr #infile="/users/PCON0003/osu10643/src/SignalCalibrationGA/data/"+chstr+"withphase"+evstr+".txt" infile = "data/" + chstr + "withphase" + evstr + ".txt" #the max time of the 126th (indexed from 0) sample temp = np.genfromtxt(infile, delimiter="\n") phi0[event] = temp[0] amp[event] = temp[1] trace = temp[2:] # phi0[event]=util.getInstPhase(eTimes,trace, 0) data[event] = util.normalize(trace) return data, amp, phi0
def build(cls, data, config): # Preprocess data to construct an embedding # Reserve 0 for the special NIL token. # return: {'char1': 1, ... ,'charN': N}, sorted by frequency of character tok2id = build_dict((normalize(word) if config.is_normalize else word for sentence, _ in data for word in sentence), offset=1) tok2id[config.UNK] = len(tok2id) + 1 # print(sorted(tok2id.values())) # print(tok2id[config.UNK]) # tok2id index from 1 assert sorted(tok2id.items(), key=lambda t: t[1])[0][1] == 1 logger.info("Built dictionary for %d features.", len(tok2id)) max_length = max(len(sentence) for sentence, _ in data) # for i,d in enumerate(data): # print('{} {}'.format(i, len(d[0]))) return cls(tok2id, max_length) # return a class instance
def mirror(Pw, N=(0, 1, 0), Q=(0, 0, 0)): ''' Mirror Pw (IN-PLACE). N = a unit vector perpendicular to S (default: (0,1,0)) Q = a point on S (default: (0,0,0)) Source: Goldman, Matrices and transformations, Graphics Gems I, 1990. ''' m = Pw.reshape((-1, 4)).T N = util.normalize(N) M = np.identity(4) M[:3, :3] -= 2 * np.outer(N, N) M[:3, 3] = 2 * np.dot(Q, N) * N m = np.dot(M, m).T m.shape = Pw.shape Pw[:] = m
def compute_llh(pfall_df, fall_df): """Computes the log likelihood that the tower fell (or not), given the probability of falling. """ # number of stims x 1 x number of hypotheses p = np.asarray(pfall_df)[:, None] # number of stims x number of feedback conditions x 1 F = np.asarray(fall_df)[:, :, None] # compute the log likelihood llh = np.log((p * F) + ((1 - p) * (1 - F))).reshape((-1, 2)) # normalize llh_norm = util.normalize(llh, axis=1)[1] # put it back in a dataframe llh_df = pd.DataFrame( llh_norm, index=fall_df.stack().index, columns=pfall_df.columns) llh_df.columns.name = 'hypothesis' llh_df.index.names = ['sample', 'stimulus', 'kappa0'] return llh_df.stack().to_frame('llh')
def drawDecansLines(self): #Not used (cx, cy) = self.center.Get() asclon = self.chart.houses.ascmc[houses.Houses.ASC] if self.options.ayanamsha != 0: asclon -= self.chartRadix.ayanamsha asclon = util.normalize(asclon) shift = asclon deg = GraphChartPDs.DEG10 i = math.pi + math.radians(shift) while i > -math.pi + math.radians(shift): x1 = cx + math.cos(i) * self.rInner y1 = cy + math.sin(i) * self.rInner x2 = cx + math.cos(i) * self.rDecans y2 = cy + math.sin(i) * self.rDecans self.bdc.DrawLine(x1, y1, x2, y2) i -= deg
def getNames(species): lista_nomes = [] for specie in species: if config and config.l_plant: config.l_plant["text"] = specie nomes = {} specie_name = specie.split(" ") name = specie_name[0] + " " + specie_name[1] r = requests.get( 'http://servicos.jbrj.gov.br/flora/taxon/' + util.normalize(name)) dado = json.loads(r.content) if(r.status_code == 200): if(dado.get('result') == None): # checa se há resultado para essa espécie no Flora Brasil nomes['status_florabrasil'] = 'nao_encontrado' nomes['nome'] = specie else: nomes['status_florabrasil'] = "" nomes['nome'] = specie # checa se o nome buscado é o aceito if(dado.get('result')[0]['taxonomicstatus'] == 'NOME_ACEITO'): nomes['status_florabrasil'] = '' nomes['florabrasil'] = dado.get( 'result')[0]['scientificname'] else: nomes['status_florabrasil'] = 'sinonimo' accepted_name = dado.get( 'result')[0]['acceptednameusage'] if (accepted_name): nomes['florabrasil'] = accepted_name else: nomes['status_florabrasil'] = 'nao_encontrato' nomes['nome'] = specie else: nomes['status'] = 'nao_encontrado' nomes['nome'] = specie lista_nomes.append(nomes) # print(lista_nomes) return lista_nomes
def get_data(species): data = [] for specie in species: if (config and config.l_plant): config.l_plant["text"] = specie name = util.remove_author(specie) r = requests.get(FLORA_OFICIAL_API + util.normalize(name)) dado = json.loads(r.content) if (r.status_code == 200): if (dado.get('result')): plant_id = dado.get('result')[0]['taxonid'] specie_data = get_specie_data(plant_id) specie_data["family"] = dado.get('result')[0]['family'] specie_data["hierarchy"] = dado.get( 'result')[0]['higherclassification'].split(";")[1] specie_data["name"] = specie data.append(specie_data) return data
def detect_beats(data, frame_rate, block_size=1024, buffer_size_seconds=1, scale=True) -> list: if scale: data = normalize(data) # init ring buffer frames = frame_rate * buffer_size_seconds buffer_size = frames // block_size index = 0 ring_buffer = RingBuffer.RingBuffer(buffer_size) peaks = [] while index < len(data) - block_size: # calculate current energy block = data[index:index + block_size] energy = calculate_energy(block) ring_buffer.put(energy) values = ring_buffer.values()[:-1] # calculate average energy in buffer # todo: save sum in buffer and change if new value is inserted avg = sum(values) / len(values) # calculate variance variance = sum([np.square(e - avg) for e in values]) / buffer_size c = (-0.0000015 * variance) + 1.5142857 # if current energy over threshold -> add to peaks index_last_peak = peaks[-1] if len(peaks) != 0 else 0 delta = index - index_last_peak min_delta = frame_rate * 0.2 if ring_buffer.is_ready() and delta >= min_delta and energy > avg * c: peaks.append(index) index += block_size return peaks
def make_hyperbola(O, X, Y, a, b, u0, u2): ''' Construct the right branch of a quadratic hyperbola in three-dimensional space of arbitrary sweep u (-infty < u < infty). In particular, the hyperbola is represented by C(u) = O - a * cosh(u) * X - b * sinh(u) * Y u0 <= u <= u2 and is oriented according to the local coordinate system defined by O, X and Y. Parameters ---------- O = the center of the hyperbola X, Y = the transverse and imaginary axes a, b = the major and minor radii u0, u2 = the parameter values of the end points Returns ------- Curve = the hyperbola Examples -------- >>> O, X, Y = ([0, 1, 0], [-1, 0, 0], [0, 0, 1]) >>> arc = nurbs.tb.make_hyperbola(O, X, Y, 1, 2, -2, 2) ''' def point(u): return O + a * np.cosh(u) * X + b * np.sinh(u) * Y def tangent(u): return a * np.sinh(u) * X + b * np.cosh(u) * Y X, Y = [util.normalize(V) for V in (X, Y)] if not np.allclose(np.dot(X, Y), 0.0): raise NonOrthogonalAxes(X, Y) if not u0 < u2: raise ImproperInput(u0, u2) if not a > 0 or not b > 0: raise ImproperInput(a, b) P0, T0, P2, T2, P = calc_input_conic(u0, u2, point, tangent) return make_open_conic(P0, T0, P2, T2, P)
def make_parabola(O, X, Y, a, u0, u2): ''' Construct a quadratic parabola in three-dimensional space of arbitrary sweep u (-infty < u < infty). In particular, the parabola is represented by C(u) = O + a * u^2 * X + 2 * a * u * Y u0 <= u <= u2 and is oriented according to the local coordinate system defined by O, X and Y. Parameters ---------- O = the vertex of the parabola X, Y = the parabola's axis and its tangent direction at O a = the focal distance u0, u2 = the parameter values of the end points Returns ------- Curve = the parabola Examples -------- >>> O, X, Y = ([0, 1, 0], [-1, 0, 0], [0, 0, 1]) >>> arc = nurbs.tb.make_parabola(O, X, Y, 2, -2, 2) ''' def point(u): return O + a * u**2 * X + 2 * a * u * Y def tangent(u): return 2 * a * u * X + 2 * a * Y X, Y = [util.normalize(V) for V in (X, Y)] if not np.allclose(np.dot(X, Y), 0.0): raise NonOrthogonalAxes(X, Y) if not u0 < u2: raise ImproperInput(u0, u2) if not a > 0: raise ImproperInput(a) P0, T0, P2, T2, P = calc_input_conic(u0, u2, point, tangent) return make_open_conic(P0, T0, P2, T2, P)
def __init__(self, chrt): self.inhouses = [0, 0, 0, 0, 0, 0, 0] #the seven planets self.dayruler = [0, 0, 0, 0, 0, 0, 0] #the seven planets self.hourruler = [0, 0, 0, 0, 0, 0, 0] #the seven planets self.inphases = [0, 0, 0] #mars, jupiter and saturn only self.scores = [0, 0, 0, 0, 0, 0, 0] #the seven planets for i in range(astrology.SE_SATURN+1): pllon = chrt.planets.planets[i].data[planets.Planet.LONG] if chrt.options.ayanamsha != 0: pllon = util.normalize(pllon-chrt.ayanamsha) housenum = chrt.houses.getHousePos(pllon, chrt.options, True) self.inhouses[i] += chrt.options.housescores[housenum] orbs = (18.0, 30.0, 40.0, 80.0, 100.0, 120.0) num = len(orbs)-1 sunlon = chrt.planets.planets[astrology.SE_SUN].data[planets.Planet.LONG] for i in range(astrology.SE_MARS, astrology.SE_SATURN+1): pllon = chrt.planets.planets[i].data[planets.Planet.LONG] for j in range(num): orb = (orbs[j+1]-orbs[j])/2 asp = orbs[j]+orb lon1 = sunlon+orb lon2 = sunlon-orb if self.inorbsinister(lon1, lon2, pllon, asp): if j == 0 or j == num-1: self.inphases[i-astrology.SE_MARS] += chrt.options.sunphases[2] elif j == 1 or j == num-2: self.inphases[i-astrology.SE_MARS] += chrt.options.sunphases[1] elif j == 2: self.inphases[i-astrology.SE_MARS] += chrt.options.sunphases[0] ar = (1, 4, 2, 5, 3, 6, 0) self.dayruler[ar[chrt.time.ph.weekday]] += chrt.options.dayhourscores[0] self.hourruler[chrt.time.ph.planetaryhour] += chrt.options.dayhourscores[1] for i in range(astrology.SE_SATURN+1): self.scores[i] += self.inhouses[i] self.scores[i] += self.dayruler[i] self.scores[i] += self.hourruler[i] for i in range(astrology.SE_MARS, astrology.SE_SATURN+1): self.scores[i] += self.inphases[i-astrology.SE_MARS]
def test_to_schema(): class User(Document): class Options(object): additional_properties = True title = 'User' id = IntField(required=True) class Resource(Document): task_id = IntField(required=True) user = DocumentField(User, required=True) class Task(Document): class Options(object): title = 'Task' description = 'A task.' definition_id = 'task' name = StringField(required=True, min_length=5) type = StringField(required=True, enum=['TYPE_1', 'TYPE_2']) resources = ArrayField(DocumentField(Resource)) created_at = DateTimeField(name='created-at', required=True) author = DocumentField(User) assert Resource.get_definition_id() == 'test_document.Resource' assert Task.get_definition_id() == 'task' expected_task_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'type': 'object', 'title': 'Task', 'description': 'A task.', 'additionalProperties': False, 'required': ['created-at', 'name', 'type'], 'properties': { 'created-at': Task.created_at.get_schema(), 'type': Task.type.get_schema(), 'name': Task.name.get_schema(), 'resources': Task.resources.get_schema(), 'author': Task.author.get_schema(), } } assert normalize(Task.get_schema()) == expected_task_schema
def cfr(h, player_to_update, pi_1, pi_2, cards, strategy, strategy_sum, regret_sum): # all this ugly modulus math calculates the player and opponent from the history length player = ((len(h)) % 2) + 1 opponent = ((len(h) + 1) % 2) + 1 if is_terminal(h): u = utility(h, cards)[player - 1] return u # info set is simply the history concatenated with the card that the current player sees info_set = str(cards[player - 1]) + h # in the main part of the CFR algorithm, we recursivly call CFR, passing in the probabilities of our current strategy # we also calculate the expected value of the info state given this strategy action_value = np.zeros(2) value = 0 for action in [PASS, BET]: if player == 1: action_value[action] = -1 * cfr( h + action_to_string[action], player_to_update, pi_1 * strategy[info_set][action], pi_2, cards, strategy, strategy_sum, regret_sum) elif player == 2: action_value[action] = -1 * cfr( h + action_to_string[action], player_to_update, pi_1, pi_2 * strategy[info_set][action], cards, strategy, strategy_sum, regret_sum) value += action_value[action] * strategy[info_set][action] # in this implementation, we only update one player at a time if player == player_to_update: # calculate the regrets and update the strategy and regret sums probs = np.array([pi_1, pi_2]) for action in [PASS, BET]: regret_sum[info_set][action] += probs[opponent - 1] * ( action_value[action] - value) strategy_sum[info_set][action] += probs[ player - 1] * strategy[info_set][action] # update the strategy strategy[info_set] = normalize(regret_sum[info_set]) return value
def __init__(self, args): # Initalize Precisions self.precision = 1.75 self.rms_threshold = 75 self.del_threshold = 1.0 self.max_threshold = 0.5 self.var_threshold = 0.0005 # Initialize Arguments self.image_path = util.normalize(args[0]) self.scene_threshold = 30 if args[2] is None else args[2] self.dupli_threshold = 20 if args[3] is None else args[3] self.sharp_threshold = 100 if args[1] is None else args[1] # Adjust Precisions self.max_threshold *= (self.sharp_threshold / 100) self.var_threshold *= (self.sharp_threshold / 100) # Declare Image Data List self.image_list = None # Declare Image Data Lists self.hashes = None self.hash_diffs = None # Declare Image Result Lists self.scenes = None self.blurred = None self.duplicates = None # Collect Directory Contents self.collect_directory() # Calculate Image Data self.process_images() # Analyze Image Data self.detect_scenes() self.detect_duplicates() # Organize Directory Contents self.organize_directory()
def rotation_matrix(angle, direction, point=None): """Return matrix to rotate about axis defined by point and direction. >>> R = rotation_matrix(math.pi/2, [0, 0, 1], [1, 0, 0]) >>> np.allclose(numpy.dot(R, [0, 0, 0, 1]), [1, -1, 0, 1]) True >>> angle = (random.random() - 0.5) * (2*math.pi) >>> direc = np.random.random(3) - 0.5 >>> point = np.random.random(3) - 0.5 >>> R0 = rotation_matrix(angle, direc, point) >>> R1 = rotation_matrix(angle-2*math.pi, direc, point) >>> is_same_transform(R0, R1) True >>> R0 = rotation_matrix(angle, direc, point) >>> R1 = rotation_matrix(-angle, -direc, point) >>> is_same_transform(R0, R1) True >>> I = np.identity(4, numpy.float64) >>> np.allclose(I, rotation_matrix(math.pi*2, direc)) True >>> np.allclose(2, numpy.trace(rotation_matrix(math.pi/2, ... direc, point))) True """ sina = math.sin(angle) cosa = math.cos(angle) direction = util.normalize(direction[:3]) # rotation matrix around unit vector R = np.diag([cosa, cosa, cosa]) R += np.outer(direction, direction) * (1.0 - cosa) direction *= sina R += np.array([[0.0, -direction[2], direction[1]], [direction[2], 0.0, -direction[0]], [-direction[1], direction[0], 0.0]]) M = np.identity(4) M[:3, :3] = R if point is not None: # rotation not around origin point = np.array(point[:3], dtype=numpy.float64, copy=False) M[:3, 3] = point - np.dot(R, point) return M
def _score_captions(self, imgs, caps, caplens, normalize=False, dropout_mask=None): """ Base scoring function w/o syntactic sugar :param caps: tensor (batch_size, max_lang_len) :param caplens: tensor (batch_size, ) :param imgs: tensor (batch_size, c, h, w) :param model: your model :return: (batch_size, ) scores """ self.eval() with torch.no_grad(): scores, targets, scores_raw, decode_lengths, alphas, sort_ind = self( imgs, caps, caplens, dropout_mask=dropout_mask) caps_sorted = caps[sort_ind] caps_predict = caps_sorted[:, 1:].contiguous() scores_raw = F.log_softmax(scores_raw, dim=2) scores_selected = torch.gather(scores_raw, 2, caps_predict.unsqueeze(2)).squeeze(2) # Mask out pad tokens caps_mask = (caps_predict != self.vocab["pad_idx"]).float() scores_masked = scores_selected * caps_mask # Sum up total loss scores_total = scores_masked.sum(1) # Calculate actual probabilities if normalize: # exp -> real probabilities scores_total = util.normalize(scores_total.exp()) # Reverse _, rev_ind = torch.sort(sort_ind) return scores_total[rev_ind]
def update(self, reward, gameState): if self.preState == None: return if not gameState.getAgentState( self.index ).isPacman: #don't update unless it is in offense position return correction = (reward + self.discount * self.getValue(gameState) ) - self.getQValue(self.preState, self.preAction) #print correction features = self.getFeatures(self.preState, self.preAction) for feature in features: #print feature #print self.weights[feature] self.weights[feature] = self.weights[ feature] + self.alpha * correction * features[feature] self.weights = util.normalize(self.weights) print self.weights
def iter_items(): classes = defaultdict(int) with open(data_path, 'r') as fh: for row in csv.reader(fh): category, _, name = row total = max(1, sum(classes.values())) # print(category, (classes[category] / total), total) if (classes[category] / total) > 0.51: # print(category, 'skip') continue name = normalize(name) if name is not None and len(name) > 1: # print(name, category) yield name, category # if category == 'Company': # removed = fingerprints.remove_types(name, normalize) # if removed is not None and removed != name: # # print(name, removed, category) # yield removed, category classes[category] += 1
def add_block(self, position, texture, immediate=True): """ Add a block with the given `texture` and `position` to the world. Parameters ---------- position : tuple of len 3 The (x, y, z) position of the block to add. texture : list of len 3 The coordinates of the texture squares. Use `tex_coords()` to generate. immediate : bool Whether or not to draw the block immediately. """ position = normalize(position) if self[position] != 0: self.remove_block(position, immediate) self[position] = texture self.update_block(position) self.model.check_neighbors(position)
def format_img(img, input_slice_number, normalize_images=False): num_slices = img.shape[0] num_windows = num_slices - input_slice_number + 1 # crop row = (img.shape[-2] - 192) // 2 col = (img.shape[-1] - 192) // 2 img = img[:, row : row + 192, col : col + 192] img = img.astype(np.float32) # noramlize Hounsfield Units if normalize_images: img = util.normalize(img) # expand dimention for tensor img_split = np.array([img[i : i + input_slice_number] for i in range(num_windows)]) img_expand = [ np.expand_dims(np.expand_dims(split, axis=0), axis=0) for split in img_split ] return img_expand
def main(): parser = argparse.ArgumentParser( description='Extracts vectors from a TSV file of many vectors.') parser.add_argument("--input", "-i", action="append", type=openfile, metavar="FILE", help='The input vector space.') parser.add_argument('--whitelist', '-w', metavar='FILE', type=openfile, help='The list of target vectors to search for.') parser.add_argument('word', nargs='*', metavar='WORD', help='Command line specified additional words.') parser.add_argument( '--sorted', '-s', action='store_true', help= 'Indicates the incoming vector space is sorted, allowing for optimization.' ) parser.add_argument( '--pos', '-p', action='store_true', help='Marks that the whitelist has POS tags specified.') args = parser.parse_args() whitewords = set() if args.whitelist: whitewords.update(read_whitelist(args.whitelist)) whitewords.update([normalize(w) for w in args.word]) for input in args.input: for line in filter_vecspace(input, whitewords, args.pos, args.sorted): print line
def run(dest, results_path): data = pd.read_csv( os.path.join(results_path, 'model_likelihood_by_trial.csv')) # compute the belief cols = ['likelihood', 'counterfactual', 'version', 'pid'] llh = data\ .set_index(cols + ['hypothesis', 'trial'])['llh']\ .unstack('hypothesis')\ .sortlevel() # compute the belief for each model models = { 'static': llh.copy(), 'learning': llh.groupby(level=cols).apply(np.cumsum), } results = pd.DataFrame([]) for model_name, model in models.items(): # normalize the probabilities so they sum to one model[:] = util.normalize(np.asarray(model), axis=1)[1] # convert to long form model = pd.melt(model.reset_index(), id_vars=cols + ['trial'], var_name='hypothesis', value_name='logp') # merge with the existing data model = pd.merge(data, model).drop('llh', axis=1) model['model'] = model_name results = results.append(model) results = results\ .set_index(cols + ['model', 'trial', 'hypothesis'])\ .sortlevel() assert not np.isnan(results['logp']).any() assert not np.isinf(results['logp']).any() results.to_csv(dest)
def calcMundaneWithoutSM(self, da, obl, placelat, ascmc2, raequasc): ra = self.dataEqu[Planet.RAEQU] decl = self.dataEqu[Planet.DECLEQU] da *= -1 ra += da ra = util.normalize(ra) lon, lat, dist = astrology.swe_cotrans(ra, decl, 1.0, obl) self.data = (lon, lat, self.data[Planet.DIST], self.data[Planet.SPLON], self.data[Planet.SPLAT], self.data[Planet.SPDIST]) self.dataEqu = (ra, decl, self.dataEqu[Planet.DISTEQU], self.dataEqu[Planet.SPRAEQU], self.dataEqu[Planet.SPDECLEQU], self.dataEqu[Planet.SPDISTEQU]) self.speculums = [] self.computePlacidianSpeculum(placelat, ascmc2) self.computeRegiomontanSpeculum(placelat, ascmc2, raequasc)
def main(): opts = util.parse_args() X, y = util.data_load(opts.dataset) skf = StratifiedKFold(n_splits=3, shuffle=True, random_state=42) for train_index, test_index in skf.split(X, y): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] X_train, X_test = util.normalize(X_train, X_test) clf = AdaBoostClassifier(n_estimators=100, random_state=0) clf.fit(X_train, y_train) """ conf_mat = np.zeros((2, 2)) for i in range(len(X_test)): pred = clf.predict(X_test[i]) true = y_test[i] conf_mat[true][pred] += 1 print(conf_mat) """ predictions = clf.predict(X_test) conf_mat = confusion_matrix(y_test, predictions) print(conf_mat)
def __init__(self, radix, y, m, d, t, cnt=0): #t is in GMT jdbirth = swisseph.julday(y, m, d, t, astrology.SE_GREG_CAL) jd = jdbirth + cnt * 365.2421904 #Find the difference in Julian days between today and the birth day. Say it Djd diffjd = jd - radix.time.jd #Find how many degrees you must rotate the whole natal chart. rotdeg = diffjd / 12.17474 #Find the Profection cycle. profcyc = rotdeg / 360.0 #Determine the number of integer cycles. intcyc = int(profcyc) #Compute the number of degrees included in the integer cycles. degintcyc = intcyc * 360.0 #Compute the number of degrees (< 360), the true profectional movement self.offs = util.normalize(rotdeg - degintcyc)
def calc(self, opts): ayanamsha = 0.0 if opts.ayanamsha != 0: swisseph.set_sid_mode(opts.ayanamsha - 1, 0, 0) tim = chart.event.DateTime(self.year, 1, 1, 0, 0, 0, False, chart.event.DateTime.GREGORIAN, chart.event.DateTime.GREENWICH, True, 0, 0, False, None, False) ayanamsha = swisseph.get_ayanamsa_ut(tim.jd) plsnum = 7 if opts.transcendental[chart.Chart.TRANSURANUS]: plsnum += 1 if opts.transcendental[chart.Chart.TRANSNEPTUNE]: plsnum += 1 if opts.transcendental[chart.Chart.TRANSPLUTO]: plsnum += 1 #calculating one per day (per hour would be too slow) for i in range(plsnum): if i != 1: #moon excepted y = self.year m = 1 d = 1 ar = [] for num in range(365): time = chart.event.DateTime(y, m, d, 0, 0, 0, False, chart.event.DateTime.GREGORIAN, chart.event.DateTime.GREENWICH, True, 0, 0, False, None, False) pl = planets.Planet(time.jd, i, self.flags) pos = pl.data[planets.Planet.LONG] if opts.ayanamsha != 0: pos = util.normalize(pos - ayanamsha) ar.append(pos) y, m, d = util.incrDay(y, m, d) self.posArr.append(ar)
def calcFullAstronomicalProc(self, fort, da, oblN): #, raN, declN): raN = fort.fortune[Fortune.RA] declN = fort.fortune[Fortune.DECL] ksi = raN + da ksi = util.normalize(ksi) roblN = math.radians(oblN) rksi = math.radians(ksi) rdeclN = math.radians(declN) longSZ = 0.0 if ksi == 90.0: longSZ = 90.0 elif ksi == 270.0: longSZ = 270.0 else: Fd = 0.0 if math.cos(rksi) != 0.0: Fd = math.degrees( math.atan( (math.cos(roblN) * math.sin(rksi) + math.sin(roblN) * math.tan(rdeclN)) / math.cos(rksi))) if ksi >= 0.0 and ksi < 90.0: longSZ = Fd elif ksi > 90.0 and ksi < 270.0: longSZ = Fd + 180.0 elif ksi > 270.0 and ksi < 360.0: longSZ = Fd + 360.0 if longSZ <= 0.0: longSZ = Fd + 360.0 latSZ = math.degrees( math.asin( math.sin(rdeclN) * math.cos(roblN) - math.cos(rdeclN) * math.sin(rksi) * math.sin(roblN))) raSZ, declSZ, distSZ = swisseph.cotrans(longSZ, latSZ, 1.0, -oblN) self.fortune = [longSZ, latSZ, raSZ, declSZ]
def main(): opts = util.parse_args() X, y = util.data_load(opts.dataset) skf = StratifiedKFold(n_splits=3, shuffle=True, random_state=42) model = create_model() for train_index, test_index in skf.split(X, y): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] X_train, X_test = util.normalize(X_train, X_test) train_dset = tf.data.Dataset.from_tensor_slices( (X_train, y_train)).batch(64, drop_remainder=False).shuffle(buffer_size=10000) test_dset = tf.data.Dataset.from_tensor_slices( (X_test, y_test)).batch(64) model.fit(train_dset, epochs=10) conf_mat = np.zeros((2, 2), dtype=int) for d, labels in test_dset: predictions = model(d) for i in range(len(d)): conf_mat[labels[i]][np.argmax(predictions[i])] += 1 print(conf_mat)