Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
smp committed Apr 11, 2023
1 parent e8abc1c commit f209d9e
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 64 deletions.
160 changes: 117 additions & 43 deletions annot/annot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,7 @@ bool annot_t::load( const std::string & f , edf_t & parent_edf )
// check EDF starttime, which might be needed
// -- but add EDF start date here too, to allow dhms printing

// clocktime_t starttime( parent_edf.header.startdate, parent_edf.header.starttime );
// IGNORE THAT FOR NOW...
clocktime_t startdatetime( parent_edf.header.startdate, parent_edf.header.starttime );
clocktime_t starttime( parent_edf.header.starttime );

// read header then data
Expand Down Expand Up @@ -1080,7 +1079,7 @@ bool annot_t::load( const std::string & f , edf_t & parent_edf )
tok[5] = ".";
tok[4] = tok[3];
tok[3] = tok[2];
tok[2] = ".";
tok[2] = ".";
}
else if ( tok.size() == 3 )
{
Expand Down Expand Up @@ -1143,11 +1142,13 @@ bool annot_t::load( const std::string & f , edf_t & parent_edf )
bool readon = false;

std::string ch;

interval_t interval = get_interval( line , tok ,
&ch ,
&readon ,
parent_edf , a , starttime , f ,
parent_edf , a ,
starttime , startdatetime,
f ,
align_annots );

//
Expand Down Expand Up @@ -1188,18 +1189,48 @@ bool annot_t::load( const std::string & f , edf_t & parent_edf )
else
{
std::vector<std::string> ntok = Helper::parse( buffer , globals::allow_space_delim ? " \t" : "\t" );

if ( ntok.size() == 0 )
Helper::halt( "invalid line following '...' end timepoint" );

// allow diff formats
if ( ntok.size() != 6 )
{
if ( ntok.size() == 4 )
{
ntok.resize( 6 );
ntok[5] = ".";
ntok[4] = ntok[3];
ntok[3] = ntok[2];
ntok[2] = ".";
}
else if ( ntok.size() == 3 )
{
ntok.resize( 6 );
ntok[5] = ".";
ntok[4] = ntok[2];
ntok[3] = ntok[1];
ntok[2] = ".";
ntok[1] = ".";
}
else
Helper::halt ( "expecting 6/4/3 columns, but found "
+ Helper::int2str( (int) ntok.size() )
+ "\n (hint: use the 'tab-only' option to ignore space delimiters)\n"
+ "line [ " + buffer + "]" );

}

std::string nch;
bool dummy;

// only need to get the start of the interval here:
interval_t ninterval = get_interval( line , ntok ,
&nch,
&dummy,
parent_edf , NULL , starttime , f ,
parent_edf , NULL ,
starttime , startdatetime,
f ,
align_annots );


Expand Down Expand Up @@ -1412,13 +1443,14 @@ interval_t annot_t::get_interval( const std::string & line ,
bool * readon ,
const edf_t & parent_edf ,
annot_t * a ,
const clocktime_t & starttime ,
const clocktime_t & starttime ,
const clocktime_t & startdatetime ,
const std::string & f ,
const bool align_annots
)
{

// std::cout << "[" << line << "]\n";
// std::cout << "[" << line << "]\n";

// 0 class
// 1 instance
Expand Down Expand Up @@ -1567,8 +1599,9 @@ interval_t annot_t::get_interval( const std::string & line ,

// assume this is either:
// single numeric value (in seconds) which is an offset past the EDF start
// OR in clock-time, in hh:mm:ss (24-hour) format

// OR in clock-time, in hh:mm:ss (24-hour) format or dd-mm-yy-hh:mm:ss
// OR in elapsed clock-time in format 0+hh:mm:ss

// with either format, the second column can be a read-on ('...')
// with either format, the second column can be a duration (in secs) if second col starts +

Expand Down Expand Up @@ -1614,76 +1647,117 @@ interval_t annot_t::get_interval( const std::string & line ,

double dbl_start = 0 , dbl_stop = 0;

// start time:
// start time

if ( is_hms1 )
{

clocktime_t atime( start_str );

// 0+hh:mm:ss format
if ( is_elapsed_hhmmss_start )
{

if ( atime.d != 0 )
Helper::halt( "elapsed clock-times cannot contain dates: format = 0+hh:mm:ss" );

// i.e. seconds past 'midnight == start of EDF'
dbl_start = atime.seconds();
//std::cout << " aatime.seconds = " << dbl_start << "\n";

}
else
{

// if dates are specified, check that annot does not start before the EDF start
// otherwose, *assume* that it starts afterwards
// i.e. if start = 10pm, then 9pm --> 23 hours later, assumed the next day

// h:m:s specification does not work for >12 hours recordings

// 1: EDF start comes before ANNOT start

// 2: annot start comes before EDF start
// --> need to ignore by
// setting special flag interval_t(1,0)

int earlier = clocktime_t::earlier( starttime , atime );
// if start is before EDF start, flag that ( special flag interval interval_t(1,0)

//std::cout << " earlier ?? " << earlier << "\n";
// day information specified?

if ( earlier == 2 ) // allow for this to be the latter time
if ( startdatetime.d != 0 && atime.d != 0 )
{
int earlier = clocktime_t::earlier( startdatetime , atime );
if ( earlier == 2 )
before_edf_start = true;
else
dbl_start = clocktime_t::ordered_difference_seconds( startdatetime , atime ) ;
}
else if ( startdatetime.d == 0 && atime.d != 0 )
{
before_edf_start = true;
// i.e. take the next occasion
// ??? OLD dbl_start = 24*60*60 - clocktime_t::difference_seconds( starttime , atime ) ;
// do not allow date info in annot if EDF start is null
Helper::halt( "cannot specify annotations with date-times if the EDF start date is null (1.1.85)" );
}
else
else
{


// std::cout << "sT1 " << starttime.as_string() << "\t"
// << atime.as_string() << "\n";

// std::cout << "T1 " << starttime.as_datetime_string() << "\t"
// << atime.as_datetime_string() << "\n";

// otherwise, no date information for the annotation, so
// a) ignore date of EDF start and
// b) assume that the time is the next to occur

dbl_start = clocktime_t::ordered_difference_seconds( starttime , atime ) ;

//std::cout << " dbl_start (X) = " << dbl_start << "\n";
}
//std::cout <<" now = " <<dbl_start << "\n";


}

}
else
{
// if here, we are assuming this is not a hh:mm:ss format time,
// if here, we are assuming this is not a (dd-mm-yy-)hh:mm:ss format time,
// so assume this is seconds

if ( ! Helper::str2dbl( start_str , &dbl_start ) )
Helper::halt( "invalid interval (start) : " + line );

}



// stop time:
if ( is_hms2 )
{

clocktime_t btime( stop_str );

if ( is_elapsed_hhmmss_stop )
dbl_stop = btime.seconds(); // was ealpsed [hh:mm:ss]
{
// was elapsed 0+hh:mm:ss

if ( btime.d != 0 )
Helper::halt( "elapsed clock-times cannot contain dates: format = 0+hh:mm:ss" );

dbl_stop = btime.seconds();
}
else
dbl_stop = clocktime_t::ordered_difference_seconds( starttime , btime ) ; // was clocktime
{
// date-time available for stop and EDF start?

if ( startdatetime.d != 0 && btime.d != 0 )
{

int earlier = clocktime_t::earlier( startdatetime , btime );

if ( earlier == 2 )
before_edf_start = true;
else
dbl_stop = clocktime_t::ordered_difference_seconds( startdatetime , btime ) ;
}
else if ( startdatetime.d == 0 && btime.d != 0 )
{
Helper::halt( "cannot specify annotations with date-times if the EDF start date is null (1.1.85)" );
}
else
{
// otherwise, no date information for the annotation, so
// a) ignore date of EDF start and
// b) assume that the time is the next to occur

dbl_stop = clocktime_t::ordered_difference_seconds( starttime , btime ) ;

}

}

}
else if ( col2dur ) // expecting ""
Expand Down
3 changes: 2 additions & 1 deletion annot/annot.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ struct annot_t
bool * ,
const edf_t & ,
annot_t * ,
const clocktime_t & ,
const clocktime_t & , // time only
const clocktime_t & , // date time
const std::string & ,
const bool align_annots
);
Expand Down
11 changes: 10 additions & 1 deletion defs/defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ int globals::sample_list_min;
int globals::sample_list_max;
std::string globals::sample_list_id;

bool globals::anon;
std::string globals::force_starttime;
std::string globals::force_startdate;

bool globals::write_naughty_list;
std::string globals::naughty_list;

Expand Down Expand Up @@ -205,7 +209,7 @@ void globals::init_defs()

version = "v0.28.0";

date = "14-Mar-2022";
date = "10-Apr-2023";

//
// Return code
Expand Down Expand Up @@ -558,6 +562,11 @@ void globals::init_defs()
sample_list_max = -1;
sample_list_id = "";

anon = false;

force_starttime = "";
force_startdate = "";

write_naughty_list = false;
naughty_list = "";

Expand Down
4 changes: 4 additions & 0 deletions defs/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ struct globals
static int sample_list_min;
static int sample_list_max;
static std::string sample_list_id;

static bool anon;
static std::string force_starttime;
static std::string force_startdate;

static bool write_naughty_list;
static std::string naughty_list;
Expand Down
39 changes: 36 additions & 3 deletions edf/edf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ std::set<int> edf_header_t::read( FILE * file , edfz_t * edfz , const std::set<s

}


// Number and direction of records/signals

nr = edf_t::get_int( &q , 8 );
Expand Down Expand Up @@ -1453,7 +1454,35 @@ bool edf_t::attach( const std::string & f ,

inp_signals_n = header.read( file , edfz , inp_signals );


//
// anon header info?
//

if ( globals::anon )
{
// ID, recording info and startdate --> NULL
header.patient_id = header.edfplus ? "X X X X" : ".";
header.recording_info = header.edfplus ? "Startdate X X X X" : ".";
header.startdate = "01.01.85";
}

//
// force EDF start times/dates
//

if ( globals::force_starttime != "" )
{
header.starttime = globals::force_starttime;
logger << " forced start-time to " << header.starttime << "\n";
}

if ( globals::force_startdate != "" )
{
header.startdate = globals::force_startdate;
logger << " forced start-date to " << header.startdate << "\n";
}

//
// Swap out any signal label aliases at this point
//
Expand Down Expand Up @@ -1573,17 +1602,21 @@ bool edf_t::attach( const std::string & f ,

if ( ! silent )
{
logger << " duration: " << Helper::timestring( timeline.total_duration_tp , '.' , false ) // not fractional
<< " | " << timeline.total_duration_tp * globals::tp_duration << " secs";

logger << " duration " << Helper::timestring( timeline.total_duration_tp , '.' , false ) // not fractional
<< ", " << timeline.total_duration_tp * globals::tp_duration << "s";

clocktime_t et( header.starttime );

if ( et.valid )
{
// nb. going to one past end:
double time_sec = ( (timeline.last_time_point_tp+1LLU) * globals::tp_duration ) ;
et.advance_seconds( time_sec );
logger << " | clocktime " << header.starttime << " - " << et.as_string() ;
logger << " | time " << header.starttime << " - " << et.as_string() ;
}

logger << " | date " << header.startdate;
logger << "\n";

// << " hms, last time-point " << Helper::timestring( ++timeline.last_time_point_tp ) << " hms after start\n";
Expand Down
Loading

0 comments on commit f209d9e

Please sign in to comment.