def natsort_values(df, col, inplace=False): df = df if inplace else df.copy() if type(col) is str: idx = np.array(natsort.index_natsorted(df[col])) else: idx = np.array(natsort.index_natsorted(col)) df["__natsort_key__"] = np.argsort(idx) df.sort_values("__natsort_key__", inplace=True) df.drop("__natsort_key__", axis=1, inplace=True) return df
def get_data(label, max_items): # generate some random data items = ['%s_%d' % (label, i + 1) for i in list(range(max_items))] x1 = np.random.randint(low=1, high=100, size=max_items).tolist() x2 = np.random.randint(low=1, high=100, size=max_items).tolist() x3 = np.random.randint(low=1, high=100, size=max_items).tolist() # insert NAs to the first row items.insert(0, NA_ID) x1.insert(0, NA_TEXT) x2.insert(0, NA_TEXT) x3.insert(0, NA_TEXT) # create pandas dataframe colname = '%s_pk' % label data = {colname: items, 'x1': x1, 'x2': x2, 'x3': x3} df = pd.DataFrame(data) # https://stackoverflow.com/questions/29580978/naturally-sorting-pandas-dataframe df = df.reindex( index=order_by_index(df.index, index_natsorted(df[colname]))) # create bokeh ColumnDataSource and DataTable columns = [ TableColumn(field=colname, title='ID'), TableColumn(field='x1', title='x1'), TableColumn(field='x2', title='x2'), TableColumn(field='x3', title='x3'), ] ds = ColumnDataSource(df) dt = DataTable(source=ds, columns=columns, width=300, height=300) return df, ds, dt
def test_index_natsorted_returns_integer_list_of_sort_order_for_input_list(): a = ['num3', 'num5', 'num2'] b = ['foo', 'bar', 'baz'] index = index_natsorted(a) assert index == [2, 0, 1] assert [a[i] for i in index] == ['num2', 'num3', 'num5'] assert [b[i] for i in index] == ['baz', 'foo', 'bar']
def buscar(busqueda, categoria): """ docstring """ if not busqueda == "": todos_los_productos = [] driver.get(mercados.select_market + busqueda) producto_a_objeto(todos_los_productos) df = pd.DataFrame(todos_los_productos) orden = "producto" ordenado = df.sort_values( by=str(orden), key=lambda x: np.argsort(index_natsorted(df[str(orden)]))) today = str(datetime.date.today()) localdir = os.getcwd() #time = str(datetime.datetime.now()).replace(" ", "_").replace(":", "-") if not os.path.isdir(localdir + "/tablas/select_market/" + today): os.mkdir(localdir + "/tablas/select_market/" + today) if not os.path.isfile( f"tablas/select_market/{today}/por_{orden}_{categoria}_tabla.csv" ): f = open( f"tablas/select_market/{today}/por_{orden}_{categoria}_tabla.csv", "x") f.write(ordenado.to_csv(index=False)) f.close() busqueda = "" orden = "" print("busqueda finalizada")
def test_index_natsorted_returns_integer_list_of_sort_order_for_input_list(): given = ["num3", "num5", "num2"] other = ["foo", "bar", "baz"] index = index_natsorted(given) assert index == [2, 0, 1] assert [given[i] for i in index] == ["num2", "num3", "num5"] assert [other[i] for i in index] == ["baz", "foo", "bar"]
def prepare(df): # Sort columns df = df.sort_index(axis=1) # Natsort all rows df = df.reindex(index=order_by_index(df.index, index_natsorted(zip(df.to_numpy())))) # Recreate index for comparison later return df
def test_index_natsorted_returns_integer_list_of_sort_order_for_input_list(): a = ["num3", "num5", "num2"] b = ["foo", "bar", "baz"] index = index_natsorted(a) assert index == [2, 0, 1] assert [a[i] for i in index] == ["num2", "num3", "num5"] assert [b[i] for i in index] == ["baz", "foo", "bar"]
def prepare(self, df): # Sort columns df = df.sort_index(axis=1) # Natsort all rows df = df.reindex(index=order_by_index(df.index, index_natsorted(zip(df.to_numpy())))) # Recreate index for comparison later df.reset_index(level=0, drop=True, inplace=True) return df
def test_index_natsorted(): # Return the indexes of how the iterable would be sorted. a = ['num3', 'num5', 'num2'] b = ['foo', 'bar', 'baz'] index = index_natsorted(a) assert index == [2, 0, 1] assert [a[i] for i in index] == ['num2', 'num3', 'num5'] assert [b[i] for i in index] == ['baz', 'foo', 'bar'] # It accepts a key argument. c = [('a', 'num3'), ('b', 'num5'), ('c', 'num2')] assert index_natsorted(c, key=itemgetter(1)) == [2, 0, 1] # It can avoid "unorderable types" on Python 3 a = [46, '5a5b2', 'af5', '5a5-4'] assert index_natsorted(a) == [3, 1, 0, 2]
def _nat_order_labels(self, mat, labels): # get the natural order indices natindices = index_natsorted(labels) # order the matrix ordered_mat = np.array(order_by_index(mat, natindices)) ordered_labels = np.array(order_by_index(labels, natindices)) return ordered_mat, ordered_labels
def natural_sort(df, by='id', index=False): ''' Sort a pandas dataframe "naturally" by column or by index. ''' if index: return df.reindex(index=natsorted(df.index)) return df.reindex(index=order_by_index(df.index, index_natsorted(df[by])))
def bar_plot_mmgbsa_results(excel_file, sort=True, titles=None): """ Load data from an Excel file with the summary of the MMGBSA results, in a sheet which has to be called "MMGBSA". Create a plot for each ligand that is found under the 'Ligand' column in the table. :param excel_file: str, Name of the Excel file with the data :param sort: bool, Whether to sort the plot by increasing MMGBSA values :param titles: list, Name for each of the plots (as many as there are ligands in the table) :return f_list: list, A list of matplotlib figures """ # sns.set_style('whitegrid') df = pd.read_excel(excel_file, sheetname="MMGBSA") df = df.reindex(index=order_by_index(df.index, index_natsorted(df.Run))) ligands = df.Ligand.unique() f_list = [] if titles is None: titles = [None for _ in ligands] elif len(titles) != len(ligands): raise ValueError('len of ligands and titles is not equal.') for lig, title in zip(ligands, titles): lig_df = df[df.Ligand == lig] if sort: lig_df.sort_values(by='MMGBSA (mean)', inplace=True) ax = lig_df.plot(x="Run", y="MMGBSA (mean)", yerr='MMGBSA (Std)', kind='bar', legend=False, figsize=figure_dims(1400), title=title) overall_mean = lig_df['MMGBSA (mean)'].mean() overall_std = lig_df['MMGBSA (mean)'].std() print("{} {:02f} {:02f}".format(lig, overall_mean, overall_std)) xmin, xmax = ax.get_xlim() # Mean line ax.plot( [xmin, xmax], [overall_mean, overall_mean], linewidth=1.5, color='blue' ) # Upper std bar ax.plot( [xmin, xmax], [overall_mean + overall_std, overall_mean + overall_std], linestyle='dashed', linewidth=1, color='blue' ) # Lower std bar ax.plot( [xmin, xmax], [overall_mean - overall_std, overall_mean - overall_std], linestyle='dashed', linewidth=1, color='blue' ) ax.set_ylim(top=0) ax.set_ylabel(ylabel=r'$\Delta$G binding (kcal/mol)', size=14) ax.set_xlabel(xlabel='Run', size=14) f = pp.gcf() f.tight_layout() f_list.append(f) return f_list
def __order_feats(self, feats_path, feats): video_name = feats_path.split('/')[-1] video_name = utils.remove_extension(video_name) frames_names = self.__annotation_dict[video_name] idx = natsort.index_natsorted(frames_names) feats = feats[idx] return feats
def sort(self, column, order): self.layoutAboutToBeChanged.emit() if order == 0: self._dataframe = self._dataframe.reindex(index=order_by_index( self._dataframe.index, index_natsorted( eval('self._dataframe.%s' % (list(self._dataframe.columns)[column]))))) else: self._dataframe = self._dataframe.reindex(index=order_by_index( self._dataframe.index, reversed( index_natsorted( eval('self._dataframe.%s' % (list(self._dataframe.columns)[column])))))) self._dataframe.reset_index(inplace=True, drop=True) self.setDataFrame(self._dataframe) self.layoutChanged.emit()
def avgPrefs(prefsfiles): """Gets average of site-specific preferences. Args: `prefsfiles` (list) List of CSV files containing preferences, must all be for same sites and characters. Returns: A `pandas.DataFrame` containing the average of the preferences in `prefsfiles`. In this returned data frame, `site` is the index >>> tf1 = tempfile.NamedTemporaryFile >>> tf2 = tempfile.NamedTemporaryFile >>> with tf1(mode='w') as file1, tf2(mode='w') as file2: ... x = file1.write('site,A,C,G,T\\n' ... '10,0.2,0.2,0.5,0.1\\n' ... '2a,0.3,0.3,0.3,0.1') ... file1.flush() ... x = file2.write('site,A,C,G,T\\n' ... '10,0.4,0.1,0.1,0.4\\n' ... '2a,0.3,0.4,0.1,0.2') ... file2.flush() ... avg = avgPrefs([file1.name, file2.name]) >>> (avg['site'] == ['2a', '10']).all() True >>> numpy.allclose(avg['A'], [0.3, 0.3]) True >>> numpy.allclose(avg['C'], [0.35, 0.15]) True >>> numpy.allclose(avg['G'], [0.2, 0.3]) True >>> numpy.allclose(avg['T'], [0.15, 0.25]) True """ assert len(prefsfiles) >= 1 prefs = [ pandas.read_csv(f, index_col='site').sort_index() for f in prefsfiles ] # make sure all have the same columns in the same order cols = prefs[0].columns for i in range(len(prefs)): assert set(cols) == set(prefs[i].columns) prefs[i] = prefs[i][cols] avgprefs = pandas.concat(prefs).groupby('site').mean().reset_index() # natural sort by site: https://stackoverflow.com/a/29582718 avgprefs = avgprefs.reindex(index=natsort.order_by_index( avgprefs.index, natsort.index_natsorted(avgprefs.site, signed=True))) return avgprefs
def natsort_values(df, cols, ascending=True): """Substitute for pd.DataFrame.sort_values """ from natsort import index_natsorted if not isinstance(cols, list): cols = [cols] values = np.array([np.argsort(index_natsorted(df[c])) for c in cols]).T ix = (pd.DataFrame(values, columns=cols) .sort_values(cols, ascending=ascending) .index) return df.iloc[list(ix)].copy()
def rebuild_dataframes(self, metabolite): """ Function to assemble datas after calculations into final dataframe with a natural sort on index level "sources" :param metabolite: metabolite to process :type metabolite: str :return data_df: dataframe ccntaining calculated concentrations :rtype: class: 'Pandas.Dataframe' :return cal_df: dataframe containing data used for calibration curves :rtype cal_df: class: 'Pandas.Dataframe' """ self._logger.debug("Rebuilding dataframes\n") data_df = pd.DataFrame.from_dict({ "sources": self.tmp_sample_dict["sources"], "Linear Concentrations (µM)": self.tmp_sample_dict["Linear result"], "Quadratic Concentrations (µM)": self.tmp_sample_dict["Quadratic result"] }) cal_df = pd.DataFrame.from_dict(self.tmp_cal_dict) # We sort values naturally by sources before putting in dfs data_df = data_df.sort_values( by="sources", key=lambda x: np.argsort(index_natsorted(x))) cal_df = cal_df.sort_values( by="sources", key=lambda x: np.argsort(index_natsorted(x))) # We need to insert the metabolite name before returning the dfs data_df = data_df.assign(metabolite=metabolite) cal_df = cal_df.assign(metabolite=metabolite) data_df.set_index(['metabolite', 'sources'], inplace=True) cal_df.set_index(['metabolite', 'sources'], inplace=True) return data_df, cal_df
def test_index_natsorted(): # Return the indexes of how the iterable would be sorted. a = ['num3', 'num5', 'num2'] b = ['foo', 'bar', 'baz'] index = index_natsorted(a) assert index == [2, 0, 1] assert [a[i] for i in index] == ['num2', 'num3', 'num5'] assert [b[i] for i in index] == ['baz', 'foo', 'bar'] assert index_natsorted(a, reverse=True) == [1, 0, 2] # It accepts a key argument. c = [('a', 'num3'), ('b', 'num5'), ('c', 'num2')] assert index_natsorted(c, key=itemgetter(1)) == [2, 0, 1] # It can avoid "unorderable types" on Python 3 a = [46, '5a5b2', 'af5', '5a5-4'] assert index_natsorted(a) == [3, 1, 0, 2] # It can sort lists of lists. data = [['a1', 'a5'], ['a1', 'a40'], ['a10', 'a1'], ['a2', 'a5']] assert index_natsorted(data) == [0, 1, 3, 2] # It can sort paths too a = ['/p/Folder (10)/', '/p/Folder/', '/p/Folder (1)/'] assert index_natsorted(a, alg=ns.PATH) == [1, 2, 0]
def _get1d_neighbors(self, electrodechans, chanlabel): # naturally sort the gridlayout natinds = index_natsorted(electrodechans) sortedelec = electrodechans[natinds] # get the channel index - ijth index chanindex = sortedelec.index(chanlabel) # get the neighboring indices and channels nbrinds = [chanindex + 1, chanindex - 1] nbrchans = [chanlabel[ind] for ind in nbrinds] return nbrchans, nbrinds
def test_index_versorted(): a = ['1.9.9a', '1.11', '1.9.9b', '1.11.4', '1.10.1'] assert index_versorted(a) == index_natsorted(a, alg=ns.VERSION) assert index_versorted(a, reverse=True) == index_versorted(a)[::-1] a = [('a', '1.9.9a'), ('a', '1.11'), ('a', '1.9.9b'), ('a', '1.11.4'), ('a', '1.10.1')] assert index_versorted(a) == [0, 2, 4, 1, 3] # It can sort paths too a = ['/p/Folder (10)/file1.1.0.tar.gz', '/p/Folder/file1.1.0.tar.gz', '/p/Folder (1)/file1.1.0 (1).tar.gz', '/p/Folder (1)/file1.1.0.tar.gz'] assert index_versorted(a, alg=ns.PATH) == [1, 3, 2, 0]
def main(): """Main function for pyim-annotate.""" args = parse_args() insertions = Insertion.from_csv(args.insertions, sep='\t') annotator = args.caller.from_args(args) annotated = list(annotator.annotate(insertions)) annotated_frame = Insertion.to_frame(annotated) annotated_frame = annotated_frame.reindex(index=order_by_index( annotated_frame.index, index_natsorted(annotated_frame.id))) annotated_frame.to_csv(str(args.output), sep='\t', index=False)
def checkdf(self, p, x): """ :return: merged dataframes """ labels = x.keys() colnames = ['standard_hue', 'ntrial', 'all_intensities', 'all_responses', 'reversal value'] df = pd.DataFrame(columns=colnames, index=labels) for label in labels: sheet = x[label] df.loc[label, colnames[0]] = float(p[p['label'] == label]['standard']) df.loc[label, colnames[1]] = len(sheet) df.loc[label, colnames[2]] = np.array((sheet['All Intensities'])) df.loc[label, colnames[3]] = np.array((sheet['All Responses'])) df.loc[label, colnames[4]] = np.array((sheet['Reversal Intensities'])) df = df.reindex(index=order_by_index(df.index, index_natsorted(df.index, reverse=False))) return df
def sumxrl(self): par, xls, count = self.readxrl() dfs = pd.concat([self.checkdf(p, x) for p, x in zip(par, xls)], axis=0) summary = dfs.groupby(level=0).agg({ 'standard_hue': 'unique', 'ntrial': lambda x: sum(x) / count, 'reversal value': [self.meanvalue, self.stdvalue], 'all_responses': [self.meanvalue, self.stdvalue] }) summary = summary.reindex(index=order_by_index( summary.index, index_natsorted(summary.index, reverse=False))) return dfs, summary
def takeMinLineMmap(linesList, outputFile, mapping, writePos, actualFilePos, mapSize, rFileSize): """ Writes the smallest line from linesList to outputFile using the mapped approach. """ minRow = natsort.index_natsorted(linesList, key=lambda x: (x is None, x)) bufferListIndex = int(minRow[0]) minLine = natsort.natsorted(linesList, key=lambda x: (x is None, x)) minBytes = bytes(minLine[0][1], 'utf-8') mapping, writePos, actualFilePos = writeln_mmap(mapping, writePos, actualFilePos, mapSize, rFileSize, outputFile, minBytes) linesList[bufferListIndex] = [None, None] return bufferListIndex, mapping, writePos, actualFilePos
def _get_stranded_f(self, half_entries, f, sort=False): counter = 0 dfs = [] chromosomes = self.chromosomes if f == "tail": chromosomes = reversed(chromosomes) default = pd.DataFrame(columns=self.columns) for chromosome in chromosomes: plus = self.dfs.get((chromosome, "+"), default) minus = self.dfs.get((chromosome, "-"), default) if sort: plus = plus.sort_values(sort_cols) minus = minus.sort_values(sort_cols) plus = getattr(plus, f)(half_entries) minus = getattr(minus, f)(half_entries) df = pd.concat([plus, minus]) if sort: df = df.sort_values(sort_cols) counter += len(df) dfs.append(df) if counter >= half_entries: break df = pd.concat(dfs) # got twice as many entries as needed before sort. Halve here: df = getattr(df, f)(half_entries) # dfs = {df.Chromosome.iloc[0]: df for df in} df = df.reset_index(drop=True) df = df.reindex(index=natsort.order_by_index( df.index, natsort.index_natsorted(zip(df.Chromosome)))) return df
def main(options): # open all files # read all headers fnames = options.inbw bw = {} header = {} chroms = {} for fname in fnames: print("opening file for input: " + os.path.split(fname)[1]) bw[fname] = pyBigWig.open(fname) header[fname] = bw[fname].chroms() chroms[fname] = list(header[fname].keys())[0] # define order based on chromosome names extracted from headers idx = natsort.index_natsorted(chroms) fnames = natsort.order_by_index(fnames, idx) # open bigwig for output print("opening bigwig file for output (%s)" % options.outfname) out_bw = pyBigWig.open(options.outfname, "w") assert (out_bw is not None) # construct sorted header and write to output header = [list(header[f].items())[0] for f in fnames] print(str(header)) out_bw.addHeader(header) # loop over sorted chromosome-names/file-names ## import data from input bw ## add read data to output bw for fname in fnames: print("exporting data from chrom; " + chroms[fname] + " (file: " + fname + ")") ints = bw[fname].intervals(chroms[fname]) chrs = [chroms[fname]] * len(ints) out_bw.addEntries(chrs, [i[0] for i in ints], ends=[i[1] for i in ints], values=[i[2] for i in ints]) print("closing bigwig file for output (%s)" % options.outfname) out_bw.close() return True
def summary_table(table): # generate summary by adding all values by piovote table summary = table.pivot_table(values=ps.DOSE_MSV, index=[ps.POINT_NAME, ps.OCCUPANCY_FACTOR], aggfunc=sum) # get sort order, natsorted gives 0,1,2,3,4,5,6,7,8,8, 10 instead of # 0, 1, 10,2, 20 etc. new_index = index_natsorted(summary.index) # sort table by point name summary = summary.iloc[new_index] summary.reset_index(inplace=True) # add corrected dose for occupancy summary[ps.DOSE_OCCUPANCY_MSV] = summary[ps.DOSE_MSV] * \ summary[ps.OCCUPANCY_FACTOR] return summary
def convert_clusters(clusters, cluster_size_min, cluster_size_max, out_path, normalise): ''' Args: clusters(Object): Clusters object cluster_size_min(int): minimum size of cluster (more than or equal to int) cluster_size_max(int): maximum size of cluster (less than or equal to int) out_path(str): Out file path ''' assert cluster_size_min > 1, 'Minimum cluster size needs to be > 1' all_clusters = [] for barcode, cluster in clusters.get_items(): cs = cluster.size('DPM') if cs >= cluster_size_min and cs <= cluster_size_max: all_clusters.extend(cluster2sfws(cluster, 'DPM', normalise)) column_names=['str1', 'chr1', 'pos1', 'frag1', 'str2', 'chr2', 'pos2','frag2', 'score'] df = pd.DataFrame(all_clusters, columns=column_names) df_out = df.reindex(index=order_by_index(df.index, index_natsorted(zip(df.chr1, df.chr2, df.pos1, df.pos2)))) df_out.to_csv(out_path, sep=' ', index=False, header=False)
def natsort_contacts(self) -> Tuple: """ Naturally sort the contacts. Keeps the applied indices in self.naturalinds Returns ------- naturalinds """ if self.naturalinds == None: self.naturalinds = index_natsorted(self.chanlabels) self.chanlabels = np.array( order_by_index(self.chanlabels, self.naturalinds)) else: warnings.warn( "Already naturally sorted contacts! Extract channel labels naturally sorted by calling " "chanlabels, and apply ordering to other channel level data with naturalinds." ) return self.naturalinds
def write_click_csv(self, n_frames): out = pd.DataFrame(self.mouse_clicks) out = out.reindex(index=order_by_index( out.index, index_natsorted(out['frame'], reverse=False))) out = out.assign(time=[0]*out.shape[0]) out = out.assign(visible=[1]*out.shape[0]) cols = ['frame', 'time', 'visible', 'x', 'y'] out = out[cols] # reindex or change the order of columns str_ = out.to_csv(sep=';', index=False) h, w = self.frames[0].shape[:2] header = 'VideoWidth:{}\nVideoHeight:{}\nDisplayWidth:0\nDisplayHeight:0\n'.format(w, h) str_ = header + str_ text_file = open(self.out_csv, 'w') n = text_file.write(str_) text_file.close() print('written {}'.format(self.out_csv))
def list_of_names(fname='SavedData/searchResult.php'): names = [] ulist = [] bs = BeautifulSoup(open(fname), features='lxml') pbar = ProgressBar() for species in pbar(bs.findAll('i')): for parents in species.parents: if parents.name == 'td': [names.append(name) for name in species.contents] [ulist.append(x) for x in names if x not in ulist] # deleting double values df = pd.DataFrame() # create dataframe df['name'] = ulist df = df.reindex(index=order_by_index( df.index, index_natsorted(df['name'], reverse=False))) # sort alphabetically df = df.reset_index(drop=True) # fix index return df
def test_index_natsorted_returns_reversed_integer_list_of_sort_order_for_input_list_with_reverse_option(): a = ['num3', 'num5', 'num2'] assert index_natsorted(a, reverse=True) == [1, 0, 2]
else "\\newline" if len(x) <= 1 else x[0], x[1:].strip()) for x in lines[footnotes_start:]]) postbl = Table([Column(data=names, name='Source Name'), Column(data=coords.ra.to_string(unit=u.hour, sep=':', precision=2), name='RA'), Column(data=coords.dec.to_string(unit=u.deg, sep=':', precision=1), name='Dec'), Column(data=radii, name='Radius'), Column(data=(radii*5.1*u.kpc).to(u.pc, u.dimensionless_angles()), name='Phys. Radius'), SEDclasscolumn, classificationcolumn, ]) import natsort postbl = postbl[natsort.index_natsorted(postbl['Source Name'])] latexdict['header_start'] = '\label{tab:positions}' latexdict['caption'] = 'Source Positions' latexdict['tablefoot'] = ('\par\nObjects with name e\#d are the diffuse ' 'counterparts to point sources. ' 'The absolute positional accuracy is ' '$\sim0.2\\arcsec$. ' 'Sources with no radius are unresolved, ' 'with ' 'upper limits of 0.3\\arcsec (0.007 pc).' 'Source names correspond to the labels in Figures ' '\\ref{fig:coverview_pointsrcs} and ' '\\ref{fig:coverview_diffuse}.' '\\\\\n' + footer)
def test_index_versorted_returns_results_identical_to_index_natsorted(): a = ["1.9.9a", "1.11", "1.9.9b", "1.11.4", "1.10.1"] # index_versorted is retained for backwards compatibility assert index_versorted(a) == index_natsorted(a)
def prof_str_index_key(a): print('*** Basic Index Call With Key ***') for _ in py23_range(1000): index_natsorted(a, key=lambda x: x.upper())
def test_index_humansorted_returns_results_identical_to_index_natsorted_with_LOCALE(): a = ['Apple', 'corn', 'Corn', 'Banana', 'apple', 'banana'] assert index_humansorted(a) == index_natsorted(a, alg=ns.LOCALE)
def test_index_versorted_returns_results_identical_to_index_natsorted(): a = ['1.9.9a', '1.11', '1.9.9b', '1.11.4', '1.10.1'] # index_versorted is retained for backwards compatibility assert index_versorted(a) == index_natsorted(a)
def test_index_natsorted_returns_integer_list_of_nested_input_list(): data = [['a1', 'a5'], ['a1', 'a40'], ['a10', 'a1'], ['a2', 'a5']] assert index_natsorted(data) == [0, 1, 3, 2]
def test_index_realsorted_is_identical_to_index_natsorted_with_real_alg(float_list): assert index_realsorted(float_list) == index_natsorted(float_list, alg=ns.REAL)
def test_index_realsorted_returns_results_identical_to_index_natsorted_with_REAL(): a = ["a50", "a51.", "a50.31", "a-50", "a50.4", "a5.034e1", "a50.300"] assert index_realsorted(a) == index_natsorted(a, alg=ns.REAL)
def test_index_humansorted_returns_results_identical_to_index_natsorted_with_LOCALE(): a = ["Apple", "corn", "Corn", "Banana", "apple", "banana"] assert index_humansorted(a) == index_natsorted(a, alg=ns.LOCALE)
def test_index_natsorted_applies_key_function_before_sorting(): c = [("a", "num3"), ("b", "num5"), ("c", "num2")] assert index_natsorted(c, key=itemgetter(1)) == [2, 0, 1]
def test_index_natsorted_handles_unorderable_types_error_on_Python3(): a = [46, "5a5b2", "af5", "5a5-4"] assert index_natsorted(a) == [3, 1, 0, 2]
from astropy.table import Table, Column from astropy import units as u from latex_info import latexdict, format_float, round_to_n, strip_trailing_zeros import natsort tbl = Table.read('core_continuum_and_line.ipac', format='ascii.ipac') inds = natsort.index_natsorted(tbl['SourceID']) tbl = tbl[inds] tbl_towrite = tbl['SourceID', 'RA', 'Dec', 'cont_flux0p2arcsec', 'cont_flux0p4arcsec', #'BrightestFittedPeakPixBrightness', 'BrightestFittedPeakPixBrightnessWithcont', 'T_corrected_0p2aperturemass', 'T_corrected_peakmass', 'Categories', #'Classification', ] for row in tbl_towrite: if '_' in row['SourceID']: row['SourceID'] = row['SourceID'].replace("_"," ") new_names = {'SourceID': 'Source ID', 'cont_flux0p2arcsec': '$S_{\\nu}(0.2\\arcsec)$', 'cont_flux0p4arcsec': '$S_{\\nu}(0.4\\arcsec)$', #'BrightestFittedPeakPixBrightness': '$T_{B,max}(\mathrm{line})$', 'BrightestFittedPeakPixBrightnessWithcont': '$T_{B,max}$',#(\mathrm{line+cont})$', 'T_corrected_0p2aperturemass': 'M$(T_B, 0.2\\arcsec)$',
def prof_str_index(a): print('*** Basic Index Call ***') for _ in py23_range(1000): index_natsorted(a)
def test_index_natsorted_applies_key_function_before_sorting(): c = [('a', 'num3'), ('b', 'num5'), ('c', 'num2')] assert index_natsorted(c, key=itemgetter(1)) == [2, 0, 1]
def test_index_natsorted_handles_unorderable_types_error_on_Python3(): a = [46, '5a5b2', 'af5', '5a5-4'] assert index_natsorted(a) == [3, 1, 0, 2]
def control_timetable(timetable, header): print(Fore.YELLOW + '{:-^30}'.format(header)) start_order_details = [] for i in range(1, 4): start_order_details.append([j[1] for j in timetable if j[0] == i]) # разбиваем интервалы по деталям, выйдет 4 списка, использ. # для подсчета ожидания order_by_details = [] for i in start_order_details[0]: order_by_details.append([j for j in timetable if j[1] == i]) # для подсчета простоев сорт. наше расписание по ГВМ timetable = ordered_timetable(timetable) # список порядка запуска деталей на ГВМ, убираем номер операции # и ГВМ со списка интервалов, для обсчета ожидания start_order_gvm = [] order_by_details_norm = [] for i in order_by_details: start_order_gvm.append([j[:1][0] for j in i]) order_by_details_norm.append([j[2:] for j in i]) # считаем послеоперационные простои ГВМ timetable = downtime(timetable) for i in range(3): index = natsort.index_natsorted(start_order_details[i]) timetable[i] = natsort.order_by_index(timetable[i], index) print(Fore.WHITE + "\nDowntime: " + Fore.CYAN + '{}'.format(min([sum(i) for i in timetable]))) for i in timetable: print(Fore.WHITE + '{!s:>18s}'.format(i)) # считаем ожидание деталей перед обработкой print("\nWaiting:") order_by_details = waiting(order_by_details_norm) # упорядочиваем ожидание для каждой детали по ГВМ: 1 2 3 index = natsort.index_natsorted(start_order_details[0]) order_by_details = natsort.order_by_index(order_by_details, index) start_order_gvm = natsort.order_by_index(start_order_gvm, index) for i in range(4): index = natsort.index_natsorted(start_order_gvm[i]) order_by_details[i] = natsort.order_by_index(order_by_details[i], index) for i in order_by_details: print(Fore.WHITE + '{!s:>16s}'.format(i)) #считаем локальний резерв order_by_details = numpy.transpose(order_by_details) print('\nLocal resource:\n') for i in range(3): for j in range(4): first = timetable[i][j] try: second = order_by_details[i+1][j] except IndexError: second = float('inf') print(" L({0}, {1}) = min({2}, {3}) = {4}".format( \ i+1, j+1, first, second, min(first, second))) print('\n') print(Style.RESET_ALL)
def test_index_natsorted_returns_integer_list_in_proper_order_for_input_paths_with_PATH(): a = ['/p/Folder (10)/', '/p/Folder/', '/p/Folder (1)/'] assert index_natsorted(a, alg=ns.PATH) == [1, 2, 0]
def test_index_natsorted_returns_integer_list_of_nested_input_list(): data = [["a1", "a5"], ["a1", "a40"], ["a10", "a1"], ["a2", "a5"]] assert index_natsorted(data) == [0, 1, 3, 2]
def test_index_realsorted_returns_results_identical_to_index_natsorted_with_REAL(): a = ['a50', 'a51.', 'a50.31', 'a-50', 'a50.4', 'a5.034e1', 'a50.300'] assert index_realsorted(a) == index_natsorted(a, alg=ns.REAL)
def test_index_natsorted_reverse(): given = ["num3", "num5", "num2"] assert index_natsorted(given, reverse=True) == index_natsorted(given)[::-1]
nirassociations = {x[:split1].strip():x[split2:split3].strip() for x in lines[1:]} goldaderassociations = {x[:split1].strip():x[split3:].strip() for x in lines[1:]} names = sorted(xrayassociations.keys()) keep = [name for name in names if not (xrayassociations[name]=='-' and nirassociations=='-')] xrayassociations_column = Column(data=[xrayassociations[x] for x in keep], name="X-ray") nirassociations_column = Column(data=[nirassociations[x] for x in keep], name="NIR") goldaderassociations_column = Column(data=[goldaderassociations[x] for x in keep], name="Goldader 1994") names_column = Column(data=keep, name='Source Name') tbl = Table([names_column, xrayassociations_column, nirassociations_column, goldaderassociations_column]) OK = (tbl['X-ray'] != '-') | (tbl['NIR'] != '-') | (tbl['Goldader 1994'] != '-') tbl=tbl[OK] import natsort tbl = tbl[natsort.index_natsorted(tbl['Source Name'])] latexdict['header_start'] = '\label{tab:associations}' latexdict['caption'] = 'Source Associations' latexdict['tablefoot'] = ('') latexdict['col_align'] = 'l'*len(tbl.colnames) #latexdict['tabletype'] = 'longtable' #latexdict['tabulartype'] = 'longtable' tbl.write(paths.tpath('associations.tex'), format='ascii.latex', latexdict=latexdict, formats={},)
import numpy as np from astropy.table import Table import paths from latex_info import latexdict, exp_to_tex, format_float import natsort tbl = Table.read("../tables/obs_meta.tbl", format="ascii", delimiter="|") tbl = tbl[natsort.index_natsorted(tbl["Date"])] latexdict["header_start"] = "\label{tab:obs_meta}" latexdict["caption"] = "Observation Metadata" latexdict["tablefoot"] = "" latexdict["col_align"] = "l" * 8 # latexdict['tabletype'] = 'longtable' # latexdict['tabulartype'] = 'longtable' tbl.write(paths.tpath("obs_meta.tex"), format="ascii.latex", latexdict=latexdict, formats={}, overwrite=True)
data,error = zip(*dataerror) #data = [sp.specfit.parinfo[ii].value # for sp in spectra] #error = [sp.specfit.parinfo[ii].error # for sp in spectra] column = table.Column(data=data, name='H77a_'+parname, unit=unit) tbl.add_column(column) column = table.Column(data=error, name='eH77a_'+parname, unit=unit) tbl.add_column(column) # sort such that e10 comes after e9 tbl = tbl[natsort.index_natsorted(tbl['ObjectName'])] detection_note = ['-' if name in detections else 'weak' if name in weakdetections else 'none' for name in tbl['ObjectName']] tbl.add_column(table.Column(data=detection_note, name='DetectionStatus')) ok = np.array([row['ObjectName'] in detections+weakdetections for row in tbl]) tbl[ok].write(paths.tpath('H77a_spectral_fits.ipac'), format='ascii.ipac') for old,new in [('ObjectName','Object Name'), ('H77a_amplitude', 'Amplitude',), ('eH77a_amplitude','$E$(Amplitude)',),
def test_index_natsorted_applies_key_function_before_sorting(): given = [("a", "num3"), ("b", "num5"), ("c", "num2")] expected = [2, 0, 1] assert index_natsorted(given, key=itemgetter(1)) == expected
def test_index_versorted_returns_results_identical_to_index_natsorted_with_VERSION(): a = ['1.9.9a', '1.11', '1.9.9b', '1.11.4', '1.10.1'] assert index_versorted(a) == index_natsorted(a, alg=ns.VERSION)
+ list( (rounded(thisspec.specfit.parinfo.AMPLITUDE0.value, thisspec.specfit.parinfo.AMPLITUDE0.error) * u.Jy).to( u.mJy ) ) + list(rounded(thisspec.specfit.parinfo.SHIFT0.value, thisspec.specfit.parinfo.SHIFT0.error) * u.km / u.s) + list(rounded(thisspec.specfit.parinfo.WIDTH0.value, thisspec.specfit.parinfo.WIDTH0.error) * u.km / u.s) + [np.round(r_eff, 1) * u.arcsec] # [np.round(thisspec.header['APAREA']*(np.pi/180.)**2, int(np.ceil(-np.log10(thisspec.header['APAREA']*(np.pi/180.)**2)))+1)*u.sr] ) # sort such that e10 comes after e9 import natsort tbl = tbl[natsort.index_natsorted(tbl["Object Name"])] detection_note = [ "-" if name in detections else "ambig" if name in ambiguousdetections else "none" for name in tbl["Object Name"] ] tbl.add_column(table.Column(data=detection_note, name="Detection Status")) ok = np.array([row["Object Name"] in detections + ambiguousdetections for row in tbl]) tbl[ok].write(paths.tpath("H2CO22_hiiregion_spectral_fits.ecsv"), format="ascii.ecsv") for row in tbl: if "_" in row["Object Name"]: row["Object Name"] = row["Object Name"].replace("_", "\_") latexdict["header_start"] = "\label{tab:absorption22}"