Skip to content

Commit a0ddcb2

Browse files
committed
change interace function from t_cg to p_cg to 1) solve the efficiency problem for windows; 2) more straightforward to use probability than the chi-square critical value to users
1 parent c4151d3 commit a0ddcb2

File tree

5 files changed

+131
-77
lines changed

5 files changed

+131
-77
lines changed

src/cxx/cold_flex.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ int cold_flex(
4444
int tmask_b2, /* I: the band id used for tmask */
4545
int valid_num_scenes, /* I: number of valid scenes */
4646
int pos, /* I: the position id of pixel */
47-
double pcg, /* I: probability threshold of change threshold */
47+
double t_cg, /* I: threshold for identfying breaks */
48+
double max_t_cg, /* I: threshold for identfying outliers */
4849
int conse, /* I: consecutive observation number */
4950
bool b_outputCM, /* I: indicate if outputting change magnitudes for object-based cold, for cold only, it is the false */
5051
int starting_date, /* I: (optional) the starting date of the whole dataset to enable reconstruct CM_date, all pixels for a tile should have the same date, only for b_outputCM is True */
@@ -67,8 +68,6 @@ int cold_flex(
6768
int i;
6869
char FUNC_NAME[] = "cold_flex";
6970
int result;
70-
double t_cg = X2(nbands, pcg);
71-
7271
if (valid_num_scenes == 0)
7372
{
7473
return (SUCCESS);
@@ -136,7 +135,7 @@ int cold_flex(
136135
/* standard_procedure for CCD */
137136
/* */
138137
/**************************************************************/
139-
result = stand_procedure_flex(valid_num_scenes, valid_date_array, ts_data, fmask_buf, nbands, id_range, t_cg, conse, b_outputCM, starting_date, rec_cg, num_fc, CM_OUTPUT_INTERVAL, CM_outputs,
138+
result = stand_procedure_flex(valid_num_scenes, valid_date_array, ts_data, fmask_buf, nbands, id_range, t_cg, max_t_cg, conse, b_outputCM, starting_date, rec_cg, num_fc, CM_OUTPUT_INTERVAL, CM_outputs,
140139
CM_outputs_date, gap_days, tmask_b1, tmask_b2);
141140
// result = stand_procedure_fixeddays(valid_num_scenes, valid_date_array, buf_b, buf_g, buf_r, buf_n, buf_s1, buf_s2, buf_t, fmask_buf, id_range,
142141
// tcg, conse, b_outputCM, starting_date, rec_cg, num_fc, CM_OUTPUT_INTERVAL, CM_outputs,
@@ -183,7 +182,8 @@ int stand_procedure_flex(
183182
int64_t *fmask_buf, /* I: mask-based time series */
184183
int nbands, /* I: input band number */
185184
int *id_range,
186-
double tcg, /* I: threshold of change threshold */
185+
double t_cg, /* I: threshold for identfying breaks */
186+
double max_t_cg, /* I: threshold for identfying outliers */
187187
int conse, /* I: consecutive observation number */
188188
bool b_outputCM, /* I: indicate if cold is running as the first step of object-based cold*/
189189
int starting_date, /* I: the starting date of the whole dataset to enable reconstruct CM_date, all pixels for a tile should have the same date, only for b_outputCM is True */
@@ -271,7 +271,6 @@ int stand_procedure_flex(
271271
int current_CM_n;
272272
float prob_angle; // change probability for angle
273273
int i_span_skip = 0;
274-
double t_cg_max = X2(nbands, 0.99999);
275274

276275
fit_cft = (float **)allocate_2d_array(nbands, LASSO_COEFFS,
277276
sizeof(float));
@@ -445,7 +444,7 @@ int stand_procedure_flex(
445444
// adj_TCG = tcg;
446445
// }
447446
adj_conse = conse;
448-
adj_TCG = tcg;
447+
adj_TCG = t_cg;
449448

450449
v_dif_mag = (float **)allocate_2d_array(nbands, adj_conse,
451450
sizeof(float));
@@ -1106,7 +1105,7 @@ int stand_procedure_flex(
11061105
}
11071106
break;
11081107
}
1109-
else if (vec_magg[0] > t_cg_max) /* false change */
1108+
else if (vec_magg[0] > max_t_cg) /* false change */
11101109
{
11111110
for (k = i_ini; k < end - 1; k++)
11121111
{
@@ -1851,7 +1850,7 @@ int stand_procedure_flex(
18511850
i_span_skip = 0;
18521851
}
18531852

1854-
else if (vec_mag[0] > t_cg_max) /*false change*/
1853+
else if (vec_mag[0] > max_t_cg) /*false change*/
18551854
{
18561855
/**********************************************/
18571856
/* */

src/cxx/cold_flex.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ int cold_flex(
1414
int tmask_b2, /* I: the band id used for tmask */
1515
int valid_num_scenes, /* I: number of valid scenes */
1616
int pos, /* I: the position id of pixel */
17-
double pcg, /* I: probability threshold of change threshold */
17+
double t_cg, /* I: threshold for identfying breaks */
18+
double max_t_cg, /* I: threshold for identfying outliers */
1819
int conse, /* I: consecutive observation number */
1920
bool b_outputCM, /* I: indicate if outputting change magnitudes for object-based cold, for cold only, it is the false */
2021
int starting_date, /* I: (optional) the starting date of the whole dataset to enable reconstruct CM_date, all pixels for a tile should have the same date, only for b_outputCM is True */
@@ -33,7 +34,8 @@ int stand_procedure_flex(
3334
int64_t *fmask_buf, /* I: mask-based time series */
3435
int nbands, /* I: input band number */
3536
int *id_range,
36-
double tcg, /* I: threshold of change threshold */
37+
double t_cg, /* I: threshold for identfying breaks */
38+
double max_t_cg, /* I: threshold for identfying outliers */
3739
int conse, /* I: consecutive observation number */
3840
bool b_outputCM, /* I: indicate if cold is running as the first step of object-based cold*/
3941
int starting_date, /* I: the starting date of the whole dataset to enable reconstruct CM_date, all pixels for a tile should have the same date, only for b_outputCM is True */

src/cxx/sccd-desktop.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,10 @@ int main(int argc, char *argv[])
773773
}
774774
else if (method == COLD_FLEX)
775775
{
776-
int nbands = 5;
776+
int nbands = 4;
777777
rec_cg_flex = malloc(NUM_FC * sizeof(Output_t_flex));
778+
double t_cg = X2(nbands, pcg);
779+
double max_t_cg = X2(nbands, 0.99999);
778780
if (rec_cg_flex == NULL)
779781
{
780782
RETURN_ERROR("ERROR allocating rec_cg_flex",
@@ -792,7 +794,7 @@ int main(int argc, char *argv[])
792794
}
793795
}
794796

795-
result = cold_flex(buf_stack, fmask_buf, sdate, nbands, 1, 4, valid_scene_count, i_col + 1, pcg, conse, b_outputCM, starting_date, rec_cg_flex, &num_fc, CM_OUTPUT_INTERVAL, CM_outputs,
797+
result = cold_flex(buf_stack, fmask_buf, sdate, nbands, 1, 4, valid_scene_count, i_col + 1, t_cg, max_t_cg, conse, b_outputCM, starting_date, rec_cg_flex, &num_fc, CM_OUTPUT_INTERVAL, CM_outputs,
796798
CM_outputs_date, gap_days);
797799

798800
// snprintf (msg_str, sizeof(msg_str), "pixel %d COLD calculation finished\n", i_col+1);

src/python/pycold/_colds_cython.pyx

+13-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from libc.stdint cimport int32_t, int64_t, uint32_t, uint64_t
1515
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
1616
from .common import reccg_dt, reccg_dt_flex, sccd_dt, nrtqueue_dt, nrtmodel_dt, pinpoint_dt
1717
np.import_array()
18+
from scipy.stats import chi2
1819

1920
try:
2021
import typing
@@ -103,11 +104,12 @@ cdef extern from "../../cxx/cold.h":
103104
short int *cm_outputs_date, double gap_days);
104105

105106
cdef extern from "../../cxx/cold_flex.h":
106-
cdef int32_t cold_flex(int64_t *ts_stack, int64_t *fmask_buf, int64_t *valid_date_array, int nbands, int tmask_b1, int tmask_b1,
107-
int32_t valid_num_scenes, int32_t pos, double tcg, int32_t conse,
108-
bool b_output_cm, int32_t starting_date, Output_t_flex *rec_cg,
109-
int32_t *num_fc, int32_t cm_output_interval, short int *cm_outputs,
110-
short int *cm_outputs_date, double gap_days);
107+
cdef int32_t cold_flex(int64_t *ts_stack, int64_t *fmask_buf, int64_t *valid_date_array, int nbands,
108+
int tmask_b1, int tmask_b1, int32_t valid_num_scenes, int32_t pos,
109+
double tcg, double max_tcg, int32_t conse, bool b_output_cm,
110+
int32_t starting_date, Output_t_flex *rec_cg, int32_t *num_fc,
111+
int32_t cm_output_interval, short int *cm_outputs,
112+
short int *cm_outputs_date, double gap_days);
111113

112114

113115
cdef extern from "../../cxx/cold.h":
@@ -601,7 +603,7 @@ cpdef _sccd_update(sccd_pack,
601603

602604
cpdef _cold_detect_flex(np.ndarray[np.int64_t, ndim=1, mode='c'] dates, np.ndarray[np.int64_t, ndim=1, mode='c'] ts_stack,
603605
np.ndarray[np.int64_t, ndim=1, mode='c'] qas, int32_t valid_num_scenes, int32_t nbands,
604-
double p_cg = 0.99, int32_t conse=6, int32_t pos=1, bint b_output_cm=False,
606+
double t_cg, double max_t_cg, int32_t conse=6, int32_t pos=1, bint b_output_cm=False,
605607
int32_t starting_date=0, int32_t n_cm=0, int32_t cm_output_interval=0,
606608
double gap_days=365.25, int32_t tmask_b1=1, int32_t tmask_b2=1):
607609
"""
@@ -614,7 +616,8 @@ cpdef _cold_detect_flex(np.ndarray[np.int64_t, ndim=1, mode='c'] dates, np.ndarr
614616
qas: 1d array, the QA cfmask bands. '0' - clear; '1' - water; '2' - shadow; '3' - snow; '4' - cloud
615617
valid_num_scenes: the number of valid observations
616618
nbands: the number of inputted bands
617-
p_cg: threshold of change magnitude, default is chi2.ppf(0.99,5)
619+
t_cg: threshold for change magnitude
620+
max_t_cg: threshold for identifying outlier
618621
pos: position id of the pixel
619622
conse: consecutive observation number
620623
b_output_cm: bool, 'True' means outputting change magnitude and change magnitude dates, only for object-based COLD
@@ -657,9 +660,9 @@ cpdef _cold_detect_flex(np.ndarray[np.int64_t, ndim=1, mode='c'] dates, np.ndarr
657660
cdef short [:] cm_outputs_view = cm_outputs # memory view
658661
cdef short [:] cm_outputs_date_view = cm_outputs_date # memory view
659662
result = cold_flex(&ts_stack_view[0], &qas_view[0], &dates_view[0], nbands, tmask_b1, tmask_b2,
660-
valid_num_scenes, pos, p_cg, conse, b_output_cm, starting_date, &rec_cg_view[0],
661-
&num_fc, cm_output_interval, &cm_outputs_view[0], &cm_outputs_date_view[0],
662-
gap_days)
663+
valid_num_scenes, pos, t_cg, max_t_cg, conse, b_output_cm, starting_date,
664+
&rec_cg_view[0], &num_fc, cm_output_interval, &cm_outputs_view[0],
665+
&cm_outputs_date_view[0], gap_days)
663666
if result != 0:
664667
raise RuntimeError("cold function fails for pos = {} ".format(pos))
665668
else:

0 commit comments

Comments
 (0)