IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40172


Ignore:
Timestamp:
Oct 16, 2017, 11:32:55 AM (9 years ago)
Author:
eugene
Message:

coded findd_dups

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ohana.20170822/src/opihi/dvo/find_dups.c

    r40171 r40172  
    11# include "data.h"
    2 // NOTE: if there are only a few uniq values, the old algorithm is not bad. 
    3 // for 10000 uniq values, 30M points take ~20sec in the new algorithm,
    4 // 3M points takess 45 sec in the old method.
    52
    6 /* given imageID and detID, make a 64bit joint ID and sort it */
    7 int find_dups (int argc, char **argv) {
     3/* given two 32bit IDs, a 64bit joint ID and sort it, along with an index (sequence) vector */
     4int uniqpair (int argc, char **argv) {
    85
    9   int Nnew, i, N;
    10   Vector *ivec, *ovec;
     6  int N;
     7  Vector *ID1vec = NULL, *ID2vec = NULL, *SEQvec = NULL, *OUTvec = NULL;
    118
    129  int VERBOSE = FALSE;
     
    1613  }
    1714
    18   Vector *cvec = NULL;
     15  Vector *SEQdup = NULL;
     16  if ((N = get_argument (argc, argv, "-save-dups"))) {
     17    remove_argument (N, &argc, argv);
     18    if ((SEQdup = SelectVector (argv[N], ANYVECTOR, TRUE)) == NULL) {
     19      gprint (GP_ERR, "invalid vector %s\n", "SEQdups");
     20      return FALSE;
     21    }
     22    remove_argument (N, &argc, argv);
     23  }
     24
     25  Vector *CNTvec = NULL;
    1926  if ((N = get_argument (argc, argv, "-c"))) {
    2027    remove_argument (N, &argc, argv);
    21     if ((cvec = SelectVector (argv[N], ANYVECTOR, TRUE)) == NULL) {
     28    if ((CNTvec = SelectVector (argv[N], ANYVECTOR, TRUE)) == NULL) {
    2229      gprint (GP_ERR, "invalid vector %s\n", argv[N]);
    2330      return FALSE;
     
    2633  }
    2734
    28   if (argc != 3) {
    29     gprint (GP_ERR, "USAGE: find_dups (ID1) (ID2) (sequence) (out) -c count\n");
     35  if (argc != 5) {
     36    gprint (GP_ERR, "USAGE: uniqpair (ID1) (ID2) (IDout) -c count\n");
    3037    return (FALSE);
    3138  }
    3239
    3340  if ((ID1vec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
    34   if ((ID2vec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) return (FALSE);
    35   if ((SEQvec = SelectVector (argv[3], ANYVECTOR, TRUE)) == NULL) return (FALSE);
     41  if ((ID2vec = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
     42  if ((OUTvec = SelectVector (argv[3], ANYVECTOR, TRUE)) == NULL) return (FALSE);
    3643
    37   if ((ID1vec->type == OPIHI_FLT) || (ID2vec->type == OPIHI_FLT) || (ID1vec->type == OPIHI_FLT)) {
    38     gprint (GP_ERR, "ERROR: ID and sequence vectors much be int\n");
     44  if ((ID1vec->type == OPIHI_FLT) || (ID2vec->type == OPIHI_FLT)) {
     45    gprint (GP_ERR, "ERROR: ID vectors much be int\n");
     46    return (FALSE);
     47  }
     48  if (ID1vec->Nelements != ID2vec->Nelements) {
     49    gprint (GP_ERR, "ERROR: ID vectors not all the same length\n");
    3950    return (FALSE);
    4051  }
    4152
    42   /* allocate the maximum possible needed */
    43   ResetVector (ovec, ivec->type, ivec->Nelements);
    44   if (cvec) {
    45     ResetVector (cvec, OPIHI_INT, ivec->Nelements);
     53  // storage for the output and count vectors
     54  int Nout = 0;
     55  int NOUT = 10000;
     56  ResetVector (OUTvec, OPIHI_INT, NOUT);
     57  if (CNTvec) {
     58    ResetVector (CNTvec, OPIHI_INT, NOUT);
    4659  }
    4760
    48   Nnew = 0;
    49 
    50   // generate the joined vector
    51 
     61  // storage for the duplicate sequences
     62  int NseqDup = 0;
     63  int NSEQDUP = 10000;
     64  if (SEQdup) {
     65    ResetVector (SEQdup, OPIHI_INT, NSEQDUP);
     66  }
    5267 
    53   // copy the input data to a temporary array to avoid damaging it with sort
     68  // incrementing pointers to the input IDs
    5469  opihi_int *ID1 = ID1vec->elements.Int;
    5570  opihi_int *ID2 = ID2vec->elements.Int;
    5671
     72  // generate the joined and sequence vectors
    5773  opihi_int *IDfull = NULL;
     74  opihi_int *SEQdata = NULL;
    5875  ALLOCATE (IDfull, opihi_int, ID1vec->Nelements);
    59   for (i = 0; i < ID1vec->Nelements; i++) {
     76  ALLOCATE (SEQdata, opihi_int, SEQvec[0].Nelements);
     77  for (int i = 0; i < ID1vec->Nelements; i++) {
    6078    IDfull[i] = ID1[i] + (ID2[i] << 32);
     79    SEQdata[i] = i;
    6180  }
    6281
    63   llsorttwo (IDfull, SEQvec->elements.Int, ivec->Nelements);
     82  llsorttwo (IDfull, SEQdata, ID1vec->Nelements);
    6483
    65   Nnew = 0;
    66   opihi_int *vtgt = ovec[0].elements.Int;
    67 
     84  opihi_int *vtgt = OUTvec->elements.Int;
    6885  opihi_int *vsrc = IDfull;
    69   opihi_int *vidx = SEQvec->elements.Int;
    7086
    7187  int onePercent = ID1vec->Nelements / 100;
    7288
    73   /* ADD sequence to output
    74      do not allocate all memory to start
    75    */
    76 
    7789  struct sigaction *old_sigaction = SetInterrupt();
    78   for (i = 0; (i < ID1vec->Nelements) && !interrupt; Nnew++) {
    79     vtgt[Nnew] = *vsrc;
     90  for (int i = 0; (i < ID1vec->Nelements) && !interrupt; Nout++) {
     91    if (Nout >= NOUT) {
     92      NOUT += 10000;
     93      REALLOCATE (vtgt, opihi_int, NOUT);
     94      if (CNTvec) {
     95        REALLOCATE (CNTvec->elements.Int, opihi_int, NOUT);
     96      }
     97    }
     98    vtgt[Nout] = *vsrc;
    8099    int Ndup = 0;
    81100    opihi_int lastValue = *vsrc;
     
    86105      if (VERBOSE && (i % onePercent == 0)) gprint (GP_ERR, ".");
    87106    }
    88     if (cvec) {
    89       cvec->elements.Int[Nnew] = Ndup;
     107    if (CNTvec) {
     108      CNTvec->elements.Int[Nout] = Ndup;
     109    }
     110    if (SEQdup && (Ndup > 1)) {
     111      // we want to save SEQdata values for the duplicates
     112      // SEQdata[i-Ndup] .. SEQdata[i-N
     113      for (j = i - Ndup; j < i; j++) {
     114        SEQdup->elements.Int[NseqDup] = SEQdata[j];
     115        NseqDup ++;
     116        if (NseqDup >= NSEQDUP) {
     117          NSEQDUP += 10000;
     118          REALLOCATE (SEQdup->elements.Int, opihi_int, NSEQDUP);
     119        }
     120      }
    90121    }
    91122  }
    92123  ClearInterrupt (old_sigaction);
    93124  if (VERBOSE) gprint (GP_ERR, "\n");
    94   free (indata);
    95125
    96   // free up extra memory
    97   ResetVector (ovec, ivec->type, Nnew);
    98   if (cvec) ResetVector (cvec, OPIHI_INT, Nnew);
     126  free (SEQdata);
     127  free (IDfull);
     128
     129  if (SEQdup) {
     130    SEQdup->Nelements = NseqDup;
     131    REALLOCATE (SEQdup->elements.Int, opihi_int, Nseqdup);
     132  }
     133
     134  // fix references and free up extra memory:
     135  OUTvec->elements.Int = vtgt;
     136  ResetVector (OUTvec, OPIHI_INT, Nout);
     137  if (cvec) ResetVector (cvec, OPIHI_INT, Nout);
    99138
    100139  return (TRUE);
Note: See TracChangeset for help on using the changeset viewer.