Re: Designing a Flexible Mathematica Program for Data Analysis
- To: mathgroup at smc.vnet.net
- Subject: [mg74697] Re: Designing a Flexible Mathematica Program for Data Analysis
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 1 Apr 2007 04:13:36 -0400 (EDT)
- References: <eul01m$87b$1@smc.vnet.net>
5000brians schrieb:
> I'm using Mathematica to perform data analysis for a project I've been
> working on for a few years. I have written quite a bit of Mathematica
> code for automating the task of analyzing the data.
>
> As time goes by, the type of data I collect changes slightly. I'm
> looking for a way for the various data set types I have to happily
> coexist within a single Mathematica session so I can compare and
> contrast all the data with a minimum of fuss.
>
> Let's say I want to create a plot of dataset 1 and dataset 2. In both
> cases, I want the same thing, a plot of parameter a versus parameter
> b. And let's say I have a bunch of code already written and it works
> just fine for dataset 1. But, dataset 2 is newer, and the data format
> is slightly different. The parameters a and b mean the same thing in
> both cases, it's just that they are represented differently in dataset
> 2 than in dataset 1.
>
> And let's say I have the following functions:
> readDataset: opens the text file and puts dataset into memory
> generateAandB: based on the data in the dataset, creates a list of
> parameters a and b for a dataset
> plotAandB: creates a plot of a vs b.
>
> In this case, I probably need two versions of readDataset and
> generateAandB, one for each of my dataset types. If I am using a
> List[], I am probably ok with one plotAandB function.
>
> But how can I automate this solution? What if I want to read in 30
> datasets with 10 different dataset types?
>
> I can't do plotAandB[generateAandB[readDataset[#]]]& /@
> listOfDatasets, because I need 10 different generateAandBs and 10
> different readDatasets.
>
> How can I make my code "datatype aware"?
>
> I know this is a bit long winded - sorry about that.
>
> Thanks for any help,
> Brian
>
>
Hi Brian,
I would prepend the data in the files with the names of the functions to use
(which should be defined in the notebook).
A simple example with only one (undefined) function for each dataset.
In[1]:=SetDirectory[StringJoin[$HomeDirectory, "\\Desktop"]];
In[2]:= tb = Import["data1.txt", "Table"]
Apply[ToExpression[tb[[1,1]]], Rest[tb], {1}]
Out[2]={{"ReadFunction1"}, {1, 1},{2, 4}, {3, 9}, {4, 16}}
Out[3]=
{ReadFunction1[1, 1],
ReadFunction1[2, 4],
ReadFunction1[3, 9],
ReadFunction1[4, 16]}
In[4]:= tb = Import["data2.txt", "Table"]
Apply[ToExpression[tb[[1,1]]], Rest[tb], {1}]
Out[4]= {{"ReadFunction2"}, {1, 1}, {4, 2}, {9, 3}, {16, 4}}
Out[5]=
{ReadFunction2[1, 1],
ReadFunction2[4, 2],
ReadFunction2[9, 3],
ReadFunction2[16, 4]}
Peter