-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDailyData.m
More file actions
1189 lines (737 loc) · 35.9 KB
/
DailyData.m
File metadata and controls
1189 lines (737 loc) · 35.9 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// DailyData.m
// Bartender
//
// Created by Tom Houpt on 12/7/10.
// Copyright 2012 Behavioral Cybernetics. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import "BarExperiment.h"
#import "BarDocument.h"
#import "DailyData.h"
#import "BarItem.h"
#import "BarGroup.h"
// weight arrays are only allocated when experiment is assigned.
// experiment is assigned either by initFromPath:withExperiment when dailyData is loaded into BarExperiment memory
// or when label is parsed to identify experiment for first time
// or when specific experiment weighing is invoked with initWithExperiment:
@implementation DailyData
-(id)init; {
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message and return nil.
[self setDefaults];
}
return self;
}
-(id)initFromPath:(NSString *)path withExperiment:(BarExperiment *)theExpt; {
// load data from file at specified path; not used for weighing
self = [super init];
if (self) {
// NSLog(@"DailyData: initFromPath");
[self setDefaults];
[self setTheExperiment:theExpt];
[self setCurrentState:kDataOnly]; // only loading data to access data, not for display or editing
[self readFromPath:path];
}
return self;
}
-(id)initWithExperiment:(BarExperiment *)theExpt; {
self = [super init];
if (self) {
[self setDefaults];
[self setTheExperiment:theExpt];
}
return self;
}
-(void)dealloc; {
// NSLog(@"DailyData: dealloc");
if (onWeight != NULL) { free(onWeight); }
if (offWeight != NULL) { free(offWeight); }
}
-(void)setDefaults; {
onTime = nil; // represented as "na" until specific time asigned
offTime = nil;
onWeight = NULL; // allocated when experiment asssigned
offWeight = NULL;
phaseName = @"<none>";
phaseDayIndex = -1;
currentState = kWeighingOn;
comment = @"";
// allocate the UUID that uniquely identifies the daily data file
CFUUIDRef myUUID;
myUUID = CFUUIDCreate(kCFAllocatorDefault);
UUID = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, myUUID));
CFRelease(myUUID);
}
// ***************************************************************************************
// ***************************************************************************************
// setters and getters
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
-(BarExperiment *)theExperiment { return theExperiment; }
-(void)setTheExperiment:(BarExperiment *)newExpt {
theExperiment = newExpt;
if (newExpt != nil) {
// now that we have an experiment,
// we know how many subjects & items we have
// so we can allocate the weight arrays
[self initWeights];
}
}
-(NSInteger)currentState { return currentState; }
-(void)setCurrentState:(NSInteger)newState { currentState = newState;}
-(NSString *) phaseName; {return phaseName; }
-(void)setPhaseName:(NSString *)pName; { phaseName = [pName copy]; }
- (NSUInteger) exptDayIndex; {
// 0 - indexed -- position of self in [theExperiment dataDays] array
NSUInteger day = [theExperiment dayIndexOfDailyData:self];
if (NSNotFound == day) { return [theExperiment numberOfDays]; }
return day;
}
-(NSInteger)phaseDayIndex; { return phaseDayIndex; }
-(void)setPhaseDayIndex:(NSInteger)pdi; { phaseDayIndex = pdi; }
-(NSString *)comment; {return comment; }
-(void)setComment:(NSString *)c; { comment = [c copy]; }
-(NSString *)UUID; { return UUID; }
-(NSDate *)onTime; { return onTime; }
-(void)setOnTime:(NSDate *)date; { onTime = date; }
-(NSDate *)offTime; { return offTime; }
-(void)setOffTime:(NSDate *)date; { offTime = date; }
-(NSDate *)modificationDate; { return modificationDate; }
-(void)setModificationDate:(NSDate *)date; { modificationDate = date; }
// ***************************************************************************************
// ***************************************************************************************
// FILE methods
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
-(void)save; {
// save the weights into the Documents/Bartender/DailyData directory
// On weights are stored in a file called "[ExptCode].onweights"
// Off weights are stored in a file called "[ExptCode] year-month-day.weights", in which the date is the save date
// i.e. if bottles in Expt TH were weighed on June 4, 2010 and weighed off June 5, 2010,
// then the weights are stored in the file "Documents/Bartender/DailyData/TH 2010-6-5.weights"
// note that the format of both onweights and weights files are identical XML plists,
// just that off weights in the .onweights file are set to NaN
// NSLog(@"DailyData: save");
if (currentState == kWeighingOn) {
[self saveOnWeights];
}
else if (currentState == kWeighingOff) {
[self saveOffWeights];
}
else if (currentState == kUserEditing) {
[self saveEdits];
}
}
-(void)saveOnWeights; {
// construct a complete path to the file to store the on weights:
// e.g. "~/Bartender/DailyData/[ExptCode]/[ExptCode].onweights"
// NSLog(@"DailyData: saveOnWeights");
// set the time of weighing on to right now
onTime = [NSDate date];
modificationDate = [NSDate date];
NSString *filename = [[theExperiment code] stringByAppendingPathExtension:@"onweights"];
NSString *dailyDataFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:filename];
[self saveToPath:dailyDataFilePath];
NSString *backup_filename = [[[theExperiment code] stringByAppendingString:[self dateStringForFile:onTime]] stringByAppendingPathExtension:@"onweights.BAK"];
NSString *backup_dailyDataFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:backup_filename];
[self saveToPath:backup_dailyDataFilePath];
// tell the expeiment that new on weights have been recorded, so we're waiting for the off weights
[theExperiment updateWithOnWeights];
}
-(void)backupOnWeights; {
// remove old ".onweights.BAK"
// rename on ".onweights" file as ".onweights.BAK"
NSString *filename = [[theExperiment code] stringByAppendingPathExtension:@"onweights"];
NSString *dailyDataFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:filename];
NSString *backupfilename = [filename stringByAppendingPathExtension:@"BAK"];
NSString *backupFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:backupfilename];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:backupFilePath error:&error];
[[NSFileManager defaultManager] moveItemAtPath:dailyDataFilePath toPath:backupFilePath error:&error];
}
-(void)backupOffWeights; {
// remove old ".weights.BAK"
// rename on ".weights" file as ".weights.BAK"
NSString *filename = [[[theExperiment code] stringByAppendingString:[self dateStringForFile:offTime]] stringByAppendingPathExtension:@"weights"];
NSString *dailyDataFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:filename];
NSString *backupfilename = [filename stringByAppendingPathExtension:@"BAK"];
NSString *backupFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:backupfilename];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:backupFilePath error:&error];
[[NSFileManager defaultManager] moveItemAtPath:dailyDataFilePath toPath:backupFilePath error:&error];
}
-(void)saveOffWeights; {
// construct a complete path to the file to store the completed on & off weights:
// e.g. "~/Bartender/DailyData/[ExptCode]/[ExptCode][Y-M-D].weights"
// NSLog(@"DailyData: saveOffWeights");
// set the time of weighing off to right now
offTime = [NSDate date];
NSString *filename = [[[theExperiment code] stringByAppendingString:[self dateStringForFile:offTime]] stringByAppendingPathExtension:@"weights"];
NSString *dailyDataFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:filename];
[self saveToPath:dailyDataFilePath];
[self backupOnWeights];
// tell the experiment that new OFF weights are saved, so it's daily data will need updating
// no longer waiting to weigh bottles off
// update the experiment with the name of the last saved daily data
// and with the current phase & phase day...
// saves experiment summary to backup path and to firebase
[theExperiment updateWithOffWeightsAtPath:dailyDataFilePath andPhaseName:phaseName andPhaseDay:phaseDayIndex];
}
-(void)saveEdits; {
// construct a complete path to the file to store the completed on & off weights:
// e.g. "~/Bartender/DailyData/[ExptCode]/[ExptCode][Y-M-D].weights"
// NSLog(@"DailyData: saveEdits");
[self backupOffWeights];
// set the time of weighing off to right now
modificationDate = [NSDate date];
NSString *filename = [[[theExperiment code] stringByAppendingString:[self dateStringForFile:offTime]] stringByAppendingPathExtension:@"weights"];
NSString *dailyDataFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:filename];
[self saveToPath:dailyDataFilePath];
}
-(void)saveToPath:(NSString *)filePath; {
// got to write out a plist consiting of the ontime, offtime, and the array of onweights and offweights...
// NSLog(@"DailyData: saveToPath: path is %@",filePath);
NSMutableDictionary *rootDictionary = [[NSMutableDictionary alloc] init];
[rootDictionary setObject:@"Bartender" forKey:@"creator"];
[rootDictionary setObject:@"2.0" forKey:@"version"];
// NSLog(@"DailyData: saveToPath: about to get time descriptions");
[rootDictionary setObject:[self onTimeDescription] forKey:@"weighedOn"];
[rootDictionary setObject:[self offTimeDescription] forKey:@"weighedOff"];
[rootDictionary setObject:[self modificationDateDescription] forKey:@"modified"];
// NSLog(@"DailyData: saveToPath: about to get phasename, phasedayindex,comment, and uuid");
[rootDictionary setObject:phaseName forKey:@"phase"];
[rootDictionary setObject:[NSNumber numberWithLong:phaseDayIndex] forKey:@"phaseDayIndex"];
[rootDictionary setObject:comment forKey:@"comment"];
[rootDictionary setObject:UUID forKey:@"UUID"];
// NSLog(@"DailyData: saveToPath: about to make subjects dictionary array");
[rootDictionary setObject:[self makeSubjectsDictionaryArray] forKey:@"SubjectsArray"];
NSString *error;
// NSLog(@"DailyData: saveToPath: about to serialize root dictionary");
id xmlData = [NSPropertyListSerialization dataFromPropertyList:(id)rootDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(xmlData) {
// NSLog(@"DailyData: No error creating Weights XML data.");
[xmlData writeToFile:filePath atomically:YES];
}
else {
// NSLog(@"DailyData: Error creating Weights XML data: %@",error);
}
}
-(NSMutableArray *)makeSubjectsDictionaryArray; {
// make an array of subject dictionaries
// e.g., make a dictionary for each subject, add that to an array
NSMutableArray *subjects = [[NSMutableArray alloc] init];
NSUInteger ratIndex;
for (ratIndex =0; ratIndex < [theExperiment numberOfSubjects]; ratIndex++) {
[subjects addObject:[self dictionaryForSubjectIndex:ratIndex]];
}
return subjects;
}
-(NSMutableDictionary *)dictionaryForSubjectIndex:(NSUInteger)ratIndex; {
double itemOn, itemOff, itemDelta;
NSUInteger itemIndex;
NSMutableDictionary *subjectDictionary = [[NSMutableDictionary alloc] init];
[subjectDictionary setObject:[theExperiment nameOfSubjectAtIndex:ratIndex] forKey:@"name"];
for (itemIndex=0;itemIndex<[theExperiment numberOfItems ]; itemIndex++) {
if ([self getWeightsForRat:ratIndex
andItem:itemIndex
onWeight:&itemOn
offWeight:&itemOff
deltaWeight:&itemDelta]) {
NSString *itemName = [[theExperiment itemAtIndex:itemIndex] name];
[subjectDictionary setObject:[NSNumber numberWithDouble:itemOn] forKey:[itemName stringByAppendingString:@" On"]];
[subjectDictionary setObject:[NSNumber numberWithDouble:itemOff] forKey:[itemName stringByAppendingString:@" Off"]];
}
} // next item
return subjectDictionary;
}
-(void)unpackSubjectDictionary:(NSDictionary *)subjectDictionary; {
double itemOn, itemOff;
NSUInteger itemIndex, ratIndex;
// should be able to extract "name" then parse to get subject index
// then for each item in the experiment, try to get values at key: "itemName On" and "itemName Off"
// then set the weights for this item for this rat
NSString *subjectName = [subjectDictionary objectForKey:@"name"];
if (nil == subjectName) { return; }
ratIndex = [theExperiment subjectIndexFromLabel:subjectName];
if (NSNotFound == ratIndex ) {
// invalid rat number
return;
}
for (itemIndex=0;itemIndex<[theExperiment numberOfItems ]; itemIndex++) {
NSString *itemName = [[theExperiment itemAtIndex:itemIndex] name];
NSString *itemOnKey = [itemName stringByAppendingString:@" On"];
NSString *itemOffKey = [itemName stringByAppendingString:@" Off"];
NSNumber *onNumber = [subjectDictionary objectForKey:itemOnKey];
NSNumber *offNumber = [subjectDictionary objectForKey:itemOffKey];
itemOn = MISSINGWGT;
itemOff = MISSINGWGT;
if (nil != onNumber) { itemOn = [onNumber doubleValue];}
if (nil != offNumber) { itemOff = [offNumber doubleValue];}
[self setWeightsForRat:ratIndex andItem:itemIndex onWeight:itemOn offWeight:itemOff];
} // next item
}
-(NSString *)dateStringForFile:(NSDate *)myDate; {
// " yyyy-MM-DD HH:mm" -- make sure numbers < 10 are preceded by zero
// notice leading space, to make file name look pretty
NSString *dateString, *hourString;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *mdyComponents = [gregorian components:(NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear ) fromDate:myDate];
NSInteger day = [mdyComponents day];
NSInteger month = [mdyComponents month];
NSInteger year = [mdyComponents year];
NSInteger hour = [mdyComponents hour];
NSInteger minute = [mdyComponents minute];
if (hour < 10 && minute < 10) { hourString = [NSString stringWithFormat:@"0%ld0%ld",hour,minute]; }
else if (hour < 10 && minute >= 10) { hourString = [NSString stringWithFormat:@"0%ld%ld",hour,minute]; }
else if (hour >=10 && minute < 10) { hourString = [NSString stringWithFormat:@"%ld0%ld",hour,minute]; }
else if (hour >= 10 && minute >=10) { hourString = [NSString stringWithFormat:@"%ld%ld",hour,minute]; }
if (month < 10 && day < 10) { dateString = [NSString stringWithFormat:@" %ld-0%ld-0%ld %@", year, month, day, hourString];}
else if (month < 10 && day >= 10) { dateString = [NSString stringWithFormat:@" %ld-0%ld-%ld %@", year, month, day, hourString];}
else if (month >= 10 && day < 10) { dateString = [NSString stringWithFormat:@" %ld-%ld-0%ld %@", year, month, day, hourString];}
else if (month >= 10 && day >= 10) { dateString = [NSString stringWithFormat:@" %ld-%ld-%ld %@", year, month, day, hourString];}
return dateString;
}
-(BOOL)readOnWeights; {
// NSLog(@"DailyData: readOnWeights");
NSString *filename = [[theExperiment code] stringByAppendingPathExtension:@"onweights"];
NSString *dailyDataFilePath = [[theExperiment getExptDailyDataDirectoryPath] stringByAppendingPathComponent:filename];
// NOTE: make sure that onweights file exists; if it doesn't return NO
[self readFromPath:dailyDataFilePath];
return YES;
}
-(void)readFromPath:(NSString *)filePath; {
NSString *errorDesc = nil;
NSPropertyListFormat format;
// NSLog(@"DailyData: readFromPath");
// load the data in xml format from the file
NSData *xmlData = [[NSFileManager defaultManager] contentsAtPath:filePath];
// NSLog(@"DailyData: readFromPath loaded xmlData");
// unpack the xmlData into the rootDictionary
NSDictionary *rootDictionary = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:xmlData
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
if (!rootDictionary) {
// NSLog(@"DailyData: Error reading expt file xml: %@, format: %ld", errorDesc, format);
}
// NSLog(@"DailyData: DailyData readFromPath read rootdictionary");
if ([rootDictionary objectForKey:@"phase"]) { phaseName = [rootDictionary objectForKey:@"phase"]; }
NSNumber *phaseDayNumber = [rootDictionary objectForKey:@"phaseDayIndex"];
if (phaseDayNumber ) { phaseDayIndex = [phaseDayNumber intValue]; }
if ([rootDictionary objectForKey:@"comment"]) { comment = [rootDictionary objectForKey:@"comment"]; }
if ([rootDictionary objectForKey:@"UUID"]) { UUID = [rootDictionary objectForKey:@"UUID"]; }
// get the on and off date-time strings
// parse the time strings
// if timeString = @"na" or nil, then set our NSDate to nil
onTime = nil;
NSString *onTimeString = [rootDictionary objectForKey:@"weighedOn"];
[self setOnTime:[self dateFromTimeString:onTimeString]];
offTime = nil;
NSString *offTimeString = [rootDictionary objectForKey:@"weighedOff"];
[self setOffTime:[self dateFromTimeString:offTimeString]];
modificationDate = nil;
NSString *modificationDateString = [rootDictionary objectForKey:@"modified"];
if (nil != modificationDateString) { [self setModificationDate:[self dateFromTimeString:modificationDateString]]; }
// NSLog(@"DailyData: DailyData readFromPath about to unpackSubjectDictionaries ");
// subjects is an array of dictionarys that should be unpacked
NSArray *subjects = [rootDictionary objectForKey:@"SubjectsArray"];
for (NSDictionary *subjectDictionary in subjects) {
[self unpackSubjectDictionary:subjectDictionary];
}
}
// ***************************************************************************************
// ***************************************************************************************
// Preference related methods...
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
-(BOOL) getPreferenceForRat:(NSUInteger)ratIndex preference:(double *)preference total:(double *)total; {
// validate the experiment and subject index
if (theExperiment == nil) { return NO; }
if (ratIndex >= [theExperiment numberOfSubjects]) { return NO; }
// does the experiment even use preferences?
if (![theExperiment hasPreference]) { return NO; }
NSUInteger sacItem, waterItem;
double sacOn, sacOff, sacIntake, waterOn, waterOff, waterIntake;
[theExperiment getIndexOfPreferenceItem:(&sacItem) overItem:(&waterItem)];
if (![self getWeightsForRat:ratIndex andItem:sacItem onWeight:&sacOn offWeight:&sacOff deltaWeight:&sacIntake]) { return NO; }
if (![self getWeightsForRat:ratIndex andItem:waterItem onWeight:&waterOn offWeight:&waterOff deltaWeight:&waterIntake]) { return NO; }
// validate the intakes before trying to calculate a preference...
// don't report totals if some data is missing
if (sacIntake == MISSINGWGT || waterIntake == MISSINGWGT) { return NO; }
// don't report total intake of 0...
if (sacIntake + waterIntake == 0) { return NO; }
// allow negative intakes
// if (sacIntake < 0 || waterIntake < 0 ) { return NO; }
// avoid divide by zero
if (![theExperiment usePreferenceOverTotal] && waterIntake == 0) { return NO; }
// calculate total and preference
(*total) = sacIntake + waterIntake;
if ([theExperiment usePreferenceOverTotal]) {
(*preference) = sacIntake / (sacIntake + waterIntake);
}
else {
(*preference) = sacIntake / waterIntake;
}
return YES;
}
// ***************************************************************************************
// ***************************************************************************************
// Weight related methods....
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
-(void)setToOnWeights { [self setCurrentState: kWeighingOn]; }
-(void)setToOffWeights { [self setCurrentState: kWeighingOff]; }
#define WGT(x,y,z) ((x)[(unsigned long)(y*[theExperiment numberOfItems]+z)])
// WGT(<double *onWeight or double *offWeight>,<int ratIndex>,<int itemIndex>)
// index into the onWeight or offWeight arrays
-(void) initWeights {
NSUInteger r,i,numRats,numItems;
onWeight = offWeight = NULL;
if (theExperiment == nil) { return; }
numRats = [theExperiment numberOfSubjects];
numItems = [theExperiment numberOfItems];
onWeight = (double *)malloc(sizeof(double) * (size_t)numRats * (size_t)numItems);
offWeight = (double *)malloc(sizeof(double) * (size_t)numRats * (size_t)numItems);
if (onWeight != NULL && offWeight != NULL) {
for (r=0;r < numRats; r++) {
for (i=0;i<numItems;i++) {
WGT(onWeight,r,i) = MISSINGWGT;
WGT(offWeight,r,i) = MISSINGWGT;
}
}
}
}
-(BOOL) validateRatIndex:(NSUInteger)ratIndex andItemIndex:(NSUInteger)itemIndex {
// make sure we have an experiment, and that the rat and item indices are within range...
if (theExperiment == nil) { return NO; }
if (onWeight == NULL || offWeight == NULL) { return NO; }
if (ratIndex >= [theExperiment numberOfSubjects] || itemIndex >= [theExperiment numberOfItems]) { return NO; }
return YES;
}
-(BOOL) getWeightsForRat:(NSUInteger)ratIndex andItem:(NSUInteger)itemIndex onWeight:(double *)onwgt offWeight:(double *)offwgt deltaWeight:(double *)deltawgt {
// set default values...
(*onwgt) = MISSINGWGT;
(*offwgt) = MISSINGWGT;
(*deltawgt) = MISSINGWGT;
// make sure we have an experiment, and that the rat and item indices are within range...
if (![self validateRatIndex:ratIndex andItemIndex:itemIndex]) { return NO; }
// get the on and off weights out of the big arrays
(*onwgt) = WGT(onWeight,ratIndex,itemIndex);
(*offwgt) = WGT(offWeight,ratIndex,itemIndex);
// if we have valid values for weights, calculate the delta...
if ( [[theExperiment itemAtIndex:itemIndex] onOffType]) {
if ( (*onwgt) != MISSINGWGT && (*offwgt) != MISSINGWGT) { (*deltawgt) = (*onwgt) - (*offwgt); }
}
else {
// not an on/off weight, so no delta -- treat delta just as onwght...
if ( (*onwgt) != MISSINGWGT) { (*deltawgt) = (*onwgt); }
}
return YES;
}
-(BOOL) setWeightsForRat:(NSUInteger)ratIndex andItem:(NSUInteger)itemIndex onWeight:(double)onwgt offWeight:(double)offwgt {
// make sure we have an experiment, and that the rat and item indices are within range...
if (![self validateRatIndex:ratIndex andItemIndex:itemIndex]) { return NO; }
[self setOnWeightForRat:ratIndex andItem:itemIndex weight:onwgt];
[self setOffWeightForRat:ratIndex andItem:itemIndex weight:offwgt];
return YES;
}
-(BOOL) setOnWeightForRat:(NSUInteger)ratIndex andItem:(NSUInteger)itemIndex weight:(double)wgt {
// make sure we have an experiment, and that the rat and item indices are within range...
if (![self validateRatIndex:ratIndex andItemIndex:itemIndex]) { return NO; }
WGT(onWeight,ratIndex,itemIndex) = wgt;
return YES;
}
-(BOOL) setOffWeightForRat:(NSUInteger)ratIndex andItem:(NSUInteger)itemIndex weight:(double)wgt {
// make sure we have an experiment, and that the rat and item indices are within range...
if (![self validateRatIndex:ratIndex andItemIndex:itemIndex]) { return NO; }
WGT(offWeight,ratIndex,itemIndex) = wgt;
return YES;
}
-(void) clearOnWeights {
// set all the on weights to the MISSINGWGT value
// note that clearing the on weights also clears the off weights
NSUInteger r,i;
if (theExperiment == nil || onWeight == NULL || offWeight == NULL) { return; }
for (r = 0;r < [theExperiment numberOfSubjects];r++) {
for (i=0;i< [theExperiment numberOfItems];i++) {
WGT(onWeight,r,i) = MISSINGWGT;
WGT(offWeight,r,i) = MISSINGWGT;
}
}
}
-(void) clearOffWeights {
// set all the off weights to the MISSINGWGT value
NSUInteger r,i;
if (theExperiment == nil || offWeight == NULL) { return; }
for(r = 0;r < [theExperiment numberOfSubjects];r++) {
for (i=0;i< [theExperiment numberOfItems];i++) {
WGT(offWeight,r,i) = MISSINGWGT;
}
}
}
-(NSUInteger) numberOfUnweighedOnItems {
// count the number of items that have missingwgt for the on weights...
// returns 0 if there is some problem...
if (theExperiment == nil || onWeight == NULL || offWeight == NULL) { return 0; }
NSUInteger unweighed = 0;
NSUInteger r,i;
for(r = 0;r < [theExperiment numberOfSubjects];r++) {
for (i=0;i<[theExperiment numberOfItems];i++) {
if (WGT(onWeight,r,i) == MISSINGWGT) unweighed++;
}
}
return(unweighed);
}
-(NSUInteger) numberOfUnweighedOffItems {
// count the number of items that have missingwgt for the off weights...
// returns 0 if there is some problem...
if (theExperiment == nil || onWeight == NULL || offWeight == NULL) { return 0; }
NSUInteger unweighed = 0;
NSUInteger r,i;
for(r = 0;r < [theExperiment numberOfSubjects];r++) {
for (i=0;i<[theExperiment numberOfItems];i++) {
if (WGT(offWeight,r,i) == MISSINGWGT) unweighed++;
}
}
return(unweighed);
}
-(NSUInteger) numberOfUnweighedItems {
// return unweigh OnItems or OffItems, depending on current state
switch (currentState) {
case kWeighingOn:
return [self numberOfUnweighedOnItems];
break;
case kWeighingOff:
return [self numberOfUnweighedOffItems];
break;
case kDisplayOnly:
return 0;
break;
}
return 0;
}
-(void) carryOverOffWeightsToOnWeights {}
//NOTE this will require referencing the last daily data, on disk?
// ***************************************************************************************
// ***************************************************************************************
///--------------------------------------------------------------------------------------------------
/// Time Methods
///--------------------------------------------------------------------------------------------------
/** return weighed on time as a string
@return NSString with the weighed on time in (weekday yyyy-MM-DD HH:MM) format for display
*/
-(NSString *)onTimeString; {
if (onTime) {
// NSDateFormatter *dateFormatter =
// [[[NSDateFormatter alloc] init] autorelease];
// [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
// [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:kDateWithDayFormatString];
return [dateFormatter stringFromDate:onTime];
}
return @"pending";
}
/** return weighed off time as a string
@return NSString with the weighed off time in (weekday yyyy-MM-DD HH:MM) format for display
*/
-(NSString *)offTimeString; {
if (offTime) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:kDateWithDayFormatString];
return [dateFormatter stringFromDate:offTime];
}
return @"pending";
}
/** return weighed ON time as a date description for storage
@return NSString with the weighed on time in (yyyy-MM-DD HH:mm:SS ±Z) format, or "na" if not defined yet
*/
-(NSString *)onTimeDescription; {
if (onTime) {
return [onTime description];
}
return @"na";
}
-(NSString *)modificationDateDescription; {
if (modificationDate) {
return [modificationDate description];
}
return @"na";
}
/** return weighed OFF time as a date description for storage
@return NSString with the weighed off time in (yyyy-MM-DD HH:mm:SS ±Z) format, or "na" if not defined yet
*/
-(NSString *)offTimeDescription; {
if (offTime) {
return [offTime description];
}
return @"na";
}
/** takes a international format time string, converts it to an NSDate
if string is nil or "na", then NSDate is set to nil
@param NSString with the time in international string representation format (yyyy-MM-DD HH:MM:SS ±HHMM)
*/
-(NSDate *)dateFromTimeString:(NSString *)timeString; {
if (nil == timeString) { return nil; }
if ([timeString isEqualToString:@"na"]) { return nil; }
return [NSDate dateWithString:timeString];
}
-(NSString *)shortTimeStringFromDate:(NSDate *)date; {
if (date) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:kDateWithDayFormatString];
return [dateFormatter stringFromDate:date];
}
return @"pending";
}
// ***************************************************************************************
// ***************************************************************************************
///--------------------------------------------------------------------------------------------------
/// Text Methods
///--------------------------------------------------------------------------------------------------
-(NSString *) getTextForItem:(NSString *)itemName atTerm:(NSUInteger)itemTerm forRat:(NSUInteger)ratIndex {
double onwgt,offwgt, deltawgt;
NSUInteger itemIndex = 0;
if (itemTerm == kWeightUnknownTerm) {
return kNoDataCellText;
}
// NOTE convert itemName into an itemIndex
itemIndex = [theExperiment itemIndexFromName:itemName];
//validate the experiment and the rat and item indices
if (![self validateRatIndex:ratIndex andItemIndex:itemIndex]) {
return kNoDataCellText;
}
if (![self getWeightsForRat:ratIndex andItem:itemIndex onWeight:&onwgt offWeight:&offwgt deltaWeight:&deltawgt]) {
return kNoDataCellText;
}
switch (itemTerm) {
case kWeightOn:
return [self weightAsNSString:onwgt];
break;
case kWeightOff:
return [self weightAsNSString:offwgt];
break;
case kWeightDelta:
return [self weightAsNSString:deltawgt];
break;
}
// fall through to default...
return kNoDataCellText;