示例#1
0
def update_patchs2sheet(service,
                        sheet_id,
                        starting_position,
                        data,
                        debug=True):
    '''
    update a list of list data to a google sheet continuously

    parameters:
        service:    a service request to google sheet
        sheet_di:   a string to identify the sheet uniquely
        starting_position:      a string existing in the sheet to represent the let-top corner of patch to fill in
        data:                   a list of list data to fill
    '''

    if debug:
        isstring(sheet_id), 'the sheet id is not a string'
        isstring(starting_position), 'the starting position is not correct'
        islistoflist(data), 'the input data is not a list of list'

    # How the input data should be interpreted.
    value_input_option = 'RAW'  # TODO: Update placeholder value.

    value_range_body = {'values': data}
    request = service.spreadsheets().values().update(
        spreadsheetId=sheet_id,
        range=starting_position,
        valueInputOption=value_input_option,
        body=value_range_body)
    response = request.execute()
示例#2
0
def safe_2dptsarray(input_pts,
                    homogeneous=False,
                    dimen_add=0,
                    warning=True,
                    debug=True):
    '''
	make sure to copy the pts array without modifying it and make the dimension to 2(3 if homogenous) x N

	parameters:
		input_pts: 		a list of 2(3 if homogenous) elements, a listoflist of 2 elements: 
						e.g., [[1,2], [5,6]], a numpy array with shape or (2, N) or (2, )
		homogeneous:		the input points are in the homogenous coordinate
		dimen_add:		additional dimension, used to accommdate for higher dimensional array
	
	outputs:
		np_pts:			2 (3 if homogenous) X N numpy array
	'''
    if homogeneous: dimension = 3 + dimen_add
    else: dimension = 2 + dimen_add

    if islist(input_pts):
        if islistoflist(input_pts):
            if debug:
                assert all(
                    len(list_tmp) == dimension for list_tmp in input_pts
                ), 'all sub-lists should have length of %d' % dimension
            np_pts = np.array(input_pts).transpose()
        else:
            if debug:
                assert len(
                    input_pts
                ) == dimension, 'the input pts list does not have a good shape'
            np_pts = np.array(input_pts).reshape((dimension, 1))
    elif isnparray(input_pts):
        input_pts = input_pts.copy()
        if input_pts.shape == (dimension, ):
            np_pts = input_pts.reshape((dimension, 1))
        else:
            np_pts = input_pts
    else:
        assert False, 'only list and numpy array for pts are supported'

    if debug:
        if homogeneous:
            assert is2dptsarray_homogeneous(
                np_pts), 'the input pts array does not have a good shape'
        else:
            assert is2dptsarray(
                np_pts), 'the input pts array does not have a good shape'

    return np_pts
示例#3
0
def safe_center_bbox(input_bbox, warning=True, debug=True):
    '''
	make sure to copy the center bbox without modifying it and make the dimension to N x 4 or N x 2

	parameters:
		input_bbox: 	a list of 4 (2) elements, a listoflist of 4 (2) elements: e.g., [[1,2,3,4], [5,6,7,8]],
						a numpy array with shape or (N, 4) or (4, ) or (N, 2) or (2, )

	outputs:
		np_bboxes:		N X 4 (2) numpy array
	'''
    if islist(input_bbox):
        if islistoflist(input_bbox):
            if debug:
                assert all(
                    len(list_tmp) == 4 or len(list_tmp) == 2 for list_tmp in
                    input_bbox), 'all sub-lists should have length of 4'
            np_bboxes = np.array(input_bbox)
        else:
            if debug:
                assert len(input_bbox) == 4 or len(
                    input_bbox
                ) == 2, 'the center bboxes list does not have a good shape'
            if len(input_bbox) == 4:
                np_bboxes = np.array(input_bbox).reshape((1, 4))
            else:
                np_bboxes = np.array(input_bbox).reshape((1, 2))
    elif isnparray(input_bbox):
        input_bbox = input_bbox.copy()
        if input_bbox.shape == (4, ): np_bboxes = input_bbox.reshape((1, 4))
        elif input_bbox.shape == (2, ): np_bboxes = input_bbox.reshape((1, 2))
        else:
            if debug:
                assert iscenterbbox(
                    input_bbox
                ), 'the input center bbox numpy array does not have a good shape'
            np_bboxes = input_bbox
    else:
        assert False, 'only list and numpy array for bbox are supported'

    return np_bboxes
示例#4
0
def safe_ptsarray_occlusion(input_pts, warning=True, debug=True):
    '''
	make sure to copy the pts array without modifying it and make the dimension to 3 x N
	the occlusion (3rd) row should contain 0, 1 or -1

	parameters:
		input_pts: 	a list of 3 elements, a listoflist of 3 elements: e.g., [[1,2], [5,6], [0, 1]],
						a numpy array with shape or (3, N) or (3, )

	outputs:
		np_pts:		3 X N numpy array, with the third row as the occlusion
	'''
    if islist(input_pts):
        if islistoflist(input_pts):
            if debug:
                assert all(len(list_tmp) == 3 for list_tmp in
                           input_pts), 'all sub-lists should have length of 3'
            np_pts = np.array(input_pts).transpose()
        else:
            if debug:
                assert len(
                    input_pts
                ) == 3, 'the input pts list does not have a good shape'
            np_pts = np.array(input_pts).reshape((3, 1))
    elif isnparray(input_pts):
        input_pts = input_pts.copy()
        if input_pts.shape == (3, ):
            np_pts = input_pts.reshape((3, 1))
        else:
            np_pts = input_pts
    else:
        assert False, 'only list and numpy array for pts are supported'

    if debug:
        assert is2dptsarray_occlusion(
            np_pts), 'the input pts array does not have a good shape'
    return np_pts
示例#5
0
def visualize_distribution(data,
                           bin_size=None,
                           vis=False,
                           save_path=None,
                           debug=True,
                           closefig=True):
    '''
    visualize the histogram of a data, which can be a dictionary or list or numpy array or tuple or a list of list
    '''
    if debug:
        assert istuple(data) or isdict(data) or islist(data) or isnparray(
            data), 'input data is not correct'

    # convert data type
    if istuple(data):
        data = list(data)
    elif isdict(data):
        data = data.values()
    elif isnparray(data):
        data = data.tolist()

    num_bins = 1000.0
    fig, ax = get_fig_ax_helper(fig=None, ax=None)

    # calculate bin size
    if bin_size is None:
        if islistoflist(data):
            max_value = np.max(np.max(data))
            min_value = np.min(np.min(data))
        else:
            max_value = np.max(data)
            min_value = np.min(data)
        bin_size = (max_value - min_value) / num_bins
    else:
        try:
            bin_size = float(bin_size)
        except TypeError:
            print('size of bin should be an float value')

    # plot
    if islistoflist(data):
        max_value = np.max(np.max(data))
        min_value = np.min(np.min(data))
        bins = np.arange(min_value - bin_size, max_value + bin_size,
                         bin_size)  # fixed bin size
        plt.xlim([min_value - bin_size, max_value + bin_size])
        for data_list_tmp in data:
            if debug:
                assert islist(data_list_tmp), 'the nested list is not correct!'
            # plt.hist(data_list_tmp, bins=bins, alpha=0.3)
            sns.distplot(data_list_tmp, bins=bins, kde=False)
            # sns.distplot(data_list_tmp, bins=bins, kde=False)
    else:
        bins = np.arange(
            min(data) - 10 * bin_size,
            max(data) + 10 * bin_size, bin_size)  # fixed bin size
        plt.xlim([min(data) - bin_size, max(data) + bin_size])
        plt.hist(data, bins=bins, alpha=0.5)

    plt.title('distribution of data')
    plt.xlabel('data (bin size = %f)' % bin_size)
    plt.ylabel('count')

    return save_vis_close_helper(fig=fig,
                                 ax=ax,
                                 vis=vis,
                                 save_path=save_path,
                                 debug=debug,
                                 closefig=closefig)