-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERPFile.m
229 lines (205 loc) · 6.36 KB
/
ERPFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// ERPFile.m
// MCAT3
//
// Created by Peter Molfese on 7/2/08.
// Copyright 2008 Fireball Presentations. All rights reserved.
//
#import "ERPFile.h"
@implementation ERPFile
@synthesize name;
@synthesize info;
@synthesize inputFile;
@synthesize outputFile;
@synthesize categories;
@synthesize categoryNames;
@synthesize channelClusters;
@synthesize type;
@synthesize theController;
@synthesize outString;
@synthesize isLoaded;
-(id)init
{
if( [super init] == nil )
{
NSLog(@"Failed to init super ERPFile");
return nil;
}
categories = [[NSMutableArray alloc] initWithCapacity:1];
categoryNames = [[NSMutableArray alloc] initWithCapacity:1];
channelClusters = nil;
self.isLoaded = NO;
self.type = 0; //type is 0 means unset; 1 is rotated; 2 is clustered; 3 is rotated & clustered
return self;
}
-(BOOL)readEGIFile:(NSString *)pathToFile
{
return 0; //not yet implemented... waiting for NS4.4 file format
}
-(BOOL)readTextFile:(NSString *)pathToFile
{
PMMatrix *newMatrix = [[PMMatrix alloc] init]; //where we'll put the matrix
NSString *theFile = [NSString stringWithContentsOfFile:pathToFile];
NSArray *rows = [theFile componentsSeparatedByString:@"\r"];
if( [rows count] < 3 )
{
rows = [theFile componentsSeparatedByString:@"\n"]; //funny line endings
}
for( NSString *myRow in rows )
{
[newMatrix addRowOfStrings:myRow];
}
[self setInfo:[NSString stringWithFormat:@"Matrix: %i x %i", [newMatrix cols], [newMatrix rows]]];
[categories addObject:newMatrix];
[categoryNames addObject:@"all"];
self.isLoaded = YES;
return 1;
}
-(NSArray *)clusterFile
{
NSArray *chToAve = [[self channelClusters] objectForKey:@"channels"];
PMMatrix *aMatrix = [categories objectAtIndex:0];
NSMutableArray *outcomeArray = [NSMutableArray new];
for( NSArray *aSet in chToAve )
{
[outcomeArray addObject:[aMatrix arrayWithAverageOfColumns:aSet]];
}
return outcomeArray;
}
-(NSString *)returnClustered
{
int i;
NSArray *clue = [self clusterFile];
if( clue == nil )
{
[NSAlert alertWithMessageText:@"MCAT encountered an error" defaultButton:@"OK" alternateButton:@"OK" otherButton:nil informativeTextWithFormat:@"Restart MCAT and check your files"];
return nil;
}
NSArray *names = [channelClusters objectForKey:@"names"];
if( [clue count] != [names count] )
{
NSLog(@"Oops, number of clusters isn't the same as number of names");
[NSAlert alertWithMessageText:@"Problem converging cluster names to averages... Please check your input files and restart MCAT3" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:nil];
return FALSE;
}
PMMatrix *newMatrix = [[PMMatrix alloc] init];
for( i=0; i<[clue count]; i++ )
{
[newMatrix addCol:[clue objectAtIndex:i]];
}
NSMutableString *outputString = [NSMutableString stringWithString:@""];
for( i=0; i<[newMatrix rows]; i++ )
{
NSArray *anyRow = [newMatrix getRowAtIndex:i];
[outputString appendString:[anyRow componentsJoinedByString:@" "]];
[outputString appendString:@"\n"];
}
return outputString;
}
-(NSString *)returnRotated
{
NSMutableString *outputString = [NSMutableString stringWithString:@""];
PMMatrix *aMatrix = [categories objectAtIndex:0];
int i;
for( i=0; i<[aMatrix cols]; i++ )
{
NSArray *aCol = [aMatrix getColAtIndex:i];
[outputString appendString:[aCol componentsJoinedByString:@" "]];
[outputString appendString:@"\n"];
}
return outputString;
}
-(NSString *)returnFile
{
int i, j;
NSArray *clue = [self clusterFile];
if( clue == nil )
{
[NSAlert alertWithMessageText:@"MCAT encountered an error" defaultButton:@"OK" alternateButton:@"OK" otherButton:nil informativeTextWithFormat:@"Restart MCAT and check your files"];
return nil;
}
NSArray *names = [channelClusters objectForKey:@"names"];
if( [clue count] != [names count] )
{
NSLog(@"Oops, number of clusters isn't the same as number of names");
[NSAlert alertWithMessageText:@"Problem converging cluster names to averages... Please check your input files and restart MCAT3" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:nil];
}
NSMutableString *outputString = [NSMutableString stringWithString:@""];
for( i=0; i< [names count]; i++ )
{
[outputString appendString:[names objectAtIndex:i]];
[outputString appendString:@" "];
for( j=0; j<[[clue objectAtIndex:i] count]; j++ )
{
[outputString appendString:[[[clue objectAtIndex:i] objectAtIndex:j] stringValue]];
[outputString appendString:@" "];
}
[outputString appendString:@"\n"];
}
return outputString;
}
-(BOOL)writeClusterdTextFile:(NSString *)pathToFile
{
[[self outString] writeToFile:pathToFile atomically:NO encoding:1 error:NULL];
return TRUE;
}
-(BOOL)writeRotatedTextFile:(NSString *)pathToFile
{
[[self outString] writeToFile:pathToFile atomically:NO encoding:1 error:NULL];
return TRUE;
}
-(BOOL)writeTextFile:(NSString *)pathToFile
{
[[self outString] writeToFile:pathToFile atomically:NO encoding:1 error:NULL];
return TRUE;
}
-(void)processFile;
{
//NSDictionary *channelClusters;
switch( self.type )
{
case 0:
NSLog(@"Do nothing to the file - just concatenate");
break;
case 1:
//NSLog(@"Type is 1: Rotate");
//code goes here;
//[self readTextFile:self.inputFile];
[self setOutputFile:[[self inputFile] stringByDeletingPathExtension]];
[self setOutputFile:[[self outputFile] stringByAppendingString:@"-r.txt"]];
//[self writeRotatedTextFile:outputFile];
self.outString = [self returnRotated];
break;
case 2:
//[self readTextFile:self.inputFile];
[self setOutputFile:[[self inputFile] stringByDeletingPathExtension]];
[self setOutputFile:[[self outputFile] stringByAppendingString:@"-c.txt"]];
//[self writeClusterdTextFile:outputFile];
self.outString = [self returnClustered];
break;
case 3:
//[self readTextFile:self.inputFile];
[self setOutputFile:[[self inputFile] stringByDeletingPathExtension]];
[self setOutputFile:[[self outputFile] stringByAppendingString:@"-cr.txt"]];
//[self writeTextFile:self.outputFile];
self.outString = [self returnFile];
break;
default:
NSLog(@"Type is unknown");
break;
}
if( self.outString == nil )
{
[NSAlert alertWithMessageText:@"outputString is empty!" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Restart MCAT"];
}
}
-(void)main
{
while( self.isLoaded == NO )
{
sleep(5);
}
[self processFile];
[theController incrementTheBar];
}
@end