def download_model_nwb_if_model_exists_in_SDD(ends_with, specimen_id_directory,
                                              s):
    '''
    Downloads the .nwb file and returns it's path for the specified model if the 
    corresponding model exists in the structured data directory
    inputs:
        ends_with: string that is to be matched with a file in the structured data directory in 
            order to return a value. i.e. if GLIF2 is being requested but there is not GLIF2 file in the
            structured data directory, a nan will be returned regardless of whether there is a value of explain 
            variance in the database. For example this would happen if the model was excluded from 
            analysis because of an aberrant parameter.  
        specimen_id_directory: path to the structured data directory used in the rest of analysis
        s: search string in the neuronal model name
    returns:
        either a nan or the path of the Allen Institute model .nwb file.
        
    '''
    sp_id = os.path.basename(specimen_id_directory)[:9]
    if sp_id == '580895033':
        print 'skipping 580895033 which is not in the api, returning np.nan'
        return np.nan
    if len(glif_api.get_neuronal_models(
            int(sp_id))) > 1:  #basic check that data is the form expected
        raise Exception('why is there more than one list for %d' % sp_id)
    # get models for the neuron
    nms = glif_api.get_neuronal_models(int(sp_id))[0]['neuronal_models']
    wkf_ids = [[
        m['name'], m['neuronal_model_runs'][0]['well_known_files'][0]['id']
    ] for m in nms]  #list
    if np.any(
        [f.endswith(ends_with) for f in os.listdir(specimen_id_directory)]):
        LIF_binary = [s in p[0] for p in wkf_ids]
        if np.any(LIF_binary):
            base_path = os.path.join(os.path.dirname(os.getcwd()),
                                     'model_nwb_files')
            download_path = os.path.join(
                base_path, sp_id + '_' + ends_with[1:7] + 'model.nwb')
            # if the file does not already exist locally, download it
            try:
                os.stat(base_path)
            except:
                os.mkdir(base_path)
            if not os.path.isfile(download_path):
                wkf_id = wkf_ids[np.where(LIF_binary)[0][0]][1]
                url = Api().construct_well_known_file_download_url(wkf_id)
                Api().retrieve_file_over_http(url, download_path)
            return download_path
        else:
            raise Exception('there is %s in the directory but not in the api' %
                            s)
    else:
        return np.nan
Exemple #2
0
def api():
    ju.read_url_post = \
        MagicMock(name='read_url_post',
                  return_value={ 'whatever': True })

    api = Api()

    return api
Exemple #3
0
def timeout_download_api():
    error_timeout = socket.timeout

    urllib_request.urlopen = Mock(side_effect=error_timeout)

    api = Api()
    api._log.error = Mock()
    # api.retrieve_file_over_http = MagicMock()

    return api
Exemple #4
0
class ApiTests(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(ApiTests, self).__init__(*args, **kwargs)

    def setUp(self):
        self.api = Api()

    def tearDown(self):
        self.api = None

    def test_do_query_post(self):
        ju.read_url_post = MagicMock(name="read_url_post", return_value={"whatever": True})

        self.api.do_query(lambda *a, **k: "http://localhost/%s" % (a[0]), lambda d: d, "wow", post=True)

        ju.read_url_post.assert_called_once_with("http://localhost/wow")

    def test_do_query_get(self):
        ju.read_url_get = MagicMock(name="read_url_get", return_value={"whatever": True})

        self.api.do_query(lambda *a, **k: "http://localhost/%s" % (a[0]), lambda d: d, "wow", post=False)

        ju.read_url_get.assert_called_once_with("http://localhost/wow")

    def test_load_api_schema(self):
        ju.read_url_get = MagicMock(name="read_url_get", return_value={"whatever": True})

        self.api.load_api_schema()

        ju.read_url_get.assert_called_once_with("http://api.brain-map.org/api/v2/data/enumerate.json")
Exemple #5
0
def failed_download_api():
    error404 = urllib_error.HTTPError(code=404,
                                      msg='not found',
                                      hdrs=Mock(),
                                      fp=Mock(),
                                      url='')

    urllib_request.urlopen = Mock(side_effect=error404)

    api = Api()
    api._log.error = Mock()
    # api.retrieve_file_over_http = MagicMock()

    return api
Exemple #6
0
def api():
    return Api()
Exemple #7
0
 def setUp(self):
     self.api = Api()