001    package cnslab.cnsnetwork;
002    
003    import java.io.*;
004    import java.util.*;
005
006    /***********************************************************************
007    * Static method library to support RecorderData.
008    * 
009    * @see
010    *   RecorderData
011    * @see
012    *   UserSenNeuron
013    *   
014    * @version
015    *   $Date: 2012-07-15 21:18:51 -0500 (Sun, 15 Jul 2012) $
016    *   $Rev: 69 $
017    *   $Author: croft $
018    * @author
019    *   Yi Dong
020    * @author
021    *   David Wallace Croft
022    ***********************************************************************/
023    public final class  RecorderDataLib
024    ////////////////////////////////////////////////////////////////////////
025    ////////////////////////////////////////////////////////////////////////
026    {
027    
028    /***********************************************************************
029    * Inserts experiment data into a RecorderData instance.
030    ***********************************************************************/ 
031    public static void  insertExperimentDataIntoRecorderData (
032      final RecorderData        recorderData,
033      final int                 experimentId,
034      final int                 trialId,
035      final String              posi,
036      final LinkedList<Double>  times )
037    ////////////////////////////////////////////////////////////////////////
038    {
039      if ( times != null
040        && times.size ( ) != 0 )
041      {
042        recorderData.receiver.put (
043          "Exp"
044            + experimentId
045            + "Tri"
046            + trialId
047            + "/"
048            + posi,
049          times );
050      }
051    }
052
053    public static void  insertExperimentRecorderDataIntoRecorderData (
054      final RecorderData  recorderData,
055      final Experiment    experiment,
056      final RecorderData  experimentRecorderData )
057    ////////////////////////////////////////////////////////////////////////
058    {
059      final ArrayList<String>
060        singleUnitNameList = experiment.recorder.suNames;
061      
062      final ArrayList<Integer>
063        singleUnitIdList = experiment.recorder.singleUnit;
064      
065      final int
066        singleUnitIdListSize = singleUnitIdList.size ( );
067      
068      final Map<String, LinkedList<Double>>
069        dataKeyToTimeListMap = experimentRecorderData.receiver;
070      
071      for ( int  subExperimentId = 0;
072        subExperimentId < experiment.subExp.length;
073        subExperimentId++ )
074      {
075        final SubExp
076          subExp = experiment.subExp [ subExperimentId ];
077        
078        final int
079          repetition = subExp.repetition;
080        
081        for ( int trialId = 0;
082          trialId < repetition;
083          trialId++ )
084        {       
085          for ( int singleUnitIdIndex = 0;
086            singleUnitIdIndex < singleUnitIdListSize;
087            singleUnitIdIndex++ ) 
088          {
089            final String
090              singleUnitName = singleUnitNameList.get ( singleUnitIdIndex );
091            
092            final String
093              dataKey = "E"
094                + subExperimentId
095                + "T"
096                + trialId
097                + "N"
098                + singleUnitName;
099            
100            final LinkedList<Double>
101              timeList = dataKeyToTimeListMap.get ( dataKey );
102            
103            insertExperimentDataIntoRecorderData (
104              recorderData,
105              subExperimentId,
106              trialId,
107              singleUnitName,
108              timeList );
109          }
110        }
111      }
112    }
113    
114    public static RecorderData  readRecorderDataFromBinaryFile (
115      final File  binaryFile )
116      throws ClassNotFoundException, IOException
117    ////////////////////////////////////////////////////////////////////////
118    {
119      ObjectInputStream  objectInputStream = null;
120      
121      try
122      {
123        objectInputStream
124          = new ObjectInputStream (
125            new BufferedInputStream (
126              new FileInputStream ( binaryFile ) ) );
127        
128        return ( RecorderData ) objectInputStream.readObject ( );
129      }
130      finally
131      {
132        if ( objectInputStream != null )
133        {        
134          objectInputStream.close ( );
135        }
136      }
137    }
138
139    /***********************************************************************
140    * Creates a RecorderData and populates it with data from a text file.
141    ***********************************************************************/ 
142    public static RecorderData  readRecorderDataFromTextFile (
143      final File  textFile )
144      throws IOException
145    ////////////////////////////////////////////////////////////////////////
146    {
147      final RecorderData
148        recorderData = new RecorderData ( );
149      
150      readRecorderDataFromTextFile (
151        recorderData,
152        textFile );
153      
154      return recorderData;
155    }
156    
157    /***********************************************************************
158    * Parses the input from a text file.
159    * 
160    * Text data file format, each neuron and each trial is one line, e.g.
161    * 
162    * 0 0 pre,0,0,suf 0.00049314 0.00065926 0.004612 0.018346 0.029955
163    * 0.055654 0.063001 0.08199 0.082669 0.087991 0.10277 0.10769 0.12964
164    * 0.14332 0.14447 0.15082 0.17663 0.17955 0.18728 0.22081 0.24705
165    * 0.25536 0.75184 0.81812 1.3039 1.4853 1.5252 1.5371 1.6114 1.7159
166    * 1.7312 1.7467 1.8764 2.0049 2.0568 2.0735 2.1627 2.194 2.2083 2.2187
167    * 2.2302 2.3075 2.3331 2.3656 2.4241 2.4364 2.4476 2.4575 2.4806 2.499
168    * 2.5649 2.5898 2.6003 2.6038 2.6444 2.6871 2.6916 2.7372 2.744 2.747
169    * 2.7927 2.8374 2.9312 3.0107 3.0625 3.0678 3.1224 3.1777 3.1779 3.2035
170    * 3.2372 3.2734 3.3049 3.3341 3.3505 3.4168 3.5189 3.5232 3.5254 3.5669
171    * 3.6241 3.6284 3.6733 3.6869 3.688 3.7007 3.7051 3.8492 3.9113 3.9176
172    * 3.9439 3.9476 3.9572 3.9689 4.0021<br>
173    * 
174    * first subexp id, then trial id, then neuron string, then neuron spikes
175    * starting at time zero. 
176    ***********************************************************************/ 
177    public static void  readRecorderDataFromTextFile (
178      final RecorderData  recorderData,
179      final File          textFile )
180      throws IOException
181    ////////////////////////////////////////////////////////////////////////
182    {
183      BufferedReader  bufferedReader = null;
184      
185      try
186      {
187        bufferedReader = new BufferedReader (
188          new FileReader ( textFile ) );
189        
190        String strLine;
191        
192        // Read File Line By Line
193        
194        while ( ( strLine = bufferedReader.readLine ( ) ) != null )
195        {
196          final String [ ]
197            res = strLine.split ( " " );
198          
199          if ( res.length < 4 )
200          {
201            throw new RuntimeException (
202              "error in file format" );
203          }
204          
205          final LinkedList<Double>
206            data = new LinkedList<Double> ( );
207          
208          for ( int  i = 3; i < res.length; i++ )
209          {
210            data.add ( Double.parseDouble ( res [ i ] ) );
211          }
212          
213          insertExperimentDataIntoRecorderData (
214            recorderData,
215            Integer.parseInt ( res [ 0 ] ),
216            Integer.parseInt ( res [ 1 ] ),
217            res [ 2 ],
218            data );
219          
220          // Print the content on the console
221          
222          // System.out.println (strLine);
223        }
224      }
225      finally
226      {
227        if ( bufferedReader != null )
228        {
229          bufferedReader.close ( );
230        }
231      }
232    }
233
234    /***********************************************************************
235    * Writes the RecorderData to the file in binary format.
236    ***********************************************************************/ 
237    public static void  writeRecorderDataToBinaryFile (
238      final RecorderData  recorderData,
239      final File          binaryFile )
240      throws IOException
241    ////////////////////////////////////////////////////////////////////////
242    {
243      ObjectOutputStream  objectOutputStream = null;
244      
245      try
246      {
247        objectOutputStream = new ObjectOutputStream (
248          new FileOutputStream ( binaryFile ) );
249        
250        objectOutputStream.writeObject ( recorderData );
251      }
252      finally
253      {
254        if ( objectOutputStream != null )
255        {
256          objectOutputStream.close ( );
257        }
258      }
259    }
260    
261    ////////////////////////////////////////////////////////////////////////
262    // private methods
263    ////////////////////////////////////////////////////////////////////////
264    
265    private  RecorderDataLib ( )
266    ////////////////////////////////////////////////////////////////////////
267    {
268      // private constructor
269    }
270    
271    ////////////////////////////////////////////////////////////////////////
272    ////////////////////////////////////////////////////////////////////////
273    }