logo
Tags down

shadow

Check if there are consecutive points in Java list greater than a threshold


By : Asad Chowdhury
Date : July 31 2020, 02:00 AM
I wish this help you It's easiest just to do it with an enhanced for loop, keeping a count of the number of elements you have seen in a contiguous run:
code :
int count = 0;
for (double d : list) {
  if (d >= threshold) {
    // Increment the counter, value was big enough.
    ++count;
    if (count >= 3) {
      return true;
    }
  } else {
    // Reset the counter, value too small.
    count = 0;
  }
}
return false;


Share : facebook icon twitter icon

Count the no of Consecutive no greater then threshold value but groupwise in a Given data frame


By : corona newbie
Date : March 29 2020, 07:55 AM
wish of those help We can use data.table. Convert the 'data.frame' to 'data.table' (setDT(df)), create a grouping variable using the run-length-id of the logical vector (temp >= 40), Grouped by 'tag', 'grp' and set the i with logical condition, we assign 'status' as the sequence of rows (seq_len(.N)), and convert the 'NA' elements in 'status' to 0
code :
library(data.table) 
setDT(df)[, grp := rleid(temp >= 40)][temp >= 40, status := seq_len(.N) , .(tag, grp)
             ][is.na(status), status := 0][]
head(df, 20)    
#    tag temp grp status
# 1:   1   43   1      1
# 2:   1   44   1      2
# 3:   1   45   1      3
# 4:   1   41   1      4
# 5:   1   43   1      5
# 6:   1   38   2      0
# 7:   1   40   3      1
# 8:   1   41   3      2
# 9:   1   39   4      0
#10:   1   37   4      0
#11:   2   37   4      0
#12:   2   39   4      0
#13:   2   45   5      1
#14:   2   42   5      2
#15:   2   41   5      3
#16:   2   43   5      4
#17:   2   44   5      5
#18:   2   39   6      0
#19:   2   38   6      0
#20:   2   37   6      0
df$status <-  with(df, ave(temp >= 40, tag, FUN = function(x) {
           rl <- rle(x)
          with(rl, sequence(lengths) * rep(values, lengths))}))
df$status 
#[1] 1 2 3 4 5 0 1 2 0 0 0 0 1 2 3 4 5 0 0 0 1 2 3 4 5
#[26] 0 1 2 0 0 0 0 1 2 3 4 5 0 0 0 1 2 3 4 5 0 1 2 0 0 0 0 1 2 3 4 5 0 0 0

Removing points from list if distance between 2 points is below a certain threshold


By : Dion Ng
Date : March 29 2020, 07:55 AM
I wish this helpful for you Not a fancy one-liner, but you can just iterate the values in the list and append them to some new list if the current value is greater than the last value in the new list, using [-1]:
code :
lst = range(10)
diff = 3

new = []
for n in lst:
    if not new or abs(n - new[-1]) >= diff:
        new.append(n)
if not new or ((n[0]-new[-1][0])**2 + (n[1]-new[-1][1])**2)**.5 >= diff:
lst = [complex(x,y) for x,y in lst]

new = []
for n in lst:
    if not new or abs(n - new[-1]) >= diff:  # same as in the first version
        new.append(n)
print(new)

find points in vector where change is greater than threshold


By : Nico
Date : March 29 2020, 07:55 AM
wish helps you I want to find positions in a vector where the value differs by more than some threshold value from an earlier point in the vector. The first change-point should be measured relative to the first value in the vector. Subsequent change-points should be measured relative to the previous change-point. , Implementing the same code in Rcpp can help with speed.
code :
library(Rcpp)
cppFunction(
  "IntegerVector foo(NumericVector vect, double difference){
    int start = 0;
    IntegerVector changepoints;
    for (int i = 0; i < vect.size(); i++){
      if((vect[i] - vect[start]) > difference || (vect[start] - vect[i]) > difference){
        changepoints.push_back (i+1);
        start = i;        
      }
    }
    return(changepoints);
  }"
  )

foo(vect = x, difference = mindiff)
# [1]  17  25  56  98 108 144 288 297 307 312 403 470 487

identical(foo(vect = x, difference = mindiff), changepoints)
#[1] TRUE
#DATA
set.seed(123)
x = cumsum(rnorm(1e5))
mindiff = 5.0

library(microbenchmark)
microbenchmark(baseR = {start = x[1]
changepoints = integer()

for (i in 1:length(x)) {
    if (abs(x[i] - start) > mindiff) {
        changepoints = c(changepoints, i)
        start = x[i]
    }
}}, Rcpp = foo(vect = x, difference = mindiff))
#Unit: milliseconds
#  expr        min        lq      mean    median        uq      max neval cld
# baseR 117.194668 123.07353 125.98741 125.56882 127.78463 139.5318   100   b
#  Rcpp   7.907011  11.93539  14.47328  12.16848  12.38791 263.2796   100  a 

Find three consecutive numbers greater than threshold group-wise in R


By : CookieMonsderp
Date : March 29 2020, 07:55 AM
I hope this helps . If it's above the threshold and it's the third such value in a row, capture the index in ends. Select the first index in ends and add one to get the index of the return time. (There may be more than 1 such group of 3 and therefore more than one element of ends. In this case, the first end needs to be used.)
Note: In your example, the speed at return time is always above the threshold. This code does not check that as a condition at all, but simply gives the first time after three rows with speeds above threshold (regardless of whether the speed at that time is still above the threshold).
code :
library(data.table)
setDT(df)

speed_thresh <- 35

df[, {above <- Speed > speed_thresh
      ends <- which(above & rowid(rleid(above)) == 3)
      .(Return_Time = Time[ends[1] + 1])}
   , Group]


#    Group Return_Time
# 1:     1          35
# 2:     2          25
# 3:     3          NA
df <- fread('
Group   Time    Speed
1       5       25
1       10      23
1       15      21
1       20      40 
1       25      42 
1       30      52 
1       35      48 
1       40      45
2       5       22
2       10      36 
2       15      38 
2       20      46 
2       25      53 
3       5       45
3       10      58 
')

Check if there are 3 consecutive values in an array which are above some threshold


By : amie
Date : March 29 2020, 07:55 AM
Does that help Approach #1
Use convolution on the mask of boolean array obtained after comparison -
code :
In [40]: a # input array
Out[40]: array([ 1,  3,  4,  5, 60, 43, 53,  4, 46, 54, 56, 78])

In [42]: N = 3 # compare N consecutive numbers

In [44]: T = 40 # threshold for comparison

In [45]: np.flatnonzero(np.convolve(a>T, np.ones(N, dtype=int),'valid')>=N)
Out[45]: array([4, 8, 9])
In [77]: from scipy.ndimage.morphology import binary_erosion

In [31]: np.flatnonzero(binary_erosion(a>T,np.ones(N, dtype=int), origin=-(N//2)))
Out[31]: array([4, 8, 9])
m = a>T
out = np.flatnonzero(m[:-2] & m[1:-1] & m[2:])
In [78]: a
Out[78]: array([ 1,  3,  4,  5, 60, 43, 53,  4, 46, 54, 56, 78])

In [79]: a = np.tile(a,100000)

In [80]: N = 3

In [81]: T = 40

# Approach #3
In [82]: %%timeit
    ...: m = a>T
    ...: out = np.flatnonzero(m[:-2] & m[1:-1] & m[2:])
1000 loops, best of 3: 1.83 ms per loop

# Approach #1
In [83]: %timeit np.flatnonzero(np.convolve(a>T, np.ones(N, dtype=int),'valid')>=N)
100 loops, best of 3: 10.9 ms per loop

# Approach #2    
In [84]: %timeit np.flatnonzero(binary_erosion(a>T,np.ones(N, dtype=int), origin=-(N//2)))
100 loops, best of 3: 11.7 ms per loop
Related Posts Related Posts :
  • Doesn't Access to external storage in API-29
  • How to add a button to the Android system navigation bar?
  • If inside for-each-loop to Stream
  • Shift array to the right by one slot
  • How to setup a Java development environment for Z3
  • how to make notify() works properly with wait()
  • IllegalFormatConversionException d != java.lang.Integer when trying to printf date?
  • Duplicate logic in backend and frontend with Domain Driven Design
  • How would I randomize an int[][]?
  • StringDate to Date coming in different Time in SimpleDateFormat in java
  • Get content uri from file path in android for video
  • function param issue in Scala 11, works in Scala 12 using resilience4j
  • Android unit testing : calling a method from fragment which uses context
  • How to remove E notation from Double value
  • Check if input is any of the values in an array
  • DateTimeParseException - could not be parsed at index 0
  • How to make a Java HTTP async request?
  • Make Javafx/Scenebuilder recognize tagged fields and methods when splitting the MainController into multiple files?
  • Why does this time fail to be parsed by DateTimeFormatter.ofPattern()?
  • Java - Merge objects of list given a condition
  • How to know if arraylist contains value in property of object?
  • Java modelling class if objects have same properties but they are fundamentally different
  • False Positive and False Negative
  • Java UnknownFormatConversionException = ','
  • Apache POI recording only 1 row in the spreadsheet
  • Getting Constructor from Java Class that would be called for argument types, not requiring exact argument & paramete
  • @JsonIgnore with Conditions
  • Does synchronizing on the static field that you are modifying make your code thread safe?
  • Compiler errors while trying to make a pop up
  • Trying to take two timestamps, subtract them, convert the difference to seconds and store in an int variable
  • How can I use Optionals instead of nested if statement?
  • How do I compare values in parallel arrays to eliminate duplicates?
  • make a method which returns the number of one digit numbers in an array
  • Does calling multiple times save() method of hibernate with same object insert new record in DB?
  • Single Number solving by Haspmap, return always be a "@"
  • What do you do about the JLabel classes? It says, "JLabel not a statement" for the error
  • Functional Interface call for a new Instance
  • When I tried to add ArrayList into ArrayList second ArrayList is repeating
  • How can I print the longest word from a user defined list?
  • enabling authentication in ignite
  • Providing OSGi Service Without Implementing Interface
  • Read manifest from JAR with gradle
  • Create one element Set the same way (if applicable) as create single element list
  • pass by reference an integer in java
  • how to generate the POI excel file in a specific location?
  • Issue trying to create a Bubble Sort using ArrayList<Integer>
  • How can I avoid typechecking in this situation
  • How to create a map<k,v> between first and last nested object using Java streams?
  • First time writing junit: Need suggestions
  • what is the best solution to split in chunk monthly given two dates startTime and EndTime in Java?
  • View Binding causes "The specified child already has a parent"
  • parse float in java with trailing junk
  • How to convert color from ColorPicker to String value in javaFX
  • Spring test integration not run how I deal with this error?
  • Java 8 Streams : Count the occurrence of elements(List<String> list1) from list of text data(List<String> li
  • What is the difference between static and protected in java?
  • 3 out of 4 conditions in regex java
  • Returning an array from a method in java?
  • problem while inserting hindi text in mysql using wildfly
  • Would a thread handling a subsequent network request be guaranteed to see the value of a volatile variable written durin
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org