Often, your data will be in the form of a comma-delimited or tab-delimited file with a .csv or .txt extension, such as you might get using Excel's Save As... command. We already discussed how to convert a Numpy array into a Damon object using DamonObj(). The same procedure is used to load text files. But first, let's create a text file using core.create_data().
Create a New Script
In IDLE, open the damon1.templates.blank module. Save it as my_script3.py in a directory of your choice.
Create Text File
In IDLE, type >>> help(core.create_data). Paste the core.create_data() function into my_script3.py. Add the dmn prefix and assign the output a name of d.
Enter values for nfac0, nfac1, ndim, and any other parameters you like. However, it is the output_as, outfile, and delimiter parameters that are to specify an output text file. Specify output_as = 'textfile', outfile = 'scrap0.csv', and delimiter = ',' . This will create two comma-delimited text files: a_data_scrap0.csv and a_model_scrap0.csv. The prefix a and a descriptor (data or model) are automatically added to the specified outfile name.
The IDLE output informs us we built two text files consisting of 10 rows (nfac0 = 10) and 8 columns (nfac1 = 8) of numerical data (not including labels) with a dimensionality of 3 (ndim = 3). The root of the file name is as specified by the outfile parameter, but Damon has added some prefix information to show that one of the files is "model" (mathematically pure) and the other is "data" (with noise and missing cells added). Because outfile does not contain any path information, Damon automatically drops the files into the "current working directory" -- the directory where you saved your Damon script my_script3.py. You could also have specified a file path name outfile = "/Home/Me/a_data_scrap0.py". Check out the data files by opening them in a text editor.
Read the Data File as a Damon Object
Read in the data file using Damon(). In IDLE, type >>> help(dmn.Damon.__init__). The Damon() documentation will pop up. At the end, under Paste Class, copy and paste the Damon() arguments into my_script3.py, add a dmn prefix and assign to the d_obj object: d_obj = dmn.Damon(data,format_,...)
For the data parameter, type in the name of the data file a_data_scrap0.csv. (You can also specify a path name if the file is not in the same directory as my_script3.py.) For the Format parameter, type 'textfile'. For the nheaders4rows and nheaders4cols parameters, type 1 and 1 -- the number of header columns/rows for the labels. Because the data is from a text file, you need to tell Damon to interpret the core data as numbers. Set validchars = ['All',['All'],'Num']. That will do the trick. (Make sure to read the validchars documentation in dmn.Damon.__init__ when you have a chance. It is quite useful.)
The defaults for the rest of the parameters should work fine. In the example, I stripped out the inline documentation for ease of reading.
Important Note Regarding Path Names
Unlike Unix-based machines (including OS X), Windows machines use backward slashes in their pathnames ("\Home\Documents\a_Data_scrap0.csv") . This can confuse Python, which reserves backslash for other uses. There are two remedies if you are using a Windows machine:
One other note, Python does not accept numerical file names.
Here is the script and the IDLE output.
There is a confusing variety of text files in the real world, with slightly different conventions. Damon may not be able to read all of them. If you run into trouble, try one of Python's or Numpy's file readers, save the results as an array, and read it into Damon specifying format_ = 'array'. Start with Numpy's genfromtxt() function.
And that is how to read in a text file.