def ImSec(imdata, xcntr, ycntr, ex_rad): """Find the image section for asymmetry""" SizeY, SizeX = imdata.shape ExceedSize = 0 #All are floor to make the size even number xmin = np.floor(xcntr - ex_rad) ymin = np.floor(ycntr - ex_rad) xmax = np.floor(xcntr + ex_rad) ymax = np.floor(ycntr + ex_rad) if xmin < 0: xmin = 0 cut_xcntr = xcntr ExceedSize = 1 else: cut_xcntr = ex_rad + np.modf(xcntr)[0] if ymin < 0: cut_ycntr = ycntr ymin = 0 ExceedSize = 1 else: cut_ycntr = ex_rad + np.modf(ycntr)[0] if xmax > SizeX - 1: xmax = SizeX ExceedSize = 1 if ymax > SizeY - 1: ymax = SizeY ExceedSize = 1 CutImDa = imdata[ymin:ymax, xmin:xmax].copy() SizeY, SizeX = CutImDa.shape return CutImDa, cut_xcntr, cut_ycntr, SizeY, SizeX, ymin, \ ymax, xmin, xmax, ExceedSize
def dec2sexstr(deci, sfigs=1, hd='h', lead_psign=False): """ Convert decimal degree to a sexagesimal string of the form 'HH:MM:SS.S' by default. Parameters ---------- sfigs : number Number of significant figures after decimal in seconds hd : string, ('h', 'd') Hour or degree convention lead_sign : Boolean Whether to include the leading sign +/- in string """ lead_psymb = '' dform = {'h': 24 / 360., 'd': 1.} (h_frac, h) = _np.modf(deci * dform[hd]) (m_frac, m) = _np.modf(h_frac * 60) s = m_frac * 60 if (lead_psign is True) & (h >= 0): lead_psymb = '+' elif (lead_psign is True) & (h < 0): lead_psymb = '-' h = abs(h) coord_string = "{0}{1:0>2d}:{2:0>2d}:{3:0>2d}.{4:0>{sfigs}d}".format( lead_psymb, int(h), _np.abs(int(m)), _np.abs(int(s)), _np.abs(int(_np.mod(s,1) * 10**sfigs)), sfigs=sfigs) return coord_string
def decimal_to_sexagesimal(decimal): ''' convert decimal hours or degrees to sexagesimal Parameters ---------- decimal | float: decimal number to be converted to sexagismal Returns ------- tuple: int: hours int: minutes float: seconds OR tuple: int: degrees int: arcminutes float: arcseconds >>> decimal_to_sexagesimal(10.1) (10, 5, 59.999999999998721) ''' fractional, integral = np.modf(decimal) min_fractional, minutes = np.modf(fractional * 60) seconds = min_fractional * 60. return integral.astype(int), minutes.astype(int), seconds
def sexa(value): sign = np.sign(value, subok=False) absolute = np.absolute(value, subok=False) fraction, whole = np.modf(absolute) fraction, minutes = np.modf(fraction * 60.0) seconds = fraction * 60.0 return sign, whole, minutes, seconds
def jd2gcal(JD=2443716): '''From Jean Meeus' Astronomical Algorithms. It starts at 0000. ''' F, Z = np.modf(JD + 0.5) if Z >= 2299161: alpha = (Z - 1867216.25) // 36524.25 A = Z + 1 + alpha - alpha // 4 else: A = Z B = A + 1524 C = (B - 122.1) // 365.25 D = np.modf(365.25 * C)[1] E = (B - D) // 30.6001 D = B - D - np.modf(30.6001 * E)[1] + F fD, D = np.modf(D) if E < 14: M = E - 1 else: M = E - 13 if M > 2: Y = C - 4716 else: Y = C - 4715 return int(Y), int(M), int(D), fD
def degreDeci2DegreMinut(degdeci=0.0): minutes, degres = np.modf(degdeci) minutes = abs(minutes) * 60 secondes, minutes = np.modf(minutes) secondes = secondes * 60 print ("%i° %i' %.2f'' " % (int(degres), int(minutes), secondes)) return degres, minutes, secondes
def test_one_argument_two_output_ufunc_inplace(self, value): v = 100. * value * u.cm / u.m v_copy = v.copy() tmp = v.copy() check = v np.modf(v, tmp, v) # cannot use out1,out2 keywords with numpy 1.7 assert check is v assert check.unit == u.dimensionless_unscaled v2 = v_copy.to(u.dimensionless_unscaled) check2 = v2 np.modf(v2, tmp, v2) assert check2 is v2 assert check2.unit == u.dimensionless_unscaled # can also replace in last position if no scaling is needed v3 = v_copy.to(u.dimensionless_unscaled) check3 = v3 np.modf(v3, v3, tmp) assert check3 is v3 assert check3.unit == u.dimensionless_unscaled # in np<1.13, without __array_ufunc__, one cannot replace input with # first output when scaling v4 = v_copy.copy() if NUMPY_LT_1_13: with pytest.raises(TypeError): np.modf(v4, v4, tmp) else: check4 = v4 np.modf(v4, v4, tmp) assert check4 is v4 assert check4.unit == u.dimensionless_unscaled
def _make_inverse_warp(from_points, to_points, output_region, approximate_grid): x_min, y_min, x_max, y_max = output_region if approximate_grid is None: approximate_grid = 1 x_steps = (x_max - x_min) / approximate_grid y_steps = (y_max - y_min) / approximate_grid x, y = numpy.mgrid[x_min:x_max:x_steps*1j, y_min:y_max:y_steps*1j] # make the reverse transform warping from the to_points to the from_points, because we # do image interpolation in this reverse fashion transform = _make_warp(to_points, from_points, x, y) if approximate_grid != 1: # linearly interpolate the zoomed transform grid new_x, new_y = numpy.mgrid[x_min:x_max+1, y_min:y_max+1] x_fracs, x_indices = numpy.modf((x_steps-1)*(new_x-x_min)/float(x_max-x_min)) y_fracs, y_indices = numpy.modf((y_steps-1)*(new_y-y_min)/float(y_max-y_min)) x_indices = x_indices.astype(int) y_indices = y_indices.astype(int) x1 = 1 - x_fracs y1 = 1 - y_fracs ix1 = (x_indices+1).clip(0, x_steps-1) iy1 = (y_indices+1).clip(0, y_steps-1) t00 = transform[0][(x_indices, y_indices)] t01 = transform[0][(x_indices, iy1)] t10 = transform[0][(ix1, y_indices)] t11 = transform[0][(ix1, iy1)] transform_x = t00*x1*y1 + t01*x1*y_fracs + t10*x_fracs*y1 + t11*x_fracs*y_fracs t00 = transform[1][(x_indices, y_indices)] t01 = transform[1][(x_indices, iy1)] t10 = transform[1][(ix1, y_indices)] t11 = transform[1][(ix1, iy1)] transform_y = t00*x1*y1 + t01*x1*y_fracs + t10*x_fracs*y1 + t11*x_fracs*y_fracs transform = [transform_x, transform_y] return transform
def fy2ymd(fyear): """Decimal year -> year, month.""" fy, y = np.modf(fyear) fmonth, y = fy*12, int(y) fm, m = np.modf(fmonth) d, m = int(np.ceil(fm*30.4375)), int(np.ceil(fm)) return [y, m, d]
def bilinear_interpolate(x, y, bins=None): """Returns interpolated density values on points (x, y). Ref: http://en.wikipedia.org/wiki/Bilinear_interpolation. """ if bins is None: bins = int(numpy.sqrt(len(x))) z, unused_xedge, unused_yedge = numpy.histogram2d(y, x, bins=[bins, bins], range=[(numpy.min(y), numpy.max(y)), (numpy.min(x), numpy.max(x))] ) xfrac, xint = numpy.modf((x - numpy.min(x)) / (numpy.max(x) - numpy.min(x)) * (bins - 1)) yfrac, yint = numpy.modf((y - numpy.min(y)) / (numpy.max(y) - numpy.min(y)) * (bins - 1)) xint = xint.astype('i') yint = yint.astype('i') z1 = numpy.zeros(numpy.array(z.shape) + 1) z1[:-1, :-1] = z # values at corners of square for interpolation q11 = z1[yint, xint] q12 = z1[yint, xint + 1] q21 = z1[yint + 1, xint] q22 = z1[yint + 1, xint + 1] return q11 * (1 - xfrac) * (1 - yfrac) + q21 * (1 - xfrac) * (yfrac) + \ q12 * (xfrac) * (1 - yfrac) + q22 * (xfrac) * (yfrac)
def __new__(cls, arg1, arg2=None): # Assume inputs are numerical, could add an extra # case to parse strings as input. # if it is not a list, convert to a list if not hasattr(arg1, 'unit'): arg1 = arg1 * u.cycle if arg1.shape == (): arg1 = arg1.reshape((1,)) with u.set_enabled_equivalencies(dimensionless_cycles): arg1 = arg1.to(u.Unit("")) # Since modf does not like dimensioned quantity if arg2 is None: ff,ii = numpy.modf(arg1) else: if not hasattr(arg2, 'unit'): arg2 = arg2 * u.cycle if arg2.shape == (): arg2 = arg2.reshape((1,)) arg2 = arg2.to(u.Unit("")) arg1S = numpy.modf(arg1) arg2S = numpy.modf(arg2) ii = arg1S[1]+arg2S[1] ff = arg2S[0] index = numpy.where(ff < -0.5) ff[index] += 1.0 ii[index] -= 1 index = numpy.where(ff > 0.5 ) ff[index] -= 1.0 ii[index] += 1 return super(Phase, cls).__new__(cls, ii.to(u.cycle), ff.to(u.cycle))
def test_modf_array(self): v = np.arange(10.) * u.m / (500. * u.cm) q = np.modf(v) n = np.modf(v.to(1).value) assert q[0].unit == u.dimensionless_unscaled assert q[1].unit == u.dimensionless_unscaled assert all(q[0].value == n[0]) assert all(q[1].value == n[1])
def subpx_hist(positions): """Historgram the decimal parts of x and y. They should be flat. If not, you probably do not have good sub-pixel accuracy.""" x, y = np.array(positions)[:, 0:2].T fracx, fracy = np.modf(x)[0], np.modf(y)[0] plt.hist(fracx, bins=np.arange(0, 1.1, 0.1), color="#667788", label="x mod 1.0") plt.hist(fracy, bins=np.arange(0, 1.1, 0.1), color="#994433", alpha=0.5, label="y mod 1.0") plt.legend() plt.show()
def moment_xyzt(sig_xyzt, *args): # rms=None, dc=None, ac=None): # ; # ; Calculate moments of a 4d signal of (x,y,z,t), i.e, # ; -RMS, i.e., a function of (x,y,t) # ; -DC (average in z), i.e., a function of (x,y,t) # ; -AC (DC subtracted out), i.e., a function of (x,y,z,t) # ;------------------------------------------------------------------- try: # return to caller d = np.shape(sig_xyzt) if np.size(d) != 4: print("Error: Variable must be 4D (x,y,z,t)") return siz = np.shape(sig_xyzt) rms = np.zeros((siz[0], siz[1], siz[2])) dc = np.zeros((siz[0], siz[1], siz[2])) if "AC" in args: ac = np.zeros((siz[0], siz[1], siz[2], siz[3])) data = sig_xyzt if np.modf(np.log2(siz[3]))[0] != 0.0: print("WARNING: Expecting a power of 2 in Z direction") if np.modf(np.log2(siz[3] - 1))[0] and (siz[3] > 1): print(" -> Truncating last point to get power of 2") data = data[:, :, 0 : (siz[3] - 2), :] siz[3] = siz[3] - 1 for ix in range(siz[1]): for iy in range(siz[2]): for it in range(siz[0]): val = RMSvalue(sig_xyzt[it, ix, iy, :]) rms[it, ix, iy] = val.valrms dc[it, ix, iy] = val.valav if "AC" in args: ac[it, ix, iy, :] = [val.acvec, val.acvec[0]] res = Bunch() if "RMS" in args: res.rms = rms if "DC" in args: res.dc = dc if "AC" in args: res.ac = ac if "RMS" not in args and "DC" not in args and "AC" not in args: print("Wrong argument") return res except: print("moment_xyz failed") return
def deg2dms(lat,lon, printIt=0): lat_frac, lat_deg = np.modf(lat) lon_frac, lon_deg = np.modf(lon) lat_min = lat_frac * 60 lon_min = lon_frac * 60 lon_sfrac, lon_min = np.modf(lon_min) lat_sfrac, lat_min = np.modf(lat_min) lat_sec = lat_sfrac * 60 lon_sec = lon_sfrac * 60 if printIt: print("lat: %s %s' %s\", lon: %s %s' %s\"" % (lat_deg, lat_min, lat_sec, lon_min, lon_sec))
def deg2iso(lat, lon, printIt=0): lat_frac, lat_deg = np.modf(lat) lon_frac, lon_deg = np.modf(lon) lat_min = lat_frac * 60. lon_min = lon_frac * 60. iso_lat_str = "%d%06.3f" % (lat_deg, abs(lat_min)) iso_lon_str = "%d%06.3f" % (lon_deg, abs(lon_min)) if printIt: print('lat: %s, lon: %s' % (iso_lat_str, iso_lon_str)) else: return iso_lat_str, iso_lon_str
def decimal_to_sexagesimal(decimal): """Convert decimal hours or degrees to sexagesimal. :param decimal: decimal number to be converted to sexagismal. :return: tuple of either (hours, minutes, seconds) or (degrees, arcminutes, arcseconds). """ fractional, integral = modf(decimal) min_fractional, minutes = modf(fractional * 60) seconds = min_fractional * 60.0 return integral.astype(int), minutes.astype(int), seconds
def degrees_to_dms(d): """ Convert a floating-point degree value into a ``(degree, arcminute, arcsecond)`` tuple. """ sign = np.copysign(1.0, d) (df, d) = np.modf(np.abs(d)) # (degree fraction, degree) (mf, m) = np.modf(df * 60.) # (minute fraction, minute) s = mf * 60. return np.floor(sign * d), sign * np.floor(m), sign * s
def interp( self, nH, T ): nH = np.array( nH ) T = np.array( T ) if nH.size != T.size: raise ValueError(' owls_ion_tables: array size mismatch !!! ') # field discovery will have nH.size == 1 and T.size == 1 # in that case we simply return 1.0 if nH.size == 1 and T.size == 1: ionfrac = 1.0 return ionfrac # find inH and fnH #----------------------------------------------------- x_nH = ( nH - self.nH[0] ) / self.DELTA_nH x_nH_clip = np.clip( x_nH, 0.0, self.nH.size-1.001 ) fnH,inH = np.modf( x_nH_clip ) inH = inH.astype( np.int32 ) # find iT and fT #----------------------------------------------------- x_T = ( T - self.T[0] ) / self.DELTA_T x_T_clip = np.clip( x_T, 0.0, self.T.size-1.001 ) fT,iT = np.modf( x_T_clip ) iT = iT.astype( np.int32 ) # short names for previously calculated iz and fz #----------------------------------------------------- iz = self.iz fz = self.fz # calculate interpolated value # use tri-linear interpolation on the log values #----------------------------------------------------- ionfrac = self.ionbal[inH, iT, iz ] * (1-fnH) * (1-fT) * (1-fz) + \ self.ionbal[inH+1, iT, iz ] * (fnH) * (1-fT) * (1-fz) + \ self.ionbal[inH, iT+1, iz ] * (1-fnH) * (fT) * (1-fz) + \ self.ionbal[inH, iT, iz+1] * (1-fnH) * (1-fT) * (fz) + \ self.ionbal[inH+1, iT, iz+1] * (fnH) * (1-fT) * (fz) + \ self.ionbal[inH, iT+1, iz+1] * (1-fnH) * (fT) * (fz) + \ self.ionbal[inH+1, iT+1, iz] * (fnH) * (fT) * (1-fz) + \ self.ionbal[inH+1, iT+1, iz+1] * (fnH) * (fT) * (fz) return 10**ionfrac
def __call__(self, ycoords, xcoords): if numpy.isscalar(ycoords): ycoords = (ycoords,) if numpy.isscalar(xcoords): xcoords = (xcoords,) yrem, yind = numpy.modf(numexpr.evaluate("(ycoords-yoffset)/yscale", local_dict=dict(ycoords=numpy.require(ycoords), yoffset=self.yoffset, yscale=self.yscale))) xrem, xind = numpy.modf(numexpr.evaluate("(xcoords-xoffset)/xscale", local_dict=dict(xcoords=numpy.require(xcoords), xoffset=self.xoffset, xscale=self.xscale))) yind = yind.astype(numpy.int) xind = xind.astype(numpy.int) res = cy.interpolator2d(yind, xind, yrem, xrem, self.data, self.ylength, self.xlength) return res
def hours_to_hms(h): """ Convert an floating-point hour value into an ``(hour, minute, second)`` tuple. """ sign = np.copysign(1.0, h) (hf, h) = np.modf(np.abs(h)) # (degree fraction, degree) (mf, m) = np.modf(hf * 60.0) # (minute fraction, minute) s = mf * 60.0 return (np.floor(sign * h), sign * np.floor(m), sign * s)
def test_one_argument_two_output_ufunc_inplace(self, value): v = 100. * value * u.cm / u.m v_copy = v.copy() tmp = v.copy() check = v np.modf(v, tmp, v) assert check is v assert check.unit == u.dimensionless_unscaled v2 = v_copy.to(u.dimensionless_unscaled) check2 = v2 np.modf(v2, tmp, v2) assert check2 is v2 assert check2.unit == u.dimensionless_unscaled # can also replace in last position if no scaling is needed v3 = v_copy.to(u.dimensionless_unscaled) check3 = v3 np.modf(v3, v3, tmp) assert check3 is v3 assert check3.unit == u.dimensionless_unscaled # And now, with numpy >= 1.13, one can also replace input with # first output when scaling v4 = v_copy.copy() check4 = v4 np.modf(v4, v4, tmp) assert check4 is v4 assert check4.unit == u.dimensionless_unscaled
def playground9(): """ Universal functions means element-wise operations. :return: """ arr = np.arange(16).reshape(4, 4) print np.sqrt(arr) print np.exp(arr) x = np.random.randn(5) y = np.random.randn(5) print np.maximum(x, y) # element-wise maximum print np.modf(x) # vectorized version of built-in Python 'divmod()'
def trilinear_interpolate(x, y, z, bins=None): """Returns interpolated density values on points (x, y, z). Ref: http://en.wikipedia.org/wiki/Trilinear_interpolation. """ if bins is None: bins = int(len(x) ** (1 / 3.0)) vals = numpy.zeros((len(x), 3), 'd') vals[:, 0] = x vals[:, 1] = y vals[:, 2] = z h, unused_edges = numpy.histogramdd(vals, bins=[bins, bins, bins] ) xfrac, xint = numpy.modf((x - numpy.min(x)) / (numpy.max(x) - numpy.min(x)) * (bins - 1)) yfrac, yint = numpy.modf((y - numpy.min(y)) / (numpy.max(y) - numpy.min(y)) * (bins - 1)) zfrac, zint = numpy.modf((z - numpy.min(z)) / (numpy.max(z) - numpy.min(z)) * (bins - 1)) xint = xint.astype('i') yint = yint.astype('i') zint = zint.astype('i') h1 = numpy.zeros(numpy.array(h.shape) + 1) h1[:-1, :-1, :-1] = h # values at corners of cube for interpolation q111 = h1[xint, yint, zint] q112 = h1[xint + 1, yint, zint] q122 = h1[xint + 1, yint + 1, zint] q121 = h1[xint, yint + 1, zint] q211 = h1[xint, yint, zint + 1] q212 = h1[xint + 1, yint, zint + 1] q222 = h1[xint + 1, yint + 1, zint + 1] q221 = h1[xint, yint + 1, zint + 1] i1 = q111 * (1 - zfrac) + q211 * (zfrac) i2 = q121 * (1 - zfrac) + q221 * (zfrac) j1 = q112 * (1 - zfrac) + q212 * (zfrac) j2 = q122 * (1 - zfrac) + q222 * (zfrac) w1 = i1 * (1 - yfrac) + i2 * (yfrac) w2 = j1 * (1 - yfrac) + j2 * (yfrac) return w1 * (1 - xfrac) + w2 * (xfrac)
def hours_to_hms(h): """ Convert an floating-point hour value into an ``(hour, minute, second)`` tuple. """ sign = np.copysign(1.0, h) (hf, h) = np.modf(np.abs(h)) # (degree fraction, degree) (mf, m) = np.modf(hf * 60.0) # (minute fraction, minute) s = mf * 60.0 check_hms_ranges(h, m, s) # throws exception if out of range return (np.floor(sign * h), np.floor(m), s)
def _sample(pts, shape, norm): (x, y, z), floor = np.modf(pts.T) floor = floor.astype(int) ceil = floor + 1 x[x < 0] = 0 y[y < 0] = 0 z[z < 0] = 0 i000 = np.ravel_multi_index((floor[2], floor[1], floor[0]), shape, mode='clip') i100 = np.ravel_multi_index((floor[2], floor[1], ceil[0]), shape, mode='clip') i010 = np.ravel_multi_index((floor[2], ceil[1], floor[0]), shape, mode='clip') i001 = np.ravel_multi_index(( ceil[2], floor[1], floor[0]), shape, mode='clip') i101 = np.ravel_multi_index(( ceil[2], floor[1], ceil[0]), shape, mode='clip') i011 = np.ravel_multi_index(( ceil[2], ceil[1], floor[0]), shape, mode='clip') i110 = np.ravel_multi_index((floor[2], ceil[1], ceil[0]), shape, mode='clip') i111 = np.ravel_multi_index(( ceil[2], ceil[1], ceil[0]), shape, mode='clip') v000 = (1-x)*(1-y)*(1-z) v100 = x*(1-y)*(1-z) v010 = (1-x)*y*(1-z) v110 = x*y*(1-z) v001 = (1-x)*(1-y)*z v101 = x*(1-y)*z v011 = (1-x)*y*z v111 = x*y*z allj = np.vstack([i000, i100, i010, i001, i101, i011, i110, i111]).T.ravel() data = np.vstack([v000, v100, v010, v001, v101, v011, v110, v111]).T.ravel() uniquej = np.unique(allj) uniquejdata = np.array([data[allj==j].sum() for j in uniquej]) return uniquej, uniquejdata / float(norm)
def jd_to_cal(julian_day, gregorian=True): """Convert a Julian day number to a date in the Julian or Gregorian calendars. Arguments: - `julian_day` : (int) Julian Day Number Keywords: - `gregorian` : (bool, default=True) If True, use Gregorian calendar, else use Julian calendar Return: - (year, month, day) : (tuple) day may be fractional """ julian_day = np.atleast_1d(julian_day) F, Z = np.modf(julian_day + 0.5) if gregorian: alpha = ((Z - 1867216.25) / 36524.25).astype(np.int64) A = Z + 1 + alpha - (alpha / 4).astype(np.int64) else: A = Z B = A + 1524 C = ((B - 122.1) / 365.25).astype(np.int64) D = (365.25 * C).astype(np.int64) E = ((B - D) / 30.6001).astype(np.int64) day = B - D - (30.6001 * E).astype(np.int64) + F mon = E - 13 mon[E < 14] = E[E < 14] - 1 year = C - 4715 year[mon > 2] = C[mon > 2] - 4716 return _scalar_if_one(year), _scalar_if_one(mon), _scalar_if_one(day)
def __init__(self, variableName, codeName, powerString, logFlag): self.power = float(powerString) self.logFlag = logFlag # data flags self.cannotAcceptDataWith_Zero = False self.cannotAcceptDataWith_Negative = False self.cannotAcceptDataWith_Positive = False if self.logFlag: self.cannotAcceptDataWith_Zero = True self.cannotAcceptDataWith_Negative = True if numpy.modf(self.power)[0]: # fractional power self.cannotAcceptDataWith_Negative = True if self.power < 0.0: self.cannotAcceptDataWith_Zero = True self.cannotAcceptDataWith_Negative = True # code if self.logFlag: self.HTML = 'ln(' + variableName + ')<sup>' + powerString + '</sup>' self.JAVA = 'Math.pow(Math.log(' + codeName + '), ' + powerString + ')' self.CPP = 'pow(log(' + codeName + '), ' + powerString + ')' self.CSHARP = 'Math.Pow(Math.Log(' + codeName + '), ' + powerString + ')' self.PYTHON = 'math.pow(math.log(' + codeName + '), ' + powerString + ')' self.SCILAB = '(log(' + codeName + ') ^ ' + powerString + ')' else: self.HTML = variableName + '<sup>' + powerString + '</sup>' self.JAVA = 'Math.pow(' + codeName + ', ' + powerString + ')' self.CPP = 'pow(' + codeName + ', ' + powerString + ')' self.CSHARP = 'Math.Pow(' + codeName + ', ' + powerString + ')' self.PYTHON = 'math.pow(' + codeName + ', ' + powerString + ')' self.PYTHON = 'math.pow(' + codeName + ', ' + powerString + ')' self.SCILAB = '(' + codeName + ' ^ ' + powerString + ')'
def _format_label(x, precision=3): fmt_str = '%%.%dg' % precision if np.isinf(x): return str(x) elif com.is_float(x): frac, whole = np.modf(x) sgn = '-' if x < 0 else '' whole = abs(whole) if frac != 0.0: val = fmt_str % frac # rounded up or down if '.' not in val: if x < 0: return '%d' % (-whole - 1) else: return '%d' % (whole + 1) if 'e' in val: return _trim_zeros(fmt_str % x) else: val = _trim_zeros(val) if '.' in val: return sgn + '.'.join(('%d' % whole, val.split('.')[1])) else: # pragma: no cover return sgn + '.'.join(('%d' % whole, val)) else: return sgn + '%0.f' % whole else: return str(x)
def __connect_shuffle_all2all__(self, groupsrc, groupdst, p=0.25): """ Connects in an all to all fashion by shuffling p*N/N 1's and 1-p*N/N 0's for every source, from the first of the source to the last of the source. Has the advantage that the number of connections is kept fixed, which can reduce inhomogeneity in the outputs. p can be an iterable, in which case the probability of each source to connect to the target i is equal to p[i]. i.e. creates a matrix M such that P(M_{src,tgt}==1) = p[tgt] with src=1, ..., Nsrc and tgt=1, ... ,Ntgt """ if not hasattr(p, '__len__'): p = p * np.ones(len(groupdst)) N = [p[i] * len(groupsrc) for i in range(len(groupdst))] connect_inst = np.zeros([len(groupsrc), len(groupdst)]).astype('bool') # It is important to know that p is intepreted as the probability to # connect to the target, not that the source connects to any target #i.e. M such that P(M_{src,tgt}==1) = p[tgt] #Create N[i] for each source i... for i in range(len(groupdst)): #Separate integer and factional parts frac, integ = np.modf(N[i]) connect_inst[:integ, i] = True # Deal with fractional part by rolling a dice and checking not to # overrun if np.random.rand() < frac and integ + 1 < len(groupsrc): connect_inst[integ + 1, i] = True #... and shuffle them for i in range(connect_inst.shape[1]): np.random.shuffle(connect_inst[:, i]) #TODO: go through binary matrix return self.__connect_by_boolean_matrix__(groupsrc, groupdst, connect_inst)
def rround(X, random_seed=3): X_frac, X_int = np.modf(X) X = X_int + (np.random.random_sample(X.shape) < X_frac) return X
def fill_matrix_speaker_samples_zero_padded(matrix2fill, row_id, data_supplied, indices, speaker_label, len_samps_per_id, label_for_zeropadded_rows, context_window_size): ''' This function fills a matrix full of zeros with the same number of rows dedicated to each speaker. If the speaker has too many samples, not all will be included. If the speaker has too few samples, only the samples that will complete a full window will be included; the rest will be replaced with zeros/zero padded. 1) I need the len of matrix, to be fully divisible by len_samps_per_id 2) len_samps_per_id needs to be divisible by context_window_total (i.e. context_window_size * 2 + 1) 2) label column assumed to be last column of matrix2fill #mini test scenario... need to put this into unittests empty_matrix = np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) #each id has 3 rows data_supplied = np.array([[2,2,3],[2,2,3],[2,2,3],[2,2,3],[2,2,3],[2,2,3],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[4,4,5],[1,1,7],[1,1,7],[1,1,7],[1,1,7],[1,1,7],[1,1,7],[1,1,7],[1,1,7],[1,1,7],[1,1,7]]) indices_too_few = [0,1,2,3,4,5] #too few samples (6/10) (total_window_size = 5) label_too_few = 1 indices_too_many = [6,7,8,9,10,11,12,13,14,15,16,17,18,19] #too many (14/10) (total_window_size = 5) label_too_many = 0 indices_just_right = [20,21,22,23,24,25,26,27,28,29] #10/10 (total_window_size = 5) label_just_right = 1 len_samps_per_id = 10 label_for_zeropadded_rows = 2 empty_matrices should be: row_id = 0 --> row_id = 10 matrix_too_few = np.array([[2,2,3,1],[2,2,3,1],[2,2,3,1],[2,2,3,1],[2,2,3,1],[0,0,0,2],[0,0,0,2],[0,0,0,2],[0,0,0,2],[0,0,0,2],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) row_id = 10 --> row_id = 20 matrix_too_many = np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[4,4,5,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) row_id = 20 --> row_id = 30 matrix_too_many = np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,7,1],[1,1,7,1],[1,1,7,1],[1,1,7,1],[1,1,7,1],[1,1,7,1],[1,1,7,1],[1,1,7,1],[1,1,7,1],[1,1,7,1]]) ''' try: frame_width = context_window_size * 2 + 1 tot_samps_speaker = len(indices) tot_samps_sets = tot_samps_speaker // frame_width tot_samps_possible = tot_samps_sets * frame_width if tot_samps_possible > len_samps_per_id: tot_samps_possible = len_samps_per_id indices = indices[:tot_samps_possible] #keep track of the samples put into the new matrix #don't want samples to exceed amount set by variable 'len_samps_per_id' samp_count = 0 for index in indices: #samples only get added to matrix if fewer than max number if samp_count < len_samps_per_id and row_id < len(matrix2fill): new_row = np.append(data_supplied[index], speaker_label) matrix2fill[row_id] = new_row samp_count += 1 row_id += 1 else: if row_id >= len(matrix2fill): raise TotalSamplesNotAlignedSpeakerSamples( "Row id exceeds length of matrix to fill.") # if all user samples used, but fewer samples put in matrix than max amount, zero pad if samp_count < len_samps_per_id and samp_count == tot_samps_possible: zero_padded = len_samps_per_id - samp_count if np.modf(zero_padded / frame_width)[0] != 0.0: raise TotalSamplesNotAlignedSpeakerSamples( "Zero padded rows don't match window frame size") for row in range(zero_padded): #leave zeros, just change label matrix2fill[row_id][-1] = label_for_zeropadded_rows row_id += 1 samp_count += 1 #once all necessary samples put into matrix, leave loop and continue w next speaker elif samp_count == len_samps_per_id: break #samp_count should not be greater than len_samps_per_id... if it is, something went wrong. elif samp_count > len_samps_per_id: raise TotalSamplesNotAlignedSpeakerSamples( "More samples collected than max amount") except TotalSamplesNotAlignedSpeakerSamples as e: print(e) row_id = False return matrix2fill, row_id
def create_parameter_files(self): # this functions retrieves all possible settable and gettable parameters from the device. # Additionally, iot gets all minimum and maximum values for the # parameters by trial and error s_node_pars = [] d_node_pars = [] # ["quex/iavg", "quex/wint"] patterns = [ "awgs", "sigins", "sigouts", "quex", "dios", "system/extclk" ] # json.dump([, s_file, default=int) # json.dump([, d_file, default=int) for pattern in patterns: print("extracting parameters of type", pattern) all_nodes = set(self.find('*{}*'.format(pattern))) s_nodes = set(self.finds('*{}*'.format(pattern))) d_nodes = all_nodes.difference(s_nodes) print(len(all_nodes)) # extracting info from the setting nodes s_nodes = list(s_nodes) default_values = self.getd(s_nodes) for s_node in s_nodes: self.setd(s_node, 1e12) max_values = self.getd(s_nodes) for s_node in s_nodes: self.setd(s_node, -1e12) min_values = self.getd(s_nodes) float_values = dict.fromkeys(s_nodes) for s_node in s_nodes: if np.pi > max_values[s_node]: float_values[s_node] = max_values[s_node] / np.pi else: float_values[s_node] = np.pi self.setd(s_node, float_values[s_node]) actual_float_values = self.getd(s_nodes) node_types = dict.fromkeys(s_nodes) for s_node in sorted(s_nodes): # self.setd(node,default_values[s_node]) fraction, integer = np.modf(actual_float_values[s_node]) if fraction != 0: node_types[s_node] = 'float' if min_values[s_node] == max_values[s_node]: node_types[s_node] = 'float_small' min_values[s_node] = 0 elif abs(min_values[s_node]) < 0.01: min_values[s_node] = 0 else: node_types[s_node] = 'int' min_values[s_node] = 0 if max_values[s_node] == 3567587328: node_types[s_node] = 'int_64' max_values[s_node] = 4294967295 elif max_values[s_node] == 1: node_types[s_node] = 'bool' elif max_values[s_node] == 0: max_values[s_node] = 255 node_types[s_node] = 'int_8bit' elif max_values[s_node] > 4294967295: node_types[s_node] = 'float' line = [ s_node.replace('/' + self._device + '/', ''), node_types[s_node], min_values[s_node], max_values[s_node] ] print(line) s_node_pars.append(line) #json.dump(line, s_file, indent=2, default=int) # extracting info from the data nodes d_nodes = list(d_nodes) # default_values=self.getd(d_nodes) default_values = np.zeros(len(d_nodes)) node_types = [''] * len(d_nodes) for i, d_node in enumerate(d_nodes): try: answer = self.getv(d_node) if isinstance(answer, dict): value = answer['value'][0] node_types[i] = 'float' elif isinstance(answer, list): try: self.setv(d_node, np.array([0, 0, 0])) node_types[i] = 'vector_gs' except: value = answer[0]['vector'] node_types[i] = 'vector_g' else: print("unknown type") except: node_types[i] = 'vector_s' # , default_values[i]] line = [ d_node.replace('/' + self._device + '/', ''), node_types[i] ] print(line) d_node_pars.append(line) #json.dump(line, d_file, indent=2, default=int) with open(self._s_file_name, 'w') as s_file: json.dump(s_node_pars, s_file, default=int, indent=2) with open(self._d_file_name, 'w') as d_file: json.dump(d_node_pars, d_file, default=int, indent=2)
def fold(self, period, dm, accel=0, nbins=50, nints=32, nbands=32, gulp=10000): """Fold data into discrete phase, subintegration and subband bins. :param period: period in seconds to fold with :type period: float :param dm: dispersion measure to dedisperse to :type dm: float :param accel: acceleration in m/s/s to fold with :type accel: float :param nbins: number of phase bins in output :type nbins: int :param nints: number of subintegrations in output :type nints: int :param nbands: number of subbands in output :type nbands: int :param gulp: number of samples in each read :type gulp: int :return: 3 dimensional data cube :rtype: :class:`~sigpyproc.FoldedData.FoldedData` .. note:: If gulp < maximum dispersion delay, gulp is taken to be twice the maximum dispersion delay. """ if np.modf(period / self.header.tsamp)[0] < 0.001: print( "WARNING: Foldng interval is an integer multiple of the sampling time" ) if nbins > period / self.header.tsamp: print( "WARNING: Number of phase bins is greater than period/sampling time" ) if (self.header.nsamples * self.header.nchans) // (nbands * nints * nbins) < 10: raise ValueError("nbands x nints x nbins is too large.") nbands = min(nbands, self.header.nchans) chan_delays = self.header.getDMdelays(dm) chan_delays_c = as_c(chan_delays) max_delay = int(chan_delays.max()) gulp = max(2 * max_delay, gulp) fold_ar = np.zeros(nbins * nints * nbands, dtype="float32") fold_ar_c = as_c(fold_ar) count_ar = np.zeros(nbins * nints * nbands, dtype="int32") count_ar_c = as_c(count_ar) for nsamps, ii, data in self.readPlan(gulp, skipback=max_delay): self.lib.foldFil(as_c(data), fold_ar_c, count_ar_c, chan_delays_c, C.c_int(max_delay), C.c_double(self.header.tsamp), C.c_double(period), C.c_double(accel), C.c_int(self.header.nsamples), C.c_int(nsamps), C.c_int(self.header.nchans), C.c_int(nbins), C.c_int(nints), C.c_int(nbands), C.c_int(ii * (gulp - max_delay))) fold_ar /= count_ar fold_ar = fold_ar.reshape(nints, nbands, nbins) return FoldedData(fold_ar, self.header.newHeader(), period, dm, accel)
# In[95]: np.subtract(a1, a2) # In[97]: np.fmod(a1, a2) # In[98]: np.mod(a1, a2) # In[99]: np.modf(a1) # In[100]: np.remainder(a1, a2) # In[101]: import numpy as np a = np.arange(10) a # In[102]: a[1]
#parameters in simulation: frequence, timestep length etc. dt = 1.5e-4 omega = 1.3963 pi = 3.141592653589793 T = 2 * pi / omega phase_wanted = int(sys.argv[1]) logfile.write('dt = ' + str(dt)) logfile.write('omega = ' + str(omega)) logfile.write('T = ' + str(dt)) logfile.write('phase: ' + str(phase_wanted) + '/16') #logfile.write('dt = '+ str(dt)) for timestep in range(5020000, 5471000, 500): phy_time = timestep * dt phase = np.modf(phy_time / T)[0] phase_index = int(np.modf(phase * 16.0)[1]) if phase_index == phase_wanted: field_file = folder + 'sol_tec_' + str( timestep) + '_field_structure.dat' filename = folder2 + 'sol_tec_' + str(timestep) + '_structure.dat' #read instantanous data try: with open(filename) as test: print('Reading', filename) a = test.readline() index_equal = a.find('=') a = a[index_equal + 1:] a = a.split() test.close() del index_equal
C = (A + B) * (-A / 2) print(C) #36 # Extract the integer part of a random array using 5 different methods Arr = np.random.uniform(2, 100, (3, 2)) print(Arr) #0. But this rounds up sometimes so it is not exactly what is asked #print(np.rint(Arr)) #equivalent but same pb: np.around(Arr) #1. This is good print(np.floor(Arr)) #2. print(np.trunc(Arr)) #3 Res = np.modf(Arr) print(Res[1]) #4 print(Arr - (Arr % 1)) #5 print(np.fix(Arr)) #37 #Create a 5x5 matrix with row values ranging from 0 to 4 A = np.mgrid[0:5, 0:5] print(A[1]) #Another solution x = np.zeros((5, 5)) print("Original array:") print(x)
print(np.exp(arr)) # result: [1.00000000e+00 2.71828183e+00 7.38905610e+00 2.00855369e+01 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03 2.98095799e+03 8.10308393e+03] x = randn(8) y = randn(8) print(x) # result: [-0.45319514 -0.33634081 -1.5487594 1.05024164 0.03497043 -0.39700642 -0.9009428 0.93696438] print(y) # result: [-0.30578685 -1.41005898 1.55261228 -0.79217705 1.26768881 1.16863963 0.93794544 0.24048242] print(np.maximum(x, y)) # 元素级最大值 # result: [-0.30578685 -0.33634081 1.55261228 1.05024164 1.26768881 1.16863963 0.93794544 0.93696438] arr = randn(7) * 5 print(arr) # result: [ 4.11697208 4.36766248 0.84191189 -5.90854028 4.29542469 -3.91484446 2.38288939] print(np.modf(arr)) # result: (array([ 0.11697208, 0.36766248, 0.84191189, -0.90854028, 0.29542469, -0.91484446, 0.38288939]), array([ 4., 4., 0., -5., 4., -3., 2.])) ## 二、利用数组进行数据处理 points = np.arange(-5, 5, 0.01) # 1000 equally spaced points xs, ys = np.meshgrid(points, points) xs # result: # array([[-5. , -4.99, -4.98, ..., 4.97, 4.98, 4.99], # [-5. , -4.99, -4.98, ..., 4.97, 4.98, 4.99], # [-5. , -4.99, -4.98, ..., 4.97, 4.98, 4.99], # ..., # [-5. , -4.99, -4.98, ..., 4.97, 4.98, 4.99], # [-5. , -4.99, -4.98, ..., 4.97, 4.98, 4.99], # [-5. , -4.99, -4.98, ..., 4.97, 4.98, 4.99]]) ys
"logical_and": lambda c1, c2: c1.cast(BooleanType()) & c2.cast(BooleanType()), "logical_or": lambda c1, c2: c1.cast(BooleanType()) | c2.cast(BooleanType()), "logical_xor": lambda c1, c2: ( # mimics xor by logical operators. (c1.cast(BooleanType()) | c2.cast(BooleanType())) & (~(c1.cast(BooleanType())) | ~(c2.cast(BooleanType())))), "maximum": F.greatest, "minimum": F.least, "modf": pandas_udf( # type: ignore[call-overload] lambda s1, s2: np.modf(s1, s2), DoubleType()), "nextafter": pandas_udf( # type: ignore[call-overload] lambda s1, s2: np.nextafter(s1, s2), DoubleType()), "right_shift": pandas_udf( # type: ignore[call-overload] lambda s1, s2: np.right_shift(s1, s2), LongType()), }) # Copied from pandas. # See also https://docs.scipy.org/doc/numpy/reference/arrays.classes.html#standard-array-subclasses def maybe_dispatch_ufunc_to_dunder_op(ser_or_index: IndexOpsMixin, ufunc: Callable, method: str, *inputs: Any, **kwargs: Any) -> IndexOpsMixin:
except: pass else: # executed if no exception is raised print(('f_low given in both input file and command line.' ' Using command line argument: %r' % opts.flow)) flow = [opts.flow for i in range(N)] # Populate structured array injections['waveform'] = [opts.approx for i in range(N)] injections['taper'] = [opts.taper for i in range(N)] injections['f_lower'] = flow injections['mchirp'] = mc injections['eta'] = eta injections['mass1'] = m1 injections['mass2'] = m2 injections['geocent_end_time'] = np.modf(samples['time'])[1] injections['geocent_end_time_ns'] = np.modf(samples['time'])[0] * 10**9 injections['distance'] = samples['distance'] injections['longitude'] = samples['longitude'] injections['latitude'] = samples['latitude'] injections['inclination'] = inclination injections['coa_phase'] = samples['phi_orb'] injections['polarization'] = samples['polarization'] injections['spin1x'] = s1x injections['spin1y'] = s1y injections['spin1z'] = s1z injections['spin2x'] = s2x injections['spin2y'] = s2y injections['spin2z'] = s2z injections['amp_order'] = [opts.amporder for i in range(N)] injections['numrel_data'] = ["" for _ in range(N)]
def test_half_ufuncs(self): """Test the various ufuncs""" a = np.array([0, 1, 2, 4, 2], dtype=float16) b = np.array([-2, 5, 1, 4, 3], dtype=float16) c = np.array([0, -1, -np.inf, np.nan, 6], dtype=float16) assert_equal(np.add(a, b), [-2, 6, 3, 8, 5]) assert_equal(np.subtract(a, b), [2, -4, 1, 0, -1]) assert_equal(np.multiply(a, b), [0, 5, 2, 16, 6]) assert_equal(np.divide(a, b), [0, 0.199951171875, 2, 1, 0.66650390625]) assert_equal(np.equal(a, b), [False, False, False, True, False]) assert_equal(np.not_equal(a, b), [True, True, True, False, True]) assert_equal(np.less(a, b), [False, True, False, False, True]) assert_equal(np.less_equal(a, b), [False, True, False, True, True]) assert_equal(np.greater(a, b), [True, False, True, False, False]) assert_equal(np.greater_equal(a, b), [True, False, True, True, False]) assert_equal(np.logical_and(a, b), [False, True, True, True, True]) assert_equal(np.logical_or(a, b), [True, True, True, True, True]) assert_equal(np.logical_xor(a, b), [True, False, False, False, False]) assert_equal(np.logical_not(a), [True, False, False, False, False]) assert_equal(np.isnan(c), [False, False, False, True, False]) assert_equal(np.isinf(c), [False, False, True, False, False]) assert_equal(np.isfinite(c), [True, True, False, False, True]) assert_equal(np.signbit(b), [True, False, False, False, False]) assert_equal(np.copysign(b, a), [2, 5, 1, 4, 3]) assert_equal(np.maximum(a, b), [0, 5, 2, 4, 3]) x = np.maximum(b, c) assert_(np.isnan(x[3])) x[3] = 0 assert_equal(x, [0, 5, 1, 0, 6]) assert_equal(np.minimum(a, b), [-2, 1, 1, 4, 2]) x = np.minimum(b, c) assert_(np.isnan(x[3])) x[3] = 0 assert_equal(x, [-2, -1, -np.inf, 0, 3]) assert_equal(np.fmax(a, b), [0, 5, 2, 4, 3]) assert_equal(np.fmax(b, c), [0, 5, 1, 4, 6]) assert_equal(np.fmin(a, b), [-2, 1, 1, 4, 2]) assert_equal(np.fmin(b, c), [-2, -1, -np.inf, 4, 3]) assert_equal(np.floor_divide(a, b), [0, 0, 2, 1, 0]) assert_equal(np.remainder(a, b), [0, 1, 0, 0, 2]) assert_equal(np.divmod(a, b), ([0, 0, 2, 1, 0], [0, 1, 0, 0, 2])) assert_equal(np.square(b), [4, 25, 1, 16, 9]) assert_equal(np.reciprocal(b), [-0.5, 0.199951171875, 1, 0.25, 0.333251953125]) assert_equal(np.ones_like(b), [1, 1, 1, 1, 1]) assert_equal(np.conjugate(b), b) assert_equal(np.absolute(b), [2, 5, 1, 4, 3]) assert_equal(np.negative(b), [2, -5, -1, -4, -3]) assert_equal(np.positive(b), b) assert_equal(np.sign(b), [-1, 1, 1, 1, 1]) assert_equal(np.modf(b), ([0, 0, 0, 0, 0], b)) assert_equal(np.frexp(b), ([-0.5, 0.625, 0.5, 0.5, 0.75], [2, 3, 1, 3, 2])) assert_equal(np.ldexp(b, [0, 1, 2, 4, 2]), [-2, 10, 4, 64, 12])
"logaddexp": pandas_udf( lambda s1, s2: np.logaddexp(s1, s2), DoubleType(), PandasUDFType.SCALAR ), "logaddexp2": pandas_udf( lambda s1, s2: np.logaddexp2(s1, s2), DoubleType(), PandasUDFType.SCALAR ), "logical_and": lambda c1, c2: c1.cast(BooleanType()) & c2.cast(BooleanType()), "logical_or": lambda c1, c2: c1.cast(BooleanType()) | c2.cast(BooleanType()), "logical_xor": lambda c1, c2: ( # mimics xor by logical operators. (c1.cast(BooleanType()) | c2.cast(BooleanType())) & (~(c1.cast(BooleanType())) | ~(c2.cast(BooleanType()))) ), "maximum": F.greatest, "minimum": F.least, "modf": pandas_udf(lambda s1, s2: np.modf(s1, s2), DoubleType(), PandasUDFType.SCALAR), "nextafter": pandas_udf( lambda s1, s2: np.nextafter(s1, s2), DoubleType(), PandasUDFType.SCALAR ), "right_shift": pandas_udf( lambda s1, s2: np.right_shift(s1, s2), LongType(), PandasUDFType.SCALAR ), } ) # Copied from pandas. # See also https://docs.scipy.org/doc/numpy/reference/arrays.classes.html#standard-array-subclasses def maybe_dispatch_ufunc_to_dunder_op( ser_or_index: "IndexOpsMixin", ufunc: Callable, method: str, *inputs: Any, **kwargs: Any ) -> "IndexOpsMixin":
def radiation_parameters(ring, dp=None, params=None, **kwargs): """Compute ring parameters from the radiation integrals. Valid for uncoupled lattices with no RF cavity or radiating element. INPUT ring Lattice object. KEYWORD params=None RingParam object to be updated. dp=0.0 Ignored if radiation is ON. Momentum deviation. dct=None Ignored if radiation is ON. Path lengthening. If specified, dp is ignored and the off-momentum is deduced from the path lengthening. method=linopt6 Method used for the analysis of the transfer matrix. See get_optics. linopt6: default linopt2: faster if no longitudinal motion and no H/V coupling, OUTPUT params RingParam object. The computed attributes are, tunes (3,) fractional (H, V, Long.) tunes fulltunes (3,) full tunes chromaticities (2,) H, V Chromaticities alphac Momentum compaction factor etac Frequency slip factor E0 Energy [eV] U0 nergy loss / turn [eV] i1 Radiation integrals - I1 [m] i2 I2 [m^-1] i3 I3 [m^-2] i4 I4 [m^-1] i5 I5 [m^-1] emittances (3,) Mode emittances J (3,) Damping partition numbers Tau (3,) Damping times [s] sigma_e Energy spread sigma_l Bunch length [m] voltage Total accelerating voltage [V] phi_s Synchrotron phase [rad] f_s Synchrotron frequency [Hz] """ rp = RingParameters() if params is None else params _, ringdata, twiss = ring.get_optics(refpts=range(len(ring) + 1), dp=dp, get_chrom=True, **kwargs) rp.chromaticities = ringdata.chromaticity * ring.periodicity integs = ring.get_radiation_integrals(dp, twiss=twiss) rp.i1, rp.i2, rp.i3, rp.i4, rp.i5 = numpy.array(integs) * ring.periodicity circumference = ring.circumference voltage = ring.rf_voltage E0 = ring.energy gamma = ring.gamma gamma2 = gamma * gamma beta = sqrt(1.0 - 1.0 / gamma2) U0 = Cgamma / 2.0 / pi * E0**4 * rp.i2 Jx = 1.0 - rp.i4 / rp.i2 Jz = 1.0 Je = 2.0 + rp.i4 / rp.i2 damping_partition_numbers = numpy.array([Jx, Jz, Je]) revolution_period = circumference / clight / beta ct = 2.0 * E0 / U0 * revolution_period rp.E0 = E0 rp.U0 = U0 emitx = Cq * gamma2 * rp.i5 / Jx / rp.i2 rp.emittances = numpy.array([emitx, nan, nan]) alphac = rp.i1 / circumference etac = 1.0 / gamma2 - alphac rp.phi_s = (pi - asin(U0 / voltage)) if U0 <= voltage else nan nus = sqrt( abs(etac * ring.harmonic_number * voltage * cos(rp.phi_s) / E0 / 2.0 / pi)) / beta rp.voltage = voltage rp.f_s = nus / revolution_period rp.Tau = ct / damping_partition_numbers rp.J = damping_partition_numbers rp.sigma_e = sqrt(Cq * gamma2 * rp.i3 / Je / rp.i2) rp.sigma_l = beta * abs(etac) * circumference / 2.0 / pi / nus * rp.sigma_e ringtunes, _ = numpy.modf(ring.periodicity * ringdata.tune) if len(ringtunes) < 3: rp.tunes = numpy.concatenate((ringtunes, (nus, ))) else: rp.tunes6 = ringtunes rp.fulltunes = ring.periodicity * twiss[-1].mu / 2.0 / pi rp.alphac = alphac rp.etac = etac return rp
-------------------------------Universal function arr = np.arange(10) arr np.sqrt(arr) np.exp(arr) x = np.random.rand(8) y = np.random.rand(8) x y np.maximum(x, y) #find max value and compose to a new matrix arr = np.random.randn(7) * 5 remainder, whole_part = np.modf(arr) remainder #digital part whole_part #the integer part #Ufunction accept out argument that allows them to operate in-place on arrays: arr np.sqrt(arr) np.sqrt(arr, arr) points = np.arange(-5, 5, 0.01) #1000 equally spaced points xs, ys = np.meshgrid(points, points) ys #using natplotlib to create visualizations of this two-dimentional array import matplotlib.pyplot as plt plt.imshow(z, cmap=plt.cm.gray); plt.colorbar()
print(a1.sum()) #หาผลรวม print(a1.prod()) #หาผลคูณ a2 = np.array([[0, -2], [3, -1], [3, -5]], float) print(a2.sum()) #modf Function arr17 = np.random.randn(7) * 5 print(arr17) print("--------------------------------------------------------------") remainder = np.modf(arr17) print(remainder) print("--------------------------------------------------------------") xR, yR = np.modf(arr17) print(xR) print(yR) arr18 = np.array([5, 3, 9], float) print(arr18.max()) print(arr18.min()) print(arr18.argmax()) # argmax() ฟังก์ชันสาหรับการหาตาแหน่งของค่าที่มากที่สุด print(arr18.argmin()) # argmin() ฟังก์ชันสาหรับการหาตาแหน่งของค่าที่น้อยที่สุด
def __getitem__(self, mixture_idx): """! Depending on the selected partition it returns accordingly the following objects: depending on the list of return items the caller function will be returned the items in the exact same order @throws If one of the desired objects cannot be loaded from disk then an IOError would be raised """ if self.random_draws is None: the_time = int(np.modf(time())[0] * 100000000) np.random.seed(the_time) sources_wavs_l = [] energies = [] prev_indexes = [] # Select with a prior probability between the list of datasets for source_idx in range(self.n_sources): dataset_idx = self.get_selected_dataset_index( mixture_idx, source_idx) # Avoid getting the same sound class inside the mixture not_equal_to = None if len(prev_indexes) > 0: prev_d_ind, prev_h_ind = prev_indexes[0] if prev_d_ind == dataset_idx: not_equal_to = prev_h_ind hier_folder_idx = self.get_selected_hierarchical_folder_index( mixture_idx, source_idx, dataset_idx, not_equal_to=not_equal_to) wav_idx = self.get_selected_sample_folder_index( mixture_idx, source_idx, dataset_idx, hier_folder_idx) prev_indexes.append([dataset_idx, hier_folder_idx]) item_folder = self.sample_folders[dataset_idx][hier_folder_idx][ wav_idx] source_tensor = self.load_item_file( os.path.join(item_folder, self.return_items[0])) # Random shifting of the source signals samples_delay = self.get_sample_delay(mixture_idx, source_idx, source_tensor.shape[-1]) delayed_source_tensor = source_tensor[:, samples_delay:samples_delay + self.selected_wav_samples] if np.allclose(delayed_source_tensor, 0): delayed_source_tensor = source_tensor[:, :self. selected_wav_samples] # Random SNR mixing energies.append(torch.sqrt(torch.sum(delayed_source_tensor**2))) sources_wavs_l.append(delayed_source_tensor) snr_ratio = self.get_snr_ratio(mixture_idx, 0) new_energy_ratio = np.sqrt(np.power(10., snr_ratio / 10.)) sources_wavs_l[0] = new_energy_ratio * sources_wavs_l[0] / ( energies[0] + 10e-8) sources_wavs_l[1] = sources_wavs_l[1] / (energies[1] + 10e-8) clean_sources_tensor = torch.cat(sources_wavs_l) mixture_tensor = torch.sum(clean_sources_tensor, dim=0, keepdim=True) clean_sources_tensor -= torch.mean(clean_sources_tensor, dim=1, keepdim=True) mixture_tensor -= torch.mean(mixture_tensor, dim=1, keepdim=True) mixture_std = torch.std(mixture_tensor, dim=1) returning_mixture = (mixture_tensor / (mixture_std + 10e-8)).squeeze() returning_sources = clean_sources_tensor / (mixture_std + 10e-8) return returning_mixture, returning_sources
# In[39]: arr1 # In[40]: arr2 # In[41]: arr1 = np.random.randn(5, 3) arr1 # In[42]: np.modf(arr1) # #### Linear algebra using numpy # In[43]: A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) B = np.array([[9, 8, 7], [6, 5, 4], [1, 2, 3]]) A.dot(B) # In[44]: A = np.arange(15).reshape(3, 5) A.T # In[45]:
# -*- coding:utf8 -*- # @TIME :2019/5/30 14:30 # @Author : 洪松 # @File : 通用函数.py import numpy as np arr = np.arange(10) print('arr: ', arr, sep='\n') '''一元函数''' print('arr_sqrt: ', np.sqrt(arr), sep='\n') print('arr_exp: ', np.exp(arr), sep='\n') '''二元函数''' x = np.random.randn(8) y = np.random.randn(8) print('x: ', x, sep='\n') print('y: ', y, sep='\n') print('np.maximum(x, y): ', np.maximum(x, y), sep='\n') # 元素级最大值 arr_1 = np.random.randn(7) print('np.modf(arr): ', np.modf(arr), sep='\n')
def daypick(frame, tables, day): #0 IS 4PM, 1440 is # print(frame) weib = tables[0] ati = tables[5] ita = tables[6] itl = tables[7] idx = tables[8] #determine school/work fixed params #????????????????? TODO FIX workstart = -1 if (frame.empshift > -1): workstart = pickwork(frame.empshift) schoolstart = -1 if (frame.schoollevel > -1): schoolstart = pickschool(frame.schoollevel) #pick a day type dayindex = 2 if (day in (1, 7)) else 1 cdtable = tables[dayindex][frame.casetype] daytype = np.random.choice(len(cdtable), p=cdtable) avg = tables[3][daytype][0] prior = tables[3][daytype][1] locs = tables[3][daytype][2] eprior = tables[3][daytype][3] #determine dependent status and school needs #TODO: GOING TO SKIP THIS FOR THE MOMENT #select activities and instances for this day, w/expected duration and locations actset = collections.Counter() for ind, i in enumerate(avg): act = ita[ind] prob = norm.rvs(loc=i[0], scale=i[1]) if prob < 0.: prob = 0.0 pf, pi = np.modf(prob) instances = int(pi + (np.random.rand() < pf)) actset[act] = min(instances, 5) df = pd.DataFrame({'act': list(actset.elements())}) df['actind'] = df['act'].apply(lambda x: ati[x]) df = df[(df['act'] // 10000) != 18] if (frame.empshift == -1): df = df[(df['act'] // 10000) != 5] df['locp'] = df['act'].apply( lambda x: itl[np.random.choice(len(locs[ati[x]]), p=locs[ati[x]])]) df['instance'] = df.groupby(['act']).cumcount() #df['assign'] = -1; #order activities by priority for each instance df['pickS'] = norm.cdf(np.random.randn(len(df))) df['startguess'] = df.apply( lambda x: np.argmax(prior[x.actind, x.instance] > x.pickS) * 5, axis=1) df = df.sort_values('startguess') df['prior'] = df.apply(lambda x: prior[x.actind, x.instance, int(np.floor(x.startguess / 5.))], axis=1) df = df.sort_values(['startguess']) #df['tlength'] = 0; #reorder activity sequences w/location preferences # I: we know sleep most likely happens at the start and end of the day # II: we know that work type activities are likely done in a chunk, so group them # III: we know some events interject other events so it's okay if activities are split # t = 0.0; # df['glength'] = np.abs(df['startguess'].diff(-1).fillna(0.)); # df['wlength1'] = np.abs(df['act'].apply(rollweibull,args=(weib,))-df['glength']) # df['wlength2'] = np.abs(df['act'].apply(rollweibull,args=(weib,))-df['glength']) # df['wlength3'] = np.abs(df['act'].apply(rollweibull,args=(weib,))-df['glength']) # df['pickL'] = norm.cdf(np.random.randn(len(df))); # df['plength'] = df.apply(lambda x: np.argmax(eprior[x.actind,x.instance] > x.pickL)*5,axis=1) # df['plength'] = np.abs( df['plength'].apply(lambda x: 0 if x > 600 else x) -df['glength']) # df['avglength'] = np.floor(( df['wlength1']+df['wlength2']+df['wlength3']+df['plength'] ) /4.0) # df['minl'] = df.apply(lambda x: x.avglength if x.avglength < x.glength else x.glength, axis=1) # df['maxl'] = df.apply(lambda x: x.avglength if x.avglength > x.glength else x.glength, axis=1) # df['length'] = df.apply(lambda x: np.random.randint(x.minl,x.maxl + 1), axis=1) df['wlength'] = df['act'].apply(rollweibull, args=(weib, )) df['cumlength'] = df['wlength'].cumsum() df['cumlength'] = np.floor(df['cumlength'] / (df['cumlength'].max() / 1440.0)) df['length'] = np.abs(df['cumlength'].diff(1).fillna(df['cumlength'])) df['prevloc'] = df['locp'].shift(1).fillna(-1) df['locx'], df['locy'] = zip(*df.apply(locApply, args=(frame, ), axis=1)) # indices = df['assign'] == -1 # p = df[indices]['prior'].values # nextact = np.random.choice(df[indices].index.values, p=p/np.sum(p)) # df['assign'].at[nextact] = i; # thresh = np.random.rand(); # e = eprior[df.at[nextact,'actind'],df.at[nextact,'instance']] # pick = np.argmax(e > thresh) # df['end'].at[nextact] = pick*5.0; # t += df['end'].at[nextact] #if(np.random.rand() < 0.05): print(df) # print(df) #finalize activity times #build the actlist and apply locations #actlist += [(t,act,locx,locy)] #actlist = []; #actlist += [(10101, 1440, frame['addrx'],frame['addry'])] actlist = df[['length', 'act', 'locx', 'locy']].values #movelist return actlist
def main(): """Main function.""" args = parse_args() syris.init(loglevel=logging.INFO, device_index=-1) lam = energy_to_wavelength(args.energy * q.keV).simplified w = args.aperture / 2 * q.um # Width of the aperture is 2w, make the aperture half the image size fov = 4 * w ss = args.supersampling d = (w ** 2 / args.fn / lam).simplified ns, ps = compute_propagation_sampling(lam, d, fov, fresnel=True) # Convolution outlier ns *= 2 ps /= ss fmt = 'n sampling: {}, ps: {}, FOV: {}, propagation distance: {}' LOG.info(fmt.format(ns, np.round(ps.rescale(q.nm), 2), fov, np.round(d.rescale(q.cm), 2))) res_a = propagate_analytically(ns * ss, w, ps, d, lam) # Power of two for FFT n = int(2 ** np.ceil(np.log2(ns))) # Supersampling of the pixel size requires supersampling^2 more data points because we enlarge # the FOV by changing the diffraction angle via changing the pixel size and this enlarged FOV is # then sampled by supersampling-smaller pixel size n_proper = n * ss ** 2 numerical_results = {} for divisor in args.numerical_divisors: if np.modf(np.log2(divisor))[0] != 0: raise ValueError('All divisors must be power of two') n_current = n_proper / divisor if n_current < n * ss: raise ValueError('divisor too large, maximum is {}'.format(ss)) numerical_results[n_current] = propagate_numerically(n_current, w, ps, d, lam) x_data = np.linspace(-2 * w.magnitude, 2 * w.magnitude, res_a.shape[0]) aperture = np.zeros(res_a.shape[1]) aperture[res_a.shape[1] / 4:3 * res_a.shape[1] / 4] = res_a[n / 4 / ss].max() if args.txt_output: txt_data = [x_data, aperture, res_a[n / 4 / ss]] txt_header = 'x\taperture\tanalytical' plt.figure() plt.plot(x_data, aperture, label='Aperture') for n_pixels, num_result in numerical_results.items(): fraction = n_pixels / float(n_proper) if args.txt_output: txt_header += '\t{}'.format(fraction) txt_data.append(num_result[n / 4 / ss]) plt.plot(x_data, num_result[n / 4 / ss], '--', label='Numerical {}'.format(fraction)) LOG.info('MSE: {}'.format(np.mean((num_result - res_a) ** 2))) plt.plot(x_data, res_a[n / 4 / ss], '-.', label='Analytical') plt.title('Analytical vs. Numerical Diffraction Pattern') plt.xlabel(r'$\mu m$') plt.ylabel(r'$I$') plt.legend(loc='best') plt.grid() if args.txt_output: txt_data = np.array(txt_data).T np.savetxt(args.txt_output, txt_data, fmt='%g', delimiter='\t', header=txt_header) plt.show()
def test_modf_scalar(self): q = np.modf(9. * u.m / (600. * u.cm)) assert q == (0.5 * u.dimensionless_unscaled, 1. * u.dimensionless_unscaled)
def fit(self, X, X_ids, X_weights, y_, n_epochs=None, show_progress=False): assert (len(X_ids) == len(X_weights)) for m in range(len(X_ids)): assert (X_ids[m].shape[0] == X_weights[m].shape[0] == y_.shape[0] == X.shape[0]) if self.core.n_features is None: self.core.set_num_features(X.shape[1]) assert self.core.n_features == X.shape[ 1], 'Different num of features in initialized graph and input' if self.core.graph is None: self.core.build_graph() self.initialize_session() used_y = self.preprocess_target(y_) if n_epochs is None: n_epochs = self.n_epochs # For reproducible results if self.seed: np.random.seed(self.seed) # Training cycle for epoch in tqdm(range(n_epochs), unit='epoch', disable=(not show_progress)): # generate permutation perm = np.random.permutation(X.shape[0]) epoch_loss = [] # iterate over batches for ret_x, ret_x_ids, ret_x_weights, ret_y in batcher( X, X_ids, X_weights, y_, perm, batch_size=self.batch_size): fd = batch_to_feeddict(ret_x, ret_x_ids, ret_x_weights, ret_y, core=self.core) ops_to_run = [ self.core.trainer, self.core.target, self.core.summary_op ] result = self.session.run(ops_to_run, feed_dict=fd) _, batch_target_value, summary_str = result epoch_loss.append(batch_target_value) # write percentage num_steps = int( (np.modf(X.shape[0] / self.batch_size)[1] + 1) * (epoch + 1)) sys.stdout.write("\r\tstep {} / {}, loss {:g}".format( self.steps, num_steps, batch_target_value)) sys.stdout.flush() '''A,B,C,all_AB = self.session.run([self.core.A,self.core.part_1,self.core.part_2,self.core.all_AB], feed_dict=fd) print(A.shape) print(B.shape) print(C.shape) print(all_AB.shape)''' #print(" ") #print("REGLARIZATION {}".format(regularization)) #print(" ") # write stats if self.need_logs: self.summary_writer.add_summary(summary_str, self.steps) self.summary_writer.flush() self.steps += 1 if (epoch != 0) and ((epoch % self.write_embedding_every) == 0): print('writing embeddings on file') with self.session.as_default(): embedding_path = self.log_dir + "/vectors_e" + str( epoch) + ".txt" embedding_file = open(embedding_path, 'w') vectors = self.core.embeddings_2.eval() i = 0 for vector in vectors: embedding_file.write(str(self.words[i]) + " ") for value in vector: embedding_file.write(str(value) + " ") embedding_file.write('\n') i += 1 embedding_file.close() if self.verbose > 1: print('[epoch {}]: mean target value: {}'.format( epoch, np.mean(epoch_loss))) with self.session.as_default(): # print norms try: meta_vector = self.core.meta.eval() '''vectors_norm = self.core.vectors_norm_sqrt.eval() for i in range(0,len(self.core.valid_examples)): idx = self.core.valid_examples[i] valid_word = self.words[idx] valid_norm = vectors_norm[idx] reweight = reweights[idx] meta = meta_vector[idx] print("{} - {} - {} - {} ".format(valid_word,valid_norm, reweight, meta)) print(" ")''' # print similarity print(self.core.n_features) print(' ') sim = self.core.similarity.eval() for i in range(0, len(self.core.valid_examples)): valid_word = self.words[ self.core.valid_examples[i]] top_k = 10 # number of nearest neighbors upper_bound_top_k = 1000 nearest = ( -sim[i, :]).argsort()[1:upper_bound_top_k + 1] log_str = "Nearest to %s:" % valid_word k_words = 0 k = 0 while (k_words < top_k): if (meta_vector[nearest[k]] == 0 ): # consider only words close_word = self.words[nearest[k]] log_str = "%s %s," % (log_str, close_word) k_words += 1 k += 1 print(log_str) print() except: pass '''tot_sim = 0.0 valid_words = [ self.words[index] for index in self.core.valid_examples ] print(" - \t"+"\t".join(valid_words)) for i in range(0,len(self.core.valid_examples)): word_i = self.words[self.core.valid_examples[i]] log_str = str(word_i)+"\t" for j in range(0,len(self.core.valid_examples)): index_word_j = self.core.valid_examples[j] word_j = self.words[index_word_j] sim_i_j = sim[i, index_word_j] log_str = "%s %g" % (log_str, sim_i_j) tot_sim += sim_i_j print(log_str) print() print("tot_sim: "+str(tot_sim)) print()''' if self.need_logs: with self.session.as_default(): self.core.saver.save(self.session, self.log_dir + "/model.ckpt") #TensorBoard: Embedding Visualization # Use the same LOG_DIR where you stored your checkpoint. #summary_writer = tf.train.SummaryWriter(LOG_DIR) # Format: tensorflow/contrib/tensorboard/plugins/projector/projector_config.proto config = projector.ProjectorConfig() # You can add multiple embeddings. Here we add only one. embedding_2 = config.embeddings.add() embedding_2.tensor_name = self.core.embeddings_2.name embedding_2.metadata_path = "metadata.tsv" # embedding_3 = config.embeddings.add() # embedding_3.tensor_name = self.core.embeddings_3.name # embedding_3.metadata_path = "log/metadata.tsv" # Link this tensor to its metadata file (e.g. labels). # Saves a configuration file that TensorBoard will read during startup. projector.visualize_embeddings(self.summary_writer, config) # write embeddings on file print('writing embeddings on file') with self.session.as_default(): embedding_path = self.log_dir + "/vectors.txt" embedding_file = open(embedding_path, 'w') vectors = self.core.embeddings_2.eval() i = 0 for vector in vectors: embedding_file.write(str(self.words[i]) + " ") for value in vector: embedding_file.write(str(value) + " ") embedding_file.write('\n') i += 1 embedding_file.close()
def rotate(im, ang, cx=-1, cy=-1, zoom=1.0): """Rotates an image of an angle "ang" (in DEGREES). The center of rotation is cx,cy. A zoom factor can be applied. (cx,cy) can be omitted :one will assume one rotates around the center of the image. If zoom is not specified, the default value of 1.0 is taken. :parameters: im: (np.ndarray[ndim=3,dtype=np.float32_t]) : array to rotate ang: (float) : rotation angle (in degrees) cx: (float) : (optional) rotation center on x axis (default: image center) cy: (float) : (optional) rotation center on x axis (default: image center) zoom: (float) : (opional) zoom factor (default =1.0) """ # TODO merge it with rotate3d or see if there is any np.rotate or other... if (zoom == 0): zoom = 1.0 if (zoom == 1.0 and ang == 0.): return im ang *= np.pi / 180 nx = im.shape[1] ny = im.shape[2] if (cx < 0): cx = nx / 2 + 1 if (cy < 0): cy = ny / 2 + 1 x = np.tile(np.arange(nx) - cx + 1, (ny, 1)).T / zoom y = np.tile(np.arange(ny) - cy + 1, (nx, 1)) / zoom ind = np.zeros((nx, ny, ang.size)) imr = np.zeros((im.shape[0], im.shape[1], im.shape[2])).\ astype(np.float32) matrot = np.array([[np.cos(ang), -np.sin(ang)], [np.sin(ang), np.cos(ang)]]) wx = x * matrot[0, 0] + y * matrot[1, 0] + cx wy = x * matrot[0, 1] + y * matrot[1, 1] + cy wx = np.clip(wx, 1., nx - 1) wy = np.clip(wy, 1., ny - 1) wx, rx = np.modf(wx) wy, ry = np.modf(wy) ind = rx + (ry - 1) * nx imr = (im[ind] * (1 - wx) + im[ind + 1] * wx) * (1 - wy) + \ (im[ind + nx] * (1 - wx) + im[ind + nx + 1] * wx) * wy return imr
def onInitializeOptions(self, is_first_run, ask_override): yn_str = {True:'y',False:'n'} default_resolution = 128 default_archi = 'df' default_face_type = 'f' if is_first_run: resolution = io.input_int("Resolution ( 64-256 ?:help skip:128) : ", default_resolution, help_message="More resolution requires more VRAM and time to train. Value will be adjusted to multiple of 16.") resolution = np.clip (resolution, 64, 256) while np.modf(resolution / 16)[0] != 0.0: resolution -= 1 self.options['resolution'] = resolution self.options['face_type'] = io.input_str ("Half or Full face? (h/f, ?:help skip:f) : ", default_face_type, ['h','f'], help_message="Half face has better resolution, but covers less area of cheeks.").lower() self.options['learn_mask'] = io.input_bool ("Learn mask? (y/n, ?:help skip:y) : ", True, help_message="Learning mask can help model to recognize face directions. Learn without mask can reduce model size, in this case converter forced to use 'not predicted mask' that is not smooth as predicted. Model with style values can be learned without mask and produce same quality result.") else: self.options['resolution'] = self.options.get('resolution', default_resolution) self.options['face_type'] = self.options.get('face_type', default_face_type) self.options['learn_mask'] = self.options.get('learn_mask', True) if (is_first_run or ask_override) and 'tensorflow' in self.device_config.backend: def_optimizer_mode = self.options.get('optimizer_mode', 1) self.options['optimizer_mode'] = io.input_int ("Optimizer mode? ( 1,2,3 ?:help skip:%d) : " % (def_optimizer_mode), def_optimizer_mode, help_message="1 - no changes. 2 - allows you to train x2 bigger network consuming RAM. 3 - allows you to train x3 bigger network consuming huge amount of RAM and slower, depends on CPU power.") else: self.options['optimizer_mode'] = self.options.get('optimizer_mode', 1) if is_first_run: self.options['archi'] = io.input_str ("AE architecture (df, liae ?:help skip:%s) : " % (default_archi) , default_archi, ['df','liae'], help_message="'df' keeps faces more natural. 'liae' can fix overly different face shapes.").lower() else: self.options['archi'] = self.options.get('archi', default_archi) default_ae_dims = 256 if self.options['archi'] == 'liae' else 512 default_e_ch_dims = 42 default_d_ch_dims = default_e_ch_dims // 2 if is_first_run: self.options['ae_dims'] = np.clip ( io.input_int("AutoEncoder dims (32-1024 ?:help skip:%d) : " % (default_ae_dims) , default_ae_dims, help_message="All face information will packed to AE dims. If amount of AE dims are not enough, then for example closed eyes will not be recognized. More dims are better, but require more VRAM. You can fine-tune model size to fit your GPU." ), 32, 1024 ) self.options['e_ch_dims'] = np.clip ( io.input_int("Encoder dims per channel (21-85 ?:help skip:%d) : " % (default_e_ch_dims) , default_e_ch_dims, help_message="More encoder dims help to recognize more facial features, but require more VRAM. You can fine-tune model size to fit your GPU." ), 21, 85 ) default_d_ch_dims = self.options['e_ch_dims'] // 2 self.options['d_ch_dims'] = np.clip ( io.input_int("Decoder dims per channel (10-85 ?:help skip:%d) : " % (default_d_ch_dims) , default_d_ch_dims, help_message="More decoder dims help to get better details, but require more VRAM. You can fine-tune model size to fit your GPU." ), 10, 85 ) self.options['d_residual_blocks'] = io.input_bool ("Add residual blocks to decoder? (y/n, ?:help skip:n) : ", False, help_message="These blocks help to get better details, but require more computing time.") self.options['remove_gray_border'] = io.input_bool ("Remove gray border? (y/n, ?:help skip:n) : ", False, help_message="Removes gray border of predicted face, but requires more computing resources.") else: self.options['ae_dims'] = self.options.get('ae_dims', default_ae_dims) self.options['e_ch_dims'] = self.options.get('e_ch_dims', default_e_ch_dims) self.options['d_ch_dims'] = self.options.get('d_ch_dims', default_d_ch_dims) self.options['d_residual_blocks'] = self.options.get('d_residual_blocks', False) self.options['remove_gray_border'] = self.options.get('remove_gray_border', False) if is_first_run: self.options['lighter_encoder'] = io.input_bool ("Use lightweight encoder? (y/n, ?:help skip:n) : ", False, help_message="Lightweight encoder is 35% faster, requires less VRAM, but sacrificing overall quality.") self.options['multiscale_decoder'] = io.input_bool ("Use multiscale decoder? (y/n, ?:help skip:n) : ", False, help_message="Multiscale decoder helps to get better details.") else: self.options['lighter_encoder'] = self.options.get('lighter_encoder', False) self.options['multiscale_decoder'] = self.options.get('multiscale_decoder', False) default_face_style_power = 0.0 default_bg_style_power = 0.0 if is_first_run or ask_override: def_pixel_loss = self.options.get('pixel_loss', False) self.options['pixel_loss'] = io.input_bool ("Use pixel loss? (y/n, ?:help skip: %s ) : " % (yn_str[def_pixel_loss]), def_pixel_loss, help_message="Default DSSIM loss good for initial understanding structure of faces. Use pixel loss after 15-25k iters to enhance fine details and decrease face jitter.") default_face_style_power = default_face_style_power if is_first_run else self.options.get('face_style_power', default_face_style_power) self.options['face_style_power'] = np.clip ( io.input_number("Face style power ( 0.0 .. 100.0 ?:help skip:%.2f) : " % (default_face_style_power), default_face_style_power, help_message="Learn to transfer face style details such as light and color conditions. Warning: Enable it only after 10k iters, when predicted face is clear enough to start learn style. Start from 0.1 value and check history changes."), 0.0, 100.0 ) default_bg_style_power = default_bg_style_power if is_first_run else self.options.get('bg_style_power', default_bg_style_power) self.options['bg_style_power'] = np.clip ( io.input_number("Background style power ( 0.0 .. 100.0 ?:help skip:%.2f) : " % (default_bg_style_power), default_bg_style_power, help_message="Learn to transfer image around face. This can make face more like dst."), 0.0, 100.0 ) else: self.options['pixel_loss'] = self.options.get('pixel_loss', False) self.options['face_style_power'] = self.options.get('face_style_power', default_face_style_power) self.options['bg_style_power'] = self.options.get('bg_style_power', default_bg_style_power)
def test_modf(self): size = ht.communication.MPI_WORLD.size start, end = -5.0, 5.0 step = (end - start) / (2 * size) npArray = np.arange(start, end, step, dtype=np.float32) comparison = np.modf(npArray) # exponential of float32 float32_tensor = ht.array(npArray, dtype=ht.float32) float32_modf = float32_tensor.modf() self.assertIsInstance(float32_modf[0], ht.DNDarray) self.assertIsInstance(float32_modf[1], ht.DNDarray) self.assertEqual(float32_modf[0].dtype, ht.float32) self.assertEqual(float32_modf[1].dtype, ht.float32) self.assert_array_equal(float32_modf[0], comparison[0]) self.assert_array_equal(float32_modf[1], comparison[1]) # exponential of float64 npArray = np.arange(start, end, step, np.float64) comparison = np.modf(npArray) float64_tensor = ht.array(npArray, dtype=ht.float64) float64_modf = float64_tensor.modf() self.assertIsInstance(float64_modf[0], ht.DNDarray) self.assertIsInstance(float64_modf[1], ht.DNDarray) self.assertEqual(float64_modf[0].dtype, ht.float64) self.assertEqual(float64_modf[1].dtype, ht.float64) self.assert_array_equal(float64_modf[0], comparison[0]) self.assert_array_equal(float64_modf[1], comparison[1]) # check exceptions with self.assertRaises(TypeError): ht.modf([0, 1, 2, 3]) with self.assertRaises(TypeError): ht.modf(object()) with self.assertRaises(TypeError): ht.modf(float32_tensor, 1) with self.assertRaises(ValueError): ht.modf(float32_tensor, (float32_tensor, float32_tensor, float64_tensor)) with self.assertRaises(TypeError): ht.modf(float32_tensor, (float32_tensor, 2)) # with split tensors # exponential of float32 npArray = np.arange(start, end, step, dtype=np.float32) comparison = np.modf(npArray) float32_tensor_distrbd = ht.array(npArray, split=0) float32_modf_distrbd = float32_tensor_distrbd.modf() self.assertIsInstance(float32_modf_distrbd[0], ht.DNDarray) self.assertIsInstance(float32_modf_distrbd[1], ht.DNDarray) self.assertEqual(float32_modf_distrbd[0].dtype, ht.float32) self.assertEqual(float32_modf_distrbd[1].dtype, ht.float32) self.assert_array_equal(float32_modf_distrbd[0], comparison[0]) self.assert_array_equal(float32_modf_distrbd[1], comparison[1]) # exponential of float64 npArray = npArray = np.arange(start, end, step, np.float64) comparison = np.modf(npArray) float64_tensor_distrbd = ht.array(npArray, split=0) float64_modf_distrbd = ( ht.zeros_like(float64_tensor_distrbd, dtype=float64_tensor_distrbd.dtype), ht.zeros_like(float64_tensor_distrbd, dtype=float64_tensor_distrbd.dtype), ) # float64_modf_distrbd = float64_tensor_distrbd.modf() float64_tensor_distrbd.modf(out=float64_modf_distrbd) self.assertIsInstance(float64_modf_distrbd[0], ht.DNDarray) self.assertIsInstance(float64_modf_distrbd[1], ht.DNDarray) self.assertEqual(float64_modf_distrbd[0].dtype, ht.float64) self.assertEqual(float64_modf_distrbd[1].dtype, ht.float64) self.assert_array_equal(float64_modf_distrbd[0], comparison[0]) self.assert_array_equal(float64_modf_distrbd[1], comparison[1])
import numpy as np #arr = np.random.randn(6, 3) #arr = np.arange(3,3) arr = np.arange(9).reshape((3, 3)) print(arr) # Matrizenmultiplikation print(np.dot(arr, arr)) #Transpose print(arr.T) # Transpose Multiplication print(np.dot(arr.T, arr)) # Ufunc arr2 = np.random.randn(7) * 5 print(arr2) print(np.modf(arr2))