Exemplo n.º 1
0
 def Header(self):
     if self.hasHeader:
         return self.header
     else:
         throwError(
             "No header was added to class 'GadgetParticles' via the function 'AddHeader'. Hence you cannot use the function 'Header' to access the Gadget header."
         )
Exemplo n.º 2
0
def readTextFile(
    file,
    noDescriptionLines,
    noRows,
    noColumns,
    dataType,
    delimiter=" ",
    VERBOSE=True,
):
    """ 
    Used to read data from a text file.
    noDescriptionLines - number of description lines.
    noRows - number of rows to read.
    noColumns - number of columns on each row (must give all the columns in the file).
    It returns [description,data], whit data in a numpy matrix of noRows x
    noColumns dimension.
    """
    if not os.path.isfile(file):
        throwError(
            "Could not find the file '%s' for reading (in function 'readTextFile')."
            % file
        )
    if VERBOSE:
        print("Reading the data from the ASCII file '%s' ... " % file, end=" ")
    f = open(file, "r")
    description = ""
    for i in range(noDescriptionLines):
        description += f.readline()
    dataSize = noRows * noColumns
    data = np.fromfile(f, dataType, dataSize, delimiter)
    data.shape = (noRows, noColumns)
    if VERBOSE:
        print("Done")

    return [description, data]
Exemplo n.º 3
0
def writeTextFile_gnuplot3D(file, description, data, VERBOSE=True):
    """ Used to write a 3D data set to a text file.
        file - the name of the output file
        description - description lines in file
        data - the matrix to be written (a numpy array of 1 or 2 dimensions)
    """
    funcName = "writeTextFile_gnuplot3D"
    if data.ndim is not 3:
        throwError(
            "In function '%s' since the data argument (=%iD array) is not a 3D numpy array."
            % (funcName, data.ndim)
        )

    if VERBOSE:
        print(
            "Writing the data to the ASCII file '%s' using the Gnuplot 3D format ... "
            % file,
            end=" ",
        )

    f = open(file, "w")
    f.write(description)

    for i in range(data.shape[0]):
        for j in range(data.shape[1]):
            data[i, j, :].tofile(f, "  ", "%12.7g")
            f.write("\n")
        f.write("\n")
    f.close()

    if VERBOSE:
        print("Done")
Exemplo n.º 4
0
def LeastSquare_nonlinearFit(X,
                             Y,
                             func,
                             deriv,
                             guess,
                             relMaxError=1.0e-3,
                             noMaxSteps=100):
    """Solves the nonlinear least square problem using the initial guess and the derivatives of the fit function with respect to the fit parameters. See for details: http://mathworld.wolfram.com/NonlinearLeastSquaresFitting.html"""
    funcName = "LeastSquare_nonlinearFit"
    if len(guess) != len(deriv):
        throwError(
            "In function '%s'. Inconsistent number of initial guess for the parameters and partial derivatives of the fit function. There are %i initial guess parameters and %i partial function derivatives."
            % (funcName, len(guess), len(deriv)))
    iteration, relError = 0, 10.0 * relMaxError
    noParam, param = len(guess), guess
    A = np.empty((len(X), len(guess)), np.float64)
    while iteration < noMaxSteps and relError > relMaxError:
        beta = Y - func(X, param)
        for i in range(noParam):
            A[:, i] = deriv[i](X, param)
        AT = A.transpose()
        a, b = np.dot(AT, A), np.dot(AT, beta)
        dParam = np.linalg.solve(a, b)
        relError = np.abs(dParam / param).max()  # maximum relative error
        param += dParam
        iteration += 1
        print(iteration, param, dParam, relError, beta)

    error = ((Y - func(X, param))**2).sum()
    success = True
    if iteration == noMaxSteps:
        success = False
    return param, error, success
Exemplo n.º 5
0
def haloEnvironment(haloPos, envData, box, VERBOSE=True, valueOutsideGrid=-1):
    """Returns an array with the environment tag of each halo. An entry of -1 means that the halo is outside the box with the given coordinates."""
    funcName = "haloEnvironment"
    if len(box) != 6:
        throwError(
            "In function '%s' the argument 'box' must be a 6 elements array. This element gives the coordinates of the environment grid data 'envData'."
            % funcName
        )
    if envData.ndim != 3:
        throwError(
            "In function '%s' the argument 'envData' must be a 3D numpy array."
            % funcName
        )

    if VERBOSE:
        print("Computing the environment of each halo ...")
    dx = (box[1::2] - box[::2]) / envData.shape
    grid = (haloPos[:, :] - box[::2]) / dx[:]
    valid = (
        (grid[:, 0] >= 0)
        * (grid[:, 0] < envData.shape[0])
        * (grid[:, 1] >= 0)
        * (grid[:, 1] < envData.shape[1])
        * (grid[:, 2] >= 0)
        * (grid[:, 2] < envData.shape[2])
    )  # select only halos within the grid
    grid = grid[valid, :]
    grid = grid.astype(np.int32)
    result = np.zeros(haloPos.shape[0], envData.dtype)
    result[~valid] = valueOutsideGrid
    result[valid] = envData[grid[:, 0], grid[:, 1], grid[:, 2]]

    return result
Exemplo n.º 6
0
 def Ids(self):
     if self.hasIds:
         return self.ids
     else:
         throwError(
             "No particle identities array was added to class 'GadgetParticles' via the function 'AddIds'. Hence you cannot use the function 'Ids' to access the position array."
         )
Exemplo n.º 7
0
 def Vel(self):
     if self.hasVel:
         return self.vel
     else:
         throwError(
             "No velocity array was added to class 'GadgetParticles' via the function 'AddVel'. Hence you cannot use the function 'Vel' to access the velocities array."
         )
Exemplo n.º 8
0
 def Pos(self):
     if self.hasPos:
         return self.pos
     else:
         throwError(
             "No position array was added to class 'GadgetParticles' via the function 'AddPos'. Hence you cannot use the function 'Pos' to access the position array."
         )
Exemplo n.º 9
0
def FindContourValue(data, enclosed_fraction, bin_type="linear"):
    """Computes the contour level value for a 2D density map such that 'enclosed_fraction' of the total mass lies within the contour.
            enclosed_fraction should be an iterable object - can contain more than 1 value.
            Use bin_type = ['linear'.'logarithm'] to specify if the density values have a linear or logarithmic distribution - this determines the bin type used to compute the histogram that determines the required contour level."""
    if bin_type not in ["linear", "logarithm"]:
        throwError(
            "Unknown bin type in function 'FindContourValue'. You inserted 'bin_type'='%s', but the function recognizes only the following values '%s'."
            % (bin_type, str(["linear", "logarithm"])))

    # Compute the histogram of the data
    temp = data.reshape(-1)
    noBins = 1000
    histogram, histogramBins = Histogram(temp,
                                         no_bins=noBins,
                                         bin_type=bin_type,
                                         weights=None)
    histogram = histogram * histogramBins
    histogram[0] = 0.0
    # Compute the cumulative sum
    cumSum = CumulativeSum(histogram, order="descending", out_type=np.float64)
    cumSum /= cumSum[0]
    # Find the bin value where the cumulative sum is >= 'enclosed_fraction'
    result = np.zeros((len(enclosed_fraction), 3))
    indices = np.arange(noBins)
    for i in range(len(enclosed_fraction)):
        validIndices = cumSum < enclosed_fraction[i]
        index = indices[validIndices][0] - 1
        result[i] = enclosed_fraction[i], histogramBins[index], cumSum[index]

    return result
Exemplo n.º 10
0
 def Mass(self):
     if self.hasMass:
         return self.mass
     elif self.hasHeader and self.header.mass[1] != 0.0:
         return np.array([self.header.mass[1]], "f4")
     else:
         throwError(
             "No particle mass array was added to class 'GadgetParticles' via the function 'AddMass'. Hence you cannot use the function 'Mass' to access the position array."
         )
Exemplo n.º 11
0
def writeHaloData(fileName, header, dataIntegers, dataFloats, VERBOSE=True):
    """Writes the halo data into a halo binary file."""

    # set some header values before writting the data
    massColumn = header.massColumn
    header.massRange[:] = (
        np.min(dataFloats[:, massColumn]),
        np.max(dataFloats[:, massColumn]),
    )
    if header.noColumnsIntegers * header.noHalos != dataIntegers.size:
        throwError(
            "The integer halo data does not match with the information stored in the halo header. There are %i halo data, but it is expected to be %i data for %i halos with %i integer columns."
            % (
                dataIntegers.size,
                header.noColumnsIntegers * header.noHalos,
                header.noHalos,
                header.noColumnsIntegers,
            )
        )
    if header.noColumnsFloats * header.noHalos != dataFloats.size:
        throwError(
            "The floating point halo data does not match with the information stored in the halo header. There are %i halo data, but it is expected to be %i data for %i halos with %i floating point columns."
            % (
                dataFloats.size,
                header.noColumnsFloats * header.noHalos,
                header.noHalos,
                header.noColumnsFloats,
            )
        )
    header.noColumns = header.noColumnsIntegers + header.noColumnsFloats

    # write the halo header
    if VERBOSE:
        print("Writing the halo data to the binary file '%s' ... " % fileName)
    f = open(fileName, "wb")
    header.tofile(f)
    header.WriteColumnNames(f)  # write the column names

    # write the integer data
    dataIntegers.shape = -1
    noBytes = dataIntegers.size * dataIntegers[0].nbytes
    __buffer = np.array([noBytes], dtype=np.uint64)
    __buffer.tofile(f)
    writeArrayEntries(dataIntegers, f, 0, dataIntegers.size)
    __buffer.tofile(f)

    # write the floating point data
    dataFloats.shape = -1
    noBytes = dataFloats.size * dataFloats[0].nbytes
    __buffer = np.array([noBytes], dtype=np.uint64)
    __buffer.tofile(f)
    writeArrayEntries(dataFloats, f, 0, dataFloats.size)
    __buffer.tofile(f)
    f.close()
Exemplo n.º 12
0
def sortHalos(
    dataIntegers,
    dataFloats,
    sortColumn,
    order="descending",
    relabelHalos=False,
    parentHaloColumn=None,
    VERBOSE=True,
):
    """Sorts the halo according to the values in the sorting column 'sortColumn'.
    NOTE: Choose 'relabelHalos'=True to assign halo IDS using the new order in which the halos were sorted.
    """
    funcName = "sortHalos"
    orderValues = ["ascending", "descending"]
    if order not in orderValues:
        throwError(
            "The argument 'order' of function '%s' can take only the values %s."
            % (funcName, str(orderValues))
        )
    if sortColumn.ndim is not 1:
        throwError(
            "The argument 'sortColumn' of function '%s' muts be a 1D numpy array whose values are used for sorting the halo properties."
            % funcName
        )

    if VERBOSE:
        print("Sorting the halos in %s order ..." % (order,))
    newOrder = None
    if order == "ascending":
        newOrder = sortColumn.argsort()
    elif order == "descending":
        newOrder = (-1 * sortColumn).argsort()
    dataIntegers[:] = dataIntegers[newOrder, :]
    dataFloats[:] = dataFloats[newOrder, :]

    if relabelHalos:
        if VERBOSE:
            print("Relabeling the halos according to their new position ...")
        dataIntegers[:, 0] = np.arange(dataIntegers.shape[0])
        if parentHaloColumn:
            if VERBOSE:
                print(
                    "Found substructure ID in halo data: column number %i."
                    % parentHaloColumn
                )
            temp = np.arange(dataIntegers.shape[0])[newOrder.argsort()]
            temp = np.hstack((temp, np.array([-1], np.int64)))
            tempS = dataIntegers[:, parentHaloColumn].flatten()
            dataIntegers[:, parentHaloColumn] = temp[tempS]
        elif VERBOSE:
            print("No information about substructure ID in halo data.")

    return dataIntegers, dataFloats
Exemplo n.º 13
0
 def tofile(self, f, BUFFER=True, bufferType=np.dtype("i4")):
     self.SetType()
     __A = np.array([self.Tuple()], dtype=self.dtype())
     __buffer = np.array([__A.nbytes], dtype=bufferType)
     if __A.nbytes != GadgetHeader.headerSize:
         throwError(
             "When writing the Gadget snapshot header to file. The header size is %i which is different from the expected size of %i."
             % (__A.nbytes, GadgetHeader.headerSize))
     if BUFFER:
         __buffer.tofile(f)
     __A.tofile(f)
     if BUFFER:
         __buffer.tofile(f)
Exemplo n.º 14
0
 def tofile(self, f, BUFFER=True):
     self.SetType()
     if self.nbytes() != MMFHeader.byteSize:
         throwError(
             "When calling the function 'MMFHeader.tofile()'. The size of the MMF header is %i while the expected size is %i. Please check which variables do not have the correct size."
             % (self.nbytes(), MMFHeader.byteSize))
     __buffer = np.array([self.nbytes()], dtype=np.uint64)
     __A = np.array([self.Tuple()], dtype=self.dtype())
     if BUFFER:
         __buffer.tofile(f)
     __A.tofile(f)
     if BUFFER:
         __buffer.tofile(f)
Exemplo n.º 15
0
def readHaloData(fileName, VERBOSE=True):
    """Reads the data from a halo binary file. NOTE: reads only 1 halo file, not multiple ones."""

    # read the header
    if VERBOSE:
        print("Reading the data in the halo file '%s' ... " % (fileName))
    header = HaloHeader()
    f = open(fileName, "rb")
    header.fromfile(f)
    header.ReadColumnNames(f)  # read the column names

    # read the integer data
    dataSize = np.uint64(header.noHalos * header.noColumnsIntegers)
    dataIntegers = np.empty(dataSize, np.int32)

    __buffer1 = np.fromfile(f, HaloHeader.bufferType, 1)[0]
    dataIntegers = readArrayEntries(dataIntegers, f, 0, dataSize)
    __buffer2 = np.fromfile(f, HaloHeader.bufferType, 1)[0]
    if __buffer1 != __buffer2:
        throwError(
            "While reading the integer data block in halo file '%s'. The buffer preceding and the buffer after the data do not have the same value (buffer1 = %s, buffer2 = %s while the expected value was %s)."
            % (
                fileName,
                __buffer1,
                __buffer2,
                dataSize * dataIntegers[0].nbytes,
            )
        )

    # read the floating point data
    dataSize = np.uint64(header.noHalos * header.noColumnsFloats)
    dataFloats = np.empty(dataSize, np.float32)

    __buffer1 = np.fromfile(f, HaloHeader.bufferType, 1)[0]
    dataFloats = readArrayEntries(dataFloats, f, 0, dataSize)
    __buffer2 = np.fromfile(f, HaloHeader.bufferType, 1)[0]
    if __buffer1 != __buffer2:
        throwError(
            "While reading the floating point data block in halo file '%s'. The buffer preceding and the buffer after the data do not have the same value (buffer1 = %s, buffer2 = %s while the expected value was %s)."
            % (
                fileName,
                __buffer1,
                __buffer2,
                dataSize * dataIntegers[0].nbytes,
            )
        )

    # return the results
    dataIntegers.shape = header.noHalos, header.noColumnsIntegers
    dataFloats.shape = header.noHalos, header.noColumnsFloats
    return header, dataIntegers, dataFloats
Exemplo n.º 16
0
def binValues(dataRange, no_bins, bin_type="linear"):
    """ Returns the bin values for 'no_bins' equally spaced in linear/logarithmic interval in the given range."""
    if bin_type not in ["linear", "logarithm"]:
        throwError(
            "Unknown bin type in function 'binValues'. You inserted 'bin_type'='%s', but the function recognizes only the following values '%s'."
            % (bin_type, str(["linear", "logarithm"])))
    binBoundary = binBoundaries(dataRange, no_bins, bin_type=bin_type)
    output = np.zeros(no_bins, binBoundary.dtype)
    if bin_type is "linear":
        output[:] = (binBoundary[0:no_bins] + binBoundary[1:no_bins + 1]) / 2.0
    if bin_type is "logarithm":
        output[:] = np.sqrt(binBoundary[0:no_bins] *
                            binBoundary[1:no_bins + 1])
    return output
Exemplo n.º 17
0
def Average(data, N=100, minSize=5):
    """Computes the average of the data set and the errors associated to the arithmetic average determination using bootstrap. It generates 'N' different bootsrap realizations."""
    funcName = "Average"
    if data.ndim != 1:
        throwError("The '%s' functions works only for a 1D data set." %
                   funcName)
    mean = np.mean(data)
    if (data.size < minSize
        ):  # the data is not large enough to get an accurate error for it
        return (mean, mean)
    meanSet = np.zeros(N, np.float64)
    for i in range(N):
        meanSet[i] = np.mean(Bootstrap(data))
    meanError = np.std(meanSet)
    return (mean, meanError)
Exemplo n.º 18
0
 def Update(self, selection):
     """Update the particle information using only the particles selected via 'selection'"""
     if selection.size != self.noParticles:
         throwError(
             "In function 'GadgetParticles::Update'. You are trying to update the particle selection using a numpy array that has a different size than the number of particles in class 'GadgetParticles'. Number of particles = %i while the selection array has length = %i."
             % (self.noParticles, selection.size))
     self.noParticles = np.sum(selection)
     if self.hasHeader:
         self.header.npart[1] = self.noParticles
         self.header.npartTotal[1] = self.noParticles
     self.pos = self.pos[selection, :]
     self.vel = self.vel[selection, :]
     self.ids = self.ids[selection]
     if self.VariableMass():
         self.mass = self.mass[selection]
Exemplo n.º 19
0
def Percentile(data, percentile=(25.0, 75.0), N=100):
    """Computes the value of the percentiles of a distribution. It uses bootstrap to compute the error associated to the percentile determination.
    It returns a list of tuples, with each tupple giving the percentile value and the bootstrap error associated in determining it."""
    funcName = "Percentile"
    if data.ndim != 1:
        throwError("The '%s' functions works only for a 1D data set." %
                   funcName)
    result = []
    for p in percentile:
        score = stats.scoreatpercentile(data, p)
        scoreSet = np.zeros(N, np.float64)
        for i in range(N):
            scoreSet[i] = stats.scoreatpercentile(Bootstrap(data), p)
        scoreError = np.std(scoreSet)
        result.append((score, scoreError))
    return result
Exemplo n.º 20
0
 def NewColumnsFloats(self, columnNames):
     """Adds new column names for the data stored in the float columns. The argument "columnNames" needs to be a list of strings of the same size as the number of float columns."""
     __noFloatCols = len(columnNames)
     if __noFloatCols != self.noColumnsFloats:
         throwError(
             "Error in function 'halo.HaloHeader.NewColumnsFloats'. The length of the list of strings = %i is different than the number of float columns = %i. The two need to be the same!"
             % (__noFloatCols, self.noColumnsFloats)
         )
     self.columnNamesFloats = np.zeros(
         (__noFloatCols, HaloHeader.columnNameLength), dtype="c"
     )
     for i in range(__noFloatCols):
         __length = [HaloHeader.columnNameLength, len(columnNames[i])][
             int(len(columnNames[i]) < HaloHeader.columnNameLength)
         ]
         self.columnNamesFloats[i, :__length] = columnNames[i][:__length]
Exemplo n.º 21
0
 def fromfile(self, f, BUFFER=True):
     if BUFFER:
         __buffer1 = np.fromfile(f, bufferType, 1)[0]
     A = np.fromfile(f, self.dtype(), 1)[0]
     if BUFFER:
         __buffer2 = np.fromfile(f, bufferType, 1)[0]
         if __buffer1 != __buffer2 or __buffer1 != MMFHeader.byteSize:
             throwError(
                 "Error reading the header of the MMF file. 'buffer1'=",
                 __buffer1,
                 "and 'buffer2'=",
                 __buffer2,
                 "when both should be",
                 MMFHeader.byteSize,
             )
     exec("%s = A" % self.TupleAsString())
Exemplo n.º 22
0
 def fromfile(self, f, BUFFER=True, numBytes_data=None):
     if BUFFER:
         __buffer1 = np.fromfile(f, bufferType, 1)[0]
     A = np.fromfile(f, self.dtype(), 1)[0]
     if BUFFER:
         __buffer2 = np.fromfile(f, bufferType, 1)[0]
         if __buffer1 != __buffer2 or __buffer1 != DensityHeader.byteSize:
             throwError(
                 "Error reading the header of the density file. 'buffer1'=",
                 __buffer1,
                 "and 'buffer2'=",
                 __buffer2,
                 "when both should be",
                 DensityHeader.byteSize,
             )
     exec("%s = A" % self.TupleAsString())
     if numBytes_data is not None and BUFFER:
         numBytes_data[:] = np.fromfile(f, bufferType, 1)[0]
Exemplo n.º 23
0
def binBoundaries(dataRange, no_bins, bin_type="linear"):
    """ Returns the bin boundaries for 'no_bins' equally spaced in linear/logarithmic interval in the given range."""
    if bin_type not in ["linear", "logarithm"]:
        throwError(
            "Unknown bin type in function 'binBoundaries'. You inserted 'bin_type'='%s', but the function recognizes only the following values '%s'."
            % (bin_type, str(["linear", "logarithm"])))
    output = None
    if bin_type is "linear":
        output = np.linspace(dataRange[0],
                             dataRange[1],
                             num=no_bins + 1,
                             endpoint=True)
    if bin_type is "logarithm":
        output = np.logspace(
            np.log10(dataRange[0]),
            np.log10(dataRange[1]),
            num=no_bins + 1,
            endpoint=True,
        )
    return output
Exemplo n.º 24
0
def CorrelationMatrix(data, N=100, minColumns=5):
    """Computes the correlation of the columns of the data set and the errors associated to the correlation coefficient using bootstrap. It generates 'N' different bootsrap realizations."""
    funcName = "CorrelationMatrix"
    if data.ndim != 2:
        throwError("The '%s' functions works only for a 2D data set." %
                   funcName)
    correlation = np.corrcoef(data, rowvar=0)
    if (data.shape[0] < minColumns
        ):  # the data is not large enough to get an accurate error for it
        return correlation, correlation
    correlationSet = np.zeros((N, correlation.shape[0] * correlation.shape[1]),
                              np.float64)
    indices = np.arange(data.shape[0])
    for i in range(N):
        select = Bootstrap(indices)
        correlationSet[i, :] = np.corrcoef(data[select, :], rowvar=0).flatten()
    correlationError = np.std(correlationSet.reshape(N, -1),
                              axis=0).reshape(correlation.shape[0],
                                              correlation.shape[1])
    return correlation, correlationError
Exemplo n.º 25
0
def readMMFHeader(file, VERBOSE=True):
    """ Reads only the MMF header from the given binary file. It returns the results as the class 'MMFHeader'.
    Takes as argument the name (or root anme) of the file from where to read the header.
    Can use VERBOSE = False to turn off the message."""
    header = MMFHeader()
    tempName = file
    if not os.path.isfile(tempName):
        tempName = MMFMultipleFiles(file, 0)
        if not os.path.isfile(tempName):
            throwError(
                "Cannot find the MMF binary file. There are no '%s' or '%s' files."
                % (file, tempName))
    if VERBOSE:
        print("Reading the header of the MMF file '%s' ... " % tempName,
              end=" ")
    f = open(tempName, "rb")
    header.fromfile(f)
    f.close()
    if VERBOSE:
        print("Done")
    return header
Exemplo n.º 26
0
def readGadgetHeader(file, INDEX=0, VERBOSE=True):
    """   Reads only the Gadget header from the given Gadget file. It returns the results as the class 'GadgetHeader'.
    Takes as argument the name (or root anme) of the file from where to read the header.
    Can use VERBOSE = False to turn off the message."""
    header = GadgetHeader()
    tempName = file
    if not os.path.isfile(tempName):
        tempName = gadgetMultipleFiles(file, INDEX)
        if not os.path.isfile(tempName):
            throwError(
                "Cannot find the gadget snapshot file. There are no '%s' or '%s' files."
                % (file, tempName))
    if VERBOSE:
        print("Reading the header of the Gadget file '%s' ... " % tempName)

    gadgetFileType, switchEndian, offsetSize = getGadgetFileType(tempName)

    f = open(tempName, "rb")
    f.seek(offsetSize, 1)
    header.fromfile(f, switchEndian=switchEndian)
    f.close()
    return header
Exemplo n.º 27
0
def writeGadgetData(file, gadgetParticles, VERBOSE=True):
    """ Writes a single Gadget file.
    It takes two arguments: the name of the output file and the data to be written as the class 'GadgetParticles'. """

    bufferType = np.dtype("i4")  # the buffer type before each block of data

    # do some error checking
    if VERBOSE:
        print("Writing the gadget data to the file '%s' ... " % file)
    if not gadgetParticles.CheckDataCompletness():
        throwError(
            "Cannot continue writing the Gadget snapshot since the data failled the completness test."
        )
    (noParticles, sameSize) = gadgetParticles.SameNumberOfParticles()
    if not sameSize:
        throwError(
            "Cannot continue writing the Gadget snapshot since the not all the Gadget particle data arrays have the same (or expected) size."
        )
    if gadgetParticles.Header().npartTotal[1] != noParticles:
        throwError(
            "Cannot continue writing the Gadget snapshot since the length of the Gadget particle data does is not the same as the expected nuber of particles from the Gadget header. Length of Gadget particle data is %i versus expected size %i (from the header)."
            % (noParticles, gadgetParticles.Header().npartTotal[1]))
    gadgetParticles.Header().num_files = np.int32(1)

    # write the header to the file
    f = open(file, "wb")
    gadgetParticles.Header().tofile(f)

    # write the data to the file
    pos = gadgetParticles.Pos().reshape(-1)
    pos.shape = -1
    __buffer = np.zeros(1, dtype=bufferType)
    __buffer[0] = pos.size * pos[0].nbytes
    __buffer.tofile(f)
    pos.tofile(f)
    __buffer.tofile(f)

    pos = gadgetParticles.Vel().reshape(-1)
    __buffer[0] = pos.size * pos[0].nbytes
    __buffer.tofile(f)
    pos.tofile(f)
    __buffer.tofile(f)

    pos = gadgetParticles.Ids().reshape(-1)
    __buffer[0] = pos.size * pos[0].nbytes
    __buffer.tofile(f)
    pos.tofile(f)
    __buffer.tofile(f)

    if gadgetParticles.hasMass:
        pos = gadgetParticles.Mass().reshape(-1)
        __buffer[0] = pos.size * pos[0].nbytes
        __buffer.tofile(f)
        pos.tofile(f)
        __buffer.tofile(f)

    f.close()
    if VERBOSE:
        print("Done.")
Exemplo n.º 28
0
def CumulativeSum(data, order="ascending", out_type=None):
    """ Computes the cumulative sum of the entries of an array. 
    order = "ascending"/"descending" to sum starting with the first/last array element
    out_type - output data type of the resulting array """

    if order not in ["ascending", "descending"]:
        throwError(
            "The cumulative ordering '%s' is not recognized in function 'CumulativeSum'. The function recognizes only the values 'ascending'/'descending' to compute the ascending/descending cumulative sum."
            % str(order))

    result = None
    if order is "ascending":
        result = np.cumsum(data, dtype=out_type)
    if order is "descending":
        dataShape = data.shape
        data.shape = -1
        temp = data[::-1]
        data.shape = dataShape
        temp2 = np.cumsum(temp, dtype=out_type)
        result = temp2[::-1]

    return result
Exemplo n.º 29
0
 def ReadColumnNames(self, f, BUFFER=True):
     """Reads the column names (16 chars for each column) from a binary halo file. """
     if BUFFER:
         __buffer1 = np.fromfile(f, HaloHeader.bufferType, 1)[0]
     A = np.fromfile(
         f, "c", self.noColumns * HaloHeader.columnNameLength
     ).reshape(-1, HaloHeader.columnNameLength)
     if BUFFER:
         __buffer2 = np.fromfile(f, HaloHeader.bufferType, 1)[0]
         if (
             __buffer1 != __buffer2
             or __buffer1 != self.noColumns * HaloHeader.columnNameLength
         ):
             throwError(
                 "Error reading the column name data stack of the halo file. 'buffer1'=",
                 __buffer1,
                 "and 'buffer2'=",
                 __buffer2,
                 "when both should be",
                 self.noColumns * HaloHeader.columnNameLength,
             )
     self.columnNamesIntegers = A[: self.noColumnsIntegers, :]
     self.columnNamesFloats = A[self.noColumnsIntegers :, :]
Exemplo n.º 30
0
def MinMax(data):
    """ Computes the min and maximum of a data set. It return two tuples: (minValue,maxValue) and (minIndex,maxIndex). """
    if data.size <= 0:
        throwError(
            "The numpy array supplied as argument to the 'MinMax' function must have at least one elemnt."
        )

    dataType = dtype2ctype[data.dtype]

    # C++ code to efficiently compute the min and max of the data entries
    MinMaxCode = """
    #line 1000 "MinMaxCode"
    %s min = data(0), max = data(0);
    size_t minIndex = 0, maxIndex = 0;
    for (int i=1; i<data.size(); ++i)
    {
        if (data(i)<min) {min=data(i); minIndex=i;}
        if (max<data(i)) {max=data(i); maxIndex=i;}
    }
    values(0) = min; values(1) = max;
    indices(0) = minIndex; indices(1) = maxIndex; """ % (dataType)

    # Call the C++ code computing the min and max values
    _a = np.array([0, 0])
    values = _a.astype(data.dtype)
    indices = _a.astype(np.uint64)
    finalShape = data.shape
    data.shape = -1
    inline(
        MinMaxCode,
        ["data", "values", "indices"],
        type_converters=converters.blitz,
    )
    data.shape = finalShape

    # return the results
    return [(values[0], values[1]), (indices[0], indices[1])]