Instruction:
How would you implement a function in Python 3 that
finds the local maxima in the input grid and perform region growing to identify objects.

Response:
def find_local_maxima(self, input_grid):
        """
        Finds the local maxima in the inputGrid and perform region growing to identify objects.

        Args:
            input_grid: Raw input data.

        Returns:
            array with labeled objects.
        """
        pixels, q_data = self.quantize(input_grid)
        centers = OrderedDict()
        for p in pixels.keys():
            centers[p] = []
        marked = np.ones(q_data.shape, dtype=int) * self.UNMARKED
        MIN_INFL = int(np.round(1 + 0.5 * np.sqrt(self.max_size)))
        MAX_INFL = 2 * MIN_INFL
        marked_so_far = []
        # Find the maxima. These are high-values with enough clearance
        # around them.
        # Work from high to low bins. The pixels in the highest bin mark their
        # neighborhoods first. If you did it from low to high the lowest maxima
        # would mark their neighborhoods first and interfere with the identification of higher maxima.
        for b in sorted(pixels.keys(),reverse=True):
            # Square starts large with high intensity bins and gets smaller with low intensity bins.
            infl_dist = MIN_INFL + int(np.round(float(b) / self.max_bin * (MAX_INFL - MIN_INFL)))
            for p in pixels[b]:
                if marked[p] == self.UNMARKED:
                    ok = False
                    del marked_so_far[:]
                    # Temporarily mark unmarked points in square around point (keep track of them in list marked_so_far).
                    # If none of the points in square were marked already from a higher intensity center,
                    # this counts as a new center and ok=True and points will remain marked.
                    # Otherwise ok=False and marked points that were previously unmarked will be unmarked.
                    for (i, j), v in np.ndenumerate(marked[p[0] - infl_dist:p[0] + infl_dist + 1,
                                                    p[1] - infl_dist:p[1]+ infl_dist + 1]):
                        if v == self.UNMARKED:
                            ok = True
                            marked[i - infl_dist + p[0],j - infl_dist + p[1]] = b

                            marked_so_far.append((i - infl_dist + p[0],j - infl_dist + p[1]))
                        else:
                            # neighborhood already taken
                            ok = False
                            break
                    # ok if point and surrounding square were not marked already.
                    if ok:
                        # highest point in its neighborhood
                        centers[b].append(p)
                    else:
                        for m in marked_so_far:
                            marked[m] = self.UNMARKED
        # Erase marks and start over. You have a list of centers now.
        marked[:, :] = self.UNMARKED
        deferred_from_last = []
        deferred_to_next = []
        # delta (int): maximum number of increments the cluster is allowed to range over. Larger d results in clusters over larger scales.
        for delta in range(0, self.delta + 1):
            # Work from high to low bins.
            for b in sorted(centers.keys(), reverse=True):
                bin_lower = b - delta
                deferred_from_last[:] = deferred_to_next[:]
                del deferred_to_next[:]
                foothills = []
                n_centers = len(centers[b])
                tot_centers = n_centers + len(deferred_from_last)
                for i in range(tot_centers):
                    # done this way to minimize memory overhead of maintaining two lists
                    if i < n_centers:
                        center = centers[b][i]
                    else:
                        center = deferred_from_last[i - n_centers]
                    if bin_lower < 0:
                        bin_lower = 0
                    if marked[center] == self.UNMARKED:
                        captured = self.set_maximum(q_data, marked, center, bin_lower, foothills)
                        if not captured:
                            # decrement to lower value to see if it'll get big enough
                            deferred_to_next.append(center)
                        else:
                            pass
                # this is the last one for this bin
                self.remove_foothills(q_data, marked, b, bin_lower, centers, foothills)
            del deferred_from_last[:]
            del deferred_to_next[:]
        return marked


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_maximum(self, q_data, marked, center, bin_lower, foothills):
        as_bin = [] # pixels to be included in peak
        as_glob = []   # pixels to be globbed up as part of foothills
        marked_so_far = []  # pixels that have already been marked
        will_be_considered_again = False
        as_bin.append(center)
        center_data = q_data[center]
        while len(as_bin) > 0:
            p = as_bin.pop(-1) # remove and return last pixel in as_bin
            if marked[p] != self.UNMARKED: # already processed
                continue
            marked[p] = q_data[center]
            marked_so_far.append(p)

            # check neighbors
            for index,val in np.ndenumerate(marked[p[0] - 1:p[0] + 2, p[1] - 1:p[1] + 2]):
                # is neighbor part of peak or part of mountain?
                if val == self.UNMARKED:
                    pixel = (index[0] - 1 + p[0],index[1] - 1 + p[1])
                    p_data = q_data[pixel]
                    if (not will_be_considered_again) and (p_data >= 0) and (p_data < center_data):
                        will_be_considered_again = True
                    if p_data >= bin_lower and (np.abs(center_data - p_data) <= self.delta):
                        as_bin.append(pixel)
                    # Do not check that this is the closest: this way, a narrow channel of globbed pixels form
                    elif p_data >= 0:
                        as_glob.append(pixel)
        if bin_lower == 0:
            will_be_considered_again = False
        big_enough = len(marked_so_far) >= self.max_size
        if big_enough:
            # remove lower values within region of influence
            foothills.append((center, as_glob))
        elif will_be_considered_again: # remove the check if you want to ignore regions smaller than max_size
            for m in marked_so_far:
                marked[m] = self.UNMARKED
            del as_bin[:]
            del as_glob[:]
            del marked_so_far[:]
        return big_enough or (not will_be_considered_again)

Response: This method grows a region at a certain bin level and checks if the maximum size of the object is reached.


Instruction:
Can you generate the documentation for the following Python 3 function
def remove_foothills(self, q_data, marked, bin_num, bin_lower, centers, foothills):
        hills = []
        for foot in foothills:
            center = foot[0]
            hills[:] = foot[1][:]
            # remove all foothills
            while len(hills) > 0:
                # mark this point
                pt = hills.pop(-1)
                marked[pt] = self.GLOBBED
                for s_index, val in np.ndenumerate(marked[pt[0]-1:pt[0]+2,pt[1]-1:pt[1]+2]):
                    index = (s_index[0] - 1 + pt[0], s_index[1] - 1 + pt[1])
                    # is neighbor part of peak or part of mountain?
                    if val == self.UNMARKED:
                        # will let in even minor peaks
                        if (q_data[index] >= 0) and \
                                (q_data[index] < bin_lower) and \
                                ((q_data[index] <= q_data[pt]) or self.is_closest(index, center, centers, bin_num)):
                            hills.append(index)
        del foothills[:]

Response: Removes foothills from a set of objects.


Instruction:
Can you generate the documentation for the following Python 3 function
def quantize(self, input_grid):
        pixels = {}
        for i in range(self.max_bin+1):
            pixels[i] = []

        data = (np.array(input_grid, dtype=int) - self.min_thresh) / self.data_increment
        data[data < 0] = -1
        data[data > self.max_bin] = self.max_bin
        good_points = np.where(data >= 0)
        for g in np.arange(good_points[0].shape[0]):
            pixels[data[(good_points[0][g], good_points[1][g])]].append((good_points[0][g], good_points[1][g]))
        return pixels, data

Response: Quantize a grid into discrete steps based on input parameters.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def edit(self, **args):
		'''
		Doesn't require manual fetching of gistID of a gist
		passing gistName will return edit the gist
		'''
		self.gist_name = ''
		if 'description' in args:
			self.description = args['description']
		else:
			self.description = ''

		if 'name' in args and 'id' in args:
			self.gist_name = args['name']
			self.gist_id = args['id']
		elif 'name' in args:
			self.gist_name = args['name']
			self.gist_id = self.getMyID(self.gist_name)
		elif 'id' in args:
			self.gist_id = args['id']
		else:
			raise Exception('Gist Name/ID must be provided')

		if 'content' in args:
			self.content = args['content']
		else:
			raise Exception('Gist content can\'t be empty')

		if (self.gist_name == ''):
			self.gist_name = self.getgist(id=self.gist_id)
			data = {"description": self.description,
  				"files": {
    				self.gist_name: {
      				"content": self.content
    				}
  				}
  		}
		else:
			data = {"description": self.description,
  				"files": {
    				self.gist_name: {
      				"content": self.content
    				}
  				}
  			}

		if self.gist_id:
			r = requests.patch(
				'%s/gists/%s'%(BASE_URL,self.gist_id),
				headers=self.gist.header,
				data=json.dumps(data),
				)
			if (r.status_code == 200):
				r_text = json.loads(r.text)
				response = {
					'updated_content': self.content,
					'created_at': r.json()['created_at'],
					'comments':r.json()['comments']
				}

				return response

		raise Exception('No such gist found')

Response: This method will edit the gist with the given name and content.


Instruction:
Write a Python 3 function for
deleting a gist by gistname or gistID

Response:
def delete(self, **args):
		'''
		Delete a gist by gistname/gistID
		'''

		if 'name' in args:
			self.gist_name = args['name']
			self.gist_id = self.getMyID(self.gist_name)
		elif 'id' in args:
			self.gist_id = args['id']
		else:
			raise Exception('Provide GistName to delete')

		url = 'gists'
		if self.gist_id:
			r = requests.delete(
				'%s/%s/%s'%(BASE_URL,url,self.gist_id),
				headers=self.gist.header
				)
			if (r.status_code == 204):
				response = {
					'id': self.gist_id,
				}
				return response

		raise Exception('Can not delete gist')


Instruction:
How would you implement a function in Python 3 that
gets a list of starred gists

Response:
def starred(self, **args):
		'''
		List the authenticated user's starred gists
		'''
		ids =[]
		r = requests.get(
			'%s/gists/starred'%BASE_URL,
			headers=self.gist.header
			)

		if 'limit' in args:
			limit = args['limit']
		else:
			limit = len(r.json())

		if (r.status_code == 200):
			for g in range(0,limit ):
				ids.append('%s/%s/%s' %(GIST_URL,r.json()[g]['user']['login'],r.json()[g]['id']))
			return ids

		raise Exception('Username not found')


Instruction:
Write a Python 3 function for
getting Gist URL - Link Clone - Link and Script - Link to embed

Response:
def links(self,**args):
		'''
		Return Gist URL-Link, Clone-Link and Script-Link to embed
		'''
		if 'name' in args:
			self.gist_name = args['name']
			self.gist_id = self.getMyID(self.gist_name)
		elif 'id' in args:
			self.gist_id = args['id']
		else:
			raise Exception('Gist Name/ID must be provided')
		if self.gist_id:
			r = requests.get(
				'%s/gists/%s'%(BASE_URL,self.gist_id),
				headers=self.gist.header,
				)
			if (r.status_code == 200):

				content = {
				'Github-User': r.json()['user']['login'],
				'GistID': r.json()['id'],
				'Gist-Link': '%s/%s/%s' %(GIST_URL,self.gist.username,r.json()['id']),
				'Clone-Link': '%s/%s.git' %(GIST_URL,r.json()['id']),
				'Embed-Script': '<script src="%s/%s/%s.js"</script>' %(GIST_URL,self.gist.username,r.json()['id'])
				}
				return content

		raise Exception('No such gist found')


Instruction:
How would you explain what the following Python 3 function does
def load_forecasts(self):
        run_date_str = self.run_date.strftime("%Y%m%d")
        forecast_file = self.forecast_path + "{0}/{1}_{2}_{3}_consensus_{0}.nc".format(run_date_str,
                                                                                self.ensemble_name,
                                                                                self.model_name,
                                                                                self.forecast_variable)
        print("Forecast file: " + forecast_file)
        forecast_data = Dataset(forecast_file)
        for size_threshold in self.size_thresholds:
            for smoothing_radius in self.smoothing_radii:
                for neighbor_radius in self.neighbor_radii:
                    hour_var = "neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}".format(neighbor_radius, smoothing_radius,
                                                                                self.forecast_variable,
                                                                                float(size_threshold))
                    period_var = "neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}".format(self.end_hour -
                                                                                self.start_hour + 1,
                                                                                neighbor_radius,
                                                                                smoothing_radius,
                                                                                self.forecast_variable,
                                                                                float(size_threshold))

                    print("Loading forecasts {0} {1} {2} {3} {4}".format(self.run_date, self.model_name,
                                                                         self.forecast_variable, size_threshold,
                                                                         smoothing_radius))
                    if hour_var in forecast_data.variables.keys():
                        self.hourly_forecasts[hour_var] = forecast_data.variables[hour_var][:]
                    if period_var in forecast_data.variables.keys():
                        self.period_forecasts[period_var] = forecast_data.variables[period_var][:]
        forecast_data.close()

Response: Load neighborhood probability forecasts.


Instruction:
Write a Python 3 script to
load observations and masking grids.

Response:
def load_obs(self,  mask_threshold=0.5):
        """
        Loads observations and masking grid (if needed).

        Args:
            mask_threshold: Values greater than the threshold are kept, others are masked.
        """
        print("Loading obs ", self.run_date, self.model_name, self.forecast_variable)
        start_date = self.run_date + timedelta(hours=self.start_hour)
        end_date = self.run_date + timedelta(hours=self.end_hour)
        mrms_grid = MRMSGrid(start_date, end_date, self.mrms_variable, self.mrms_path)
        mrms_grid.load_data()
        if len(mrms_grid.data) > 0:
            self.raw_obs[self.mrms_variable] = np.where(mrms_grid.data > 100, 100, mrms_grid.data)
            self.period_obs[self.mrms_variable] = self.raw_obs[self.mrms_variable].max(axis=0)
            if self.obs_mask:
                mask_grid = MRMSGrid(start_date, end_date, self.mask_variable, self.mrms_path)
                mask_grid.load_data()
                self.raw_obs[self.mask_variable] = np.where(mask_grid.data >= mask_threshold, 1, 0)
                self.period_obs[self.mask_variable] = self.raw_obs[self.mask_variable].max(axis=0)


Instruction:
Here you have a function in Python 3, explain what it does
def load_coordinates(self):
        coord_file = Dataset(self.coordinate_file)
        if "lon" in coord_file.variables.keys():
            self.coordinates["lon"] = coord_file.variables["lon"][:]
            self.coordinates["lat"] = coord_file.variables["lat"][:]
        else:
            self.coordinates["lon"] = coord_file.variables["XLONG"][0]
            self.coordinates["lat"] = coord_file.variables["XLAT"][0]
        coord_file.close()

Response: Loads lat - lon coordinates from a netCDF file.


Instruction:
Make a summary of the following Python 3 code
def evaluate_hourly_forecasts(self):
        score_columns = ["Run_Date", "Forecast_Hour", "Ensemble Name", "Model_Name", "Forecast_Variable",
                         "Neighbor_Radius", "Smoothing_Radius", "Size_Threshold", "ROC", "Reliability"]
        all_scores = pd.DataFrame(columns=score_columns)
        for h, hour in enumerate(range(self.start_hour, self.end_hour + 1)):
            for neighbor_radius in self.neighbor_radii:
                n_filter = disk(neighbor_radius)
                for s, size_threshold in enumerate(self.size_thresholds):
                    print("Eval hourly forecast {0:02d} {1} {2} {3} {4:d} {5:d}".format(hour, self.model_name,
                                                                                self.forecast_variable,
                                                                                self.run_date, neighbor_radius,
                                                                                size_threshold))
                    hour_obs = fftconvolve(self.raw_obs[self.mrms_variable][h] >= self.obs_thresholds[s],
                                           n_filter, mode="same")
                    hour_obs[hour_obs > 1] = 1
                    hour_obs[hour_obs < 1] = 0
                    if self.obs_mask:
                        hour_obs = hour_obs[self.raw_obs[self.mask_variable][h] > 0]
                    for smoothing_radius in self.smoothing_radii:
                        hour_var = "neighbor_prob_r_{0:d}_s_{1:d}_{2}_{3:0.2f}".format(neighbor_radius,
                                                                                smoothing_radius,
                                                                                self.forecast_variable,
                                                                                size_threshold)
                        if self.obs_mask:
                            hour_forecast = self.hourly_forecasts[hour_var][h][self.raw_obs[self.mask_variable][h] > 0]
                        else:
                            hour_forecast = self.hourly_forecasts[hour_var][h]
                        roc = DistributedROC(thresholds=self.probability_levels, obs_threshold=0.5)
                        roc.update(hour_forecast, hour_obs)
                        rel = DistributedReliability(thresholds=self.probability_levels, obs_threshold=0.5)
                        rel.update(hour_forecast, hour_obs)
                        row = [self.run_date, hour, self.ensemble_name, self.model_name, self.forecast_variable,
                               neighbor_radius,
                               smoothing_radius, size_threshold, roc, rel]
                        all_scores.loc[hour_var + "_{0:d}".format(hour)] = row
        return all_scores

Response: Evaluates the hourly forecasts.


Instruction:
Can you generate the documentation for the following Python 3 function
def evaluate_period_forecasts(self):
        score_columns = ["Run_Date", "Ensemble Name", "Model_Name", "Forecast_Variable", "Neighbor_Radius",
                         "Smoothing_Radius", "Size_Threshold",  "ROC", "Reliability"]
        all_scores = pd.DataFrame(columns=score_columns)
        if self.coordinate_file is not None:
            coord_mask = np.where((self.coordinates["lon"] >= self.lon_bounds[0]) &
                                  (self.coordinates["lon"] <= self.lon_bounds[1]) &
                                  (self.coordinates["lat"] >= self.lat_bounds[0]) &
                                  (self.coordinates["lat"] <= self.lat_bounds[1]) &
                                  (self.period_obs[self.mask_variable] > 0))
        else:
            coord_mask = None
        for neighbor_radius in self.neighbor_radii:
            n_filter = disk(neighbor_radius)
            for s, size_threshold in enumerate(self.size_thresholds):
                period_obs = fftconvolve(self.period_obs[self.mrms_variable] >= self.obs_thresholds[s],
                                         n_filter, mode="same")
                period_obs[period_obs > 1] = 1
                if self.obs_mask and self.coordinate_file is None:
                    period_obs = period_obs[self.period_obs[self.mask_variable] > 0]
                elif self.obs_mask and self.coordinate_file is not None:
                    period_obs = period_obs[coord_mask[0], coord_mask[1]]
                else:
                    period_obs = period_obs.ravel()
                for smoothing_radius in self.smoothing_radii:
                    print("Eval period forecast {0} {1} {2} {3} {4} {5}".format(self.model_name,
                                                                                self.forecast_variable,
                                                                                self.run_date,
                                                                                neighbor_radius,
                                                                                size_threshold, smoothing_radius))
                    period_var = "neighbor_prob_{0:d}-hour_r_{1:d}_s_{2:d}_{3}_{4:0.2f}".format(self.end_hour -
                                                                                self.start_hour + 1,
                                                                                neighbor_radius,
                                                                                smoothing_radius,
                                                                                self.forecast_variable,
                                                                                size_threshold)
                    if self.obs_mask and self.coordinate_file is None:
                        period_forecast = self.period_forecasts[period_var][self.period_obs[self.mask_variable] > 0]
                    elif self.obs_mask and self.coordinate_file is not None:
                        period_forecast = self.period_forecasts[period_var][coord_mask[0], coord_mask[1]]
                    else:
                        period_forecast = self.period_forecasts[period_var].ravel()
                    roc = DistributedROC(thresholds=self.probability_levels, obs_threshold=0.5)
                    roc.update(period_forecast, period_obs)
                    rel = DistributedReliability(thresholds=self.probability_levels, obs_threshold=0.5)
                    rel.update(period_forecast, period_obs)
                    row = [self.run_date, self.ensemble_name, self.model_name, self.forecast_variable, neighbor_radius,
                           smoothing_radius, size_threshold, roc, rel]
                    all_scores.loc[period_var] = row
        return all_scores

Response: Evaluates the ROC and Reliability scores for the full - period forecasts over the full - period.


Instruction:
Explain what the following Python 3 code does
def _migrate_library(workspace_dir: pathlib.Path, do_logging: bool=True) -> pathlib.Path:

    library_path_11 = workspace_dir / "Nion Swift Workspace.nslib"
    library_path_12 = workspace_dir / "Nion Swift Library 12.nslib"
    library_path_13 = workspace_dir / "Nion Swift Library 13.nslib"

    library_paths = (library_path_11, library_path_12)
    library_path_latest = library_path_13

    if not os.path.exists(library_path_latest):
        for library_path in reversed(library_paths):
            if os.path.exists(library_path):
                if do_logging:
                    logging.info("Migrating library: %s -> %s", library_path, library_path_latest)
                shutil.copyfile(library_path, library_path_latest)
                break

    return library_path_latest

Response: Migrate the current version of the Nion Swift Library to the latest version.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def status(self):
        for valor, rotulo in ESTADOS_OPERACAO:
            if self.ESTADO_OPERACAO == valor:
                return rotulo
        return u'(desconhecido: {})'.format(self.ESTADO_OPERACAO)

Response:
Nome amigável do campo ESTADO_OPERACAO conforme a Tabela de
        Informações do Status do SAT.


Instruction:
Here you have a function in Python 3, explain what it does
def analisar(retorno):
        resposta = analisar_retorno(forcar_unicode(retorno),
                funcao='ConsultarStatusOperacional',
                classe_resposta=RespostaConsultarStatusOperacional,
                campos=RespostaSAT.CAMPOS + (
                        ('NSERIE', as_clean_unicode),
                        ('TIPO_LAN', as_clean_unicode),
                        ('LAN_IP', normalizar_ip),
                        ('LAN_MAC', unicode),
                        ('LAN_MASK', normalizar_ip),
                        ('LAN_GW', normalizar_ip),
                        ('LAN_DNS_1', normalizar_ip),
                        ('LAN_DNS_2', normalizar_ip),
                        ('STATUS_LAN', as_clean_unicode),
                        ('NIVEL_BATERIA', as_clean_unicode),
                        ('MT_TOTAL', as_clean_unicode),
                        ('MT_USADA', as_clean_unicode),
                        ('DH_ATUAL', as_datetime),
                        ('VER_SB', as_clean_unicode),
                        ('VER_LAYOUT', as_clean_unicode),
                        ('ULTIMO_CF_E_SAT', as_clean_unicode),
                        ('LISTA_INICIAL', as_clean_unicode),
                        ('LISTA_FINAL', as_clean_unicode),
                        ('DH_CFE', as_datetime_or_none),
                        ('DH_ULTIMA', as_datetime),
                        ('CERT_EMISSAO', as_date),
                        ('CERT_VENCIMENTO', as_date),
                        ('ESTADO_OPERACAO', int),
                    ),
                campos_alternativos=[
                        # se falhar resultarão apenas os 5 campos padrão
                        RespostaSAT.CAMPOS,
                    ]
            )
        if resposta.EEEEE not in ('10000',):
            raise ExcecaoRespostaSAT(resposta)
        return resposta

Response:
Analisar uma resposta do
       .


Instruction:
Write a Python 3 script for
reading forecasts from csv files and merges them with the input data from the step csv files.

Response:
def merge_input_csv_forecast_json(input_csv_file, forecast_json_path, condition_models, dist_models):
    """
    Reads forecasts from json files and merges them with the input data from the step csv files.

    Args:
        input_csv_file: Name of the input data csv file being processed
        forecast_json_path: Path to the forecast json files toplevel directory
        condition_models: List of models used to forecast hail or no hail
        dist_models: List of models used to forecast the hail size distribution

    Returns:

    """
    try:
        run_date = input_csv_file[:-4].split("_")[-1]
        print(run_date)
        ens_member = "_".join(input_csv_file.split("/")[-1][:-4].split("_")[3:-1])
        ens_name = input_csv_file.split("/")[-1].split("_")[2]
        input_data = pd.read_csv(input_csv_file, index_col="Step_ID")
        full_json_path = forecast_json_path + "{0}/{1}/".format(run_date, ens_member)
        track_ids = sorted(input_data["Track_ID"].unique())
        model_pred_cols = []
        condition_models_ns = []
        dist_models_ns = []
        gamma_params = ["Shape", "Location", "Scale"]
        for condition_model in condition_models:
            model_pred_cols.append(condition_model.replace(" ", "-") + "_Condition")
            condition_models_ns.append(condition_model.replace(" ", "-"))
        for dist_model in dist_models:
            dist_models_ns.append(dist_model.replace(" ", "-"))
            for param in gamma_params:
                model_pred_cols.append(dist_model.replace(" ", "-") + "_" + param)
        pred_data = pd.DataFrame(index=input_data.index, columns=model_pred_cols,
                                dtype=float)
        for track_id in track_ids:
            track_id_num = track_id.split("_")[-1]
            json_filename = full_json_path + "{0}_{1}_{2}_model_track_{3}.json".format(ens_name,
                                                                                run_date,
                                                                                ens_member,
                                                                                track_id_num)
            json_file = open(json_filename)
            json_data = json.load(json_file)
            json_file.close()
            for s, step in enumerate(json_data["features"]):
                step_id = track_id + "_{0:02d}".format(s)
                for cond_model in condition_models_ns:
                    pred_data.loc[step_id, cond_model + "_Condition"]  = step["properties"]["condition_" + cond_model]
                for dist_model in dist_models_ns:
                    pred_data.loc[step_id, [dist_model + "_" + p
                                            for p in gamma_params]] = step["properties"]["dist_" + dist_model]
        out_data = input_data.merge(pred_data, left_index=True, right_index=True)
        return out_data, ens_name, ens_member
    except Exception as e:
        print(traceback.format_exc())
        raise e


Instruction:
Write a Python 3 script for
calling from item to indicate its data or metadata has changed.

Response:
def mark_data_dirty(self):
        """ Called from item to indicate its data or metadata has changed."""
        self.__cache.set_cached_value_dirty(self.__display_item, self.__cache_property_name)
        self.__initialize_cache()
        self.__cached_value_dirty = True


Instruction:
Can you generate the documentation for the following Python 3 function
def __initialize_cache(self):
        if self.__cached_value_dirty is None:
            self.__cached_value_dirty = self.__cache.is_cached_value_dirty(self.__display_item, self.__cache_property_name)
            self.__cached_value = self.__cache.get_cached_value(self.__display_item, self.__cache_property_name)

Response: Initialize the cache values.


Instruction:
Can you create a Python 3 function that
computes the data associated with this processor.

Response:
def recompute_data(self, ui):
        """Compute the data associated with this processor.

        This method is thread safe and may take a long time to return. It should not be called from
         the UI thread. Upon return, the results will be calculated with the latest data available
         and the cache will not be marked dirty.
        """
        self.__initialize_cache()
        with self.__recompute_lock:
            if self.__cached_value_dirty:
                try:
                    calculated_data = self.get_calculated_data(ui)
                except Exception as e:
                    import traceback
                    traceback.print_exc()
                    traceback.print_stack()
                    raise
                self.__cache.set_cached_value(self.__display_item, self.__cache_property_name, calculated_data)
                self.__cached_value = calculated_data
                self.__cached_value_dirty = False
                self.__cached_value_time = time.time()
            else:
                calculated_data = None
            if calculated_data is None:
                calculated_data = self.get_default_data()
                if calculated_data is not None:
                    # if the default is not None, treat is as valid cached data
                    self.__cache.set_cached_value(self.__display_item, self.__cache_property_name, calculated_data)
                    self.__cached_value = calculated_data
                    self.__cached_value_dirty = False
                    self.__cached_value_time = time.time()
                else:
                    # otherwise remove everything from the cache
                    self.__cache.remove_cached_value(self.__display_item, self.__cache_property_name)
                    self.__cached_value = None
                    self.__cached_value_dirty = None
                    self.__cached_value_time = 0
            self.__recompute_lock.release()
            if callable(self.on_thumbnail_updated):
                self.on_thumbnail_updated()
            self.__recompute_lock.acquire()


Instruction:
Can you create a Python 3 function that
returns a ThumbnailSource object for the given UI and display item.

Response:
def thumbnail_source_for_display_item(self, ui, display_item: DisplayItem.DisplayItem) -> ThumbnailSource:
        """Returned ThumbnailSource must be closed."""
        with self.__lock:
            thumbnail_source = self.__thumbnail_sources.get(display_item)
            if not thumbnail_source:
                thumbnail_source = ThumbnailSource(ui, display_item)
                self.__thumbnail_sources[display_item] = thumbnail_source

                def will_delete(thumbnail_source):
                    del self.__thumbnail_sources[thumbnail_source._display_item]

                thumbnail_source._on_will_delete = will_delete
            else:
                assert thumbnail_source._ui == ui
            return thumbnail_source.add_ref()


Instruction:
How would you code a function in Python 3 to
load plug - ins.

Response:
def load_plug_ins(app, root_dir):
    """Load plug-ins."""
    global extensions

    ui = app.ui

    # a list of directories in which sub-directories PlugIns will be searched.
    subdirectories = []

    # the default location is where the directory main packages are located.
    if root_dir:
        subdirectories.append(root_dir)

    # also search the default data location; create directory there if it doesn't exist to make it easier for user.
    # default data location will be application specific.
    data_location = ui.get_data_location()
    if data_location is not None:
        subdirectories.append(data_location)
        # create directories here if they don't exist
        plugins_dir = os.path.abspath(os.path.join(data_location, "PlugIns"))
        if not os.path.exists(plugins_dir):
            logging.info("Creating plug-ins directory %s", plugins_dir)
            os.makedirs(plugins_dir)

    # search the Nion/Swift subdirectory of the default document location too,
    # but don't create directories here - avoid polluting user visible directories.
    document_location = ui.get_document_location()
    if document_location is not None:
        subdirectories.append(os.path.join(document_location, "Nion", "Swift"))
        # do not create them in documents if they don't exist. this location is optional.

    # build a list of directories that will be loaded as plug-ins.
    PlugInDir = collections.namedtuple("PlugInDir", ["directory", "relative_path"])
    plugin_dirs = list()

    # track directories that have already been searched.
    seen_plugin_dirs = list()

    # for each subdirectory, look in PlugIns for sub-directories that represent the plug-ins.
    for subdirectory in subdirectories:
        plugins_dir = os.path.abspath(os.path.join(subdirectory, "PlugIns"))

        if os.path.exists(plugins_dir) and not plugins_dir in seen_plugin_dirs:
            logging.info("Loading plug-ins from %s", plugins_dir)

            # add the PlugIns directory to the system import path.
            sys.path.append(plugins_dir)

            # now build a list of sub-directories representing plug-ins within plugins_dir.
            sorted_relative_paths = sorted([d for d in os.listdir(plugins_dir) if os.path.isdir(os.path.join(plugins_dir, d))])
            plugin_dirs.extend([PlugInDir(plugins_dir, sorted_relative_path) for sorted_relative_path in sorted_relative_paths])

            # mark plugins_dir as 'seen' to avoid search it twice.
            seen_plugin_dirs.append(plugins_dir)
        else:
            logging.info("NOT Loading plug-ins from %s (missing)", plugins_dir)

    version_map = dict()
    module_exists_map = dict()

    plugin_adapters = list()

    import nionswift_plugin
    for module_info in pkgutil.iter_modules(nionswift_plugin.__path__):
        plugin_adapters.append(ModuleAdapter(nionswift_plugin.__name__, module_info))

    for directory, relative_path in plugin_dirs:
        plugin_adapters.append(PlugInAdapter(directory, relative_path))

    progress = True
    while progress:
        progress = False
        plugin_adapters_copy = copy.deepcopy(plugin_adapters)
        plugin_adapters = list()
        for plugin_adapter in plugin_adapters_copy:
            manifest_path = plugin_adapter.manifest_path
            manifest = plugin_adapter.manifest
            if manifest:
                manifest_valid = True
                if not "name" in manifest:
                    logging.info("Invalid manifest (missing 'name'): %s", manifest_path)
                    manifest_valid = False
                if not "identifier" in manifest:
                    logging.info("Invalid manifest (missing 'identifier'): %s", manifest_path)
                    manifest_valid = False
                if "identifier" in manifest and not re.match("[_\-a-zA-Z][_\-a-zA-Z0-9.]*$", manifest["identifier"]):
                    logging.info("Invalid manifest (invalid 'identifier': '%s'): %s", manifest["identifier"], manifest_path)
                    manifest_valid = False
                if not "version" in manifest:
                    logging.info("Invalid manifest (missing 'version'): %s", manifest_path)
                    manifest_valid = False
                if "requires" in manifest and not isinstance(manifest["requires"], list):
                    logging.info("Invalid manifest ('requires' not a list): %s", manifest_path)
                    manifest_valid = False
                if not manifest_valid:
                    continue
                for module in manifest.get("modules", list()):
                    if module in module_exists_map:
                        module_exists = module_exists_map.get(module)
                    else:
                        module_exists = importlib.util.find_spec(module) is not None
                        module_exists_map[module] = module_exists
                    if not module_exists:
                        logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (" + plugin_adapter.module_path + ").")
                        logging.info("Cannot satisfy requirement (%s): %s", module, manifest_path)
                        manifest_valid = False
                        break
                for requirement in manifest.get("requires", list()):
                    # TODO: see https://packaging.pypa.io/en/latest/
                    requirement_components = requirement.split()
                    if len(requirement_components) != 3 or requirement_components[1] != "~=":
                        logging.info("Invalid manifest (requirement '%s' invalid): %s", requirement, manifest_path)
                        manifest_valid = False
                        break
                    identifier, operator, version_specifier = requirement_components[0], requirement_components[1], requirement_components[2]
                    if identifier in version_map:
                        if Utility.compare_versions("~" + version_specifier, version_map[identifier]) != 0:
                            logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (" + plugin_adapter.module_path + ").")
                            logging.info("Cannot satisfy requirement (%s): %s", requirement, manifest_path)
                            manifest_valid = False
                            break
                    else:
                        # requirements not loaded yet; add back to plugin_adapters, but don't mark progress since nothing was loaded.
                        logging.info("Plug-in '" + plugin_adapter.module_name + "' delayed (%s) (" + plugin_adapter.module_path + ").", requirement)
                        plugin_adapters.append(plugin_adapter)
                        manifest_valid = False
                        break
                if not manifest_valid:
                    continue
                version_map[manifest["identifier"]] = manifest["version"]
            # read the manifests, if any
            # repeat loop of plug-ins until no plug-ins left in the list
            #   if all dependencies satisfied for a plug-in, load it
            #   otherwise defer until next round
            #   stop if no plug-ins loaded in the round
            #   count on the user to have correct dependencies
            module = plugin_adapter.load()
            if module:
                __modules.append(module)
            progress = True
    for plugin_adapter in plugin_adapters:
        logging.info("Plug-in '" + plugin_adapter.module_name + "' NOT loaded (requirements) (" + plugin_adapter.module_path + ").")

    notify_modules("run")


Instruction:
Here you have a function in Python 3, explain what it does
def getMyID(self,gist_name):
		'''
		Getting gistID of a gist in order to make the workflow
		easy and uninterrupted.
		'''
		r = requests.get(
			'%s'%BASE_URL+'/users/%s/gists' % self.user,
			headers=self.gist.header
			)
		if (r.status_code == 200):
			r_text = json.loads(r.text)
			limit = len(r.json())

			for g,no in zip(r_text, range(0,limit)):
				for ka,va in r.json()[no]['files'].iteritems():
					if str(va['filename']) == str(gist_name):
						return r.json()[no]['id']
			return 0

		raise Exception('Username not found')

Response: Get the gist ID of a gist


Instruction:
Create a Python 3 function to
close the document controller.

Response:
def close(self):
        """Close the document controller.

        This method must be called to shut down the document controller. There are several
        paths by which it can be called, though.

           * User quits application via menu item. The menu item will call back to Application.exit which will close each
             document controller by calling this method.
           * User quits application using dock menu item. The Qt application will call aboutToClose in the document windows
           * User closes document window via menu item.
           * User closes document window via close box.

        The main concept of closing is that it is always triggered by the document window closing. This can be initiated
        from within Python by calling request_close on the document window. When the window closes, either by explicit request
        or by the user clicking a close box, it will invoke the about_to_close method on the document window. At this point,
        the window would still be open, so the about_to_close message can be used to tell the document controller to save anything
        it needs to save and prepare for closing.
        """
        assert self.__closed == False
        self.__closed = True
        self.finish_periodic()  # required to finish periodic operations during tests
        # dialogs
        for weak_dialog in self.__dialogs:
            dialog = weak_dialog()
            if dialog:
                try:
                    dialog.request_close()
                except Exception as e:
                    pass
        # menus
        self._file_menu = None
        self._edit_menu = None
        self._processing_menu = None
        self._view_menu = None
        self._window_menu = None
        self._help_menu = None
        self._library_menu = None
        self._processing_arithmetic_menu = None
        self._processing_reduce_menu = None
        self._processing_transform_menu = None
        self._processing_filter_menu = None
        self._processing_fourier_menu = None
        self._processing_graphics_menu = None
        self._processing_sequence_menu = None
        self._processing_redimension_menu = None
        self._display_type_menu = None

        if self.__workspace_controller:
            self.__workspace_controller.close()
            self.__workspace_controller = None
        self.__call_soon_event_listener.close()
        self.__call_soon_event_listener = None
        self.__filtered_display_items_model.close()
        self.__filtered_display_items_model = None
        self.filter_controller.close()
        self.filter_controller = None
        self.__display_items_model.close()
        self.__display_items_model = None
        # document_model may be shared between several DocumentControllers, so use reference counting
        # to determine when to close it.
        self.document_model.remove_ref()
        self.document_model = None
        self.did_close_event.fire(self)
        self.did_close_event = None
        super().close()


Instruction:
How would you explain what the following Python 3 function does
def add_periodic(self, interval: float, listener_fn):
        class PeriodicListener:
            def __init__(self, interval: float, listener_fn):
                self.interval = interval
                self.__listener_fn = listener_fn
                # the call function is very performance critical; make it fast by using a property
                # instead of a logic statement each time.
                if callable(listener_fn):
                    self.call = self.__listener_fn
                else:
                    def void(*args, **kwargs):
                        pass
                    self.call = void
                self.next_scheduled_time = time.time() + interval
            def close(self):
                self.__listener_fn = None
                def void(*args, **kwargs):
                    pass
                self.call = void
        listener = PeriodicListener(interval, listener_fn)
        def remove_listener(weak_listener):
            with self.__weak_periodic_listeners_mutex:
                self.__weak_periodic_listeners.remove(weak_listener)
        weak_listener = weakref.ref(listener, remove_listener)
        with self.__weak_periodic_listeners_mutex:
            self.__weak_periodic_listeners.append(weak_listener)
        return listener

Response: Add a listener function and return a token. Token can be closed or deleted to unlisten.


Instruction:
Write a Python 3 script for
updating the display items model with a new container filter and sorting.

Response:
def __update_display_items_model(self, display_items_model: ListModel.FilteredListModel, data_group: typing.Optional[DataGroup.DataGroup], filter_id: typing.Optional[str]) -> None:
        """Update the data item model with a new container, filter, and sorting.

        This is called when the data item model is created or when the user changes
        the data group or sorting settings.
        """

        with display_items_model.changes():  # change filter and sort together
            if data_group is not None:
                display_items_model.container = data_group
                display_items_model.filter = ListModel.Filter(True)
                display_items_model.sort_key = None
                display_items_model.filter_id = None
            elif filter_id == "latest-session":
                display_items_model.container = self.document_model
                display_items_model.filter = ListModel.EqFilter("session_id", self.document_model.session_id)
                display_items_model.sort_key = DataItem.sort_by_date_key
                display_items_model.sort_reverse = True
                display_items_model.filter_id = filter_id
            elif filter_id == "temporary":
                display_items_model.container = self.document_model
                display_items_model.filter = ListModel.NotEqFilter("category", "persistent")
                display_items_model.sort_key = DataItem.sort_by_date_key
                display_items_model.sort_reverse = True
                display_items_model.filter_id = filter_id
            elif filter_id == "none":  # not intended to be used directly
                display_items_model.container = self.document_model
                display_items_model.filter = ListModel.Filter(False)
                display_items_model.sort_key = DataItem.sort_by_date_key
                display_items_model.sort_reverse = True
                display_items_model.filter_id = filter_id
            else:  # "all"
                display_items_model.container = self.document_model
                display_items_model.filter = ListModel.EqFilter("category", "persistent")
                display_items_model.sort_key = DataItem.sort_by_date_key
                display_items_model.sort_reverse = True
                display_items_model.filter_id = None


Instruction:
Write a Python 3 function that can
return the data item with keyboard focus.

Response:
def focused_data_item(self) -> typing.Optional[DataItem.DataItem]:
        """Return the data item with keyboard focus."""
        return self.__focused_display_item.data_item if self.__focused_display_item else None


Instruction:
Can you tell what is the following Python 3 function doing
def selected_display_item(self) -> typing.Optional[DisplayItem.DisplayItem]:
        # first check for the [focused] data browser
        display_item = self.focused_display_item
        if not display_item:
            selected_display_panel = self.selected_display_panel
            display_item = selected_display_panel.display_item if selected_display_panel else None
        return display_item

Response: Return the selected display item.


Instruction:
How would you explain what the following Python 3 function does
def _get_two_data_sources(self):
        selected_display_items = self.selected_display_items
        if len(selected_display_items) < 2:
            selected_display_items = list()
            display_item = self.selected_display_item
            if display_item:
                selected_display_items.append(display_item)
        if len(selected_display_items) == 1:
            display_item = selected_display_items[0]
            data_item = display_item.data_item if display_item else None
            if display_item and len(display_item.graphic_selection.indexes) == 2:
                index1 = display_item.graphic_selection.anchor_index
                index2 = list(display_item.graphic_selection.indexes.difference({index1}))[0]
                graphic1 = display_item.graphics[index1]
                graphic2 = display_item.graphics[index2]
                if data_item:
                    if data_item.is_datum_1d and isinstance(graphic1, Graphics.IntervalGraphic) and isinstance(graphic2, Graphics.IntervalGraphic):
                        crop_graphic1 = graphic1
                        crop_graphic2 = graphic2
                    elif data_item.is_datum_2d and isinstance(graphic1, Graphics.RectangleTypeGraphic) and isinstance(graphic2, Graphics.RectangleTypeGraphic):
                        crop_graphic1 = graphic1
                        crop_graphic2 = graphic2
                    else:
                        crop_graphic1 = self.__get_crop_graphic(display_item)
                        crop_graphic2 = crop_graphic1
                else:
                    crop_graphic1 = self.__get_crop_graphic(display_item)
                    crop_graphic2 = crop_graphic1
            else:
                crop_graphic1 = self.__get_crop_graphic(display_item)
                crop_graphic2 = crop_graphic1
            return (display_item, crop_graphic1), (display_item, crop_graphic2)
        if len(selected_display_items) == 2:
            display_item1 = selected_display_items[0]
            crop_graphic1 = self.__get_crop_graphic(display_item1)
            display_item2 = selected_display_items[1]
            crop_graphic2 = self.__get_crop_graphic(display_item2)
            return (display_item1, crop_graphic1), (display_item2, crop_graphic2)
        return None

Response: Get two sensible data sources which may be the same.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def calculate_origin_and_size(canvas_size, data_shape, image_canvas_mode, image_zoom, image_position) -> typing.Tuple[typing.Any, typing.Any]:
    if data_shape is None:
        return None, None
    if image_canvas_mode == "fill":
        data_shape = data_shape
        scale_h = float(data_shape[1]) / canvas_size[1]
        scale_v = float(data_shape[0]) / canvas_size[0]
        if scale_v < scale_h:
            image_canvas_size = (canvas_size[0], canvas_size[0] * data_shape[1] / data_shape[0])
        else:
            image_canvas_size = (canvas_size[1] * data_shape[0] / data_shape[1], canvas_size[1])
        image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5)
    elif image_canvas_mode == "fit":
        image_canvas_size = canvas_size
        image_canvas_origin = (0, 0)
    elif image_canvas_mode == "1:1":
        image_canvas_size = data_shape
        image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5)
    elif image_canvas_mode == "2:1":
        image_canvas_size = (data_shape[0] * 0.5, data_shape[1] * 0.5)
        image_canvas_origin = (canvas_size[0] * 0.5 - image_canvas_size[0] * 0.5, canvas_size[1] * 0.5 - image_canvas_size[1] * 0.5)
    else:
        image_canvas_size = (canvas_size[0] * image_zoom, canvas_size[1] * image_zoom)
        canvas_rect = Geometry.fit_to_size(((0, 0), image_canvas_size), data_shape)
        image_canvas_origin_y = (canvas_size[0] * 0.5) - image_position[0] * canvas_rect[1][0] - canvas_rect[0][0]
        image_canvas_origin_x = (canvas_size[1] * 0.5) - image_position[1] * canvas_rect[1][1] - canvas_rect[0][1]
        image_canvas_origin = (image_canvas_origin_y, image_canvas_origin_x)
    return image_canvas_origin, image_canvas_size

Response: Calculate origin and size for canvas size data shape and image display parameters.


Instruction:
Create a Python 3 function to
read data items from the data reference handler and return as a list.

Response:
def read_library(persistent_storage_system, ignore_older_files) -> typing.Dict:
    """Read data items from the data reference handler and return as a list.

    Data items will have persistent_object_context set upon return, but caller will need to call finish_reading
    on each of the data items.
    """
    data_item_uuids = set()
    utilized_deletions = set()  # the uuid's skipped due to being deleted
    deletions = list()

    reader_info_list, library_updates = auto_migrate_storage_system(persistent_storage_system=persistent_storage_system,
                                                                    new_persistent_storage_system=persistent_storage_system,
                                                                    data_item_uuids=data_item_uuids,
                                                                    deletions=deletions,
                                                                    utilized_deletions=utilized_deletions,
                                                                    ignore_older_files=ignore_older_files)

    # next, for each auto migration, create a temporary storage system and read items from that storage system
    # using auto_migrate_storage_system. the data items returned will have been copied to the current storage
    # system (persistent object context).
    for auto_migration in reversed(persistent_storage_system.get_auto_migrations()):
        old_persistent_storage_system = FileStorageSystem(auto_migration.library_path, auto_migration.paths) if auto_migration.paths else auto_migration.storage_system
        new_reader_info_list, new_library_updates = auto_migrate_storage_system(persistent_storage_system=old_persistent_storage_system,
                                                                                new_persistent_storage_system=persistent_storage_system,
                                                                                data_item_uuids=data_item_uuids,
                                                                                deletions=deletions,
                                                                                utilized_deletions=utilized_deletions,
                                                                                ignore_older_files=ignore_older_files)
        reader_info_list.extend(new_reader_info_list)
        library_updates.update(new_library_updates)

    assert len(reader_info_list) == len(data_item_uuids)

    library_storage_properties = persistent_storage_system.library_storage_properties

    for reader_info in reader_info_list:
        properties = reader_info.properties
        properties = Utility.clean_dict(copy.deepcopy(properties) if properties else dict())
        version = properties.get("version", 0)
        if version == DataItem.DataItem.writer_version:
            data_item_uuid = uuid.UUID(properties.get("uuid", uuid.uuid4()))
            library_update = library_updates.get(data_item_uuid, dict())
            library_storage_properties.setdefault("connections", list()).extend(library_update.get("connections", list()))
            library_storage_properties.setdefault("computations", list()).extend(library_update.get("computations", list()))
            library_storage_properties.setdefault("display_items", list()).extend(library_update.get("display_items", list()))

    # mark deletions that need to be tracked because they've been deleted but are also present in older libraries
    # and would be migrated during reading unless they explicitly are prevented from doing so (via data_item_deletions).
    # utilized deletions are the ones that were attempted; if nothing was attempted, then no reason to track it anymore
    # since there is nothing to migrate in the future.
    library_storage_properties["data_item_deletions"] = [str(uuid_) for uuid_ in utilized_deletions]

    connections_list = library_storage_properties.get("connections", list())
    assert len(connections_list) == len({connection.get("uuid") for connection in connections_list})

    computations_list = library_storage_properties.get("computations", list())
    assert len(computations_list) == len({computation.get("uuid") for computation in computations_list})

    # migrations

    if library_storage_properties.get("version", 0) < 2:
        for data_group_properties in library_storage_properties.get("data_groups", list()):
            data_group_properties.pop("data_groups")
            display_item_references = data_group_properties.setdefault("display_item_references", list())
            data_item_uuid_strs = data_group_properties.pop("data_item_uuids", list())
            for data_item_uuid_str in data_item_uuid_strs:
                for display_item_properties in library_storage_properties.get("display_items", list()):
                    data_item_references = [d.get("data_item_reference", None) for d in display_item_properties.get("display_data_channels", list())]
                    if data_item_uuid_str in data_item_references:
                        display_item_references.append(display_item_properties["uuid"])
        data_item_uuid_to_display_item_uuid_map = dict()
        data_item_uuid_to_display_item_dict_map = dict()
        display_to_display_item_map = dict()
        display_to_display_data_channel_map = dict()
        for display_item_properties in library_storage_properties.get("display_items", list()):
            display_to_display_item_map[display_item_properties["display"]["uuid"]] = display_item_properties["uuid"]
            display_to_display_data_channel_map[display_item_properties["display"]["uuid"]] = display_item_properties["display_data_channels"][0]["uuid"]
            data_item_references = [d.get("data_item_reference", None) for d in display_item_properties.get("display_data_channels", list())]
            for data_item_uuid_str in data_item_references:
                data_item_uuid_to_display_item_uuid_map.setdefault(data_item_uuid_str, display_item_properties["uuid"])
                data_item_uuid_to_display_item_dict_map.setdefault(data_item_uuid_str, display_item_properties)
            display_item_properties.pop("display", None)
        for workspace_properties in library_storage_properties.get("workspaces", list()):
            def replace1(d):
                if "children" in d:
                    for dd in d["children"]:
                        replace1(dd)
                if "data_item_uuid" in d:
                    data_item_uuid_str = d.pop("data_item_uuid")
                    display_item_uuid_str = data_item_uuid_to_display_item_uuid_map.get(data_item_uuid_str)
                    if display_item_uuid_str:
                        d["display_item_uuid"] = display_item_uuid_str
            replace1(workspace_properties["layout"])
        for connection_dict in library_storage_properties.get("connections", list()):
            source_uuid_str = connection_dict["source_uuid"]
            if connection_dict["type"] == "interval-list-connection":
                connection_dict["source_uuid"] = display_to_display_item_map.get(source_uuid_str, None)
            if connection_dict["type"] == "property-connection" and connection_dict["source_property"] == "slice_interval":
                connection_dict["source_uuid"] = display_to_display_data_channel_map.get(source_uuid_str, None)

        def fix_specifier(specifier_dict):
            if specifier_dict.get("type") in ("data_item", "display_xdata", "cropped_xdata", "cropped_display_xdata", "filter_xdata", "filtered_xdata"):
                if specifier_dict.get("uuid") in data_item_uuid_to_display_item_dict_map:
                    specifier_dict["uuid"] = data_item_uuid_to_display_item_dict_map[specifier_dict["uuid"]]["display_data_channels"][0]["uuid"]
                else:
                    specifier_dict.pop("uuid", None)
            if specifier_dict.get("type") == "data_item":
                specifier_dict["type"] = "data_source"
            if specifier_dict.get("type") == "data_item_object":
                specifier_dict["type"] = "data_item"
            if specifier_dict.get("type") == "region":
                specifier_dict["type"] = "graphic"

        for computation_dict in library_storage_properties.get("computations", list()):
            for variable_dict in computation_dict.get("variables", list()):
                if "specifier" in variable_dict:
                    specifier_dict = variable_dict["specifier"]
                    if specifier_dict is not None:
                        fix_specifier(specifier_dict)
                if "secondary_specifier" in variable_dict:
                    specifier_dict = variable_dict["secondary_specifier"]
                    if specifier_dict is not None:
                        fix_specifier(specifier_dict)
            for result_dict in computation_dict.get("results", list()):
                fix_specifier(result_dict["specifier"])

        library_storage_properties["version"] = DocumentModel.DocumentModel.library_version

    # TODO: add consistency checks: no duplicated items [by uuid] such as connections or computations or data items

    assert library_storage_properties["version"] == DocumentModel.DocumentModel.library_version

    persistent_storage_system.rewrite_properties(library_storage_properties)

    properties = copy.deepcopy(library_storage_properties)

    for reader_info in reader_info_list:
        data_item_properties = Utility.clean_dict(reader_info.properties if reader_info.properties else dict())
        if data_item_properties.get("version", 0) == DataItem.DataItem.writer_version:
            data_item_properties["__large_format"] = reader_info.large_format
            data_item_properties["__identifier"] = reader_info.identifier
            properties.setdefault("data_items", list()).append(data_item_properties)

    def data_item_created(data_item_properties: typing.Mapping) -> str:
        return data_item_properties.get("created", "1900-01-01T00:00:00.000000")

    properties["data_items"] = sorted(properties.get("data_items", list()), key=data_item_created)

    return properties


Instruction:
Write a Python 3 script for
migrating items from the persistent storage system to the object context.

Response:
def auto_migrate_storage_system(*, persistent_storage_system=None, new_persistent_storage_system=None, data_item_uuids=None, deletions: typing.List[uuid.UUID] = None, utilized_deletions: typing.Set[uuid.UUID] = None, ignore_older_files: bool = True):
    """Migrate items from the storage system to the object context.

    Files in data_item_uuids have already been loaded and are ignored (not migrated).

    Files in deletes have been deleted in object context and are ignored (not migrated) and then added
    to the utilized deletions list.

    Data items will have persistent_object_context set upon return, but caller will need to call finish_reading
    on each of the data items.
    """
    storage_handlers = persistent_storage_system.find_data_items()
    ReaderInfo = collections.namedtuple("ReaderInfo", ["properties", "changed_ref", "large_format", "storage_handler", "identifier"])
    reader_info_list = list()
    for storage_handler in storage_handlers:
        try:
            large_format = isinstance(storage_handler, HDF5Handler.HDF5Handler)
            properties = Migration.transform_to_latest(storage_handler.read_properties())
            reader_info = ReaderInfo(properties, [False], large_format, storage_handler, storage_handler.reference)
            reader_info_list.append(reader_info)
        except Exception as e:
            logging.debug("Error reading %s", storage_handler.reference)
            import traceback
            traceback.print_exc()
            traceback.print_stack()
    library_storage_properties = persistent_storage_system.library_storage_properties
    for deletion in copy.deepcopy(library_storage_properties.get("data_item_deletions", list())):
        if not deletion in deletions:
            deletions.append(deletion)
    preliminary_library_updates = dict()
    library_updates = dict()
    if not ignore_older_files:
        Migration.migrate_to_latest(reader_info_list, preliminary_library_updates)
    good_reader_info_list = list()
    count = len(reader_info_list)
    for index, reader_info in enumerate(reader_info_list):
        storage_handler = reader_info.storage_handler
        properties = reader_info.properties
        try:
            version = properties.get("version", 0)
            if version == DataItem.DataItem.writer_version:
                data_item_uuid = uuid.UUID(properties["uuid"])
                if not data_item_uuid in data_item_uuids:
                    if str(data_item_uuid) in deletions:
                        utilized_deletions.add(data_item_uuid)
                    else:
                        auto_migrate_data_item(reader_info, persistent_storage_system, new_persistent_storage_system, index, count)
                        good_reader_info_list.append(reader_info)
                        data_item_uuids.add(data_item_uuid)
                        library_update = preliminary_library_updates.get(data_item_uuid)
                        if library_update:
                            library_updates[data_item_uuid] = library_update
        except Exception as e:
            logging.debug("Error reading %s", storage_handler.reference)
            import traceback
            traceback.print_exc()
            traceback.print_stack()
    return good_reader_info_list, library_updates


Instruction:
Here you have a function in Python 3, explain what it does
def rewrite_properties(self, properties):
        with self.__properties_lock:
            self.__properties = properties
        self.__write_properties(None)

Response: Set the properties and write to disk.


Instruction:
How would you explain what the following Python 3 function does
def analisar(retorno):
        resposta = analisar_retorno(forcar_unicode(retorno),
                funcao='AtivarSAT',
                classe_resposta=RespostaAtivarSAT,
                campos=(
                        ('numeroSessao', int),
                        ('EEEEE', unicode),
                        ('mensagem', unicode),
                        ('cod', unicode),
                        ('mensagemSEFAZ', unicode),
                        ('CSR', unicode),
                    ),
                campos_alternativos=[
                        # se a ativação falhar espera-se o padrão de campos
                        # no retorno...
                        RespostaSAT.CAMPOS,
                    ]
            )
        if resposta.EEEEE not in (
                ATIVADO_CORRETAMENTE,
                CSR_ICPBRASIL_CRIADO_SUCESSO,):
            raise ExcecaoRespostaSAT(resposta)
        return resposta

Response: Analisar uma : class : RespostaAtivarSAT a partir do retorno.


Instruction:
How would you code a function in Python 3 to
create a view task for this hardware source.

Response:
def create_view_task(self, frame_parameters: dict=None, channels_enabled: typing.List[bool]=None, buffer_size: int=1) -> ViewTask:
        """Create a view task for this hardware source.

        .. versionadded:: 1.0

        :param frame_parameters: The frame parameters for the view. Pass None for defaults.
        :type frame_parameters: :py:class:`FrameParameters`
        :param channels_enabled: The enabled channels for the view. Pass None for defaults.
        :type channels_enabled: List of booleans.
        :param buffer_size: The buffer size if using the grab_earliest method. Default is 1.
        :type buffer_size: int
        :return: The :py:class:`ViewTask` object.
        :rtype: :py:class:`ViewTask`

        Callers should call close on the returned task when finished.

        See :py:class:`ViewTask` for examples of how to use.
        """
        ...


Instruction:
Write a Python 3 function for
loading configuration from yaml source

Response:
def from_yaml():
    """ Load configuration from yaml source(s), cached to only run once """
    default_yaml_str = snippets.get_snippet_content('hatchery.yml')
    ret = yaml.load(default_yaml_str, Loader=yaml.RoundTripLoader)
    for config_path in CONFIG_LOCATIONS:
        config_path = os.path.expanduser(config_path)
        if os.path.isfile(config_path):
            with open(config_path) as config_file:
                config_dict = yaml.load(config_file, Loader=yaml.RoundTripLoader)
                if config_dict is None:
                    continue
                for k, v in config_dict.items():
                    if k not in ret.keys():
                        raise ConfigError(
                            'found garbage key "{}" in {}'.format(k, config_path)
                        )
                    ret[k] = v
    return ret


Instruction:
Make a summary of the following Python 3 code
def from_pypirc(pypi_repository):
    ret = {}
    pypirc_locations = PYPIRC_LOCATIONS
    for pypirc_path in pypirc_locations:
        pypirc_path = os.path.expanduser(pypirc_path)
        if os.path.isfile(pypirc_path):
            parser = configparser.SafeConfigParser()
            parser.read(pypirc_path)
            if 'distutils' not in parser.sections():
                continue
            if 'index-servers' not in parser.options('distutils'):
                continue
            if pypi_repository not in parser.get('distutils', 'index-servers'):
                continue
            if pypi_repository in parser.sections():
                for option in parser.options(pypi_repository):
                    ret[option] = parser.get(pypi_repository, option)
    if not ret:
        raise ConfigError(
            'repository does not appear to be configured in pypirc ({})'.format(pypi_repository) +
            ', remember that it needs an entry in [distutils] and its own section'
        )
    return ret

Response: Load configuration from. pypirc file


Instruction:
Can you generate a brief explanation for the following Python 3 code
def pypirc_temp(index_url):
    pypirc_file = tempfile.NamedTemporaryFile(suffix='.pypirc', delete=False)
    print(pypirc_file.name)
    with open(pypirc_file.name, 'w') as fh:
        fh.write(PYPIRC_TEMPLATE.format(index_name=PYPIRC_TEMP_INDEX_NAME, index_url=index_url))
    return pypirc_file.name

Response: Create a temporary pypirc file for interaction with twine


Instruction:
Given the following Python 3 function, write the documentation
def get_api(version: str, ui_version: str=None) -> API_1:
    ui_version = ui_version if ui_version else "~1.0"
    return _get_api_with_app(version, ui_version, ApplicationModule.app)

Response: Get a versioned interface matching the given version and ui_version.


Instruction:
Can you tell what is the following Python 3 function doing
def mask_xdata_with_shape(self, shape: DataAndMetadata.ShapeType) -> DataAndMetadata.DataAndMetadata:
        mask = self._graphic.get_mask(shape)
        return DataAndMetadata.DataAndMetadata.from_data(mask)

Response: Return the extended data mask created by this graphic as extended data.


Instruction:
Here you have a function in Python 3, explain what it does
def end(self, value: typing.Union[float, NormPointType]) -> None:
        self.set_property("end", value)

Response: Set the end property in relative coordinates.


Instruction:
Write a Python 3 script to
set the start property in relative coordinates.

Response:
def start(self, value: typing.Union[float, NormPointType]) -> None:
        """Set the end property in relative coordinates.

        End may be a float when graphic is an Interval or a tuple (y, x) when graphic is a Line."""
        self.set_property("start", value)


Instruction:
Can you generate the documentation for the following Python 3 function
def data(self, data: numpy.ndarray) -> None:
        self.__data_item.set_data(numpy.copy(data))

Response:
Set the data.

        :param data: A numpy ndarray.

        .. versionadded:: 1.0

        Scriptable: Yes


Instruction:
How would you code a function in Python 3 to
return the extended data of this data item display.

Response:
def display_xdata(self) -> DataAndMetadata.DataAndMetadata:
        """Return the extended data of this data item display.

        Display data will always be 1d or 2d and either int, float, or RGB data type.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        display_data_channel = self.__display_item.display_data_channel
        return display_data_channel.get_calculated_display_values(True).display_data_and_metadata


Instruction:
Can you tell what is the following Python 3 function doing
def set_dimensional_calibrations(self, dimensional_calibrations: typing.List[CalibrationModule.Calibration]) -> None:
        self.__data_item.set_dimensional_calibrations(dimensional_calibrations)

Response: Set the dimensional calibrations.


Instruction:
Explain what the following Python 3 code does
def get_metadata_value(self, key: str) -> typing.Any:
        return self._data_item.get_metadata_value(key)

Response: Get the metadata value for the given key.


Instruction:
How would you code a function in Python 3 to
set the metadata value for the given key.

Response:
def set_metadata_value(self, key: str, value: typing.Any) -> None:
        """Set the metadata value for the given key.

        There are a set of predefined keys that, when used, will be type checked and be interoperable with other
        applications. Please consult reference documentation for valid keys.

        If using a custom key, we recommend structuring your keys in the '<group>.<attribute>' format followed
        by the predefined keys. e.g. 'session.instrument' or 'camera.binning'.

        Also note that some predefined keys map to the metadata ``dict`` but others do not. For this reason, prefer
        using the ``metadata_value`` methods over directly accessing ``metadata``.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        self._data_item.set_metadata_value(key, value)


Instruction:
Can you generate the documentation for the following Python 3 function
def graphics(self) -> typing.List[Graphic]:
        return [Graphic(graphic) for graphic in self.__display_item.graphics]

Response: Return the graphics attached to this data item.


Instruction:
Implement a function in Python 3 to
add a point graphic to the data item.

Response:
def add_point_region(self, y: float, x: float) -> Graphic:
        """Add a point graphic to the data item.

        :param x: The x coordinate, in relative units [0.0, 1.0]
        :param y: The y coordinate, in relative units [0.0, 1.0]
        :return: The :py:class:`nion.swift.Facade.Graphic` object that was added.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        graphic = Graphics.PointGraphic()
        graphic.position = Geometry.FloatPoint(y, x)
        self.__display_item.add_graphic(graphic)
        return Graphic(graphic)


Instruction:
How would you implement a function in Python 3 that
returns the mask of any extended data on this data item as extended data.

Response:
def mask_xdata(self) -> DataAndMetadata.DataAndMetadata:
        """Return the mask by combining any mask graphics on this data item as extended data.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        display_data_channel = self.__display_item.display_data_channel
        shape = display_data_channel.display_data_shape
        mask = numpy.zeros(shape)
        for graphic in self.__display_item.graphics:
            if isinstance(graphic, (Graphics.SpotGraphic, Graphics.WedgeGraphic, Graphics.RingGraphic, Graphics.LatticeGraphic)):
                mask = numpy.logical_or(mask, graphic.get_mask(shape))
        return DataAndMetadata.DataAndMetadata.from_data(mask)


Instruction:
Create a Python 3 function for
returning the data item associated with this display panel.

Response:
def data_item(self) -> DataItem:
        """Return the data item associated with this display panel.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        display_panel = self.__display_panel
        if not display_panel:
            return None
        data_item = display_panel.data_item
        return DataItem(data_item) if data_item else None


Instruction:
Write a Python 3 script for
setting the data item associated with this display panel.

Response:
def set_data_item(self, data_item: DataItem) -> None:
        """Set the data item associated with this display panel.

        :param data_item: The :py:class:`nion.swift.Facade.DataItem` object to add.

        This will replace whatever data item, browser, or controller is currently in the display panel with the single
        data item.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        display_panel = self.__display_panel
        if display_panel:
            display_item = data_item._data_item.container.get_display_item_for_data_item(data_item._data_item) if data_item._data_item.container else None
            display_panel.set_display_panel_display_item(display_item)


Instruction:
Write a Python 3 script for
adding a data item to the group.

Response:
def add_data_item(self, data_item: DataItem) -> None:
        """Add a data item to the group.

        :param data_item: The :py:class:`nion.swift.Facade.DataItem` object to add.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        display_item = data_item._data_item.container.get_display_item_for_data_item(data_item._data_item) if data_item._data_item.container else None
        if display_item:
            self.__data_group.append_display_item(display_item)


Instruction:
Can you generate the documentation for the following Python 3 function
def close(self) -> None:
        self.__data_channel_buffer.stop()
        self.__data_channel_buffer.close()
        self.__data_channel_buffer = None
        if not self.__was_playing:
            self.__hardware_source.stop_playing()

Response:
Close the task.

        .. versionadded:: 1.0

        This method must be called when the task is no longer needed.


Instruction:
Implement a function in Python 3 to
record data and return a list of data_and_metadata objects.

Response:
def record(self, frame_parameters: dict=None, channels_enabled: typing.List[bool]=None, timeout: float=None) -> typing.List[DataAndMetadata.DataAndMetadata]:
        """Record data and return a list of data_and_metadata objects.

        .. versionadded:: 1.0

        :param frame_parameters: The frame parameters for the record. Pass None for defaults.
        :type frame_parameters: :py:class:`FrameParameters`
        :param channels_enabled: The enabled channels for the record. Pass None for defaults.
        :type channels_enabled: List of booleans.
        :param timeout: The timeout in seconds. Pass None to use default.
        :return: The list of data and metadata items that were read.
        :rtype: list of :py:class:`DataAndMetadata`
        """
        if frame_parameters:
            self.__hardware_source.set_record_frame_parameters(self.__hardware_source.get_frame_parameters_from_dict(frame_parameters))
        if channels_enabled is not None:
            for channel_index, channel_enabled in enumerate(channels_enabled):
                self.__hardware_source.set_channel_enabled(channel_index, channel_enabled)
        self.__hardware_source.start_recording()
        return self.__hardware_source.get_next_xdatas_to_finish(timeout)


Instruction:
Write a Python 3 script to
create a record task for this hardware source.

Response:
def create_record_task(self, frame_parameters: dict=None, channels_enabled: typing.List[bool]=None) -> RecordTask:
        """Create a record task for this hardware source.

        .. versionadded:: 1.0

        :param frame_parameters: The frame parameters for the record. Pass None for defaults.
        :type frame_parameters: :py:class:`FrameParameters`
        :param channels_enabled: The enabled channels for the record. Pass None for defaults.
        :type channels_enabled: List of booleans.
        :return: The :py:class:`RecordTask` object.
        :rtype: :py:class:`RecordTask`

        Callers should call close on the returned task when finished.

        See :py:class:`RecordTask` for examples of how to use.
        """
        return RecordTask(self.__hardware_source, frame_parameters, channels_enabled)


Instruction:
Can you tell what is the following Python 3 function doing
def create_view_task(self, frame_parameters: dict=None, channels_enabled: typing.List[bool]=None, buffer_size: int=1) -> ViewTask:
        return ViewTask(self.__hardware_source, frame_parameters, channels_enabled, buffer_size)

Response: Create a view task for this hardware source.


Instruction:
Can you generate the documentation for the following Python 3 function
def grab_next_to_finish(self, timeout: float=None) -> typing.List[DataAndMetadata.DataAndMetadata]:
        self.start_playing()
        return self.__hardware_source.get_next_xdatas_to_finish(timeout)

Response: Grabs the next frame to finish and returns it as data and metadata.


Instruction:
Given the following Python 3 function, write the documentation
def set_control_output(self, name: str, value: float, *, options: dict=None) -> None:
        self.__instrument.set_control_output(name, value, options)

Response: Set the value of a control asynchronously.


Instruction:
Create a Python 3 function for
returning the value of a float property.

Response:
def get_property_as_float(self, name: str) -> float:
        """Return the value of a float property.

        :return: The property value (float).

        Raises exception if property with name doesn't exist.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        return float(self.__instrument.get_property(name))


Instruction:
Given the following Python 3 function, write the documentation
def set_property_as_float(self, name: str, value: float) -> None:
        self.__instrument.set_property(name, float(value))

Response: Set the value of a float property.


Instruction:
Create a Python 3 function to
return the list of data items.

Response:
def data_items(self) -> typing.List[DataItem]:
        """Return the list of data items.

        :return: The list of :py:class:`nion.swift.Facade.DataItem` objects.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        return [DataItem(data_item) for data_item in self.__document_model.data_items]


Instruction:
How would you explain what the following Python 3 function does
def display_items(self) -> typing.List[Display]:
        return [Display(display_item) for display_item in self.__document_model.display_items]

Response: Return the list of display items.


Instruction:
Implement a Python 3 function for
returning the list of data items that are data sources for the data item.

Response:
def get_source_data_items(self, data_item: DataItem) -> typing.List[DataItem]:
        """Return the list of data items that are data sources for the data item.

        :return: The list of :py:class:`nion.swift.Facade.DataItem` objects.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        return [DataItem(data_item) for data_item in self._document_model.get_source_data_items(data_item._data_item)] if data_item else None


Instruction:
How would you code a function in Python 3 to
return the dependent data items the data item argument.

Response:
def get_dependent_data_items(self, data_item: DataItem) -> typing.List[DataItem]:
        """Return the dependent data items the data item argument.

        :return: The list of :py:class:`nion.swift.Facade.DataItem` objects.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        return [DataItem(data_item) for data_item in self._document_model.get_dependent_data_items(data_item._data_item)] if data_item else None


Instruction:
How would you implement a function in Python 3 that
creates an empty data item in the library.

Response:
def create_data_item(self, title: str=None) -> DataItem:
        """Create an empty data item in the library.

        :param title: The title of the data item (optional).
        :return: The new :py:class:`nion.swift.Facade.DataItem` object.
        :rtype: :py:class:`nion.swift.Facade.DataItem`

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        data_item = DataItemModule.DataItem()
        data_item.ensure_data_source()
        if title is not None:
            data_item.title = title
        self.__document_model.append_data_item(data_item)
        return DataItem(data_item)


Instruction:
Can you tell what is the following Python 3 function doing
def create_data_item_from_data(self, data: numpy.ndarray, title: str=None) -> DataItem:
        return self.create_data_item_from_data_and_metadata(DataAndMetadata.DataAndMetadata.from_data(data), title)

Response: Create a data item in the library from an ndarray.


Instruction:
Can you tell what is the following Python 3 function doing
def create_data_item_from_data_and_metadata(self, data_and_metadata: DataAndMetadata.DataAndMetadata, title: str=None) -> DataItem:
        data_item = DataItemModule.new_data_item(data_and_metadata)
        if title is not None:
            data_item.title = title
        self.__document_model.append_data_item(data_item)
        return DataItem(data_item)

Response: Create a new data item in the library from a data and metadata object.


Instruction:
Make a summary of the following Python 3 code
def copy_data_item(self, data_item: DataItem) -> DataItem:
        data_item = copy.deepcopy(data_item._data_item)
        self.__document_model.append_data_item(data_item)
        return DataItem(data_item)

Response: Copy a data item.


Instruction:
How would you explain what the following Python 3 function does
def get_or_create_data_group(self, title: str) -> DataGroup:
        return DataGroup(self.__document_model.get_or_create_data_group(title))

Response: Get or create a data group.


Instruction:
Here you have a function in Python 3, explain what it does
def get_data_item_for_hardware_source(self, hardware_source, channel_id: str=None, processor_id: str=None, create_if_needed: bool=False, large_format: bool=False) -> DataItem:
        assert hardware_source is not None
        hardware_source_id = hardware_source._hardware_source.hardware_source_id
        document_model = self._document_model
        data_item_reference_key = document_model.make_data_item_reference_key(hardware_source_id, channel_id, processor_id)
        return self.get_data_item_for_reference_key(data_item_reference_key, create_if_needed=create_if_needed, large_format=large_format)

Response: Get the data item associated with a hardware source and channel id and processor id. Optionally create if missing.


Instruction:
Given the following Python 3 function, write the documentation
def get_data_item_for_reference_key(self, data_item_reference_key: str=None, create_if_needed: bool=False, large_format: bool=False) -> DataItem:
        document_model = self._document_model
        data_item_reference = document_model.get_data_item_reference(data_item_reference_key)
        data_item = data_item_reference.data_item
        if data_item is None and create_if_needed:
            data_item = DataItemModule.DataItem(large_format=large_format)
            data_item.ensure_data_source()
            document_model.append_data_item(data_item)
            document_model.setup_channel(data_item_reference_key, data_item)
            data_item.session_id = document_model.session_id
            data_item = document_model.get_data_item_reference(data_item_reference_key).data_item
        return DataItem(data_item) if data_item else None

Response: Get the data item associated with a data item reference key. Optionally create if none is found.


Instruction:
How would you code a function in Python 3 to
get the data item with the given UUID.

Response:
def get_data_item_by_uuid(self, data_item_uuid: uuid_module.UUID) -> DataItem:
        """Get the data item with the given UUID.

        .. versionadded:: 1.0

        Status: Provisional
        Scriptable: Yes
        """
        data_item = self._document_model.get_data_item_by_uuid(data_item_uuid)
        return DataItem(data_item) if data_item else None


Instruction:
How would you implement a function in Python 3 that
gets the graphic with the given UUID.

Response:
def get_graphic_by_uuid(self, graphic_uuid: uuid_module.UUID) -> Graphic:
        """Get the graphic with the given UUID.

        .. versionadded:: 1.0

        Status: Provisional
        Scriptable: Yes
        """
        for display_item in self._document_model.display_items:
            for graphic in display_item.graphics:
                if graphic.uuid == graphic_uuid:
                    return Graphic(graphic)
        return None


Instruction:
Write a Python 3 script to
return whether the library value for the given key exists.

Response:
def has_library_value(self, key: str) -> bool:
        """Return whether the library value for the given key exists.

        Please consult the developer documentation for a list of valid keys.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        desc = Metadata.session_key_map.get(key)
        if desc is not None:
            field_id = desc['path'][-1]
            return bool(getattr(ApplicationData.get_session_metadata_model(), field_id, None))
        return False


Instruction:
How would you explain what the following Python 3 function does
def get_library_value(self, key: str) -> typing.Any:
        desc = Metadata.session_key_map.get(key)
        if desc is not None:
            field_id = desc['path'][-1]
            return getattr(ApplicationData.get_session_metadata_model(), field_id)
        raise KeyError()

Response: Get the library value for the given key.


Instruction:
Can you tell what is the following Python 3 function doing
def set_library_value(self, key: str, value: typing.Any) -> None:
        desc = Metadata.session_key_map.get(key)
        if desc is not None:
            field_id = desc['path'][-1]
            setattr(ApplicationData.get_session_metadata_model(), field_id, value)
            return
        raise KeyError()

Response: Set the library value for the given key.


Instruction:
Write a Python 3 script to
delete the library value for the given key.

Response:
def delete_library_value(self, key: str) -> None:
        """Delete the library value for the given key.

        Please consult the developer documentation for a list of valid keys.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        desc = Metadata.session_key_map.get(key)
        if desc is not None:
            field_id = desc['path'][-1]
            setattr(ApplicationData.get_session_metadata_model(), field_id, None)
            return
        raise KeyError()


Instruction:
Explain what the following Python 3 code does
def all_display_panels(self) -> typing.List[DisplayPanel]:
        return [DisplayPanel(display_panel) for display_panel in self.__document_controller.workspace_controller.display_panels]

Response: Return the list of display panels currently visible.


Instruction:
Here you have a function in Python 3, explain what it does
def get_display_panel_by_id(self, identifier: str) -> DisplayPanel:
        display_panel = next(
            (display_panel for display_panel in self.__document_controller.workspace_controller.display_panels if
            display_panel.identifier.lower() == identifier.lower()), None)
        return DisplayPanel(display_panel) if display_panel else None

Response: Return the display panel with the identifier.


Instruction:
Here you have a function in Python 3, explain what it does
def display_data_item(self, data_item: DataItem, source_display_panel=None, source_data_item=None):
        for display_panel in self.__document_controller.workspace_controller.display_panels:
            if display_panel.data_item == data_item._data_item:
                display_panel.request_focus()
                return DisplayPanel(display_panel)
        result_display_panel = self.__document_controller.next_result_display_panel()
        if result_display_panel:
            display_item = self.__document_controller.document_model.get_display_item_for_data_item(data_item._data_item)
            result_display_panel.set_display_panel_display_item(display_item)
            result_display_panel.request_focus()
            return DisplayPanel(result_display_panel)
        return None

Response: Displays a new data item and gives it keyboard focus.


Instruction:
Here you have a function in Python 3, explain what it does
def show_get_string_message_box(self, caption: str, text: str, accepted_fn, rejected_fn=None, accepted_text: str=None, rejected_text: str=None) -> None:
        workspace = self.__document_controller.workspace_controller
        workspace.pose_get_string_message_box(caption, text, accepted_fn, rejected_fn, accepted_text, rejected_text)

Response: Show a dialog box and ask for a string.


Instruction:
How would you implement a function in Python 3 that
creates a data item in the library from data.

Response:
def create_data_item_from_data(self, data: numpy.ndarray, title: str=None) -> DataItem:
        """Create a data item in the library from data.

        .. versionadded:: 1.0
        .. deprecated:: 1.1
           Use :py:meth:`~nion.swift.Facade.Library.create_data_item_from_data` instead.

        Scriptable: No
        """
        return DataItem(self.__document_controller.add_data(data, title))


Instruction:
Can you tell what is the following Python 3 function doing
def create_data_item_from_data_and_metadata(self, data_and_metadata: DataAndMetadata.DataAndMetadata, title: str=None) -> DataItem:
        data_item = DataItemModule.new_data_item(data_and_metadata)
        if title is not None:
            data_item.title = title
        self.__document_controller.document_model.append_data_item(data_item)
        return DataItem(data_item)

Response: Create a data item from the data and metadata.


Instruction:
Implement a Python 3 function for
getting or create a data group.

Response:
def get_or_create_data_group(self, title: str) -> DataGroup:
        """Get (or create) a data group.

        .. versionadded:: 1.0
        .. deprecated:: 1.1
           Use :py:meth:`~nion.swift.Facade.Library.create_data_item_from_data` instead.

        Scriptable: No
        """
        return DataGroup(self.__document_controller.document_model.get_or_create_data_group(title))


Instruction:
Write a Python 3 script for
returning the document windows.

Response:
def document_windows(self) -> typing.List[DocumentWindow]:
        """Return the document windows.

        .. versionadded:: 1.0

        Scriptable: Yes
        """
        return [DocumentWindow(document_controller) for document_controller in self.__application.document_controllers]


Instruction:
Can you tell what is the following Python 3 function doing
def create_calibration(self, offset: float=None, scale: float=None, units: str=None) -> CalibrationModule.Calibration:
        return CalibrationModule.Calibration(offset, scale, units)

Response: Create a new calibration object with the specified offset scale and units.


Instruction:
Here you have a function in Python 3, explain what it does
def create_data_descriptor(self, is_sequence: bool, collection_dimension_count: int, datum_dimension_count: int) -> DataAndMetadata.DataDescriptor:
        return DataAndMetadata.DataDescriptor(is_sequence, collection_dimension_count, datum_dimension_count)

Response: Create a data descriptor.