Best Python code snippet using autotest_python
synthetic.py
Source:synthetic.py  
...39        ionisation.init_splines(self.altitude)40        self.integrated_block_list = []41        self.block_nx = self.dataset.header['block_nx']42        self.block_nx_int = self._reduce_list_to_2d(self.block_nx)43    def _get_ne(self, block, block_ion):44        """45        Returns the electron density of a single block, based on the interpolated ionisation values.46        :param block: block to calculate the electron density47        :param block_ion: interpolated ionisation values for 'block'48        :return: electron densities as a numpy array with the same shape as 'block'49        """50        block_p = block["p"] * self.dataset.units.unit_pressure51        block_T = block["T"] * self.dataset.units.unit_temperature52        block_ne = block_p / ((1 + 1.1 / block_ion) * self.dataset.units.k_B * block_T)53        return block_ne54    def _integrate_block(self, block, l_edge, r_edge):55        """56        Integrates a given block along the given line of sight.57        :param block: the block to integrate58        :param l_edge: contains the left edge of the block, as a ndim length list [x0(, y0, z0)]59        :param r_edge: contains the right edge of the block, as an ndim length list [x1(, y1, z1)]60        :return: 2D numpy array, containing the integrated block. If the block is originally 2D the block itself61                 is returned.62        """63        if self.dataset.header["ndim"] == 2:64            return block65        if self.line_of_sight == 'x':66            x = np.linspace(l_edge[0], r_edge[0], self.block_nx[0])67            result = np.zeros_like(block[0, :, :])68            for i, j in np.ndindex(result.shape):69                col = block[:, i, j]70                integrated_col = np.trapz(col, x)71                result[i, j] = integrated_col72        elif self.line_of_sight == 'y':73            y = np.linspace(l_edge[1], r_edge[1], self.block_nx[1])74            result = np.zeros_like(block[:, 0, :])75            for i, j in np.ndindex(result.shape):76                col = block[i, :, j]77                integrated_col = np.trapz(col, y)78                result[i, j] = integrated_col79        else:80            z = np.linspace(l_edge[2], r_edge[2], self.block_nx[2])81            result = np.zeros_like(block[:, :, 0])82            for i, j in np.ndindex(result.shape):83                col = block[i, j, :]84                integrated_col = np.trapz(col, z)85                result[i, j] = integrated_col86        return result87    def _reduce_list_to_2d(self, list_in):88        """89        Used for integrating the block. Converts a 3D array to 2D, depending on the line of sight.90        :param list_in: list of length 2 or 391        :return: numpy array of dimension 2, if list_in is 2-dim as input it is returned92        """93        if self.dataset.header['ndim'] == 2:94            return np.asarray(list_in)95        if self.line_of_sight == 'x':96            array2d = np.asarray([list_in[1], list_in[2]])97        elif self.line_of_sight == 'y':98            array2d = np.asarray([list_in[0], list_in[2]])99        else:100            array2d = np.asarray(list_in[:-1])101        return array2d102    def _merge_integrated_blocks(self):103        """104        Merges all 2D integrated blocks into a single 2D array depending on their position in the Morton curve.105        All 2D integrated blocks are regridded to the finest level available in the current list of blocks.106        As only 2D matrices have to be regridded instead of 3D, this means a large speedup in runtime.107        :return: 2D numpy array containing the merged integrated view108        """109        self.integrated_block_list = np.asarray(self.integrated_block_list)110        # Initialise merged 2D matrix111        maxlvl = np.max(self.dataset.block_lvls)112        refined_nx = 2**(maxlvl - 1) * self.dataset.header['domain_nx']113        refined_nx = self._reduce_list_to_2d(refined_nx)114        merged_result = np.zeros(tuple(refined_nx))115        # NOTE: one can also add all the blocks at a specific level together, and THEN regrid the resulting 2D116        #       matrix to the maximum level. This is faster, however, because the 2D matrix still contains zeros117        #       from the blocks not at the level at that moment considered, these are also accounted for during118        #       interpolation. This has unintended consequences near the block edges between two different levels,119        #       such as clearly visible grid lines and very small values not equal to zero (where they should be zero).120        # iterate over blocks and corresponding levels in dataset121        for ileaf, blocklvl in enumerate(self.dataset.block_lvls):122            grid_power_diff = 2 ** (maxlvl - blocklvl)          # power of 2 difference between level and max level123            regrid_width = self.block_nx_int * grid_power_diff  # index width of the block at max resolution124            # regrid current block to max level125            block = regridding.regrid_2dmatrix(self.integrated_block_list[ileaf], tuple(regrid_width))126            # retrieve current index in morton curve, reduce to 2D (due to integration)127            block_morton_idx = self._reduce_list_to_2d(self.dataset.block_ixs[ileaf])128            # calculate left and right index positions of the block using morton index129            idx0 = (block_morton_idx - 1) * grid_power_diff * self.block_nx_int130            idx1 = idx0 + tuple(regrid_width)131            # add this block to the integrated result132            merged_result[idx0[0]:idx1[0], idx0[1]:idx1[1]] += block133        return merged_result134    def _plot_synthetic_view(self, view):135        bounds_x, bounds_y = self._reduce_list_to_2d(self.dataset.get_bounds())136        norm = None137        if self.logscale:138            norm = matplotlib.colors.LogNorm()139        im = self.ax.imshow(np.rot90(view), extent=[*bounds_x, *bounds_y], cmap=self.cmap, norm=norm)140        self.colorbar = self.fig.colorbar(im)141        self.ax.set_title("integrated over {}".format(self.line_of_sight))142class h_alpha(_syntheticmain):143    """144    Calculates a H-alpha synthetic image, based on a method described by Heinzel (2005).145    """146    def __init__(self, dataset, **kwargs):147        print(">> Creating H-alpha view...")148        super().__init__(dataset, **kwargs)149        self.cmap = kwargs.get("cmap", "Reds_r")150        self.logscale = kwargs.get("logscale", True)151        self._calculate_halpha_view()152    def _calculate_halpha_view(self):153        for ileaf, offset in enumerate(self.dataset.block_offsets):154            block = datfile_utilities.get_single_block_data(self.dataset.file, offset, self.dataset.block_shape)155            # this adds the temperature and pressure to the block156            block = process_data.create_data_dict(block, self.dataset.header)157            # interpolate ionisation and f parameter for each block158            block_ion, block_fpar = ionisation.block_interpolate_ionisation_f(block, self.dataset)159            block_ne = super()._get_ne(block, block_ion)160            n2 = block_ne**2 / (block_fpar * 1e16)              # parameter f is interpolated in units of 1e16 cm-3161            # calculate block opacity162            block_kappa = (np.pi * self.dataset.units.ec**2 / (self.dataset.units.m_e * self.dataset.units.c)) * \163                            self.f23 * n2 * self._gaussian(block)164            # integrate block along line of sight to get opacity165            l_edge, r_edge = process_data.get_block_edges(ileaf, self.dataset)166            opacity = super()._integrate_block(block_kappa, l_edge, r_edge)167            S = self._source_function()168            intensity = S * (1 - np.exp(-opacity))169            if self.simulation_type == 'filament':170                Ibgr = 2.2 * S171                intensity += Ibgr * np.exp(-opacity)172            # add integrated block to list, this preserves block index173            self.integrated_block_list.append(intensity)174        # merge all integrated blocks into one single 2D array175        view = super()._merge_integrated_blocks()176        # plot final result177        super()._plot_synthetic_view(view)178    def _gaussian(self, block):179        block_T = block["T"] * self.dataset.units.unit_temperature180        ksi = 5 * 1e5  # microturbulence in cm/s181        nu_0 = self.dataset.units.c / (6562.8 * 1e-8)       # H-alpha wavelength is 6562.8 Angstrom182        delta_nu = 0183        delta_nuD = (nu_0 / self.dataset.units.c) * \184                    np.sqrt(2 * self.dataset.units.k_B * block_T / self.dataset.units.m_p + ksi ** 2)185        phi_nu = 1.0 / (np.sqrt(np.pi) * delta_nuD) * np.exp(-delta_nu / delta_nuD) ** 2186        return phi_nu187    def _source_function(self):188        H = self.altitude * 1e5     # altitude in cm189        # dilution factor W190        W = 0.5 * ( 1 - np.sqrt(1 - (self.dataset.units.Rsun**2 / (self.dataset.units.Rsun + H)**2)) )191        return W * 0.17 * 4.077 * 1e-5192class faraday(_syntheticmain):193    """194    Calculates the Faraday rotation effect for a given dataset. Only for MHD.195    """196    def __init__(self, dataset, **kwargs):197        print(">> Creating Faraday view...")198        if not dataset.header["physics_type"] == "mhd":199            print("calculating Faraday rotation measure is only possible for an MHD dataset")200            return201        if not dataset.header["ndim"] == 3:202            print("calculating Faraday rotation measure is only possible for a 3D dataset")203            return204        super().__init__(dataset, **kwargs)205        self.cmap = kwargs.get("cmap", "jet")206        self.logscale = kwargs.get("logscale", False)207        self._calculate_faraday_view()208    def _calculate_faraday_view(self):209        for ileaf, offset in enumerate(self.dataset.block_offsets):210            block = datfile_utilities.get_single_block_data(self.dataset.file, offset, self.dataset.block_shape)211            # add pressure and temperature to block212            block = process_data.create_data_dict(block, self.dataset.header)213            block_ion, block_fpar = ionisation.block_interpolate_ionisation_f(block, self.dataset)214            block_ne = super()._get_ne(block, block_ion)215            prefactor = self.dataset.units.ec**3 / (2*np.pi*self.dataset.units.m_e**2 * self.dataset.units.c**4)216            if self.line_of_sight == 'x':217                b_key = 'b1'218            elif self.line_of_sight == 'y':219                b_key = 'b2'220            else:221                b_key = 'b3'222            b_para = block[b_key] * self.dataset.units.unit_magneticfield223            l_edge, r_edge = process_data.get_block_edges(ileaf, self.dataset)224            fara_measure = super()._integrate_block(block_ne*b_para, l_edge, r_edge) * prefactor225            self.integrated_block_list.append(fara_measure)226        view = super()._merge_integrated_blocks()227        super()._plot_synthetic_view(view)228        self.colorbar.set_label("rad cm$^{-2}$")unidic2cabocha.py
Source:unidic2cabocha.py  
...101      if j>=0:102        x[7]=self[t].misc[j+9:]103      x[9]=self[t].upos104      self._cabocha._features.append(",".join(x[0:10]))105  def _get_ne(self,index):106    f=self._cabocha._features[index]107    if f.startswith("åè©,åºæåè©,"):108      t=f.split(",")109      if t[2]=="人å":110        return("B-PERSON")111      if t[2]=="å°å":112        return("B-LOCATION")113      return("B-ORGANIZATION")114    return("O")115  def toString(self,format=4):116    if format==4:117      return str(self)118    if not hasattr(self._cabocha,"_sentences"):119      self._makeChunks()120    if format>0 and not hasattr(self._cabocha,"_features"):121      self._makeFeatures()122    result=""123    for k,s in enumerate(self._cabocha._sentences):124      if format==0 or format==2:125        x=len(self._cabocha._chunkinfo[k])126        l,m,n=[],[],[" "*(x*2)]*x127        for i,d,t,h,f,w,z in self._cabocha._chunkinfo[k]:128          l.append(len(w)+len([c for c in w if ord(c)>127]))129          m.append(w)130          if d<0:131            continue132          n[i]="-"*(d*2-1)+"D"+n[i][d*2:]133          for j in range(i+1,d-1):134            n[j]=n[j][0:d*2-1]+"|"+n[j][d*2:]135        h=max([(x-i)*2+j for i,j in enumerate(l)])136        for i in range(x):137          result+=" "*(h-(x-i)*2-l[i])+m[i]+n[i][i*2:].rstrip()+"\n"138        result+="EOS\n"139      if format==1 or format==2:140        for i,d,t,h,f,w,z in self._cabocha._chunkinfo[k]:141          result+="* "+str(i)+" "+str(d)+"D "+str(h)+"/"+str(f)+" 0.000000\n"142          for t in s[i]:143            result+=self[t].form+"\t"+self._cabocha._features[t]+"\t"+self._get_ne(t)+"\t"+str(self[t].id)+"<-"+self[t].deprel144            if self[t] is self[t].head:145              result+="\n"146            else:147              result+="-"+str(self[t].head.id)+"\n"148        result+="EOS\n"149      if format==3:150        result+="<sentence>\n"151        for i,d,t,h,f,w,z in self._cabocha._chunkinfo[k]:152          result+=' <chunk id="'+str(i)+'" link="'+str(d)+'" rel="D" score="0.000000" head="'+str(self[s[i][h]].id)+'" func="'+str(self[s[i][f]].id)+'">\n'153          for t in s[i]:154            result+='  <tok id="'+str(self[t].id)+'" feature="'+self._cabocha._features[t]+'" head="'+str(self[t].head.id)+'" rel="'+self[t].deprel+'">'+self[t].form+'</tok>\n'155          result+=" </chunk>\n"156        result+="</sentence>\n"157    return result158  def size(self):159    return len(self)-1160  def token_size(self):161    return len(self)-1162  def token(self,index):163    if not hasattr(self._cabocha,"_sentences"):164      self._makeChunks()165    c=None166    for s1,s2 in zip(self._cabocha._sentences,self._cabocha._chunkinfo):167      for c1,c2 in zip(s1,s2):168        i,d,t,h,f,w,z=c2169        if t==index:170          c=Chunk(c1,c2)171          break172    return Token(self,c,index)173  def chunk_size(self):174    if not hasattr(self._cabocha,"_sentences"):175      self._makeChunks()176    return sum(len(s) for s in self._cabocha._sentences)177  def chunk(self,index):178    if not hasattr(self._cabocha,"_sentences"):179      self._makeChunks()180    for s1,s2 in zip(self._cabocha._sentences,self._cabocha._chunkinfo):181      for c1,c2 in zip(s1,s2):182        i,d,t,h,f,w,z=c2183        if z==index:184          return Chunk(c1,c2)185    return None186  def sentence_size(self):187    if not hasattr(self._cabocha,"_sentences"):188      self._makeChunks()189    return len(self._cabocha._sentences)190  def sentence(self,index=0):191    if self._result>"":192      return self._result.split("# text = ")[index+1].split("\n")[0]193    if not hasattr(self._cabocha,"_sentences"):194      self._makeChunks()195    return "".join(w for i,d,t,h,f,w,z in self._cabocha._chunkinfo[index])196  def renew(self):197    self._makeChunks()198    self._makeFeatures()199class Chunk(object):200  additional_info=None201  feature_list_size=0202  score=0.0203  def __init__(self,chunk,chunkinfo):204    i,d,t,h,f,w,z=chunkinfo205    self.token_pos=t206    self.head_pos=h207    self.func_pos=f208    self.link=-1 if d<0 else d-i+z209    self.token_size=len(chunk)210    self._surface=w211  def __repr__(self):212    return self._surface213  def feature_list(self,index):214    return None215class Token(object):216  additional_info=ne=None217  def __init__(self,tree,chunk,index):218    self.surface=self.normalized_surface=tree[index+1].form219    self.chunk=chunk220    if not hasattr(tree._cabocha,"_features"):221      tree._makeFeatures()222    self.feature=tree._cabocha._features[index+1]223    self.feature_list_size=len(self.feature.split(","))224    self.ne=tree._get_ne(index+1)225  def __repr__(self):226    return self.normalized_surface227  def feature_list(self,index):228    return self.feature.split(",")[index]229class Parser(object):230  def __init__(self,UniDic=None):231    self.UniDic2UD=unidic2ud.UniDic2UD(UniDic,UDPipe="japanese-modern")232  def parse(self,sentence):233    t=Tree(self.UniDic2UD(sentence,raw=True))234    t._cabocha._parser=self235    return t236  def parseToString(self,sentence):...conll_get.py
Source:conll_get.py  
...7	"""8	if not sentence:9		return []10	if type(sentence[0]) is list:11		return [_get_ne(sent) for sent in sentence]12	else:13		return _get_ne(sentence)14def _get_ne(sentence):15	_b = re.compile("^B")16	_i = re.compile("^I")17	ne = []18	for tup in sentence:19		# add a new tuple or NE20		if _b.search(tup[2]):21			ne.append([tup])22		# add to the most recent B or I tag23		# the additional NE information24		if _i.search(tup[2]):25			ne[-1].append(tup)26	return ne27def get_nouns(sentence):28	"""  For either a list of sentences, or a single sentence...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
