Friday, August 24, 2018

Saving and incrementally updating nearest-neighbor model in R

There are several nearest neighbor R packages (e.g., FNN, RANN, yaImpute) but none of them seem to allow saving off the NN data structure (cover tree, KD tree etc.) so that the nearest neighbors of new queries can be calculated without reconstructing the whole tree. Are there any such functions in R?

I am looking for a function that returns a data structure that I can update incrementally as new data arrives to perform approximate K nearest neighbor search.

Solved

There is a good reason why no NN package does that.

The reason is that the "NN data structure" necessarily includes all the input data points (in the form of a KD tree), so there is no space savings against the input data. It appears that there would be time savings in not having to re-create the KD-tree for each new input, but this is not the case, alas.

The reason is that the time to build a KD-tree is, in general, worse than linearithmic. This means that, for large inputs, it makes sense to sort the data before building the KD-tree because that will produce the KD-tree faster and it will be better balanced, which will improve the search too (it is also worse than logarithmic, in general). This approach would speed up modeling and evaluation but discourage incremental updates, of course.

Your best bet, I think, if to find a generic KD-tree package and use it instead.


The nabor package lets you build a tree and subsequently perform queries on it. But I don't think it lets you update the tree incrementally.


Monday, August 20, 2018

Background performFetchWithCompletionHandler using Blocks causes crash

I have an app that successfully fetches and displays RSS Feeds that I'd like to add background fetch. I receive: Thread 1 EXC_BAD_ACCESS (code=1, Address=0x10) where indicated below.

In app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //setup background fetch
[application setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum];

return YES;

}

        //background fetch new RSS Feeds
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSLog(@"performFetchWithCompletionHandler");
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MasterViewController *navigationController = [mainStoryboard instantiateViewControllerWithIdentifier:@"MasterView"];
    MasterViewController *viewController = navigationController;

    [viewController fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
        completionHandler(result);
    }];
}

In my main ViewController:

@property (nonatomic, copy) void (^completionHandler)(BOOL);
- (void) fetchNewDataWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler;
- (void) startParsingWithCompletionHandler:(void (^)(BOOL))completionHandler;

-(void)fetchNewDataWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

self.completionHandler = UIBackgroundFetchResultNewData; //completionHandler;
[self startParsingWithCompletionHandler:^(BOOL success){

    if (success) {
        completionHandler(UIBackgroundFetchResultNewData);
        NSLog(@"completionHandler");
    }
    else{
        NSLog(@"error");
    }
}];

}

    - (void) storyIsDone//called when parser completed one rss feed
{
    numberOfCompletedStories ++;
    if (numberOfCompletedStories == self.rssFeedAddresses.count)
    {
            //if all the feeds are done cancel time-out timer
        [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(stopParsing) object: nil];
        [self.activityIndicator stopAnimating];
        storysAreLoading = NO;
        [self.refreshControl endRefreshing];
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reloadRSSfeeds) name: @"ReloadFeeds" object: nil];
        canRefresh = YES;
        NSLog(@"call back");
        self.completionHandler (YES);//crash here:  Thread 1 EXC_BAD_ACCESS (code=1, Address=0x10)
    }//else not yet complete
}

The output I receive is:

performFetchWithCompletionHandler

call back

Solved

self.completionHandler = UIBackgroundFetchResultNewData; 

does not match types. completionHandler is of type (void (^)(UIBackgroundFetchResult)) while UIBackgroundFetchResultNewData is of type NSUInteger.

typedef enum : NSUInteger {
   UIBackgroundFetchResultNewData,
   UIBackgroundFetchResultNoData,
   UIBackgroundFetchResultFailed 
} UIBackgroundFetchResult;

So when you call self.completionHandler (YES), self.completionHandler is an NSUInteger, so NSUInteger(YES) doesn't make much sense.


Try this: Create a variable var completionHandler:((UIBackgroundFetchResult)->Void)! when you receive a push notification set

self.completionHandler = completionHandler.

Then do your background stuff, typically download new data from a server. When this is done, call the completionHandler like this:

if let handler = self.completionHandler{
    UIBackgroundFetchResult.NewData
}
self.completionHandler = nil

Then you should get rid of that warning.


Sunday, August 19, 2018

PostgreSQL: how to display function or trigger or view code for a UNIQUE item?

This question is a followup to some of the answers at this question:

How to display the function, procedure, triggers source code in postgresql?

(also see here: https://stackoverflow.com/a/20549944/538962)

While the answers to this question "work", I am running into a problem.

If I query for a list of functions, and in that query, include a sub-query to return the code for each function, the query breaks when a schema includes two or more functions with the same name (and different function signatures). For example, some_function(arg1 integer) vs some_function(arg1 char): functions have the same name yet different arguments.

Here's the query:

SELECT
            n.nspname AS schema
            , p.proname AS function_or_trigger_name
            -- THIS SUB-QUERY RETURNS MULTIPLE RECORDS IF 
            -- SEPARATE FUNCTIONS HAVE THE SAME NAME AND DIFFERENT SIGNATURES
            , (
                SELECT
                          r.routine_definition 
                FROM
                          information_schema.routines AS r
                WHERE
                          r.specific_schema LIKE n.nspname
                    AND   r.routine_name LIKE p.proname
                    AND   r.routine_schema <> 'information_schema'
                    AND  r.specific_schema <> 'pg_catalog'
              ) AS code_content
            , pg_catalog.pg_get_function_result(p.oid) AS result_data_type
            , pg_catalog.pg_get_function_arguments(p.oid) AS argument_data_types
            , CASE
                WHEN p.proisagg THEN 'agg'
                WHEN p.proiswindow THEN 'window'
                WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
                ELSE 'normal'
              END AS func_type
FROM
            pg_catalog.pg_proc p
LEFT JOIN   pg_catalog.pg_namespace n
     ON     n.oid = p.pronamespace
WHERE
     --     pg_catalog.pg_function_is_visible(p.oid)
            n.nspname <> 'pg_catalog'
     AND    n.nspname <> 'information_schema'
ORDER BY
            schema DESC
            , func_type
            , function_or_trigger_name;

So the question is: is there a way to make a JOIN in the sub-query to prevent duplicates when functions have the same name?

Friday, August 17, 2018

ngAnnotate - Warning: StringMap expected string key

I get this warning when using Grunt and grunt-ng-annotate.

There is no reference in the warning to where the error is in the file, which makes debugging it hard.

Any ideas?

Solved

The issue turned out to be use of ES6 notation, in this case arrow functions (=>), default parameters and let.

I haven't looked in detail as to why ngAnnotate doesn't support this.

To find where the issues were I overrode the ngAnnotate warning with grunt switch --force and later in the build uglify complained about the ES6 syntax with more detail.


I also faced the same problem but in my case, there was a different issue.

One of our team members has initialized function parameter to some default value. Something like the following.

$scope.functionName = function(defaultVar = false){ 
    //some code 
}

and in my gulp script, there was a line

.pipe(plugins.if(release, plugins.ngAnnotate()))

So when I have removed this line the build script automatically printed the error in console pointing to the exact file and line number where the error was.

Finally, I was able to solve it by removing that variable initialization code.

Hope this will help someone...