2.2.4. Adding TAttribute when data are already available

It is possible to add new attribute in a given TDataServer object that would contain data using two different methods. The first one rely on the already existing data to create a new variable by simply writting the equation, which internally is calling a TAttributeFormula object. The following piece of code shows how to create a third variable from the geyser.dat file, simply as an equation from the existing variables:

tds = DataServer.TDataServer("foo","pouet")
tds.fileDataRead("geyser.dat")
# Adding a new attribute
x3 = DataServer.TAttribute("x3","0.5*x2+sin(x1)")

This method can deal with double and vector-based attributes (of course no equation can be estimate when one of the input variable in the formula is a string one, so this method will crash).

Another way recently introduced is to add an attribute from an array of double (or it’s equivalent in python, meaning a numpy.array) by calling the method addAttributeUsingData. The idea is to be able to add information that would have been processed by methods aside from the Uranie ones. The signature of the function is the name of the new attribute as first argumet, the second one is an array of double and the third one is the size of the array. There are two ways, recommended, that uses object with built-in method that provide the size (to prevent from mis-typing problem between the array and its size). Obviously, the size of the array must be equal to the number of patterns in the existing TDataServer object.

Here is an example using the myData.dat, in C++:

import numpy as np

tds = DataServer.TDataServer("foo","tru")
tds.fileDataRead("myData.dat")
# Defining a vector with 11 elements
x2 = np.array([-10,-8,-6,-4,-2,0,2,4,6,8,10], dtype=np.float64)
# Call the method using the address of first element and the size of it
tds.addAttributeUsingData("x2", x2, len(x2))

This method should only be used to create double-based attributes (as the size of the array would be chaotic if it were to be a vector of varying size). Obviously, no string-based attribute can be constructed like this.

Warning

The method addAttributeUsingData should only be called either when no data AND no attribute are stored in the dataserver or when there are data and the new array of double provided has the same size (number of patterns) as the data already available within the dataserver.