1 This file contains the code
for the subroutines
in
2 Perl
-speaks
-NONMEMs data module
. It is
not functional by itself
.
3 The code should be transferred to the module autogenerated by dia2code
4 using the fill_diacode
.pl script
.
6 TODO
: Fix the synced attribute so that all methods that change the
state of the
7 data object sets synced
=0
11 start include statements
12 use Digest
::MD5
'md5_hex';
24 use Time
::HiRes
qw(gettimeofday);
25 my @primary_column_names = ('ID', 'DATE', 'DAT1', 'DAT2', 'DAT3' ,'L1', 'L2', 'DV', 'MDV', 'RAW_', 'MRG_', 'RPT_', 'TIME', 'DROP', 'SKIP', 'EVID', 'AMT', 'RATE', 'SS', 'II', 'ADDL', 'CMT', 'PCMT', 'CALL');
28 # }}} include statements
33 # The structure of the data class is subject-centric, recognising that
34 # the subjects included in a study often can be regarded as
35 # independent. A class for the subject level exists within PsN and is
36 # called the individual class. A data object consists of at least one
37 # but probably many individual objects plus optional comments.
47 # my $data_obj = data -> new ( filename => 'test040314.dta' );
49 # $data_obj -> renumber_ascending;
51 # my $subsets_ref = $data_obj -> case_deletion( bins => 10 );
53 # my @subsets = @{$subsets_ref};
63 # <a HREF="model.html">model</a>, <a HREF="output.html">output</a>,
64 # <a HREF="tool/modelfit.html">tool::modelfit</a>,
65 # <a HREF="tool.html">tool</a>
71 # model, output, tool::modelfit, tool
82 # If the column holding the subject identifier is not the
83 # first, it can be specified using the I<idcolumn> attribute
85 # I<ignoresign> determines which rows that are regarded as
86 # comments. Corresponds to the IGNORE= option in the $DATA
87 # record in a NONMEM model file.
89 $this -> {'use_data_table'} = 0;
91 ( $this -> {'directory'},
92 $this -> {'filename'} ) = OSspecific
::absolute_path
( $this -> {'directory'},
93 $this->{'filename'} );
95 debug
-> warn( level
=> 2,
96 message
=> "data -> new: Data object initialized from file: ".
99 # sub register_in_database {
101 # # Backslashes messes up the sql syntax
102 # my $file_str = $this->{'filename'};
103 # my $dir_str = $this->{'directory'};
104 # $file_str =~ s/\\/\//g;
105 # $dir_str =~ s/\\/\//g;
108 # my $md5sum = md5_hex(OSspecific::slurp_file($this-> full_name ));
110 # connect("DBI:mysql:host=".$PsN::config -> {'_'} -> {'database_server'}.
111 # ";databse=".$PsN::config -> {'_'} -> {'project'},
112 # $PsN::config -> {'_'} -> {'user'},
113 # $PsN::config -> {'_'} -> {'password'},
114 # {'RaiseError' => 1});
116 # my $sth = $dbh -> prepare( "SELECT data_id FROM ".$PsN::config -> {'_'} -> {'project'}.
118 # "WHERE filename = '$file_str' AND ".
119 # "directory = '$dir_str' AND ".
120 # "md5sum = '".$md5sum."'" );
121 # $sth -> execute or debug -> die( message => $sth->errstr ) ;
122 # my $select_arr = $sth -> fetchall_arrayref;
123 # if ( scalar @{$select_arr} > 0 ) {
124 # debug -> warn( level => 1,
125 # message => "Found an old entry in the database matching the ".
126 # "current data file" );
127 # if ( scalar @{$select_arr} > 1 ) {
128 # debug -> warn( level => 1,
129 # message => "Found more than one matching entry in database".
130 # ", using the first" );
132 # $this -> {'data_id'} = $select_arr->[0][0];
134 # my ( $date_str, $time_str );
135 # if ( $Config{osname} eq 'MSWin32' ) {
136 # $date_str = `date /T`;
137 # $time_str = ' '.`time /T`;
140 # $date_str = `date`;
144 # my $date_time = $date_str.$time_str;
145 # $sth = $dbh -> prepare("INSERT INTO ".$PsN::config -> {'_'} -> {'project'}.
146 # ".data (filename,date,directory,md5sum) ".
147 # "VALUES ('$file_str', '$date_time', '$dir_str','".
150 # $this -> {'data_id'} = $sth->{'mysql_insertid'};
153 # $dbh -> disconnect;
157 unless ( ( defined $this -> {'header'} and
158 scalar @
{$this -> {'header'}} > 0 ) or
159 ( defined $this -> {'individuals'} and
160 scalar @
{$this -> {'individuals'}} > 0 ) ) {
161 if ( -e
$this -> full_name
) {
162 if ( $this -> {'target'} eq 'mem' ) {
163 # ®ister_in_database( $this ) if ( $PsN::config -> {'_'} -> {'use_database'} and
164 # $this -> {'use_data_table'} );
165 $this -> _read_header
;
166 $this -> _read_individuals
;
167 $this -> {'synced'} = 1;
169 $this -> {'synced'} = 0;
172 debug
-> die(message
=> "No header, individuals, and no file " . $this -> full_name
. " on disk.")
173 unless $this -> {'ignore_missing_files'};
174 $this -> {'synced'} = 0;
177 if ( $this -> {'target'} eq 'mem') {
178 if ( -e
$this -> {'filename'} ) {
179 $this -> _read_header
;
180 # ®ister_in_database if ( $PsN::config -> {'_'} -> {'use_database'} and
181 # $this -> {'use_data_table'} );
182 $this -> _read_individuals
;
183 $this -> {'synced'} = 1;
185 debug
-> die(message
=> "No file:".$this->{'filename'}." on disk" )
186 unless $this -> {'ignore_missing_files'};
187 $this -> {'synced'} = 0;
194 if ( $this -> {'synced'} ) {
196 foreach my $head ( @
{$this -> {'header'}} ) {
197 $this -> {'column_head_indices'} -> {$head} = $i;
201 # $Data::Dumper::Maxdepth = 3;
202 # die Dumper $this -> {'individuals'};
208 # {{{ register_in_database
209 start register_in_database
210 if ( $PsN::config
-> {'_'} -> {'use_database'} ) {
211 # Backslashes messes up the sql syntax
212 my $file_str = $self->{'filename'};
213 my $dir_str = $self->{'directory'};
214 $file_str =~ s/\\/\//g
;
215 $dir_str =~ s/\\/\//g
;
217 my $project = $PsN::config
-> {'_'} -> {'project'};
219 my $md5sum = md5_hex
(OSspecific
::slurp_file
($self-> full_name
));
221 my $dbh = DBI
-> connect("DBI:mysql:host=".$PsN::config
-> {'_'} -> {'database_server'}.
222 ";databse=".$project,
223 $PsN::config
-> {'_'} -> {'user'},
224 $PsN::config
-> {'_'} -> {'password'},
225 {'RaiseError' => 1});
232 my $sth = $dbh -> prepare
( "SELECT data_id FROM ".$project.
234 "WHERE filename = '$file_str' AND ".
235 "directory = '$dir_str' AND ".
236 "md5sum = '".$md5sum."'" );
237 $sth -> execute
or debug
-> die( message
=> $sth->errstr ) ;
238 $select_arr = $sth -> fetchall_arrayref
;
241 if ( scalar @
{$select_arr} > 0 ) {
242 'debug' -> warn( level
=> 1,
243 message
=> "Found an old entry in the database matching the ".
244 "current data file" );
245 if ( scalar @
{$select_arr} > 1 ) {
246 'debug' -> warn( level
=> 1,
247 message
=> "Found more than one data matching entry in database".
248 ", using the first" );
250 $self -> {'data_id'} = $select_arr->[0][0];
252 my $sth = $dbh -> prepare
( "SELECT individual_id FROM ".$project.".data_individual ".
253 "WHERE data_id = '".$self -> {'data_id'}."'" );
254 $sth -> execute
or debug
-> die( message
=> $sth->errstr ) ;
255 my $id_arr = $sth -> fetchall_arrayref
;
256 map( $_ = $_ -> [0], @
{$id_arr} );
257 $self -> {'individual_ids'} = $id_arr;
258 } elsif ( defined $self -> {'individuals'} ) {
259 my ( $date_str, $time_str );
260 if( $Config{osname
} eq 'MSWin32' ){
261 $date_str = `date /T`;
262 $time_str = ' '.`time /T`;
269 my $date_time = $date_str.$time_str;
270 my ( $columns, $values );
271 my $res_str = $resampled ?
'1' : '0';
272 if ( defined $model_id ) {
273 $columns = '(model_id, filename, date, directory, md5sum, resampled)';
274 $values = "('$model_id', '$file_str', '$date_time', '$dir_str','".
275 $md5sum."', '$res_str' )";
277 $columns = '(filename, date, directory, md5sum, resampled)';
278 $values = "('$file_str', '$date_time', '$dir_str','".$md5sum."', '$res_str' )";
280 $sth = $dbh -> prepare
("INSERT INTO ".$PsN::config
-> {'_'} -> {'project'}.
281 ".data $columns VALUES $values");
283 $self -> {'data_id'} = $sth->{'mysql_insertid'};
285 if ( defined $self -> {'data_id'} ) {
287 my $columns = "( id_key, id )";
288 if( $#individual_ids >= 0 ) {
289 $self -> register_di_relation
( individual_ids
=> \
@individual_ids );
291 my $inds = scalar @
{$self -> {'individuals'}};
292 $dbh -> do( "LOCK TABLES ".$PsN::config
-> {'_'} -> {'project'}.
293 ".individual WRITE" );
294 # $sth = $dbh -> prepare( "SELECT MAX(individual_id)".
295 # " FROM ".$PsN::config -> {'_'} -> {'project'}.
297 $dbh -> do( 'USE '.$PsN::config
-> {'_'} -> {'project'} );
298 $sth = $dbh -> prepare
( "SHOW TABLE STATUS LIKE 'individual'" );
299 $sth -> execute
or debug
-> die( message
=> $sth->errstr ) ;
300 my $select_arr = $sth -> fetchall_arrayref
;
301 my $first_id_id = $select_arr -> [0][10] ?
302 $select_arr -> [0][10] : 0;
303 # my $first_id_id = $select_arr -> [0][0] ? ($select_arr -> [0][0] + 1) : 0;
304 my $last_id_id = $first_id_id + $inds - 1;
305 for( my $i = 0; $i < $inds; $i++ ) {
306 if( defined $self -> {'individuals'}[$i] ) {
307 my $id_id = $self -> {'individuals'}[$i] -> idnumber
;
308 $values = $values."," if ( defined $values );
309 $values = $values."( $i, $id_id )";
312 $sth = $dbh -> prepare
( "INSERT INTO ".$PsN::config
-> {'_'} -> {'project'}.
313 ".individual $columns VALUES $values" );
315 $dbh -> do( "UNLOCK TABLES" );
316 @individual_ids = ($first_id_id .. $last_id_id);
317 $self -> register_di_relation
( individual_ids
=> \
@individual_ids );
319 $self -> {'individual_ids'} = \
@individual_ids;
324 $data_id = $self -> {'data_id'}; # return the data_id
326 end register_in_database
327 # }}} register_in_database
329 # {{{ register_di_relation
330 start register_di_relation
331 if ( $PsN::config
-> {'_'} -> {'use_database'} and
332 defined $self -> {'data_id'} and $#individual_ids >= 0 ) {
333 my $dbh = DBI
-> connect("DBI:mysql:host=".$PsN::config
-> {'_'} -> {'database_server'}.
334 ";databse=".$PsN::config
-> {'_'} -> {'project'},
335 $PsN::config
-> {'_'} -> {'user'},
336 $PsN::config
-> {'_'} -> {'password'},
337 {'raiseerror' => 1});
340 my $columns = "( data_id, individual_id )";
341 foreach my $individual_id ( @individual_ids ) {
342 if ( defined $individual_id ) {
343 $values = $values."," if ( defined $values );
344 $values = $values."(".$self -> {'data_id'}.", $individual_id )";
347 $sth = $dbh -> prepare
( "INSERT INTO ".$PsN::config
-> {'_'} -> {'project'}.
348 ".data_individual $columns VALUES $values" );
350 $sth -> finish
if ( defined $sth );
353 end register_di_relation
354 # }}} register_di_relation
360 $full_name = $self -> {'directory'} . $self -> {'filename'};
370 # The bootstrap method draws I<samples> number of boostrap
371 # samples from the data set. The I<subjects> arguments
372 # determines the size of each sample (default equals to the
373 # number of individuals in the original data set). The method
374 # returns references to three arrays: I<boot_samples_ref>,
375 # which holds the bootstrap data sets, I<incl_individuals_ref>
376 # which holds arrays containing the subject identifiers (ID's)
377 # for the included individuals of each bootstrap data set and
378 # I<included_keys_ref> which holds the key or index of the
379 # included individuals. The key or index is an integer
380 # starting at 1 for the first individual in the original data
381 # set and increasing by one for each following.
382 $self -> synchronize
;
383 my @header = @
{$self -> {'header'}};
384 my $individuals = $self -> {'individuals'};
387 my $status_bar = status_bar
-> new
( steps
=> $samples );
388 ui
-> print( category
=> 'bootstrap',
389 message
=> $status_bar -> print_step
,
392 for ( my $i = 1; $i <= $samples; $i++ ) {
393 my $new_name = defined $name_stub ?
$name_stub."_$i.dta" : "bs$i.dta";
394 $new_name = $directory.'/'.$new_name;
395 my ( $boot, $incl_ind_ref, $incl_key_ref ) =
396 $self -> resample
( subjects
=> $subjects,
398 new_name
=> $new_name,
400 stratify_on
=> $stratify_on,
401 model_id
=> $model_ids[$i-1] );
402 push( @included_keys, $incl_key_ref );
403 push( @incl_individuals, $incl_ind_ref );
404 # $boot -> renumber_ascending;
405 push( @boot_samples, $boot );
406 # $boot -> synchronize;
408 if( $status_bar -> tick
() ){
409 ui
-> print( category
=> 'bootstrap',
410 message
=> $status_bar -> print_step
,
415 ui
-> print( category
=> 'bootstrap',
416 message
=> ' ... done' );
426 $self -> synchronize
;
427 my ( @header, $individuals, @bs_inds, $key_ref, @id_ids, @bs_id_ids );
428 @id_ids = @
{$self -> {'individual_ids'}} if( defined $self -> {'individual_ids'} );
429 if ( defined $stratify_on ) {
430 unless ( $resume and -e
$new_name ) {
431 @header = @
{$self -> {'header'}};
432 $individuals = $self -> {'individuals'};
434 if( $stratify_on =~ /\D/ ){
435 %strata = %{$self -> factors
( column_head
=> $stratify_on )};
436 if ( $strata{'Non-unique values found'} eq '1' ) {
437 debug
-> die( message
=> "Individuals were found to have multiple values in the $stratify_on column. ".
438 "The column $stratify_on cannot be used for stratification of the resampling." );
441 %strata = %{$self -> factors
( column
=> $stratify_on )};
442 if ( $strata{'Non-unique values found'} eq '1' ) {
443 debug
-> die( message
=> "Individuals were found to have multiple values in column number $stratify_on. ".
444 "Column $stratify_on cannot be used for stratification of the resampling." );
448 while( my ( $factor, $key_list ) = each %strata ) {
449 my $keys = scalar @
{$key_list};
450 for ( my $i = 0; $i < $keys; $i++ ) {
451 my $list_ref = random_uniform_integer
(1,0,$keys-1);
452 push( @bs_inds, $individuals ->
453 [ $key_list -> [$list_ref] ] -> copy
);
454 push( @included_keys, $key_list -> [$list_ref] );
455 push( @incl_individuals, $individuals ->
456 [ $key_list -> [$list_ref] ] -> idnumber
);
457 push( @bs_id_ids, $id_ids[ $key_list -> [$list_ref] ] );
461 $boot = data
-> new
( header
=> \
@header,
462 idcolumn
=> $self -> {'idcolumn'},
463 ignoresign
=> $self -> {'ignoresign'},
464 individuals
=> \
@bs_inds,
465 filename
=> $new_name,
466 ignore_missing_files
=> 1,
468 $boot -> renumber_ascending
;
471 #$boot -> target( $target );
474 unless ( $resume and -e
$new_name ) {
475 @header = @
{$self -> {'header'}};
476 $individuals = $self -> {'individuals'};
477 for ( my $i = 1; $i <= $subjects; $i++ ) {
478 $key_ref = random_uniform_integer
(1,0,scalar @
{$individuals}-1);
479 push( @bs_inds, $individuals -> [ $key_ref ] -> copy
);
480 push( @included_keys, $key_ref );
481 push( @incl_individuals, $individuals -> [ $key_ref ] -> idnumber
);
482 push( @bs_id_ids, $id_ids[ $key_ref ] );
485 # MUST FIX: If a file already exists with the same name,
486 # the created bs data set will be appended to this. IT
487 # MUST BE OVERWRITTEN!
488 $boot = data
-> new
( header
=> \
@header,
489 idcolumn
=> $self -> {'idcolumn'},
490 ignoresign
=> $self -> {'ignoresign'},
491 individuals
=> \
@bs_inds,
492 filename
=> $new_name,
493 ignore_missing_files
=> 1,
495 $boot -> renumber_ascending
;
497 $boot -> target
( $target );
499 # If we are resuming, we still need to generate the
500 # pseudo-random sequence and initiate a data object
501 for ( my $i = 1; $i <= $subjects; $i++ ) {
502 random_uniform_integer
(1,0,scalar @
{$individuals}-1)
504 $boot = data
-> new
( idcolumn
=> $self -> {'idcolumn'},
505 ignoresign
=> $self -> {'ignoresign'},
506 filename
=> $new_name,
507 ignore_missing_files
=> 1,
512 if( $target eq 'disk'){
516 $boot -> register_in_database
( individual_ids
=> \
@bs_id_ids,
518 model_id
=> $model_id );
528 # case_deletion creates subsets of the data. The number of
529 # subsets is specified by the bins argument. The individuals
530 # of each subset is selected randomly or in ascending
531 # numerical order depending on the selection argument that can
532 # be either 'consecutive' or 'random'. case_column must be
533 # specified to give the method something to base the selection
534 # on. Valid case_column values are either the column number
535 # (pure digits) or the name of the column in the (optional)
537 $self -> synchronize
;
538 my @header = @
{$self -> {'header'}};
539 if ( not defined $case_column ) {
540 debug
-> die( message
=> "case_column must be specified" );
542 if ( not $case_column =~ /^\d/ ) {
543 for ( my $i = 0; $i <= $#header; $i++ ) {
544 $case_column = $i+1 if ( $header[$i] eq $case_column );
548 $bins = defined $bins ?
$bins :
549 scalar keys %{$self -> factors
( column
=> $case_column)};
550 my %factors = %{$self -> factors
( column
=> $case_column )};
551 if ( $factors{'Non-unique values found'} eq '1' ) {
552 debug
-> die( message
=> "Individuals were found to have multiple values in column number $case_column. ".
553 "Column $case_column cannot be used for case deletion." );
556 my $maxbins = scalar keys %factors;
557 my @ftrs = sort { $a <=> $b } keys %factors;
558 my $individuals = $self -> {'individuals'};
559 my $maxkey = scalar @
{$individuals} - 1;
561 my ( @tmp_ftrs, @binsize ) =
563 my ( $k, $j, $i ) = ( 0, 0, 0 );
564 # Create the binsizes
565 for ( $j = 0; $j < $maxbins; $j++ ) {
567 $k = 0 if( $k >= $bins );
569 $self -> _fisher_yates_shuffle
( array
=> \
@ftrs ) if( $selection eq 'random' );
570 for ( $k = 0; $k < $bins; $k++ ) {
571 for ( $j = 0; $j < $binsize[ $k ]; $j++ ) {
572 # print "SK: ",$skipped_keys[ $k ]," F: ",$factors{ $ftrs[ $i ] },"\n";
573 push( @
{$skipped_keys[ $k ]}, @
{$factors{ $ftrs[ $i ] }} );
574 push( @
{$skipped_values[ $k ]}, $ftrs[ $i++ ] );
578 for ( $k = 0; $k < $bins; $k++ ) {
581 SELKEYS
: foreach my $key ( 0..$maxkey ) {
582 foreach my $skipped ( @
{$skipped_keys[ $k ]} ) {
583 if ( $key == $skipped ) {
584 push( @
{$skipped_ids[ $k ]}, $individuals ->
585 [ $skipped ] -> idnumber
);
586 push( @del_inds, $individuals -> [ $key ] -> copy
);
590 push( @cd_inds, $individuals -> [ $key ] -> copy
);
592 # Set ignore_missing_files = 1 to make it possible to get the result
594 my $newdata = data
->
595 new
( header
=> \
@header,
596 ignoresign
=> $self -> {'ignoresign'},
597 idcolumn
=> $self -> {'idcolumn'},
598 individuals
=> \
@cd_inds,
600 filename
=> 'cd'.$k+1 .'.dta',
601 ignore_missing_files
=> 1 );
602 my $deldata = data
->
603 new
( header
=> \
@header,
604 ignoresign
=> $self -> {'ignoresign'},
605 idcolumn
=> $self -> {'idcolumn'},
606 individuals
=> \
@del_inds,
608 filename
=> 'del'.$k+1 .'.dta',
609 ignore_missing_files
=> 1 );
610 push( @subsets, $newdata );
611 push( @remainders, $deldata );
621 # filename: new data file name.
623 # target: keep the copy in memory ('mem') or write it to disk and flush the memory ('disk').
625 ($directory, $filename) = OSspecific
::absolute_path
( $directory, $filename );
627 # Clone self into new data object. Why don't the individuals get cloned too?
628 # strange. need to set synced to 0 AND set the {'individuals'} to undef.
629 cp
($self -> full_name
, $directory.$filename );
630 $new_data = Storable
::dclone
( $self );
631 $new_data -> {'synced'} = 0;
632 $new_data -> {'individuals'} = undef;
633 $new_data -> synchronize
;
635 # Set the new file name for the copy
636 $new_data -> directory
( $directory );
637 $new_data -> filename
( $filename );
647 # Returns the number of individuals in the data set.
648 $self -> synchronize
;
649 $num = scalar @
{$self -> {'individuals'}};
658 $self -> synchronize
;
660 my $first_id = $self -> {'individuals'}[0];
662 debug
-> die( message
=> "No individuals defined in data object based on ".
663 $self -> full_name
) unless ( defined $first_id );
665 # Check if $column(-index) is defined and valid, else try to find index
668 my @data_row = split( /,/, $first_id -> subject_data
-> [0] );
669 if( $#columns >= 0 ) {
670 foreach my $column ( @columns ) {
671 unless ( defined $column && defined( $data_row[$column-1] ) ) {
672 debug
-> die( message
=> "Error in data -> factors: ".
673 "invalid column number: \"$column\"\n".
674 "Valid column numbers are 1 to ".
675 scalar @
{$first_id -> subject_data
->[0]}."\n" );
678 } elsif ( $#column_heads >= 0 ) {
679 foreach my $column_head ( @column_heads ) {
680 unless (defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})) {
681 debug
-> die( message
=> "Error in data -> factors: unknown column: \"$column_head\" ".
682 "Valid column headers are (in no particular order):\n".
683 join(', ',keys(%{$self -> {'column_head_indices'}})) );
685 my $column = $self -> {'column_head_indices'}{$column_head};
686 push( @columns, $column );
687 debug
-> warn( level
=> 2,
688 message
=> "$column_head is in column number $column" );
692 debug
-> die( message
=> "No column or column_head defined" );
695 if( $global_largest or $global_smallest or
696 $largest_per_individual or $smallest_per_individual ) {
697 if( not scalar @
{$self -> {'individuals'}} == scalar @
{$against_data -> individuals
} ) {
698 debug
-> die( message
=> "Both data object must hold the same number of individuals ".
699 "and observations when calling data -> diff" );
701 for( my $i = 0; $i < scalar @
{$self -> {'individuals'}}; $i++ ) {
702 my %id_diffs = %{$self -> {'individuals'}[$i] ->
703 diff
( against_individual
=> $against_data -> individuals
-> [$i],
704 columns
=> \
@columns,
705 absolute_diff
=> $absolute_diff,
706 diff_as_fraction
=> $diff_as_fraction,
707 largest
=> ( $global_largest or $largest_per_individual ),
708 smallest
=> ( $global_smallest or $smallest_per_individual ) )};
709 if( $global_largest ) {
710 for( my $j = 0; $j <= $#columns; $j++ ) {
711 my $label = defined $column_heads[$j] ?
$column_heads[$j] : $columns[$j];
712 if( not defined $diff_results{$label} or not defined $diff_results{$label}{'diff'} or
713 $id_diffs{$columns[$j]}{'diff'} > $diff_results{$label}{'diff'} ) {
714 $diff_results{$label}{'diff'} = $id_diffs{$columns[$j]}{'diff'};
715 $diff_results{$label}{'self'} = $id_diffs{$columns[$j]}{'self'};
716 $diff_results{$label}{'test'} = $id_diffs{$columns[$j]}{'test'};
722 die "data -> diff is only implemented for finding the largest difference at any observation at this point\n";
731 if ( defined $parm and $parm ne $self -> {'filename'} ) {
732 $self -> {'filename'} = $parm;
733 $self -> {'data_id'} = undef;
744 my %factors = $self -> factors
( 'return_occurences' => 1,
745 'unique_in_individual' => $unique_in_individual,
746 'column_head' => $column_head,
747 'column' => $column);
750 while (my ($factor, $amount) = each %factors) {
751 if ( $factor == $self -> {'missing_data'} && $ignore_missing ) {
757 while (my ($factor, $amount) = each %factors) {
758 if ( $factor == $self -> {'missing_data'} && $ignore_missing ) {
761 $fractions{$factor} = $amount/$sum;
773 # Either column (number, starting at 1) or column_head must be specified.
775 # The default behaviour is to return a hash with the factors as keys
776 # and as values references to arrays with the order numbers (not the ID numbers)
777 # of the individuals that contain this factor
779 # If unique_in_individual is true (1), the returned hash will contain
780 # an element with key 'Non-unique values found' and value 1 if any
781 # individual contain more than one value in the specified column.
783 # Return occurences will calculate the occurence of each
784 # factor value. Several occurences in one individual counts as
785 # one occurence. The elements of the returned hash will have the factors
786 # as keys and the number of occurences as values.
789 $self -> synchronize
;
791 # Check if $column(-index) is defined and valid, else try to find index
793 my $first_id = $self -> {'individuals'}[0];
795 debug
-> die( message
=> "No individuals defined in data object based on ".
796 $self -> full_name
) unless ( defined $first_id );
798 my @data_row = split( /,/, $first_id -> subject_data
-> [0] );
799 unless ( defined $column && defined( $data_row[$column-1] ) ) {
800 unless (defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})) {
801 debug
-> die( message
=> "Error in data -> factors: unknown column: \"$column_head\" ".
802 "or invalid column number: \"$column\".\n".
803 "Valid column numbers are 1 to ".scalar @data_row ."\n".
804 "Valid column headers are (in no particular order):\n".
805 join(', ',keys(%{$self -> {'column_head_indices'}})) );
807 $column = $self -> {'column_head_indices'}{$column_head};
808 debug
-> warn( level
=> 2,
809 message
=> "$column_head is in column number $column" );
814 foreach my $individual ( @
{$self -> {'individuals'}} ) {
815 my @ifactors = keys %{$individual -> factors
( column
=> $column )};
816 if ( scalar @ifactors > 1 and $unique_in_individual ) {
817 %factors = ( 'Non-unique values found' => 1 );
820 debug
-> die( message
=> "No value found in column $column in individual ".
821 $individual -> idnumber
) if ( scalar @ifactors == 0 );
823 # Return occurences will calculate the occurence of each
824 # factor value. Several occurences in one individual counts as
827 if ( $return_occurences ) {
828 foreach my $ifactor ( @ifactors ) {
829 $factors{$ifactor}++;
832 foreach my $ifactor ( @ifactors ) {
833 push( @
{$factors{$ifactor}}, $key );
843 # {{{ find_individual
845 # start find_individual
846 # foreach my $tmp_ind ( @{$self -> individuals} ) {
847 # if ( $tmp_ind -> key == $key ) {
848 # $individual = $tmp_ind;
852 # if ( defined $individual ) {
854 # $individual = $individual -> copy;
857 # print "No individual with key $key found in call to ".
858 # "data -> find_individual\n" if ( $self -> debug );
860 # end find_individual
868 my $header = $self -> {'header'};
870 # format the data for NONMEM (simple comma-separated layout)
871 if ( defined $self -> {'comment'} ) {
872 my @comment = @
{$self -> {'comment'}};
878 my $wrap = ( defined $self -> {'wrap_column'} and
879 defined $self -> {'cont_column'} );
881 my @primary_columns = defined $self -> {'primary_columns'} ?
882 @
{$self -> {'primary_columns'}} : ();
883 my @secondary_columns = defined $self -> {'secondary_columns'} ?
884 @
{$self -> {'secondary_columns'}} : ();
885 if ( defined $header and defined $self -> {'ignoresign'} ) {
887 if ( $self -> {'ignoresign'} ne '@' ) {
888 $istr = $self -> {'ignoresign'};
892 for ( my $i = 0; $i <= $#secondary_columns ; $i++ ) {
894 for ( my $j = 0; $j < scalar @
{$secondary_columns[$i]} ; $j++ ) {
895 my $jstr = $j == 0 ?
'' : ',';
896 $sstr = $sstr.$jstr.$secondary_columns[$i][$j][0];
898 push( @h_data, $sstr."\n" );
900 push( @form_data, @h_data );
902 for ( my $i = 0; $i <= $#primary_columns ; $i++ ) {
903 my $jstr = $i == 0 ?
'' : ',';
904 $pstr = $pstr.$jstr.$primary_columns[$i][0];
906 push( @form_data, $pstr."\n" );
908 push( @form_data, $istr.join(',',@
{$self -> {'header'}})."\n" );
912 foreach my $individual ( @
{$self -> {'individuals'}} ) {
913 foreach my $row ( @
{$individual -> subject_data
} ) {
915 for ( my $i = 0; $i <= $#secondary_columns ; $i++ ) {
917 for ( my $j = 0; $j < scalar @
{$secondary_columns[$i]} ; $j++ ) {
918 my $jstr = $j == 0 ?
'' : ',';
919 if ( $secondary_columns[$i][$j][0] eq 'CONT' ) {
920 $sstr = $sstr.$jstr.'1';
922 my @data_row = split( /,/, $row );
923 $sstr = $sstr.$jstr.$data_row[$secondary_columns[$i][$j][1]];
926 push( @r_data, $sstr."\n" );
928 push( @form_data, @r_data );
930 for ( my $i = 0; $i <= $#primary_columns ; $i++ ) {
931 my $jstr = $i == 0 ?
'' : ',';
932 if ( $primary_columns[$i][0] eq 'CONT' ) {
933 $pstr = $pstr.$jstr.'0';
935 my @data_row = split( /,/, $row );
936 $pstr = $pstr.$jstr.$data_row[$primary_columns[$i][1]];
939 push( @form_data, $pstr."\n" );
943 foreach my $individual ( @
{$self -> {'individuals'}} ) {
944 foreach my $row ( @
{$individual -> subject_data
} ) {
945 push( @form_data, $row ."\n" );
958 # This method removes columns that has '=DROP' value in the
959 # model header as given by $INPUT. The model header must be
960 # transfered to this method through the model_header
961 # argument. The model_header argument should be a
962 # two-dimensional array where each position in the first
963 # dimension should be a reference to a 1*2 array holding the
964 # column name and value. Any ignore-sign must be removed.
966 debug
-> die( message
=> 'model header must be defined' )
967 if ( $#model_header < 0 );
968 # Important that the drop_dropped method of the model::problem
969 # class is in sync with this method.
970 $self -> synchronize
;
972 $self -> {'header'} = [];
975 for( my $i = 0; $i <= $#model_header; $i++ ) {
976 $self -> {'idcolumn'} = $counter if ( $model_header[$i][0] eq 'ID' );
977 if( $model_header[$i][1] eq 'DROP' and
978 not $model_header[$i][0] =~ /DAT(E|1|2|3)/ ) {
983 push( @
{$self -> {'header'}}, $model_header[$i][0] );
987 foreach my $individual ( @
{$self -> {'individuals'}} ) {
988 $individual -> drop_columns
( drop
=> \
@drop );
991 $self -> {'synced'} = 0;
992 # $Data::Dumper::Maxdepth = 2;
994 # die Dumper $self -> {'individuals'};
1003 $self -> synchronize
;
1004 $self -> cont_column
( $cont_column ) if ( defined $cont_column );
1005 $self -> wrap_column
( $wrap_column ) if ( defined $wrap_column );
1006 $self -> prepare_wrap
( model_header
=> \
@model_header );
1007 @secondary_columns = @
{$self -> {'secondary_columns'}}
1008 if ( defined $self -> {'secondary_columns'} );
1009 @primary_columns = @
{$self -> {'primary_columns'}}
1010 if ( defined $self -> {'primary_columns'} );
1018 $self -> {'cont_column'} = undef;
1019 $self -> {'wrap_column'} = undef;
1020 $self -> {'secondary_columns'} = undef;
1021 $self -> {'primary_columns'} = undef;
1030 my $cont_column = $self -> {'cont_column'};
1031 my $wrap_column = $self -> {'wrap_column'};
1032 debug
-> die( message
=> 'cont_column ('.$cont_column.') must be less or equal '.
1033 'to the requested number of columns in each row ('.
1034 ($wrap_column).')' )
1035 if ( $cont_column > $wrap_column );
1037 if ( scalar @model_header > 0 ) {
1038 @header = @model_header;
1040 @header = @
{$self -> {'header'}};
1043 my ( @primary, @secondary, @date_columns );
1045 for ( my $i = 0; $i <= $#header; $i++ ) {
1046 my $name = ref( $header[$i] ) eq 'ARRAY' ?
$header[$i][0] : $header[$i];
1047 my $value = ref( $header[$i] ) eq 'ARRAY' ?
$header[$i][1] : undef;
1048 next if ( $name eq 'ID' );
1050 foreach my $prim ( @primary_column_names ) {
1051 if ( $name eq $prim or $value eq $prim ) {
1052 push( @primary, [$name, $i, $value] );
1054 my $col = ($#primary+2)>= $cont_column ?
($#primary+3) : ($#primary+2);
1055 push( @date_columns, $col ) if ( $name =~ /DAT(E|1|2|3)/ );
1058 push( @secondary, [$name, $i, $value] ) if ( not $found );
1061 my $prim_num = scalar @primary;
1062 debug
-> die( message
=> 'The number of primary columns (that need to '.
1063 'be part of the row with CONT=0) ('.($prim_num+1).
1064 ') is larger than the required number of columns (wrap_column='.
1065 $wrap_column.') - 1' )
1066 if ( scalar $prim_num > ($wrap_column-2) );
1068 my ( $i, $dum ) = ( 0, 1 );
1070 for ( my $j = 1; $j <= $wrap_column; $j++ ) {
1072 push( @tmp, ['ID', $self -> {'idcolumn'}-1] );
1073 } elsif ( $j == $wrap_column ) {
1074 if ( $j == $cont_column ) {
1075 push( @tmp, ['CONT', undef] );
1078 if ( defined $primary[$i] ) {
1079 $val = $primary[$i];
1080 } elsif ( defined $secondary[0] ) {
1081 $val = shift(@secondary);
1083 $val = ['XX'.$dum++,$self -> {'idcolumn'}-1];
1088 push( @
{$self -> {'primary_columns'}}, @tmp );
1090 if ( $j == $cont_column ) {
1091 push( @tmp, ['CONT', undef] );
1093 if ( $i <= $#primary ) {
1094 push( @tmp, $primary[$i] );
1097 my $val = defined $secondary[0] ?
shift(@secondary) :
1098 ['XX'.$dum++,$self -> {'idcolumn'}-1];
1106 while ( $i <= $#secondary ) {
1108 for ( my $j = 1; $j <= $wrap_column; $j++ ) {
1110 push( @tmp, ['ID', $self -> {'idcolumn'}-1] );
1111 } elsif ( $j == $wrap_column ) {
1112 if ( $j == $cont_column ) {
1113 push( @tmp, ['CONT', undef] );
1115 my $val = defined $secondary[$i] ?
$secondary[$i] :
1116 ['XX'.$dum++,$self -> {'idcolumn'}-1];
1120 unshift( @
{$self -> {'secondary_columns'}}, \
@tmp );
1122 if ( $j == $cont_column ) {
1123 push( @tmp, ['CONT', undef] );
1126 if ( $#date_columns >= 0 ) {
1127 foreach my $col ( @date_columns ) {
1128 # This is a date column which may have to be dropped
1129 # and thus will not appear as a secondary
1130 # column. Nothing should be pushed. The indexes in
1131 # model::problem::pk::_format_record will be ok.
1132 $isdate = 1 if ( $col == $j ) ;
1136 push( @tmp, ['XX'.$dum++,$self -> {'idcolumn'}-1] );
1138 if ( $i <= $#secondary ) {
1139 push( @tmp, $secondary[$i] );
1142 push( @tmp, ['XX'.$dum++,$self -> {'idcolumn'}-1] );
1154 # {{{ have_missing_data
1155 start have_missing_data
1157 # Either I<column> or I<column_head> must be specified.
1159 # This method looks through the data column with index I<column> or
1160 # (optional) header name I<column_head> and returns O if no missing
1161 # data indicator was found or 1 otherwise.
1163 $self -> synchronize
;
1164 my $first_id = $self -> {'individuals'}[0];
1165 debug
-> die( message
=> "No individuals defined in data object based on ".
1166 $self -> full_name
) unless ( defined $first_id );
1167 my @data_row = split( /,/ , $first_id -> subject_data
-> [0] );
1168 unless ( defined $column && defined( $data_row[$column-1] ) ) {
1169 unless(defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})){
1170 die "Error in data -> have_missing_data: unknown column: \"$column_head\" or invalid column number: \"$column\"\n";
1172 $column = $self -> {'column_head_indices'}{$column_head};
1175 $self -> flush
if ( $self -> {'target'} eq 'disk' );
1177 # In case anyone wonders, the ternary statment ( bool ? true :
1178 # false ) below will possibly make a minuscle memory
1179 # optimization. But hey, why not :)
1181 $return_value = defined $self -> {'have_missing_data'} ?
$self -> {'have_missing_data'} -> {$column} : 0;
1183 end have_missing_data
1184 # }}} have_missing_data
1189 #$self -> synchronize;
1190 push( @
{$self -> {'individuals'}}, @
{$mergeobj -> individuals
} );
1199 # Either column or column_head must be specified. Column_head must be a string that
1200 # identifies a column in the (optional ) data file header.
1202 # The if-statement below used to be a cache of allready calculated
1203 # means. But since individuals can be accessed in so many ways, we
1204 # don't know when this cache should be updated. Its easier to
1205 # recalculate the max. Maybe we can include this optimization in the
1206 # future, if it turns out to be a bottleneck
1207 # my $tmp_column = $self -> {'column_head_indices'}{$column_head};
1208 # if ( defined $self -> {'max'}[$tmp_column] ) {
1209 # $return_value = $self -> {'max'}[$tmp_column] ;
1211 $self -> synchronize
;
1212 my $first_id = $self -> {'individuals'}[0];
1213 debug
-> die( message
=> "data -> max: No individuals defined in data object based on " .
1214 $self -> full_name
) unless defined $first_id;
1216 my @data_row = split( /,/ , $first_id -> subject_data
->[0] );
1218 unless ( defined $column && defined( $data_row[$column-1] ) ) {
1219 unless (defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})) {
1220 die "Error in data -> max: unknown column: \"$column_head\" or invalid column number: \"$column\"\n";
1222 $column = $self -> {'column_head_indices'}{$column_head};
1225 foreach my $individual ( @
{$self -> {'individuals'}} ) {
1226 my $ifactors = $individual -> factors
( 'column' => $column );
1227 foreach ( keys %{$ifactors} ) {
1228 next if ( $_ == $self -> {'missing_data_token'} );
1229 if ( defined ($return_value) ) {
1230 $return_value = $_ > $return_value ?
$_ : $return_value;
1237 # $self -> {'max'}[$column] = $return_value;
1238 $self -> flush
if ( $self -> {'target'} eq 'disk' );
1250 my $tmp_column = $self -> {'column_head_indices'}{$column_head};
1252 # The if-statement below used to be a cache of allready calculated
1253 # means. But since individuals can be accessed in so many ways, we
1254 # don't know when this cache should be updated. Its easier to
1255 # recalculate the min. Maybe we can include this optimization in the
1256 # future, if it turns out to be a bottleneck
1257 # if ( defined $self -> {'min'}[$tmp_column] ) {
1258 # $return_value = $self -> {'min'}[$tmp_column] ;
1260 $self -> synchronize
;
1261 my $first_id = $self -> {'individuals'}[0];
1262 die "data -> min: No individuals defined in data object based on ",
1263 $self -> full_name
,"\n" unless defined $first_id;
1265 my @data_row = split( /,/ , $first_id -> subject_data
->[0] );
1267 unless ( defined $column && defined( $data_row[$column-1] ) ) {
1268 unless (defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})) {
1269 die "Error in data -> min: unknown column: \"$column_head\" or invalid column number: \"$column\"\n";
1271 $column = $self -> {'column_head_indices'}{$column_head};
1274 foreach my $individual ( @
{$self -> {'individuals'}} ) {
1275 my $ifactors = $individual -> factors
( 'column' => $column );
1276 foreach ( keys %{$ifactors} ) {
1277 next if ( $_ == $self -> {'missing_data_token'} );
1278 if ( defined ($return_value) ) {
1279 $return_value = $_ < $return_value ?
$_ : $return_value;
1285 # $self -> {'min'}[$column] = $return_value;
1286 $self -> flush
if ( $self -> {'target'} eq 'disk' );
1298 $self -> synchronize
;
1299 my $first_id = $self -> {'individuals'}[0];
1300 die "data -> median: No individuals defined in data object based on ",
1301 $self -> full_name
,"\n" unless defined $first_id;
1303 my @data_row = split( /,/ , $first_id -> subject_data
->[0] );
1305 unless ( defined $column && defined( $data_row[$column-1] ) ) {
1306 unless(defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})){
1307 die "Error in data -> median: unknown column: \"$column_head\" or invalid column number: \"$column\"\n";
1309 $column = $self -> {'column_head_indices'}{$column_head};
1313 if( defined $self -> {'median'}[$column] ){
1314 return $self -> {'median'}[$column];
1319 foreach my $individual ( @
{$self -> {'individuals'}} ) {
1320 if( $unique_in_individual ){
1321 my $ifactors = $individual -> factors
( 'column' => $column );
1323 foreach ( keys %{$ifactors} ) {
1324 next if ( $_ == $self -> {'missing_data_token'} );
1325 push( @median_array, $_ );
1328 my $ifactors = $individual -> subject_data
;
1330 for(my $i=0; $i<=$#{$ifactors}; $i++ ) {
1331 my @data_row = split( /,/ , $ifactors -> [$i] );
1332 next if ( $data_row[$column-1] == $self -> {'missing_data_token'} );
1333 push(@median_array, $data_row[$column-1]);
1337 @median_array = sort {$a <=> $b} @median_array ;
1339 if( @median_array % 2 ){
1340 $return_value = $median_array[$#median_array / 2];
1342 $return_value = ( $median_array[@median_array / 2] +
1343 $median_array[(@median_array - 2) / 2] ) / 2;
1346 $self -> {'median'}[$column] = $return_value;
1356 # Returns mean value of a column
1357 # If a individual contains more then 1 value (i.e. if an
1358 # individual has different values in different samples a mean
1359 # value of all individuals if calculate first, then the mean
1360 # value of the column If hi_cutoff is defined the mean function
1361 # will cut all value below the cutoff, and set their value to
1362 # 0. It's used to calculate the HI-mean/LOW-mean of a column for
1363 # e.g. Hockey-stick covariates If both hi_cutoff and low_cutoff
1364 # are defined only the hi_cutoff will be used. See L</max>.
1365 my $tmp_column = $self -> {'column_head_indices'}{$column_head};
1366 $self -> synchronize
;
1367 my $first_id = $self -> {'individuals'}[0];
1368 die "data -> mean: No individuals defined in data object based on ",
1369 $self -> full_name
,"\n" unless defined $first_id;
1371 my @data_row = split( /,/, $first_id -> subject_data
->[0] );
1373 unless ( defined $column && defined( $data_row[$column-1] ) ) {
1374 unless (defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})) {
1375 die "Error in data -> mean: unknown column: \"$column_head\" or invalid column number: \"$column\"\n";
1377 $column = $self -> {'column_head_indices'}{$column_head};
1381 ## Here the calculation starts
1382 my $num_individuals = 0;
1385 my $all_data_rows=0;
1386 foreach my $individual ( @
{$self ->{'individuals'}} ) {
1388 my $ifactors = $individual -> subject_data
;
1389 my $individual_sum = 0;
1391 for(my $i=0; $i<=$#{$ifactors}; $i++ ) {
1393 # data is stored in strings. We need to split them into an
1396 my @data_row = split( /,/, $ifactors -> [$i] );
1397 if ( $data_row[$column-1] == $self -> {'missing_data_token'} ) {
1398 # print "Skipping row with missing data\n";
1402 if( defined $subset_column and not eval ( $data_row[$subset_column-1].$subset_syntax ) ) {
1403 # print "Skipping row outside subset: syntax: ".($subset_column-1)." $subset_syntax\n";
1407 if (defined $hi_cutoff) {
1408 if ($data_row[$column-1]>$hi_cutoff) {
1409 $individual_sum += $data_row[$column-1]-$hi_cutoff;
1413 if (defined $low_cutoff) {
1414 if ($data_row[$column-1]<$low_cutoff) {
1415 $individual_sum += $low_cutoff - $data_row[$column-1];
1419 $individual_sum += $data_row[$column-1];
1424 if( $global_mean ) {
1425 $sum += $individual_sum;
1426 $num_individuals += $data_rows;
1428 if( $data_rows != 0 ) {
1429 $sum += $individual_sum/$data_rows;
1431 $num_individuals ++;
1433 $all_data_rows += $data_rows;
1435 if( $num_individuals != 0 ) {
1436 $return_value = $sum / $num_individuals;
1438 # print "DR: $all_data_rows\n";
1439 # print "\nNIM: $num_individuals $return_value\n";
1450 # This sub returns standard deviation for a specific column
1451 # If there are more than one sample/individual the value used for that specific
1452 # individual is the mean value of its samples.
1453 # The cut-offs are for hockey stick variables. I.e. If one individual value is
1454 # lower than the hi-cutoff the individual value will be zero.
1455 # HI_cutoff is used to calculate the HI-mean of a column.
1456 # If cut_off is undef it won't be used
1458 my $tmp_column = $self -> {'column_head_indices'}{$column_head};
1459 $self -> synchronize
;
1460 my $first_id = $self -> {'individuals'}[0];
1461 debug
-> die( message
=> "No individuals defined in data object based on ".
1462 $self -> full_name
) unless defined $first_id;
1464 my @data_row = split( /,/ , $first_id -> subject_data
->[0] );
1466 unless ( defined $column && defined( $data_row[$column-1] ) ) {
1467 unless (defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})) {
1468 debug
-> die( message
=> "Unknown column: \"$column_head\" or "
1469 ."invalid column number: \"$column\"" );
1471 $column = $self -> {'column_head_indices'}{$column_head};
1475 ## Here the calculation starts
1476 my $num_individuals = 0;
1479 if (defined $hi_cutoff) {
1480 $mean = $self->mean(column
=> $column,
1481 hi_cutoff
=> $hi_cutoff,
1482 global_mean
=> $global_sd );
1483 } elsif (defined $low_cutoff) {
1484 $mean = $self->mean(column
=> $column,
1485 low_cutoff
=> $low_cutoff,
1486 global_mean
=> $global_sd );
1488 $mean = $self->mean( column
=> $column,
1489 subset_column
=> $subset_column,
1490 subset_syntax
=> $subset_syntax,
1491 global_mean
=> $global_sd );
1494 foreach my $individual ( @
{$self -> {'individuals'}} ) {
1495 my $ifactors = $individual -> subject_data
;
1496 my $individual_sum = 0;
1498 for(my $i=0; $i<=$#{$ifactors}; $i++ ) {
1500 # data is stored in strings. We need to split them into an
1503 my @data_row = split( /,/, $ifactors -> [$i] );
1505 if ( $data_row[$column-1] == $self -> {'missing_data_token'} ) {
1506 # print "Skipping row with missing data\n";
1510 if( defined $subset_column and not eval ( $data_row[$subset_column-1].$subset_syntax ) ) {
1511 # print "Skipping row outside subset: syntax: ".($subset_column-1)." $subset_syntax\n";
1515 if (defined $hi_cutoff) {
1516 if ($ifactors->[$i]->[$column-1]>$hi_cutoff) {
1518 $individual_sum += ($data_row[$column-1] - $hi_cutoff - $mean) ** 2;
1520 $individual_sum += $data_row[$column-1]-$hi_cutoff;
1524 if (defined $low_cutoff) {
1525 if ($ifactors->[$i]->[$column-1]<$low_cutoff) {
1527 $individual_sum += ($low_cutoff - $data_row[$column-1] - $mean) ** 2;
1529 $individual_sum += $low_cutoff - $data_row[$column-1];
1534 $individual_sum += ($data_row[$column-1] - $mean) ** 2;
1536 $individual_sum += $data_row[$column-1];
1543 $sum += $individual_sum;
1544 $num_individuals += $data_rows;
1546 if( $data_rows != 0 ) {
1547 $sum += ($individual_sum/$data_rows - $mean) ** 2;
1552 if( $num_individuals < 2 ) {
1555 if( $num_individuals != 0 ) {
1556 $return_value = (1/($num_individuals-1)*$sum) ** 0.5;
1569 my $tmp_column = $self -> {'column_head_indices'}{$column_head};
1570 if ( defined $self -> {'range'}[$tmp_column] ) {
1571 $return_value = $self -> {'range'}[$tmp_column];
1573 my $old_target = $self -> {'target'};
1574 $self -> {'target'} = 'mem';
1575 $self -> synchronize
;
1576 $return_value = $self -> max
( column
=> $column,
1577 column_head
=> $column_head ) -
1578 $self -> min
( column
=> $column,
1579 column_head
=> $column_head );
1580 $self -> {'range'}[$column] = $return_value;
1581 if ( $old_target eq 'disk' ) {
1582 $self -> flush
if ( $self -> {'target'} eq 'disk' );
1583 $self -> {'target'} = 'disk';
1593 # Recalculates a column based on expression. Also, see L</max>.
1594 $self -> synchronize
;
1596 # Check if $column(-index) is defined and valid, else try to find index using column_head
1597 my $first_id = $self -> {'individuals'}[0];
1598 die "data -> recalc_column: No individuals defined in data object based on ",
1599 $self -> full_name
,"\n" unless defined $first_id;
1601 my @data_row = split( /,/ , $first_id -> subject_data
->[0] );
1603 unless ( defined $column && defined( $data_row[$column-1] ) ) {
1604 if(defined($column_head) && defined($self -> {'column_head_indices'}{$column_head})){
1605 die "Error in data -> recalc_column: unknown column: \"$column_head\" or column number: \"$column\"\n";
1607 $column = $self -> {'column_head_indices'}{$column_head};
1611 for my $individual ( @
{$self -> {'individuals'}} ) {
1612 $individual -> recalc_column
( column
=> $column,
1613 expression
=> $expression );
1619 # {{{ renumber_ascending
1621 start renumber_ascending
1623 # Renumbers the individuals (changes the subject identifiers) so that
1624 # all have unique integer numbers starting with start_at and
1625 # ascending. The primary use of this
1626 # method is not to order the individuals after their identifiers but to
1627 # ensure that all individuals have unique identifiers.
1629 $self -> synchronize
;
1630 foreach my $individual ( @
{$self -> {'individuals'}} ) {
1631 $individual -> idnumber
( $start_at++ );
1633 $self -> {'synced'} = 0;
1635 end renumber_ascending
1637 # }}} renumber_ascending
1639 # {{{ renumber_descending
1641 start renumber_descending
1643 # See L</renumber_ascending>.
1644 $self -> synchronize
;
1645 foreach my $individual ( @
{$self -> {'individuals'}} ) {
1646 $individual -> idnumber
( $start_at-- );
1648 $self -> {'synced'} = 0;
1650 end renumber_descending
1652 # }}} renumber_descending
1654 # {{{ single_valued_data
1656 start single_valued_data
1660 # ($single_value_data_set, $remainder, $column_indexes) =
1661 # $data_object -> single_valued_data( subset_name => 'subset.dta',
1662 # remainder_name => 'remainder.dta',
1664 # do_not_test_columns => [1..18,24,26];
1666 # my $single_value_column_indexes = $column_indexes -> [0];
1667 # my $all_other_column_indexes = $column_indexes -> [1];
1669 # Analyses the content of each column, based on the
1670 # ID column, and returns two new data objects: One
1671 # that contains all columns that is has only one value per
1672 # individual and one that contains the
1673 # remainding data. This is useful for creating compact 'extra'
1674 # data sets that can be read in via user-defined sub-routines
1675 # when the number of columns needed exceeds the maximum that
1676 # NONMEM allows (e.g. 20 in NONMEM version V).
1678 # The I<do_not_test_columns> argument specifies on which columns
1679 # to skip the single value test
1681 my @multi_value_flags;
1682 my @individuals = @
{$self -> {'individuals'}};
1683 # Initiate the flags:
1684 if ( defined $individuals[0] ) {
1685 my @data = @
{$individuals[0] -> {'subject_data'}};
1686 my @data_row = split( /,/ , $data[0] );
1687 for ( my $i = 0; $i < scalar @data_row; $i++ ) {
1689 foreach my $dntc ( @do_not_test_columns ) {
1690 $dnt_flag = 1 if ( $i == $dntc - 1 );
1692 $multi_value_flags[$i] = $dnt_flag;
1695 die "data -> single_valued_data: No data in ID number 1\n";
1698 for ( my $id = 0; $id <= $#individuals; $id++ ) {
1699 my @data = @
{$individuals[$id] -> {'subject_data'}};
1700 my @data_row = split( /,/, $data[0] );
1701 for ( my $j = 0; $j < scalar @data_row; $j++ ) {
1703 for ( my $i = 0; $i <= $#data; $i++ ) {
1704 my @data_row = split( /,/ , $data[$i] );
1705 $col_unique{$data_row[$j]}++;
1707 my $factors = scalar keys %col_unique;
1708 $multi_value_flags[$j]++ if ( $factors > 1 );
1711 for ( my $i = 0; $i <= $#multi_value_flags; $i++ ) {
1712 if ( $multi_value_flags[$i] ) {
1713 push ( @
{$column_indexes[1]}, $i + 1);
1715 push ( @
{$column_indexes[0]}, $i + 1);
1718 ( $single_value_data_set, $remainder ) =
1719 $self -> subset_vertically
( column_indexes
=> $column_indexes[0],
1720 subset_name
=> $subset_name,
1721 return_remainder
=> 1,
1722 remainder_name
=> $remainder_name,
1724 keep_first_row_only
=> 1);
1726 end single_valued_data
1730 # {{{ subset_vertically
1732 start subset_vertically
1736 # $subset = $data_object -> subset_vertically ( column_indexes => [1,2,6],
1737 # subset_name => 'subset.dta' );
1739 # This basic usage returns a new data object containing
1740 # columns 1,2 and 6 from the original data plus the
1741 # idcolumn. The new data object will be associated with the
1742 # file 'subset.dta'.
1744 # You get the remaining data, i.e. the original data minus
1745 # the created subset by specifying
1747 # ( $subset, $remainder ) =
1748 # $data_object -> subset_vertically ( column_indexes => [1,2,6],
1749 # subset_name => 'subset.dta',
1750 # return_remainder => 1,
1751 # remainder_name => 'remainder.dta' );
1753 # If you would like to flush the created data sets to disk and
1754 # save memory, set the I<target> argument to 'disk'. The
1755 # default value 'mem' will keep the whole data object in RAM.
1757 # The I<keep_first_row_only> argument can be used to reduce
1758 # the size of the subset data obejct by excluding all but the
1759 # first row of data from each individual.
1761 my @individuals = @
{$self -> {'individuals'}};
1762 # Create remainder index array if necessary
1763 my @remainder_indexes;
1764 if ( defined $individuals[0] ) {
1765 my @data = @
{$individuals[0] -> {'subject_data'}};
1766 my $idcolumn = $individuals[0] -> {'idcolumn'};
1767 # print "IC: $idcolumn\n";
1769 foreach my $use_index ( @column_indexes ) {
1770 $id_flag = 1 if ( $use_index == $idcolumn );
1772 if ( $return_remainder ) {
1773 # @remainder_indexes = ( $idcolumn );
1774 for ( my $i = 0; $i < scalar split(/,/,$data[0]); $i++ ) {
1776 foreach my $use_index ( @column_indexes ) {
1777 $rem_flag = 0 if ( $i == $use_index -1 );
1779 # $i == $idcolumn -1 );
1781 push( @remainder_indexes, $i + 1 ) if ( $rem_flag );
1783 unshift( @remainder_indexes, $idcolumn ) if ( $id_flag );
1785 unshift( @column_indexes, $idcolumn ) unless ( $id_flag );
1787 die "data -> single_valued_data: No data in ID number 1\n";
1790 # print "SS: @column_indexes\n";
1791 # print "R : @remainder_indexes\n";
1795 for ( my $id = 0; $id <= $#individuals; $id++ ) {
1796 my $idnumber = $individuals[$id] -> idnumber
;
1797 my $idcolumn = $individuals[$id] -> idcolumn
;
1798 my @data = @
{$individuals[$id] -> {'subject_data'}};
1801 my $use_rows = $keep_first_row_only ?
0 : $#data;
1802 for ( my $i = 0; $i <= $use_rows; $i++ ) {
1804 my @data_row = split( /,/, $data[$i] );
1805 foreach my $use_index ( @column_indexes ) {
1806 push( @new_row, $data_row[$use_index-1] );
1808 # print "@new_row $#new_row\n";
1809 push( @new_data, join( ',', @new_row ) );
1811 for ( my $i = 0; $i <= $#data; $i++ ) {
1812 if ( $return_remainder ) {
1814 my @data_row = split( /,/, $data[$i] );
1815 foreach my $use_index ( @remainder_indexes ) {
1816 push( @new_row_2, $data_row[$use_index-1] );
1818 # print "@new_row_2 $#new_row_2\n";
1819 push( @new_data_2, join( ',' , @new_row_2 ) );
1822 my $new_id = data
::individual
-> new
( idnumber
=> $idnumber,
1823 idcolumn
=> $idcolumn,
1824 subject_data
=> \
@new_data );
1825 push( @new_ids, $new_id );
1826 if ( $return_remainder ) {
1828 $new_id_2 = data
::individual
-> new
( idnumber
=> $idnumber,
1829 idcolumn
=> $idcolumn,
1830 subject_data
=> \
@new_data_2 );
1831 push( @new_ids_2, $new_id_2 );
1834 my @header = @
{$self -> {'header'}};
1836 foreach my $use_index ( @column_indexes ) {
1837 push( @new_header, @header[$use_index-1] );
1840 if( defined $self -> {'comment'} ){
1841 my @comment = @
{$self -> {'comment'}};
1842 $comment = \
@comment;
1844 $subset = data
-> new
( filename
=> $subset_name,
1845 directory
=> $self -> {'directory'},
1846 ignoresign
=> $self -> {'ignoresign'},
1847 header
=> \
@new_header,
1848 comment
=> $comment,
1849 individuals
=> \
@new_ids,
1851 ignore_missing_files
=> 1 );
1852 if ( $return_remainder ) {
1854 foreach my $use_index ( @remainder_indexes ) {
1855 push( @new_header_2, @header[$use_index-1] );
1857 $remainder = data
-> new
( filename
=> $remainder_name,
1858 directory
=> $self -> {'directory'},
1859 ignoresign
=> $self -> {'ignoresign'},
1860 header
=> \
@new_header_2,
1861 comment
=> $comment,
1862 individuals
=> \
@new_ids_2,
1864 ignore_missing_files
=> 1 );
1867 end subset_vertically
1875 # if ( defined $expression and defined $bins ) {
1876 # die "data -> subset: expression and bins may not both be specified\n";
1878 # if ( not ( defined $expression or defined $bins ) ) {
1879 # die "data -> subset: expression or bins must be specified\n";
1881 $self -> synchronize
;
1882 my @header = @
{$self -> {'header'}};
1883 my @comment = defined $self -> {'comment'} ? @
{$self -> {'comment'}} : ();
1887 my @ids = @
{$self -> {'individuals'}};
1888 if ( defined $stratify_on ) {
1889 my $work_data = $self -> copy
( filename
=> 'work_data.dta',
1891 my %strata = %{$work_data -> factors
( column
=> $stratify_on )};
1892 # $Data::Dumper::Maxdepth = 1;
1893 # print Dumper \%strata;
1895 while ( my ( $factor, $keys ) = each %strata ) {
1896 foreach my $key ( @
{$keys} ) {
1898 while ( defined $rnd_ids{$factor}{$rnd_num} ) {
1901 $rnd_ids{$factor}{$rnd_num} = $ids[$key];
1905 while ( my ( $factor, $rnd_nums ) = each %rnd_ids ) {
1906 my @sort_rnd_nums = sort { $a <=> $b } keys %{$rnd_nums};
1907 for ( my $i = 0; $i <= $#sort_rnd_nums; $i ) {
1908 for ( my $j = 0; $j < $bins; $j++ ) {
1910 push( @subset_ids, [$rnd_ids{$factor}{$sort_rnd_nums[$i]} -> copy
] );
1911 push( @incl_ids, [$rnd_ids{$factor}{$sort_rnd_nums[$i]} -> idnumber
] );
1913 push( @
{$subset_ids[$j]}, $rnd_ids{$factor}{$sort_rnd_nums[$i]} -> copy
);
1914 push( @
{$incl_ids[$j]}, $rnd_ids{$factor}{$sort_rnd_nums[$i]} -> idnumber
);
1917 last if $i > $#sort_rnd_nums;
1922 for ( my $j = 0; $j < $bins; $j++ ) {
1923 my $sdata = data
-> new
( header
=> \
@header,
1924 comment
=> \
@comment,
1925 ignoresign
=> $self -> {'ignoresign'},
1926 individuals
=> $subset_ids[$j],
1927 ignore_missing_files
=> 1,
1929 idcolumn
=> $self -> {'idcolumn'},
1930 filename
=> "subset_$j.dta" );
1932 push( @subsets, $sdata );
1935 for ( my $i = 0; $i <= $#ids; $i++ ) {
1937 while ( defined $rnd_ids{$rnd_num} ) {
1940 $rnd_ids{$rnd_num} = $ids[$i];
1942 my @keys = sort { $a <=> $b } keys %rnd_ids;
1944 for ( my $i = 0; $i <= $#keys; $i ) {
1945 for ( my $j = 0; $j < $bins; $j++ ) {
1947 push( @subset_ids, [$rnd_ids{$keys[$i]} -> copy
] );
1948 push( @incl_ids, [$rnd_ids{$keys[$i]} -> idnumber
] );
1950 push( @
{$subset_ids[$j]}, $rnd_ids{$keys[$i]} -> copy
);
1951 push( @
{$incl_ids[$j]}, $rnd_ids{$keys[$i]} -> idnumber
);
1954 last if $i > $#keys;
1958 for ( my $j = 0; $j < $bins; $j++ ) {
1959 my $sdata = data
-> new
( header
=> \
@header,
1960 comment
=> \
@comment,
1961 ignoresign
=> $self -> {'ignoresign'},
1962 individuals
=> $subset_ids[$j],
1963 ignore_missing_files
=> 1,
1965 idcolumn
=> $self -> {'idcolumn'},
1966 filename
=> "subset_$j.dta" );
1968 push( @subsets, $sdata );
1980 $self -> synchronize
;
1981 my @header = @
{$self -> {'header'}};
1982 my @comment = defined $self -> {'comment'} ? @
{$self -> {'comment'}} : ();
1983 my @subset_inds = ();
1985 foreach my $individual ( @
{$self -> {'individuals'}} ) {
1986 if ( $individual -> evaluate_expression
( column
=> $based_on,
1987 expression
=> $expression ) ) {
1988 push( @subset_inds, $individual -> copy
);
1989 push( @incl_individuals, $individual -> idnumber
);
1990 push( @included_keys, $key );
1994 $subset = data
-> new
( header
=> \
@header,
1995 comment
=> \
@comment,
1996 ignoresign
=> $self -> {'ignoresign'},
1997 individuals
=> \
@subset_inds,
1998 idcolumn
=> $self -> {'idcolumn'},
1999 filename
=> "subset.dta" );
2009 if ( $parm eq 'disk' and $self -> {'target'} eq 'mem' ) {
2010 $self -> {'target'} = 'disk';
2012 } elsif ( $parm eq 'mem' and $self -> {'target'} eq 'disk' ) {
2013 $self -> {'target'} = 'mem';
2014 $self -> synchronize
;
2025 die "ERROR: data -> _write: No filename set in data object.\n"
2026 if( $filename eq '' );
2028 # $Data::Dumper::Maxdepth = 2;
2029 # die Dumper $self -> {'individuals'};
2031 if( not defined $self -> {'individuals'} ){
2033 # If we don't have any individuals and write to a new
2034 # filename, we must first read individuals from the old
2035 # file. A call to synchronize will do that. There is no risk
2036 # of a infinite loop here since synchronize allways writes to
2039 unless( $filename eq $self -> full_name
){
2040 $self -> synchronize
;
2044 open(FILE
,">$filename") ||
2045 die "Could not create $filename\n";
2046 my $data_ref = $self -> format_data
;
2047 my @data = @
{$data_ref};
2053 # if ( $PsN::config -> {'_'} -> {'use_database'} and
2054 # $self -> {'use_data_table'} ) {
2055 # # Backslashes messes up the sql syntax
2056 # my $file_str = $self->{'filename'};
2057 # my $dir_str = $self->{'directory'};
2058 # $file_str =~ s/\\/\//g;
2059 # $dir_str =~ s/\\/\//g;
2062 # my $md5sum = md5_hex(OSspecific::slurp_file($self-> full_name ));
2063 # my ( $date_str, $time_str );
2064 # if ( $Config{osname} eq 'MSWin32' ) {
2065 # $date_str = `date /T`;
2066 # $time_str = ' '.`time /T`;
2069 # $date_str = `date`;
2073 # my $date_time = $date_str.$time_str;
2074 # my $dbh = DBI -> connect("DBI:mysql:host=".$PsN::config -> {'_'} -> {'database_server'}.
2075 # ";databse=".$PsN::config -> {'_'} -> {'project'},
2076 # $PsN::config -> {'_'} -> {'user'},
2077 # $PsN::config -> {'_'} -> {'password'},
2079 # 'RaiseError' => 1});
2081 # if ( defined $self -> {'data_id'} ) {
2082 # $sth = $dbh -> prepare( "UPDATE ".$PsN::config -> {'_'} -> {'project'}.
2084 # "SET filename='$file_str',date='$date_time',".
2085 # "directory='$dir_str',md5sum='$md5sum' ".
2086 # "WHERE data_id='".$self -> {'data_id'}."'" );
2087 # $sth -> execute or debug -> die( message => $sth->errstr ) ;
2089 # $sth = $dbh -> prepare("INSERT INTO ".$PsN::config -> {'_'} -> {'project'}.
2090 # ".data (filename,date,directory,md5sum) ".
2091 # "VALUES ('$file_str', '$date_time', '$dir_str','".
2094 # $self -> {'data_id'} = $sth->{'mysql_insertid'};
2097 # $dbh -> disconnect;
2107 # synchronizes the object with the file on disk and empties
2108 # most of the objects attributes to save memory.
2109 if( defined $self -> {'individuals'} and
2110 ( !$self -> {'synced'} or $force ) ) {
2113 # $self -> {'header'} = undef;
2114 $self -> {'comment'} = undef;
2115 $self -> {'individuals'} = undef;
2116 $self -> {'synced'} = 0;
2117 $self -> {'column_head_indices'} = undef;
2118 $self -> {'have_missing_data'} = undef;
2127 # synchronizes the object with the file on disk
2128 unless( $self -> {'synced'} ){
2129 if( defined $self -> {'individuals'} and
2130 scalar @
{$self -> {'individuals'}} > 0 ){
2131 # We should not read new data from file if we
2132 # have an individuals defined?
2133 # Perhaps there should be an attribute
2134 # 'from_file' that overrides this and reads in
2135 # the data from the file specified in filename
2136 # and overwrites whatever the object already
2138 # if( -e $self -> {'filename'} ){
2139 # $self -> _read_header;
2140 # $self -> _read_individuals;
2144 if( -e
$self -> full_name
){
2145 unless( defined $self -> {'header'} and scalar @
{$self -> {'header'}} > 0 ){
2146 $self -> _read_header
;
2148 $self -> _read_individuals
;
2150 debug
-> die( message
=> "Fatal error: datafile: " . $self -> full_name
. " does not exist." );
2156 foreach my $head ( @
{$self -> {'header'}} ){
2157 $self -> {'column_head_indices'} -> {$head} = $i;
2160 $self -> {'synced'} = 1;
2166 # {{{ _fisher_yates_shuffle
2168 start _fisher_yates_shuffle
2170 my $arr_ref = $parm{'array'};
2171 debug
-> warn( level
=> 1,
2172 message
=> "Array of zero length received" )
2173 if ( scalar @
{$arr_ref} < 1 );
2175 for ($i = @
$arr_ref; --$i; ) {
2176 my $j = random_uniform_integer
(1,0,$i);
2177 # my $j = int rand ($i+1);
2178 # print "$j $j_new\n";
2179 @
$arr_ref[$i,$j] = @
$arr_ref[$j,$i];
2182 end _fisher_yates_shuffle
2184 # }}} _fisher_yates_shuffle
2190 my $filename = $self -> full_name
;
2191 my $ignoresign = $self -> ignoresign
;
2192 my ( @data, @new_record, $row, $tmp_row, @header, $hdrstring );
2194 open(DATAFILE
,"$filename") ||
2195 die "Could not open $filename for reading";
2197 while (<DATAFILE
>) {
2199 # @new_record = split(/\,|\s+/,$_);
2200 if ( ! (/^\s*\d+|^\s*\./) ) {
2201 $data[$row] = $tmp_row;
2204 # We have reached the first data-row, return.
2205 $columns = scalar split(/\,\s*|\s+/);
2211 if ( defined $self -> {'cont_column'} and not $self -> {'table_file'} ) {
2212 my $data_len = $#data;
2213 for ( my $i = $data_len; $i >= 0; $i-- ) {
2214 my @arr = split(/\,\s*|\s+/,$data[$i]);
2215 if ( $arr[$self -> {'cont_column'}-1] eq 'CONT' ) {
2216 my $start = $i == $data_len ?
0 : 1;
2217 for ( my $j = $start; $j <= $#arr; $j++ ) {
2218 if ( $j != ($self -> {'cont_column'}-1) ) {
2219 push( @header, $arr[$j] );
2225 $header[0] =~ s/$ignoresign//
2226 if ( defined $self->ignoresign );
2227 shift( @header ) if ( $header[0] eq "" );
2229 chomp( $hdrstring = pop(@data));
2230 @header = split(/\,\s*|\s+/,$hdrstring);
2231 $header[0] =~ s/$ignoresign//
2232 if ( defined $self->ignoresign );
2233 shift( @header ) if ( $header[0] eq "" );
2234 if( $self -> {'table_file'} ) {
2236 for( my $i = 1; $i <= scalar @header; $i++ ) {
2237 if( $header[$i-1] eq 'CONT' ) {
2238 if ( defined $self -> {'cont_column'} and not $i == $self -> {'cont_column'} ) {
2239 debug
-> warn( level
=> 1,
2240 message
=> "The supplied columns for the CONT data item (".
2241 $self -> {'cont_column'}.") does not match the column where the CONT ".
2242 "header was found ($i), using $i" );
2244 $self -> {'cont_column'} = $i;
2246 push( @new_header, $header[$i-1] );
2249 @header = @new_header;
2250 for( my $i = 1; $i <= scalar @header; $i++ ) {
2251 if( $header[$i-1] eq 'ID' ) {
2252 if ( defined $self -> {'idcolumn'} and not $i == $self -> {'idcolumn'} ) {
2253 debug
-> warn( level
=> 1,
2254 message
=> "The supplied columns for the ID data item (".
2255 $self -> {'idcolumn'}.") does not match the column where the CONT ".
2256 "header was found ($i), using $i" );
2258 $self -> {'idcolumn'} = $i;
2264 # I'm not certain on how to deal with this conflict. I'm leaving it commented because I believe this code should not be here.
2266 #<<<<<<< data_subs.pm
2267 # $header[0] =~ s/$ignoresign//
2268 # if ( defined $self->ignoresign );
2269 # shift( @header ) if ( $header[0] eq "" );
2273 # It is ok with data sets without a header.
2274 # unless( scalar @header > 0 ){ debug -> die( message => 'Datafile ' . $self -> full_name . ' is empty.' ); }
2276 $self -> {'header'} = \
@header;
2277 $self -> {'comment'} = \
@data;
2278 # if ( $PsN::config -> {'_'} -> {'use_database'} and
2279 # $self -> {'use_data_table'} ) {
2280 # my $dbh = DBI -> connect("DBI:mysql:host=".$PsN::config -> {'_'} -> {'database_server'}.
2281 # ";databse=".$PsN::config -> {'_'} -> {'project'},
2282 # $PsN::config -> {'_'} -> {'user'},
2283 # $PsN::config -> {'_'} -> {'password'},
2284 # {'RaiseError' => 1});
2285 # if ( scalar @header < 1 ) {
2286 # for ( my $i = 1; $i <= $columns; $i++ ) {
2287 # push( @header, $i );
2290 # for ( my $i = 0; $i <= $#header; $i++ ) {
2291 # my $sth = $dbh -> prepare("INSERT INTO ".$PsN::config -> {'_'} -> {'project'}.
2293 # "(name,number,data_id) ".
2294 # "VALUES ('".$header[$i]."', '".($i+1).
2295 # "', '".$self -> {'data_id'}."' )");
2297 # push( @{$self -> {'data_column_ids'}}, $sth->{'mysql_insertid'} );
2300 # $dbh -> disconnect;
2307 # {{{ _read_individuals
2309 start _read_individuals
2311 my $idcol = $self -> idcolumn
;
2312 my $filename = $self -> full_name
;
2313 debug
-> warn( level
=> 1,
2314 message
=> "Building array of individuals from file " . $self -> {'filename'} );
2315 open(DATAFILE
,"$filename") ||
2316 die "Could not open $filename for reading";
2317 my ( @new_row, $new_ID, $old_ID, @init_data );
2320 while (sysread DATAFILE
, $buffer, 4096) {
2321 $lines += ($buffer =~ tr/\n//);
2323 seek( DATAFILE
, 0,0 );
2326 my $status_bar = status_bar
-> new
( steps
=> $lines );
2328 ui
-> print( category
=> 'scm',
2329 message
=> "Reading data file: ".$self -> filename
);
2330 ui
-> print( category
=> 'scm',
2331 message
=> $status_bar -> print_step
(),
2334 my ( $sth, $dbh, $first_row_id, $first_value_id );
2336 # if ( $PsN::config -> {'_'} -> {'use_database'} and
2337 # $self -> {'use_data_table'} ) {
2338 # $dbh = DBI -> connect("DBI:mysql:host=".$PsN::config -> {'_'} -> {'database_server'}.
2339 # ";databse=".$PsN::config -> {'_'} -> {'project'},
2340 # $PsN::config -> {'_'} -> {'user'},
2341 # $PsN::config -> {'_'} -> {'password'},
2342 # {'RaiseError' => 1});
2343 # my $sth = $dbh -> prepare( "SELECT data_row_id FROM ".$PsN::config -> {'_'} -> {'project'}.
2345 # "WHERE data_id='".$self -> {'data_id'}."'" );
2346 # $sth -> execute or debug -> die( message => $sth->errstr ) ;
2347 # my $select_arr = $sth -> fetchall_arrayref;
2348 # if ( scalar @{$select_arr} > 0 ) {
2349 # for ( my $i = 0; $i < scalar @{$select_arr}; $i++ ) {
2350 # push( @{$self -> {'data_row_ids'}}, $select_arr->[$i][0] );
2352 # $sth = $dbh -> prepare( "SELECT data_value_id FROM ".$PsN::config -> {'_'} -> {'project'}.
2354 # "WHERE data_id='".$self -> {'data_id'}."'" );
2355 # $sth -> execute or debug -> die( message => $sth->errstr ) ;
2356 # my $select_val = $sth -> fetchall_arrayref;
2357 # for ( my $i = 0; $i < scalar @{$select_val}; $i++ ) {
2358 # push( @{$self -> {'data_value_ids'}}, $select_val->[$i][0] );
2361 # $dbh -> disconnect;
2363 # $dbh -> do( "LOCK TABLES ".$PsN::config -> {'_'} -> {'project'}.
2364 # ".data_row WRITE, ".$PsN::config -> {'_'} -> {'project'}.
2365 # ".data_value WRITE" );
2366 # $sth = $dbh -> prepare( "SELECT MAX(data_row_id) FROM ".$PsN::config -> {'_'} -> {'project'}.
2368 # $sth -> execute or debug -> die( message => $sth->errstr ) ;
2369 # my $select_arr = $sth -> fetchall_arrayref;
2370 # $first_row_id = defined $select_arr -> [0][0] ? $select_arr -> [0][0] : 0;
2371 # $sth = $dbh -> prepare( "SELECT MAX(data_value_id) FROM ".$PsN::config -> {'_'} -> {'project'}.
2373 # $sth -> execute or debug -> die( message => $sth->errstr ) ;
2374 # my $select_arr = $sth -> fetchall_arrayref;
2375 # $first_value_id = defined $select_arr -> [0][0] ? $select_arr -> [0][0] : 0;
2382 my $row_counter = 0;
2384 ROW
: while ( <DATAFILE
> ) {
2386 my @new_row = split(/\,\s*|\s+/);
2387 # This regexp check is not time consuming.
2388 if ( /^\s*\d+|^\s*\./ ) {
2389 if ( defined $self -> {'cont_column'} ) {
2390 if ( $new_row[$self -> {'cont_column'} - 1] == 1 ) {
2391 if ( not $self -> {'table_file'} ) { # Skip the CONT=1 rows if this is a table file
2392 for ( my $i = $#new_row; $i > 0; $i-- ) {
2393 if ( $i != ($self -> {'cont_column'} - 1) ) {
2394 unshift( @
{$full_row}, $new_row[$i] );
2400 for ( my $i = $#new_row; $i >= 0; $i-- ) {
2401 # if ( $i != ($self -> {'cont_column'} - 1) or $self -> {'table_file'} ) {
2402 if ( $i != ($self -> {'cont_column'} - 1) ) {
2403 unshift( @
{$full_row}, $new_row[$i] );
2408 @
{$full_row} = @new_row;
2410 $new_ID = $full_row -> [$idcol-1]; # index starts at 0
2411 $old_ID = $new_ID if ( not defined $old_ID );
2413 # Check if column miss data at some row (This adds about 30% of init time)
2414 my $mdt = $self -> {'missing_data_token'};
2415 for( my $i = 0; $i <= $#{$full_row}; $i++ ){
2416 $self -> {'have_missing_data'} -> {$i+1} = 1
2417 if( $full_row -> [$i] == $mdt ); # == is slower but safer than eq
2419 # if ( $PsN::config -> {'_'} -> {'use_database'} and
2420 # $self -> {'use_data_table'} and $insert ) {
2422 # $insert_rows = $insert_rows."," if ( defined $insert_rows );
2423 # $insert_rows = $insert_rows.
2424 # "('$row_counter', '".$self -> {'data_id'}."' )";
2425 # for ( my $j = 0; $j <= $#{$full_row}; $j++ ) {
2426 # $insert_values = $insert_values."," if ( defined $insert_values );
2427 # $insert_values = $insert_values.
2428 # "('".$full_row -> [$j]."', '".
2429 # ($first_row_id+$row_counter)."', '".
2430 # $self -> {'data_column_ids'}->[$j].
2431 # "', '".$self -> {'data_id'}."' )";
2435 if ( $new_ID != $old_ID ) {
2436 my @subject_data = @init_data;
2437 my $id = data
::individual
-> new
( idcolumn
=> $idcol,
2438 subject_data
=> \
@subject_data,
2439 data_id
=> $self -> {'data_id'} );
2440 push( @
{$self -> {'individuals'}}, $id );
2441 @init_data =(join( ",", @
{$full_row}));
2443 push( @init_data, join( ",", @
{$full_row}) );
2448 if ( $status_bar -> tick
() ) {
2449 ui
-> print( category
=> 'scm',
2450 message
=> $status_bar -> print_step
(),
2456 # if ( $PsN::config -> {'_'} -> {'use_database'} and
2457 # $self -> {'use_data_table'} and $insert ) {
2458 # $dbh -> do("INSERT INTO ".$PsN::config -> {'_'} -> {'project'}.
2460 # "(number,data_id) ".
2461 # "VALUES ".$insert_rows);
2462 # push( @{$self -> {'data_row_ids'}}, ($first_row_id..$first_row_id+$row_counter) );
2463 # $dbh -> do( "INSERT INTO ".$PsN::config -> {'_'} -> {'project'}.
2465 # "(value,data_row_id,data_column_id,data_id) ".
2466 # "VALUES ".$insert_values );
2467 # push( @{$self -> {'data_value_ids'}},
2468 # ($first_value_id..$first_value_id+($row_counter*
2469 # scalar @{$self->{'data_column_ids'}})));
2470 # $dbh -> do( "UNLOCK TABLES" );
2471 # $dbh -> disconnect;
2474 if ( $#init_data >= 0 ) {
2475 push( @
{$self -> {'individuals'}},
2476 data
::individual
-> new
( idcolumn
=> $idcol,
2477 subject_data
=> \
@init_data ) );
2479 ui
-> print( category
=> 'scm',
2480 message
=> " ... done" );
2482 # $self -> _write( filename => 'test.dta' );
2484 end _read_individuals
2486 # }}} _read_individuals