001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.timeseries.collection;
031
032import java.util.Arrays;
033import java.util.Collection;
034import java.util.HashMap;
035import java.util.Map;
036import java.util.Map.Entry;
037
038import org.openimaj.ml.timeseries.IncompatibleTimeSeriesException;
039import org.openimaj.ml.timeseries.TimeSeries;
040import org.openimaj.ml.timeseries.TimeSeriesSetException;
041
042/**
043 * A collection of time series which share exactly the same time steps.
044 * Given that this is true, SynchronisedTimeSeriesProcessor instances can perform interesting analysis
045 *  
046 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
047 * @param <ALLINPUT> The collection input type
048 * @param <SINGLEINPUT> The type of a single time point data
049 * @param <INTERNALSERIES> The type of the internal series held in this collection
050 * @param <TIMESERIES> 
051 *
052 */
053public abstract class SynchronisedTimeSeriesCollection<
054        ALLINPUT, 
055        SINGLEINPUT, 
056        TIMESERIES extends SynchronisedTimeSeriesCollection<ALLINPUT, SINGLEINPUT,TIMESERIES,INTERNALSERIES>,
057        INTERNALSERIES extends TimeSeries<ALLINPUT,SINGLEINPUT,INTERNALSERIES>
058> extends TimeSeriesCollection<
059        ALLINPUT, 
060        SINGLEINPUT,
061        TIMESERIES,
062        INTERNALSERIES
063>
064{
065        
066        long[] time;
067
068        /**
069         * initialise the underlying time series holder
070         */
071        public SynchronisedTimeSeriesCollection(){
072                super();
073        }
074        
075        @Override
076        public void addTimeSeries(String name, INTERNALSERIES series) throws IncompatibleTimeSeriesException{
077                if(this.time == null){
078                        this.time = series.getTimes();
079                }
080                else if(!Arrays.equals(this.time, series.getTimes())){
081                        throw new IncompatibleTimeSeriesException();
082                }
083                
084                super.addTimeSeries(name, series);
085        }
086        
087        @Override
088        public INTERNALSERIES series(String name){
089                return this.timeSeriesHolder.get(name);
090        }
091        
092        @Override
093        public Collection<INTERNALSERIES> allseries(){
094                return this.timeSeriesHolder.values();
095        }
096
097        @Override
098        public long[] getTimes() {
099                return this.time;
100        }
101
102        @Override
103        public void set(long[] time, Map<String, ALLINPUT> data) throws TimeSeriesSetException {
104                this.time = time;
105                INTERNALSERIES intitalInstance = this.internalNewInstance();
106                this.timeSeriesHolder = new HashMap<String,INTERNALSERIES>();
107                for (Entry<String, ALLINPUT> l : data.entrySet()) {
108                        INTERNALSERIES intital = intitalInstance.newInstance();
109                        intital.set(time, l.getValue());
110                        this.timeSeriesHolder.put(l.getKey(), intital);
111                }
112        }
113
114        @Override
115        public Map<String, ALLINPUT> getData() {
116                Map<String, ALLINPUT> ret = new HashMap<String, ALLINPUT>();
117                for (Entry<String, INTERNALSERIES> held: this.timeSeriesHolder.entrySet()) {
118                        ret.put(held.getKey(), held.getValue().getData());
119                }
120                return ret ;
121        }
122
123        @Override
124        public int size() {
125                if(this.time!=null)return this.time.length;
126                return 0;
127        }
128
129        @Override
130        public void internalAssign(TIMESERIES interpolate) {
131                this.time = interpolate.time;
132                this.timeSeriesHolder = interpolate.timeSeriesHolder;
133        }
134        
135        
136
137        @Override
138        public String toString() {
139                return "Synchronised Time series";
140        }
141        
142        /**
143         * In some way flatten the held time series such that the output is:
144         * 
145         * @return [ALLDATAs1t1,ALLDATAs2t1,...,ALLDATAs1,t2,...,ALLDATAsntm] etc.
146         */
147        public abstract ALLINPUT flatten();
148        
149}